Skip to content

Commit 2b437e9

Browse files
author
peng.li24
committed
feat: add 6 cross-type distance() methods — MultiLineString↔Polygon, MultiLineString↔MultiPolygon, Polygon↔MultiLineString, Polygon↔MultiPolygon, MultiPolygon↔MultiLineString, LineString↔MultiLineString
1 parent 9cab895 commit 2b437e9

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

shapely/geometry/linestring.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ template <typename T> class Point;
2626
#ifndef SHAPELY_GEOMETRY_LINEARRING_DEFINED
2727
template <typename T> class LinearRing;
2828
#endif
29+
#ifndef SHAPELY_GEOMETRY_MULTILINESTRING_DEFINED
30+
template <typename T> class MultiLineString;
31+
#endif
2932

3033
template <typename T = double>
3134
class LineString {
@@ -47,6 +50,7 @@ class LineString {
4750
double distance(const LineString& other) const;
4851
template <typename U> double distance(const Polygon<U>& other) const;
4952
template <typename U> double distance(const Point<U>& other) const;
53+
template <typename U> double distance(const MultiLineString<U>& other) const;
5054

5155
// -- Predicates --
5256
template <typename U> bool contains(const Point<U>& other) const;
@@ -175,6 +179,7 @@ class LineString {
175179

176180
#include "shapely/geometry/point.h"
177181
#include "shapely/geometry/polygon.h"
182+
#include "shapely/geometry/multilinestring.h"
178183
#include "shapely/geometry/base.h"
179184

180185
#include <geos/geom/Point.h>
@@ -241,6 +246,10 @@ double LineString<T>::distance(const Point<U>& o) const {
241246
geos::operation::distance::DistanceOp op(geos_linestring_.get(), o.geos_point_.get());
242247
return op.distance();
243248
}
249+
template <typename T> template <typename U>
250+
double LineString<T>::distance(const MultiLineString<U>& o) const {
251+
geos::operation::distance::DistanceOp op(geos_linestring_.get(), o.geos_mls_.get()); return op.distance();
252+
}
244253

245254
// Python: shapely/geometry/base.py predicates L753-L813
246255
// -- Predicates (macro) ------------------------------------------------------

shapely/geometry/multilinestring.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ template <typename T> class LineString;
2424
#ifndef SHAPELY_GEOMETRY_POINT_DEFINED
2525
template <typename T> class Point;
2626
#endif
27+
#ifndef SHAPELY_GEOMETRY_POLYGON_DEFINED
28+
template <typename T> class Polygon;
29+
#endif
30+
#ifndef SHAPELY_GEOMETRY_MULTIPOLYGON_DEFINED
31+
template <typename T> class MultiPolygon;
32+
#endif
2733

2834
template <typename T = double>
2935
class MultiLineString {
@@ -43,6 +49,8 @@ class MultiLineString {
4349
// -- Distance --
4450
template <typename U> double distance(const Point<U>& other) const;
4551
template <typename U> double distance(const LineString<U>& other) const;
52+
template <typename U> double distance(const Polygon<U>& other) const;
53+
template <typename U> double distance(const MultiPolygon<U>& other) const;
4654
double distance(const MultiLineString& other) const;
4755

4856
// -- Predicates --
@@ -125,6 +133,7 @@ class MultiLineString {
125133
template <typename U> friend class Point;
126134
template <typename U> friend class LineString;
127135
template <typename U> friend class Polygon;
136+
template <typename U> friend class MultiPolygon;
128137
std::unique_ptr<geos::geom::MultiLineString> geos_mls_;
129138
geos::geom::GeometryFactory::Ptr factory_;
130139
};
@@ -140,6 +149,8 @@ class MultiLineString {
140149

141150
#include "shapely/geometry/point.h"
142151
#include "shapely/geometry/linestring.h"
152+
#include "shapely/geometry/polygon.h"
153+
#include "shapely/geometry/multipolygon.h"
143154
#include "shapely/geometry/base.h"
144155
#include <geos/geom/Point.h>
145156
#include <geos/geom/LineString.h>
@@ -200,6 +211,14 @@ template <typename T>
200211
double MultiLineString<T>::distance(const MultiLineString& o) const {
201212
geos::operation::distance::DistanceOp op(geos_mls_.get(), o.geos_mls_.get()); return op.distance();
202213
}
214+
template <typename T> template <typename U>
215+
double MultiLineString<T>::distance(const Polygon<U>& o) const {
216+
geos::operation::distance::DistanceOp op(geos_mls_.get(), o.geos_polygon_.get()); return op.distance();
217+
}
218+
template <typename T> template <typename U>
219+
double MultiLineString<T>::distance(const MultiPolygon<U>& o) const {
220+
geos::operation::distance::DistanceOp op(geos_mls_.get(), o.geos_mp_.get()); return op.distance();
221+
}
203222

204223
// -- Predicates (macros) ----------------------------------------------------
205224
#define MLS_PRED_PT(METHOD, GEOS_FN) \

shapely/geometry/multipolygon.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ template <typename T> class Point;
2727
#ifndef SHAPELY_GEOMETRY_LINESTRING_DEFINED
2828
template <typename T> class LineString;
2929
#endif
30+
#ifndef SHAPELY_GEOMETRY_MULTILINESTRING_DEFINED
31+
template <typename T> class MultiLineString;
32+
#endif
3033

3134
template <typename T = double>
3235
class MultiPolygon {
@@ -47,6 +50,7 @@ class MultiPolygon {
4750
template <typename U> double distance(const Point<U>& other) const;
4851
template <typename U> double distance(const LineString<U>& other) const;
4952
template <typename U> double distance(const Polygon<U>& other) const;
53+
template <typename U> double distance(const MultiLineString<U>& other) const;
5054
double distance(const MultiPolygon& other) const;
5155

5256
// -- Predicates --
@@ -141,6 +145,7 @@ class MultiPolygon {
141145
template <typename U> friend class Point;
142146
template <typename U> friend class LineString;
143147
template <typename U> friend class Polygon;
148+
template <typename U> friend class MultiLineString;
144149
std::unique_ptr<geos::geom::MultiPolygon> geos_mp_;
145150
geos::geom::GeometryFactory::Ptr factory_;
146151
};
@@ -157,6 +162,7 @@ class MultiPolygon {
157162
#include "shapely/geometry/point.h"
158163
#include "shapely/geometry/linestring.h"
159164
#include "shapely/geometry/polygon.h"
165+
#include "shapely/geometry/multilinestring.h"
160166
#include "shapely/geometry/base.h"
161167
#include <geos/geom/Point.h>
162168
#include <geos/geom/LineString.h>
@@ -225,6 +231,10 @@ template <typename T>
225231
double MultiPolygon<T>::distance(const MultiPolygon& o) const {
226232
geos::operation::distance::DistanceOp op(geos_mp_.get(), o.geos_mp_.get()); return op.distance();
227233
}
234+
template <typename T> template <typename U>
235+
double MultiPolygon<T>::distance(const MultiLineString<U>& o) const {
236+
geos::operation::distance::DistanceOp op(geos_mp_.get(), o.geos_mls_.get()); return op.distance();
237+
}
228238

229239
// -- Predicates (macros) ----------------------------------------------------
230240
#define MPY_PRED(PREFIX, OTHER, GETTER, GEOS_FN) \

shapely/geometry/polygon.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ template <typename T> class Point;
2727
#ifndef SHAPELY_GEOMETRY_POLYGON_DEFINED
2828
template <typename T> class Polygon;
2929
#endif
30+
#ifndef SHAPELY_GEOMETRY_MULTILINESTRING_DEFINED
31+
template <typename T> class MultiLineString;
32+
#endif
33+
#ifndef SHAPELY_GEOMETRY_MULTIPOLYGON_DEFINED
34+
template <typename T> class MultiPolygon;
35+
#endif
3036

3137
// ============================================================================
3238
// LinearRing (Python: shapely/geometry/polygon.py L23-L107)
@@ -204,6 +210,8 @@ class Polygon {
204210
double distance(const Polygon& other) const;
205211
template <typename U> double distance(const LineString<U>& other) const;
206212
template <typename U> double distance(const Point<U>& other) const;
213+
template <typename U> double distance(const MultiLineString<U>& other) const;
214+
template <typename U> double distance(const MultiPolygon<U>& other) const;
207215

208216
// -- Predicates --
209217
template <typename U> bool contains(const Point<U>& other) const;
@@ -323,6 +331,8 @@ class Polygon {
323331

324332
#include "shapely/geometry/linestring.h"
325333
#include "shapely/geometry/point.h"
334+
#include "shapely/geometry/multilinestring.h"
335+
#include "shapely/geometry/multipolygon.h"
326336
#include "shapely/geometry/base.h"
327337

328338
#include <geos/geom/LineString.h>
@@ -734,6 +744,14 @@ double Polygon<T>::distance(const Point<U>& o) const {
734744
geos::operation::distance::DistanceOp op(geos_polygon_.get(), o.geos_point_.get());
735745
return op.distance();
736746
}
747+
template <typename T> template <typename U>
748+
double Polygon<T>::distance(const MultiLineString<U>& o) const {
749+
geos::operation::distance::DistanceOp op(geos_polygon_.get(), o.geos_mls_.get()); return op.distance();
750+
}
751+
template <typename T> template <typename U>
752+
double Polygon<T>::distance(const MultiPolygon<U>& o) const {
753+
geos::operation::distance::DistanceOp op(geos_polygon_.get(), o.geos_mp_.get()); return op.distance();
754+
}
737755

738756
// Python: shapely/geometry/base.py predicates L753-L813
739757
// -- Predicates (macro) ------------------------------------------------------

tests/module.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ PYBIND11_MODULE(shapelycpp, m) {
8787
.def("distance", [](const LineString<double>& self, const LineString<double>& o) {
8888
return self.distance(o);
8989
})
90+
.def("distance", [](const LineString<double>& self, const MultiLineString<double>& o) {
91+
return self.distance(o);
92+
})
9093
.def("intersects", [](const LineString<double>& self, const LineString<double>& o) {
9194
return self.intersects(o);
9295
})
@@ -121,6 +124,12 @@ PYBIND11_MODULE(shapelycpp, m) {
121124
.def("distance", [](const Polygon<double>& self, const Polygon<double>& o) {
122125
return self.distance(o);
123126
})
127+
.def("distance", [](const Polygon<double>& self, const MultiLineString<double>& o) {
128+
return self.distance(o);
129+
})
130+
.def("distance", [](const Polygon<double>& self, const MultiPolygon<double>& o) {
131+
return self.distance(o);
132+
})
124133
.def("intersects", [](const Polygon<double>& self, const Polygon<double>& o) {
125134
return self.intersects(o);
126135
})
@@ -221,6 +230,12 @@ PYBIND11_MODULE(shapelycpp, m) {
221230
.def("distance", [](const MultiLineString<double>& self, const LineString<double>& o) {
222231
return self.distance(o);
223232
})
233+
.def("distance", [](const MultiLineString<double>& self, const Polygon<double>& o) {
234+
return self.distance(o);
235+
})
236+
.def("distance", [](const MultiLineString<double>& self, const MultiPolygon<double>& o) {
237+
return self.distance(o);
238+
})
224239
.def("distance", [](const MultiLineString<double>& self, const MultiLineString<double>& o) {
225240
return self.distance(o);
226241
})
@@ -262,6 +277,9 @@ PYBIND11_MODULE(shapelycpp, m) {
262277
.def("distance", [](const MultiPolygon<double>& self, const Polygon<double>& o) {
263278
return self.distance(o);
264279
})
280+
.def("distance", [](const MultiPolygon<double>& self, const MultiLineString<double>& o) {
281+
return self.distance(o);
282+
})
265283
.def("distance", [](const MultiPolygon<double>& self, const MultiPolygon<double>& o) {
266284
return self.distance(o);
267285
})

0 commit comments

Comments
 (0)