Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor numeric constants (Earth radius) #86

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/accessors-geog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,28 @@ double get_y(PyObjectGeography a) {
return s2geog::s2_y(geog->geog());
}

double distance(PyObjectGeography a, PyObjectGeography b, double radius = EARTH_RADIUS_METERS) {
double distance(PyObjectGeography a,
PyObjectGeography b,
double radius = numeric_constants::EARTH_RADIUS_METERS) {
const auto& a_index = a.as_geog_ptr()->geog_index();
const auto& b_index = b.as_geog_ptr()->geog_index();
return s2geog::s2_distance(a_index, b_index) * radius;
}

double area(PyObjectGeography a, double radius = EARTH_RADIUS_METERS) {
double area(PyObjectGeography a, double radius = numeric_constants::EARTH_RADIUS_METERS) {
return s2geog::s2_area(a.as_geog_ptr()->geog()) * radius * radius;
}

double length(PyObjectGeography a, double radius = EARTH_RADIUS_METERS) {
double length(PyObjectGeography a, double radius = numeric_constants::EARTH_RADIUS_METERS) {
return s2geog::s2_length(a.as_geog_ptr()->geog()) * radius;
}

double perimeter(PyObjectGeography a, double radius = EARTH_RADIUS_METERS) {
double perimeter(PyObjectGeography a, double radius = numeric_constants::EARTH_RADIUS_METERS) {
return s2geog::s2_perimeter(a.as_geog_ptr()->geog()) * radius;
}

void init_accessors(py::module& m) {
m.attr("EARTH_RADIUS_METERS") = py::float_(EARTH_RADIUS_METERS);
m.attr("EARTH_RADIUS_METERS") = py::float_(numeric_constants::EARTH_RADIUS_METERS);

m.def("centroid",
py::vectorize(&centroid),
Expand Down Expand Up @@ -132,7 +134,7 @@ void init_accessors(py::module& m) {
py::vectorize(&distance),
py::arg("a"),
py::arg("b"),
py::arg("radius") = EARTH_RADIUS_METERS,
py::arg("radius") = numeric_constants::EARTH_RADIUS_METERS,
R"pbdoc(
Calculate the distance between two geographies.

Expand All @@ -150,7 +152,7 @@ void init_accessors(py::module& m) {
m.def("area",
py::vectorize(&area),
py::arg("a"),
py::arg("radius") = EARTH_RADIUS_METERS,
py::arg("radius") = numeric_constants::EARTH_RADIUS_METERS,
R"pbdoc(
Calculate the area of the geography.

Expand All @@ -166,7 +168,7 @@ void init_accessors(py::module& m) {
m.def("length",
py::vectorize(&length),
py::arg("a"),
py::arg("radius") = EARTH_RADIUS_METERS,
py::arg("radius") = numeric_constants::EARTH_RADIUS_METERS,
R"pbdoc(
Calculates the length of a line geography, returning zero for other types.

Expand All @@ -182,7 +184,7 @@ void init_accessors(py::module& m) {
m.def("perimeter",
py::vectorize(&perimeter),
py::arg("a"),
py::arg("radius") = EARTH_RADIUS_METERS,
py::arg("radius") = numeric_constants::EARTH_RADIUS_METERS,
R"pbdoc(
Calculates the perimeter of a polygon geography, returning zero for other types.

Expand Down
8 changes: 6 additions & 2 deletions src/constants.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "s2/s2earth.h"

namespace spherely {

const double EARTH_RADIUS_METERS = 6371.01 * 1000;
struct numeric_constants {
static constexpr double EARTH_RADIUS_METERS = S2Earth::RadiusMeters();
};

}
} // namespace spherely
4 changes: 2 additions & 2 deletions src/geoarrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ py::array_t<PyObjectGeography> from_geoarrow(py::object input,
options.set_oriented(oriented);
options.set_projection(projection.s2_projection());
if (planar) {
auto tol = S1Angle::Radians(tessellate_tolerance / EARTH_RADIUS_METERS);
auto tol = S1Angle::Radians(tessellate_tolerance / numeric_constants::EARTH_RADIUS_METERS);
options.set_tessellate_tolerance(tol);
}
if (geometry_encoding.is(py::none())) {
Expand Down Expand Up @@ -213,7 +213,7 @@ ArrowArrayHolder to_geoarrow(py::array_t<PyObjectGeography> input,
options.set_precision(precision);
options.set_projection(projection.s2_projection());
if (planar) {
auto tol = S1Angle::Radians(tessellate_tolerance / EARTH_RADIUS_METERS);
auto tol = S1Angle::Radians(tessellate_tolerance / numeric_constants::EARTH_RADIUS_METERS);
options.set_tessellate_tolerance(tol);
}

Expand Down
6 changes: 4 additions & 2 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class FromWKT {
s2geog::geoarrow::ImportOptions options;
options.set_oriented(oriented);
if (planar) {
auto tol = S1Angle::Radians(tessellate_tolerance / EARTH_RADIUS_METERS);
auto tol =
S1Angle::Radians(tessellate_tolerance / numeric_constants::EARTH_RADIUS_METERS);
options.set_tessellate_tolerance(tol);
}
m_reader = std::make_shared<s2geog::WKTReader>(options);
Expand Down Expand Up @@ -51,7 +52,8 @@ class FromWKB {
s2geog::geoarrow::ImportOptions options;
options.set_oriented(oriented);
if (planar) {
auto tol = S1Angle::Radians(tessellate_tolerance / EARTH_RADIUS_METERS);
auto tol =
S1Angle::Radians(tessellate_tolerance / numeric_constants::EARTH_RADIUS_METERS);
options.set_tessellate_tolerance(tol);
}
m_reader = std::make_shared<s2geog::WKBReader>(options);
Expand Down
Loading