GEOS 3.15.0dev
Geometry.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2009 2011 Sandro Santilli <strk@kbt.io>
7 * Copyright (C) 2005 2006 Refractions Research Inc.
8 * Copyright (C) 2001-2002 Vivid Solutions Inc.
9 *
10 * This is free software; you can redistribute and/or modify it under
11 * the terms of the GNU Lesser General Public Licence as published
12 * by the Free Software Foundation.
13 * See the COPYING file for more information.
14 *
15 **********************************************************************
16 *
17 * Last port: geom/Geometry.java rev. 1.112
18 *
19 **********************************************************************/
20
21#pragma once
22
23#ifndef USE_UNSTABLE_GEOS_CPP_API
24#ifndef _MSC_VER
25# warning "The GEOS C++ API is unstable, please use the C API instead"
26# warning "HINT: #include geos_c.h"
27#else
28#pragma message("The GEOS C++ API is unstable, please use the C API instead")
29#pragma message("HINT: #include geos_c.h")
30#endif
31#endif
32
33#include <geos/export.h>
34#include <geos/geom/Envelope.h>
35#include <geos/geom/Dimension.h> // for Dimension::DimensionType
36#include <geos/geom/GeometryComponentFilter.h> // for inheritance
37#include <geos/geom/CoordinateSequence.h> // to materialize CoordinateSequence
38
39#include <algorithm>
40#include <string>
41#include <iostream>
42#include <vector>
43#include <memory>
44
45#ifdef _MSC_VER
46#pragma warning(push)
47#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
48#pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list
49#endif
50
51// Forward declarations
52namespace geos {
53namespace geom {
54class Coordinate;
55class CoordinateFilter;
56class CoordinateSequence;
57class CoordinateSequenceFilter;
58class GeometryComponentFilter;
59class GeometryFactory;
60class GeometryFilter;
61class PrecisionModel;
62class Point;
63class IntersectionMatrix;
64}
65namespace io { // geos.io
66} // namespace geos.io
67}
68
69namespace geos { // geos
70namespace geom { // geos::geom
71
73enum GeometryTypeId : int {
90 GEOS_CIRCULARSTRING,
91 GEOS_COMPOUNDCURVE,
92 GEOS_CURVEPOLYGON,
93 GEOS_MULTICURVE,
94 GEOS_MULTISURFACE,
95};
96
97enum GeometrySortIndex {
98 SORTINDEX_POINT = 0,
99 SORTINDEX_MULTIPOINT = 1,
100 SORTINDEX_LINESTRING = 2,
101 SORTINDEX_LINEARRING = 3,
102 SORTINDEX_MULTILINESTRING = 4,
103 SORTINDEX_POLYGON = 5,
104 SORTINDEX_MULTIPOLYGON = 6,
105 SORTINDEX_GEOMETRYCOLLECTION = 7,
106 SORTINDEX_CIRCULARSTRING = 8,
107 SORTINDEX_COMPOUNDCURVE = 9,
108 SORTINDEX_CURVEPOLYGON = 10,
109 SORTINDEX_MULTICURVE = 11,
110 SORTINDEX_MULTISURFACE = 12,
111};
112
196class GEOS_DLL Geometry {
197
198public:
199
200 friend class GeometryFactory;
201
203 using ConstVect = std::vector<const Geometry*>;
204
206 using NonConstVect = std::vector<Geometry*>;
207
209 using Ptr = std::unique_ptr<Geometry> ;
210
212 std::unique_ptr<Geometry> clone() const { return std::unique_ptr<Geometry>(cloneImpl()); }
213
215 virtual ~Geometry();
216
217
225 const GeometryFactory*
227 {
228 return _factory;
229 }
230
244 void
245 setUserData(void* newUserData)
246 {
247 _userData = newUserData;
248 }
249
256 void*
258 {
259 return _userData;
260 }
261
272 int getSRID() const
273 {
274 return SRID;
275 }
276
280 virtual void
281 setSRID(int newSRID)
282 {
283 SRID = newSRID;
284 }
285
291
293 virtual const CoordinateXY* getCoordinate() const = 0; //Abstract
294
300 virtual std::unique_ptr<CoordinateSequence> getCoordinates() const = 0; //Abstract
301
303 virtual std::size_t getNumPoints() const = 0; //Abstract
304
306 virtual bool isSimple() const;
307
309 virtual std::string getGeometryType() const = 0; //Abstract
310
312 virtual bool hasCurvedComponents() const;
313
315 virtual GeometryTypeId getGeometryTypeId() const = 0; //Abstract
316
324 virtual std::size_t
326 {
327 return 1;
328 }
329
332 virtual const Geometry*
333 getGeometryN(std::size_t /*n*/) const
334 {
335 return this;
336 }
337
347 bool isValid() const;
348
350 virtual bool isEmpty() const = 0; //Abstract
351
353 virtual bool
355 {
356 return false;
357 }
358
360 virtual Dimension::DimensionType getDimension() const = 0; //Abstract
361
364 return getDimension() == d;
365 }
366
369 return d == getDimension();
370 }
371
372 bool isPuntal() const {
373 return isDimensionStrict(Dimension::P);
374 }
375
376 bool isLineal() const {
377 return isDimensionStrict(Dimension::L);
378 }
379
380 bool isPolygonal() const {
381 return isDimensionStrict(Dimension::A);
382 }
383
384 bool isMixedDimension() const;
385 bool isMixedDimension(Dimension::DimensionType* baseDim) const;
386
387 bool isCollection() const {
388 int t = getGeometryTypeId();
389 return t == GEOS_GEOMETRYCOLLECTION ||
390 t == GEOS_MULTIPOINT ||
393 }
394
395 static GeometryTypeId multiTypeId(GeometryTypeId typeId) {
396 switch (typeId) {
397 case GEOS_POINT: return GEOS_MULTIPOINT;
399 case GEOS_POLYGON: return GEOS_MULTIPOLYGON;
400 default: return typeId;
401 }
402 }
403
405 virtual uint8_t getCoordinateDimension() const = 0; //Abstract
406
407 virtual bool hasZ() const = 0;
408
409 virtual bool hasM() const = 0;
410
427 virtual std::unique_ptr<Geometry> getBoundary() const = 0; //Abstract
428
430 virtual int getBoundaryDimension() const = 0; //Abstract
431
433 virtual std::unique_ptr<Geometry> getEnvelope() const;
434
439 virtual const Envelope* getEnvelopeInternal() const = 0;
440
457 virtual bool disjoint(const Geometry* other) const;
458
463 virtual bool touches(const Geometry* other) const;
464
466 virtual bool intersects(const Geometry* g) const;
467
490 virtual bool crosses(const Geometry* g) const;
491
496 virtual bool within(const Geometry* g) const;
497
499 virtual bool contains(const Geometry* g) const;
500
506 virtual bool overlaps(const Geometry* g) const;
507
522 bool relate(const Geometry* g,
523 const std::string& intersectionPattern) const;
524
525 bool
526 relate(const Geometry& g, const std::string& intersectionPattern) const
527 {
528 return relate(&g, intersectionPattern);
529 }
530
532 std::unique_ptr<IntersectionMatrix> relate(const Geometry* g) const;
533
534 std::unique_ptr<IntersectionMatrix> relate(const Geometry& g) const;
535
541 bool equals(const Geometry* g) const;
542
581 bool covers(const Geometry* g) const;
582
613 bool coveredBy(const Geometry* g) const;
614
615
617 virtual std::string toString() const;
618
619 virtual std::string toText() const;
620
625 std::unique_ptr<Geometry> buffer(double distance) const;
626
634 std::unique_ptr<Geometry> buffer(double distance, int quadrantSegments) const;
635
672 std::unique_ptr<Geometry> buffer(double distance, int quadrantSegments,
673 int endCapStyle) const;
674
678 virtual std::unique_ptr<Geometry> convexHull() const;
679
686 std::unique_ptr<Geometry> reverse() const { return std::unique_ptr<Geometry>(reverseImpl()); }
687
697 std::unique_ptr<Geometry> intersection(const Geometry* other) const;
698
708 std::unique_ptr<Geometry> Union(const Geometry* other) const;
709 // throw(IllegalArgumentException *, TopologyException *);
710
728 Ptr Union() const;
729 // throw(IllegalArgumentException *, TopologyException *);
730
741 std::unique_ptr<Geometry> difference(const Geometry* other) const;
742
752 std::unique_ptr<Geometry> symDifference(const Geometry* other) const;
753
760 virtual bool equalsExact(const Geometry* other, double tolerance = 0)
761 const = 0; // Abstract
762
767 virtual bool equalsIdentical(const Geometry* other) const = 0;
768
769 virtual void apply_rw(const CoordinateFilter* filter) = 0; //Abstract
770 virtual void apply_ro(CoordinateFilter* filter) const = 0; //Abstract
771 virtual void apply_rw(GeometryFilter* filter);
772 virtual void apply_ro(GeometryFilter* filter) const;
773 virtual void apply_rw(GeometryComponentFilter* filter);
774 virtual void apply_ro(GeometryComponentFilter* filter) const;
775
784 virtual void apply_rw(CoordinateSequenceFilter& filter) = 0;
785
792 virtual void apply_ro(CoordinateSequenceFilter& filter) const = 0;
793
803 template <class T>
804 void
806 {
807 for(std::size_t i = 0, n = getNumGeometries(); i < n; ++i) {
808 f.filter(getGeometryN(i));
809 }
810 }
811
817 virtual void normalize() = 0; //Abstract
818
820 int compareTo(const Geometry* geom) const;
821
823 virtual double getArea() const;
824
826 virtual double getLength() const;
827
833 virtual double distance(const Geometry* g) const;
834
835
847 virtual bool isWithinDistance(const Geometry* geom,
848 double cDistance) const;
849
859 virtual std::unique_ptr<Point> getCentroid() const;
860
862 //
865 virtual bool getCentroid(CoordinateXY& ret) const;
866
877 std::unique_ptr<Point> getInteriorPoint() const;
878
885
891 virtual void geometryChangedAction() = 0;
892
893protected:
895 virtual Geometry* cloneImpl() const = 0;
896
898 virtual Geometry* reverseImpl() const = 0;
899
901 template<typename T>
902 static bool hasNonEmptyElements(const std::vector<T>* geometries) {
903 return std::any_of(geometries->begin(), geometries->end(), [](const T& g) { return !g->isEmpty(); });
904 }
905
907 static bool hasNullElements(const CoordinateSequence* list);
908
910 template<typename T>
911 static bool hasNullElements(const std::vector<T>* geometries) {
912 return std::any_of(geometries->begin(), geometries->end(), [](const T& g) { return g == nullptr; });
913 }
914
915// static void reversePointOrder(CoordinateSequence* coordinates);
916// static Coordinate& minCoordinate(CoordinateSequence* coordinates);
917// static void scroll(CoordinateSequence* coordinates,Coordinate* firstCoordinate);
918// static int indexOf(Coordinate* coordinate,CoordinateSequence* coordinates);
919//
924 virtual bool isEquivalentClass(const Geometry* other) const;
925
926 static void checkNotGeometryCollection(const Geometry* g);
927
928 virtual int compareToSameClass(const Geometry* geom) const = 0; //Abstract
929
930 template<typename T>
931 static int compare(const T& a, const T& b)
932 {
933 std::size_t i = 0;
934 std::size_t j = 0;
935 while(i < a.size() && j < b.size()) {
936 const auto& aGeom = *a[i];
937 const auto& bGeom = *b[j];
938
939 int comparison = aGeom.compareTo(&bGeom);
940 if(comparison != 0) {
941 return comparison;
942 }
943
944 i++;
945 j++;
946 }
947
948 if(i < a.size()) {
949 return 1;
950 }
951
952 if(j < b.size()) {
953 return -1;
954 }
955
956 return 0;
957 }
958
959 bool equal(const CoordinateXY& a, const CoordinateXY& b,
960 double tolerance) const;
961 int SRID;
962
963 Geometry(const Geometry& geom);
964
974 Geometry(const GeometryFactory* factory);
975
976 template<typename T>
977 static std::vector<std::unique_ptr<Geometry>> toGeometryArray(std::vector<std::unique_ptr<T>> && v) {
978 static_assert(std::is_base_of<Geometry, T>::value, "");
979 std::vector<std::unique_ptr<Geometry>> gv(v.size());
980 for (std::size_t i = 0; i < v.size(); i++) {
981 gv[i] = std::move(v[i]);
982 }
983 return gv;
984 }
985
986 static std::vector<std::unique_ptr<Geometry>> toGeometryArray(std::vector<std::unique_ptr<Geometry>> && v) {
987 return std::move(v);
988 }
989
990protected:
991
992 virtual int getSortIndex() const = 0;
993
994
995private:
996
997 class GEOS_DLL GeometryChangedFilter : public GeometryComponentFilter {
998 public:
999 void filter_rw(Geometry* geom) override;
1000 };
1001
1002 static GeometryChangedFilter geometryChangedFilter;
1003
1008 const GeometryFactory* _factory;
1009
1010 void* _userData;
1011};
1012
1017GEOS_DLL std::ostream& operator<< (std::ostream& os, const Geometry& geom);
1018
1019struct GEOS_DLL GeometryGreaterThen {
1020 bool operator()(const Geometry* first, const Geometry* second);
1021};
1022
1023
1025GEOS_DLL std::string geosversion();
1026
1032GEOS_DLL std::string jtsport();
1033
1034// We use this instead of std::pair<unique_ptr<Geometry>> because C++11
1035// forbids that construct:
1036// http://lwg.github.com/issues/lwg-closed.html#2068
1037struct GeomPtrPair {
1038 typedef std::unique_ptr<Geometry> GeomPtr;
1039 GeomPtr first;
1040 GeomPtr second;
1041};
1042
1043} // namespace geos::geom
1044} // namespace geos
1045
1046#ifdef _MSC_VER
1047#pragma warning(pop)
1048#endif
Geometry classes support the concept of applying a coordinate filter to every coordinate in the Geome...
Definition CoordinateFilter.h:43
Interface for classes which provide operations that can be applied to the coordinates in a Coordinate...
Definition CoordinateSequenceFilter.h:56
The internal representation of a list of coordinates inside a Geometry.
Definition CoordinateSequence.h:56
DimensionType
Definition Dimension.h:29
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition Envelope.h:59
Definition GeometryComponentFilter.h:41
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:70
Geometry classes support the concept of applying a Geometry filter to the Geometry.
Definition GeometryFilter.h:45
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:196
virtual std::unique_ptr< Geometry > getBoundary() const =0
Returns the boundary, or an empty geometry of appropriate dimension if this Geometry is empty.
virtual int getBoundaryDimension() const =0
Returns the dimension of this Geometrys inherent boundary.
virtual bool equalsIdentical(const Geometry *other) const =0
Returns true if the two geometries are of the same type and their vertices corresponding by index are...
std::vector< Geometry * > NonConstVect
A vector of non-const Geometry pointers.
Definition Geometry.h:206
void applyComponentFilter(T &f) const
Apply a filter to each component of this geometry. The filter is expected to provide a ....
Definition Geometry.h:805
virtual const CoordinateXY * getCoordinate() const =0
Returns a vertex of this Geometry, or NULL if this is the empty geometry.
std::unique_ptr< Geometry > symDifference(const Geometry *other) const
Returns a set combining the points in this Geometry not in other, and the points in other not in this...
std::unique_ptr< Geometry > reverse() const
Computes a new geometry which has all component coordinate sequences in reverse order (opposite orien...
Definition Geometry.h:686
virtual ~Geometry()
Destroy Geometry and all components.
virtual void apply_rw(CoordinateSequenceFilter &filter)=0
const PrecisionModel * getPrecisionModel() const
Get the PrecisionModel used to create this Geometry.
std::unique_ptr< IntersectionMatrix > relate(const Geometry *g) const
Returns the DE-9IM intersection matrix for the two Geometrys.
virtual void geometryChangedAction()=0
Notifies this Geometry that its Coordinates have been changed by an external party.
virtual bool equalsExact(const Geometry *other, double tolerance=0) const =0
Returns true iff the two Geometrys are of the same type and their vertices corresponding by index are...
virtual bool isDimensionStrict(Dimension::DimensionType d) const
Checks whether this Geometry consists only of components having dimension d.
Definition Geometry.h:368
virtual void normalize()=0
virtual void apply_ro(CoordinateSequenceFilter &filter) const =0
virtual bool intersects(const Geometry *g) const
Returns true if disjoint returns false.
virtual bool crosses(const Geometry *g) const
virtual GeometryTypeId getGeometryTypeId() const =0
Return an integer representation of this Geometry type.
int compareTo(const Geometry *geom) const
Comparator for sorting geometry.
virtual std::unique_ptr< Geometry > convexHull() const
Returns the smallest convex Polygon that contains all the points in the Geometry.
virtual bool hasDimension(Dimension::DimensionType d) const
Checks whether any component of this geometry has dimension d.
Definition Geometry.h:363
virtual std::string getGeometryType() const =0
Return a string representation of this Geometry type.
virtual uint8_t getCoordinateDimension() const =0
Returns the coordinate dimension of this Geometry (2=XY, 3=XYZ or XYM, 4=XYZM).
virtual bool isWithinDistance(const Geometry *geom, double cDistance) const
Tests whether the distance from this Geometry to another is less than or equal to a specified value.
virtual double getLength() const
Returns the length of this Geometry.
virtual double distance(const Geometry *g) const
std::unique_ptr< Point > getInteriorPoint() const
Computes an interior point of this Geometry.
std::unique_ptr< Geometry > intersection(const Geometry *other) const
Returns a Geometry representing the points shared by this Geometry and other.
virtual bool getCentroid(CoordinateXY &ret) const
Computes the centroid of this Geometry as a Coordinate.
std::unique_ptr< Geometry > Ptr
An unique_ptr of Geometry.
Definition Geometry.h:209
virtual const Geometry * getGeometryN(std::size_t) const
Returns a pointer to the nth Geometry in this collection (or self if this is not a collection)
Definition Geometry.h:333
virtual bool hasCurvedComponents() const
Returns whether the Geometry contains curved components.
virtual bool within(const Geometry *g) const
Returns true if the DE-9IM intersection matrix for the two Geometrys is T*F**F***.
virtual std::size_t getNumPoints() const =0
Returns the count of this Geometrys vertices.
static bool hasNullElements(const CoordinateSequence *list)
Returns true if the CoordinateSequence contains any null elements.
virtual Geometry * reverseImpl() const =0
Make a geometry with coordinates in reverse order.
void geometryChanged()
Notifies this Geometry that its Coordinates have been changed by an external party (using a Coordinat...
void setUserData(void *newUserData)
A simple scheme for applications to add their own custom data to a Geometry. An example use might be ...
Definition Geometry.h:245
virtual bool isEmpty() const =0
Returns whether or not the set of points in this Geometry is empty.
bool relate(const Geometry *g, const std::string &intersectionPattern) const
Returns true if the elements in the DE-9IM intersection matrix for the two Geometrys match the elemen...
Geometry(const GeometryFactory *factory)
Construct a geometry with the given GeometryFactory.
bool isValid() const
Tests the validity of this Geometry.
std::vector< const Geometry * > ConstVect
A vector of const Geometry pointers.
Definition Geometry.h:203
const GeometryFactory * getFactory() const
Gets the factory which contains the context in which this geometry was created.
Definition Geometry.h:226
virtual bool touches(const Geometry *other) const
Returns true if the DE-9IM intersection matrix for the two Geometrys is FT*******,...
virtual std::unique_ptr< Geometry > getEnvelope() const
Returns this Geometrys bounding box.
virtual std::string toString() const
Returns the Well-known Text representation of this Geometry.
std::unique_ptr< Geometry > buffer(double distance, int quadrantSegments) const
Returns a buffer region around this Geometry having the given width and with a specified number of se...
virtual Geometry * cloneImpl() const =0
Make a deep-copy of this Geometry.
virtual bool isSimple() const
Returns false if the Geometry not simple.
static bool hasNonEmptyElements(const std::vector< T > *geometries)
Returns true if the array contains any non-empty Geometrys.
Definition Geometry.h:902
Ptr Union() const
Computes the union of all the elements of this geometry. Heterogeneous GeometryCollections are fully ...
void * getUserData() const
Gets the user data object for this geometry, if any.
Definition Geometry.h:257
std::unique_ptr< Geometry > difference(const Geometry *other) const
Returns a Geometry representing the points making up this Geometry that do not make up other.
virtual bool disjoint(const Geometry *other) const
virtual bool contains(const Geometry *g) const
Returns true if other.within(this) returns true.
virtual bool isEquivalentClass(const Geometry *other) const
Returns whether the two Geometrys are equal, from the point of view of the equalsExact method.
std::unique_ptr< Geometry > buffer(double distance) const
virtual std::unique_ptr< Point > getCentroid() const
Computes the centroid of this Geometry.
bool coveredBy(const Geometry *g) const
Tests whether this geometry is covered by the specified geometry.
virtual std::unique_ptr< CoordinateSequence > getCoordinates() const =0
Returns this Geometry vertices. Caller takes ownership of the returned object.
std::unique_ptr< Geometry > Union(const Geometry *other) const
Returns a Geometry representing all the points in this Geometry and other.
virtual std::size_t getNumGeometries() const
Returns the number of geometries in this collection, or 1 if this is not a collection.
Definition Geometry.h:325
std::unique_ptr< Geometry > clone() const
Make a deep-copy of this Geometry.
Definition Geometry.h:212
virtual double getArea() const
Returns the area of this Geometry.
virtual bool isRectangle() const
Polygon overrides to check for actual rectangle.
Definition Geometry.h:354
virtual bool overlaps(const Geometry *g) const
Returns true if the DE-9IM intersection matrix for the two Geometrys is T*T***T** (for two points or ...
bool covers(const Geometry *g) const
Returns true if this geometry covers the specified geometry.
virtual Dimension::DimensionType getDimension() const =0
Returns the dimension of this Geometry (0=point, 1=line, 2=surface)
std::unique_ptr< Geometry > buffer(double distance, int quadrantSegments, int endCapStyle) const
Computes a buffer area around this geometry having the given width and with a specified accuracy of a...
virtual const Envelope * getEnvelopeInternal() const =0
Returns the minimum and maximum x and y values in this Geometry, or a null Envelope if this Geometry ...
int getSRID() const
Returns the ID of the Spatial Reference System used by the Geometry.
Definition Geometry.h:272
static bool hasNullElements(const std::vector< T > *geometries)
Returns true if the vector contains any null elements.
Definition Geometry.h:911
virtual void setSRID(int newSRID)
Sets the ID of the Spatial Reference System used by the Geometry.
Definition Geometry.h:281
bool equals(const Geometry *g) const
Returns true if the DE-9IM intersection matrix for the two Geometrys is T*F**FFF*.
Specifies the precision model of the Coordinate in a Geometry.
Definition PrecisionModel.h:87
@ GEOS_MULTILINESTRING
Definition geos_c.h:223
@ GEOS_GEOMETRYCOLLECTION
Definition geos_c.h:227
@ GEOS_POINT
Definition geos_c.h:213
@ GEOS_MULTIPOLYGON
Definition geos_c.h:225
@ GEOS_POLYGON
Definition geos_c.h:219
@ GEOS_MULTIPOINT
Definition geos_c.h:221
@ GEOS_LINESTRING
Definition geos_c.h:215
std::string jtsport()
Return the version of JTS this GEOS release has been ported from.
std::string geosversion()
Return current GEOS version.
GeometryTypeId
Geometry types.
Definition Geometry.h:73
@ GEOS_MULTILINESTRING
a collection of linestrings
Definition Geometry.h:85
@ GEOS_LINEARRING
a linear ring (linestring with 1st point == last point)
Definition Geometry.h:79
@ GEOS_GEOMETRYCOLLECTION
a collection of heterogeneus geometries
Definition Geometry.h:89
@ GEOS_POINT
a point
Definition Geometry.h:75
@ GEOS_LINESTRING
a linestring
Definition Geometry.h:77
@ GEOS_POLYGON
a polygon
Definition Geometry.h:81
@ GEOS_MULTIPOLYGON
a collection of polygons
Definition Geometry.h:87
@ GEOS_MULTIPOINT
a collection of points
Definition Geometry.h:83
Basic namespace for all GEOS functionalities.
Definition geos.h:38