Skip to content

Commit fa4edf3

Browse files
author
peng.li24
committed
feat: add linestring() overload for std::vector<std::array<double,2>>
C++ callers can now pass native coordinate lists directly without needing a numpy intermediate (list_to_numpy_line bridge). - Add #include <array> to geometry_py.h - New inline linestring(const std::vector<std::array<double,2>>& pts) flattens 2D points into a linear buffer and constructs LineString<double>
1 parent 8335fce commit fa4edf3

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Build directories
22
build/
33
cmake-build-*/
4-
4+
issue/
55
# Compiled output
66
*.o
77
*.so

pycpp/geometry_py.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "../shapely/geometry/multipolygon.h"
2222
#include "../shapely/geometry/geometrycollection.h"
2323

24+
#include <array>
2425
#include <cstring>
2526
#include <tuple>
2627
#include <vector>
@@ -153,6 +154,19 @@ inline LineString<double> linestring(const py::array& arr) {
153154
return LineString<double>(tmp.data(), static_cast<size_t>(n), 2);
154155
}
155156

157+
// Accept native C++ coordinate list: std::vector<std::array<double, 2>>
158+
// No numpy intermediate — flattens directly into LineString<double>.
159+
inline LineString<double> linestring(const std::vector<std::array<double, 2>>& pts) {
160+
if (pts.size() < 2)
161+
throw std::runtime_error("LineString: rows must be >= 2");
162+
std::vector<double> flat(pts.size() * 2);
163+
for (size_t i = 0; i < pts.size(); ++i) {
164+
flat[i * 2] = pts[i][0];
165+
flat[i * 2 + 1] = pts[i][1];
166+
}
167+
return LineString<double>(flat.data(), pts.size(), 2);
168+
}
169+
156170
// -- Polygon --
157171
inline Polygon<double> polygon(const py::array_t<double>& arr) {
158172
auto buf = arr.request();

0 commit comments

Comments
 (0)