From 9d5a77ac01c4edd7f74410dae7755bc2e87c13ee Mon Sep 17 00:00:00 2001 From: B Krishna Chaitanya Date: Mon, 17 Aug 2020 01:44:45 +0530 Subject: [PATCH] PyMEOS: Initial (and incomplete) integration with shapely --- README.md | 8 ++++---- pymeos/include/geometry.hpp | 30 ++++++++++++++++++++++++++++++ pymeos/source/pybind.cpp | 28 +--------------------------- setup.cfg | 1 + 4 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 pymeos/include/geometry.hpp diff --git a/README.md b/README.md index 7f35995..8f08021 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Note: libgeos-dev might need to be installed on your system. ```python import datetime -from pymeos import GeomPoint +from shapely.geometry import Point from pymeos.temporal import TGeomPointInst, TGeomPointSeq @@ -43,9 +43,9 @@ def datetime_utc(year, month, day, hour=0, minute=0, second=0): # Example creation of trajectory (temporal sequence of geometries) trajectory = TGeomPointSeq({ - TGeomPointInst(GeomPoint(0, 0), datetime_utc(2012, 1, 1, 8, 0)), - TGeomPointInst(GeomPoint(2, 0), datetime_utc(2012, 1, 1, 8, 10)), - TGeomPointInst(GeomPoint(2, 1), datetime_utc(2012, 1, 1, 8, 15)), + TGeomPointInst(Point(0, 0), datetime_utc(2012, 1, 1, 8, 0)), + TGeomPointInst(Point(2, 0), datetime_utc(2012, 1, 1, 8, 10)), + TGeomPointInst(Point(2, 1), datetime_utc(2012, 1, 1, 8, 15)), }) print(trajectory) diff --git a/pymeos/include/geometry.hpp b/pymeos/include/geometry.hpp new file mode 100644 index 0000000..dec6bee --- /dev/null +++ b/pymeos/include/geometry.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include + +namespace py = pybind11; + +namespace pybind11 { +namespace detail { +// Casting between GeomPoint and shapely.geometry.point.Point +template <> class type_caster { +public: + typedef GeomPoint type; + bool load(handle src, bool) { + if (!src) return false; + GeomPoint point(src.attr("x").cast(), src.attr("y").cast()); + value = point; + return true; + } + + static handle cast(const GeomPoint &src, return_value_policy policy, handle /* parent */) { + auto Point = py::module::import("shapely.geometry").attr("Point"); + py::object p = Point(src.x(), src.y()); + return p.inc_ref().ptr(); + } + PYBIND11_TYPE_CASTER(type, _("shapely.geometry.point.Point")); +}; +} // namespace detail +} // namespace pybind11 diff --git a/pymeos/source/pybind.cpp b/pymeos/source/pybind.cpp index dc49c9d..5cf25a9 100644 --- a/pymeos/source/pybind.cpp +++ b/pymeos/source/pybind.cpp @@ -7,6 +7,7 @@ #include "box_module.hpp" #include "common.hpp" +#include "geometry.hpp" #include "io_module.hpp" #include "range_module.hpp" #include "temporal_module.hpp" @@ -19,33 +20,6 @@ PYBIND11_MODULE(_pymeos, m) { // TODO will we ever get a chance to do finish_geos()? init_geos(); - py::class_(m, "GeomPoint") - .def(py::init()) - .def(py::init()) - .def(py::init()) - .def(py::init()) - .def(py::init()) - .def(py::self + py::self, py::arg("other")) - .def(py::self - py::self, py::arg("other")) - .def(py::self == py::self, py::arg("other")) - .def(py::self != py::self, py::arg("other")) - .def(py::self < py::self, py::arg("other")) - .def(py::self <= py::self, py::arg("other")) - .def(py::self > py::self, py::arg("other")) - .def(py::self >= py::self, py::arg("other")) - .def("__str__", &to_ostream) - .def("__repr__", &to_ostream) - .def("compare", &GeomPoint::compare, py::arg("other")) - .def_property_readonly("x", &GeomPoint::x) - .def_property_readonly("y", &GeomPoint::y) - .def_property_readonly("srid", &GeomPoint::srid) - .def("fromWKB", &GeomPoint::fromWKB) - .def("toWKB", &GeomPoint::toWKB) - .def("fromWKT", &GeomPoint::fromWKT) - .def("toWKT", &GeomPoint::toWKT) - .def("fromHEX", &GeomPoint::fromHEX) - .def("toHEX", &GeomPoint::toHEX); - def_box_module(m); def_io_module(m); def_range_module(m); diff --git a/setup.cfg b/setup.cfg index c76f8d4..85c74ae 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,6 +31,7 @@ keywords = mobility, movement, geotemporal, trajectory, geos, meos [options] setup_requires = pybind11~=2.5 + Shapely~=1.7 zip_safe = false packages = find: