https://github.com/pybind/pybind11
pip install "pybind11[global]"
#include <pybind11/pybind11.h>
#include <iostream>
/* For geometry operations */
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Geometry.h>
/* For WKT read/write */
#include <geos/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
/* Geometry/GeometryFactory */
using geos::geom::Geometry;
using geos::geom::GeometryFactory;
/* WKTReader/WKTWriter */
using geos::io::WKTReader;
using geos::io::WKTWriter;
namespace py = pybind11;
using namespace std;
string get_intersect()
{
/* New factory with default (float) precision model */
GeometryFactory::Ptr factory = GeometryFactory::create();
/*
* Reader requires a factory to bind the geometry to
* for shared resources like the PrecisionModel
*/
WKTReader reader(*factory);
/* Input WKT strings */
std::string wkt_a("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))");
std::string wkt_b("POLYGON((5 5, 15 5, 15 15, 5 15, 5 5))");
/* Convert WKT to Geometry */
std::unique_ptr<Geometry> geom_a(reader.read(wkt_a));
std::unique_ptr<Geometry> geom_b(reader.read(wkt_b));
/* Calculate intersection */
std::unique_ptr<Geometry> inter = geom_a->intersection(geom_b.get());
/* Convert Geometry to WKT */
WKTWriter writer;
writer.setTrim(true);
std::string inter_wkt = writer.write(inter.get());
/* Print out results */
std::cout << "Geometry A: " << wkt_a << std::endl;
std::cout << "Geometry B: " << wkt_b << std::endl;
return inter_wkt;
}
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
m.def("get_intersect", &get_intersect, "A function which adds two numbers");
}
cmake_minimum_required(VERSION 3.12)
project(example VERSION 1.0.0 LANGUAGES C CXX)
find_package(GEOS 3.11 REQUIRED)
find_package(pybind11 REQUIRED)
include_directories(
/usr/local/lib
)
link_directories(
/usr/local/lib
)
pybind11_add_module(example example.cpp)
target_link_libraries(example PRIVATE GEOS::geos)
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <iostream>
namespace py = pybind11;
using namespace std;
py::list return_list(){
py::list lst;
lst.append(3);
return lst;
}
void print_vector(const std::vector<int> &v) {
for (auto item : v)
std::cout << item << "\n";
}
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
m.def("return_list", &return_list, "A function which adds two numbers");
m.def("print_vector", &print_vector, "A function which adds two numbers");
}
cmake_minimum_required(VERSION 3.0)
project(example)
add_definitions(-Wall -Wextra -Wpedantic)
find_package(pybind11 REQUIRED)
find_package(GEOS REQUIRED)
include_directories(${GEOS_INCLUDE_DIR})
pybind11_add_module(example example.cpp)
target_link_libraries(example PRIVATE ${GEOS_LIBRARY})
网友评论