|
| 1 | +// Internal GEOS utility functions shared across geometry types. |
| 2 | +// NOT part of public API — do not include directly. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | +#include <sstream> |
| 9 | +#include <geos/geom/Geometry.h> |
| 10 | +#include <geos/geom/Point.h> |
| 11 | +#include <geos/geom/LineString.h> |
| 12 | +#include <geos/geom/Polygon.h> |
| 13 | +#include <geos/geom/CoordinateSequence.h> |
| 14 | +#include <geos/geom/Envelope.h> |
| 15 | +#include <geos/io/WKTWriter.h> |
| 16 | +#include <geos/io/WKBWriter.h> |
| 17 | +#include <geos/algorithm/distance/DiscreteHausdorffDistance.h> |
| 18 | +#include <geos/simplify/TopologyPreservingSimplifier.h> |
| 19 | +#include <geos/operation/valid/IsValidOp.h> |
| 20 | + |
| 21 | +namespace shapely { |
| 22 | +namespace detail { |
| 23 | + |
| 24 | +// --- WKT / WKB serialization ------------------------------------------------ |
| 25 | + |
| 26 | +inline std::string geos_to_wkt(const geos::geom::Geometry* g) { |
| 27 | + if (!g || g->isEmpty()) return "GEOMETRYCOLLECTION EMPTY"; |
| 28 | + geos::io::WKTWriter writer; |
| 29 | + return writer.write(g); |
| 30 | +} |
| 31 | + |
| 32 | +inline std::string geos_to_wkb_hex(const geos::geom::Geometry* g) { |
| 33 | + if (!g || g->isEmpty()) return ""; |
| 34 | + geos::io::WKBWriter writer; |
| 35 | + std::ostringstream oss; |
| 36 | + writer.writeHEX(*g, oss); |
| 37 | + return oss.str(); |
| 38 | +} |
| 39 | + |
| 40 | +// --- Predicates ------------------------------------------------------------- |
| 41 | + |
| 42 | +inline bool geos_contains(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 43 | + if (!a || !b) return false; |
| 44 | + return a->contains(b); |
| 45 | +} |
| 46 | + |
| 47 | +inline bool geos_within(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 48 | + if (!a || !b) return false; |
| 49 | + return a->within(b); |
| 50 | +} |
| 51 | + |
| 52 | +inline bool geos_crosses(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 53 | + if (!a || !b) return false; |
| 54 | + return a->crosses(b); |
| 55 | +} |
| 56 | + |
| 57 | +inline bool geos_disjoint(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 58 | + if (!a || !b) return true; |
| 59 | + return a->disjoint(b); |
| 60 | +} |
| 61 | + |
| 62 | +inline bool geos_overlaps(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 63 | + if (!a || !b) return false; |
| 64 | + return a->overlaps(b); |
| 65 | +} |
| 66 | + |
| 67 | +inline bool geos_touches(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 68 | + if (!a || !b) return false; |
| 69 | + return a->touches(b); |
| 70 | +} |
| 71 | + |
| 72 | +inline bool geos_covers(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 73 | + if (!a || !b) return false; |
| 74 | + return a->covers(b); |
| 75 | +} |
| 76 | + |
| 77 | +inline bool geos_covered_by(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 78 | + if (!a || !b) return false; |
| 79 | + return a->coveredBy(b); |
| 80 | +} |
| 81 | + |
| 82 | +inline bool geos_equals(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 83 | + if (!a && !b) return true; |
| 84 | + if (!a || !b) return false; |
| 85 | + return a->equals(b); |
| 86 | +} |
| 87 | + |
| 88 | +inline bool geos_equals_exact(const geos::geom::Geometry* a, const geos::geom::Geometry* b, double tol) { |
| 89 | + if (!a && !b) return true; |
| 90 | + if (!a || !b) return false; |
| 91 | + return a->equalsExact(b, tol); |
| 92 | +} |
| 93 | + |
| 94 | +inline std::string geos_relate(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 95 | + if (!a || !b) return ""; |
| 96 | + return a->relate(b)->toString(); |
| 97 | +} |
| 98 | + |
| 99 | +inline bool geos_relate_pattern(const geos::geom::Geometry* a, const geos::geom::Geometry* b, const std::string& pat) { |
| 100 | + if (!a || !b) return false; |
| 101 | + return a->relate(b, pat); |
| 102 | +} |
| 103 | + |
| 104 | +// --- Geometry property helpers ---------------------------------------------- |
| 105 | + |
| 106 | +inline std::string geos_geom_type(const geos::geom::Geometry* g) { |
| 107 | + if (!g) return "GeometryCollection"; |
| 108 | + return g->getGeometryType(); |
| 109 | +} |
| 110 | + |
| 111 | +inline bool geos_is_simple(const geos::geom::Geometry* g) { |
| 112 | + return g && g->isSimple(); |
| 113 | +} |
| 114 | + |
| 115 | +inline bool geos_is_valid(const geos::geom::Geometry* g) { |
| 116 | + return g && g->isValid(); |
| 117 | +} |
| 118 | + |
| 119 | +inline bool geos_is_empty(const geos::geom::Geometry* g) { |
| 120 | + return !g || g->isEmpty(); |
| 121 | +} |
| 122 | + |
| 123 | +inline int geos_has_z(const geos::geom::Geometry* g) { |
| 124 | + return g ? (g->getCoordinateDimension() > 2) : 0; |
| 125 | +} |
| 126 | + |
| 127 | +inline double geos_area(const geos::geom::Geometry* g) { |
| 128 | + return g ? g->getArea() : 0.0; |
| 129 | +} |
| 130 | + |
| 131 | +inline double geos_length(const geos::geom::Geometry* g) { |
| 132 | + return g ? g->getLength() : 0.0; |
| 133 | +} |
| 134 | + |
| 135 | +inline double geos_hausdorff_distance(const geos::geom::Geometry* a, const geos::geom::Geometry* b) { |
| 136 | + if (!a || !b) return 0.0; |
| 137 | + return geos::algorithm::distance::DiscreteHausdorffDistance(*a, *b).distance(); |
| 138 | +} |
| 139 | + |
| 140 | +inline std::vector<double> geos_bounds(const geos::geom::Geometry* g) { |
| 141 | + if (!g || g->isEmpty()) return {0, 0, 0, 0}; |
| 142 | + const geos::geom::Envelope* env = g->getEnvelopeInternal(); |
| 143 | + return {env->getMinX(), env->getMinY(), env->getMaxX(), env->getMaxY()}; |
| 144 | +} |
| 145 | + |
| 146 | +} // namespace detail |
| 147 | +} // namespace shapely |
0 commit comments