c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/local/include",
"/usr/include/python3.12"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-arm64"
}
],
"version": 4
}
https://github.com/pybind/pybind11
pip install "pybind11[global]"
tar -xf geos-3.7.0beta1.tar.gz
cd geos-3.7.0beta1
mkdir build
cd build
#指定安装位置
#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/geos ..
cmake ..
make
make install
.
|-- CMakeLists.txt
|-- build
|-- include
| `-- a.h
|-- pybind.cpp
`-- src
`-- a.cpp
.
|-- CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(example VERSION 1.0.0 LANGUAGES C CXX)
find_package(GEOS 3.12 REQUIRED)
find_package(pybind11 REQUIRED)
file(GLOB SRC_LIST1 "*.cpp")
file(GLOB SRC_LIST2 "src/*.cpp")
include_directories(${GEOS_INCLUDE_DIR})
include_directories(
/usr/local/include
${CMAKE_CURRENT_SOURCE_DIR}/include
)
link_directories(
/usr/local/lib
)
# add_subdirectory(pybind11)
pybind11_add_module(example ${SRC_LIST1} ${SRC_LIST2})
target_link_libraries(example PRIVATE GEOS::geos)
|-- build
|-- include
| `-- a.h
#ifndef A_H_
#define A_H_
#include <iostream>
#include <geos/io/WKTReader.h>
#include <geos/linearref/LengthIndexOfPoint.h>
#include <geos/linearref/LengthIndexedLine.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/Coordinate.h>
#include <string>
int add(int i, int j);
std::string lineInterpolatePoint(const std::string wkt, float index);
std::string getIntersect();
#endif
|-- pybind.cpp
#include <pybind11/pybind11.h>
#include "a.h"
PYBIND11_MODULE(example, m)
{
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
m.def("add_lambda", [](int a, int b)
{ return a + b; }, "A function which adds two numbers");
m.def("getIntersect", &getIntersect, "getIntersect");
m.def("lineInterpolatePoint", &lineInterpolatePoint, "lineInterpolatePoint",
pybind11::arg("line_wkt"), pybind11::arg("index") = 0.6);
}
`-- src
`-- a.cpp
#include <iostream>
#include <geos/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
#include <geos/linearref/LengthIndexOfPoint.h>
#include <geos/linearref/LengthIndexedLine.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/Coordinate.h>
#include <string>
#include "a.h"
int add(int i, int j) {
return i + j;
}
std::string getIntersect()
{
geos::geom::GeometryFactory::Ptr factory = geos::geom::GeometryFactory::create();
geos::io::WKTReader reader(*factory);
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))");
std::unique_ptr<geos::geom::Geometry> geom_a(reader.read(wkt_a));
std::unique_ptr<geos::geom::Geometry> geom_b(reader.read(wkt_b));
std::unique_ptr<geos::geom::Geometry> inter = geom_a->intersection(geom_b.get());
geos::io::WKTWriter writer;
writer.setTrim(true);
std::string inter_wkt = writer.write(inter.get());
return inter_wkt;
}
std::string lineInterpolatePoint(const std::string line_wkt, float index)
{
geos::io::WKTReader wktReader;
std::unique_ptr<geos::geom::Geometry> geom(wktReader.read(line_wkt));
if (!geom)
{
std::cout << "wkt有错误" << std::endl;
return "";
}
std::string geomType = geom->getGeometryType();
if (geomType != "LineString")
{
std::cout << "wkt需要是linestring类型" << std::endl;
return "";
}
std::unique_ptr<geos::geom::LineString> line(static_cast<geos::geom::LineString *>(geom.release()));
geos::linearref::LengthIndexedLine lengthIndexedLine(line.release());
geos::geom::Coordinate c = lengthIndexedLine.extractPoint(0.5);
geos::geom::GeometryFactory::Ptr geomFactory = geos::geom::GeometryFactory::create();
std::unique_ptr<geos::geom::Point> point = geomFactory->createPoint(c);
// std::cout << point->getX() << '\t' << point->getY() << std::endl;
geos::io::WKTWriter wktWriter;
std::string pointWkt = wktWriter.write(point.get());
return pointWkt;
}