Skip to content

Commit

Permalink
simplify, use s2geography::s2_x/y
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Nov 26, 2024
1 parent a614171 commit 581cec5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 92 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ endif()

set(CPP_SOURCES
src/accessors-geog.cpp
src/coords.cpp
src/creation.cpp
src/geography.cpp
src/io.cpp
Expand Down
42 changes: 42 additions & 0 deletions src/accessors-geog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ PyObjectGeography convex_hull(PyObjectGeography a) {
return make_py_geography(s2geog::s2_convex_hull(a_ptr));
}

double get_x(PyObjectGeography a) {
auto geog = a.as_geog_ptr();
if (geog->geog_type() != GeographyType::Point) {
throw py::value_error("Only Point geometries supported");
}
return s2geog::s2_x(geog->geog());
}

double get_y(PyObjectGeography a) {
auto geog = a.as_geog_ptr();
if (geog->geog_type() != GeographyType::Point) {
throw py::value_error("Only Point geometries supported");
}
return s2geog::s2_y(geog->geog());
}

double distance(PyObjectGeography a, PyObjectGeography b, double radius = EARTH_RADIUS_METERS) {
const auto& a_index = a.as_geog_ptr()->geog_index();
const auto& b_index = b.as_geog_ptr()->geog_index();
Expand Down Expand Up @@ -78,6 +94,32 @@ void init_accessors(py::module& m) {
)pbdoc");

m.def("get_x",
py::vectorize(&get_x),
py::arg("a"),
R"pbdoc(
Returns the longitude value of the Point (in degrees).
Parameters
----------
a: :py:class:`Geography` or array_like
Geography object(s).
)pbdoc");

m.def("get_y",
py::vectorize(&get_y),
py::arg("a"),
R"pbdoc(
Returns the latitude value of the Point (in degrees).
Parameters
----------
a: :py:class:`Geography` or array_like
Geography object(s).
)pbdoc");

m.def("distance",
py::vectorize(&distance),
py::arg("a"),
Expand Down
64 changes: 0 additions & 64 deletions src/coords.cpp

This file was deleted.

2 changes: 0 additions & 2 deletions src/spherely.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace py = pybind11;
void init_geography(py::module&);
void init_creation(py::module&);
void init_predicates(py::module&);
void init_coords(py::module&);
void init_accessors(py::module&);
void init_io(py::module&);
#if defined(S2GEOGRAPHY_VERSION_MAJOR) && \
Expand All @@ -29,7 +28,6 @@ PYBIND11_MODULE(spherely, m) {
init_geography(m);
init_creation(m);
init_predicates(m);
init_coords(m);
init_accessors(m);
init_io(m);
#if defined(S2GEOGRAPHY_VERSION_MAJOR) && \
Expand Down
25 changes: 25 additions & 0 deletions tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ def test_convex_hull(geog, expected) -> None:
assert spherely.equals(actual, expected)


def test_get_x_y() -> None:
# scalar
a = spherely.point(1.5, 2.6)
assert spherely.get_x(a) == pytest.approx(1.5, abs=1e-14)
assert spherely.get_y(a) == pytest.approx(2.6, abs=1e-14)

# array
arr = np.array([spherely.point(0, 1), spherely.point(1, 2), spherely.point(2, 3)])

actual = spherely.get_x(arr)
expected = np.array([0, 1, 2], dtype="float64")
np.testing.assert_allclose(actual, expected)

actual = spherely.get_y(arr)
expected = np.array([1, 2, 3], dtype="float64")
np.testing.assert_allclose(actual, expected)

# only points are supported
with pytest.raises(ValueError):
spherely.get_x(spherely.linestring([(0, 1), (1, 2)]))

with pytest.raises(ValueError):
spherely.get_y(spherely.linestring([(0, 1), (1, 2)]))


@pytest.mark.parametrize(
"geog_a, geog_b, expected",
[
Expand Down
25 changes: 0 additions & 25 deletions tests/test_coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,3 @@
import pytest

import spherely


def test_get_x_y() -> None:
# scalar
a = spherely.point(1.5, 2.6)
assert spherely.get_x(a) == pytest.approx(1.5, abs=1e-14)
assert spherely.get_y(a) == pytest.approx(2.6, abs=1e-14)

# array
arr = np.array([spherely.point(0, 1), spherely.point(1, 2), spherely.point(2, 3)])

actual = spherely.get_x(arr)
expected = np.array([0, 1, 2], dtype="float64")
np.testing.assert_allclose(actual, expected)

actual = spherely.get_y(arr)
expected = np.array([1, 2, 3], dtype="float64")
np.testing.assert_allclose(actual, expected)

# only points are supported
with pytest.raises(ValueError):
spherely.get_x(spherely.linestring([(0, 1), (1, 2)]))

with pytest.raises(ValueError):
spherely.get_y(spherely.linestring([(0, 1), (1, 2)]))

0 comments on commit 581cec5

Please sign in to comment.