GEOS  3.11.1
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 
38 #include <algorithm>
39 #include <string>
40 #include <iostream>
41 #include <vector>
42 #include <memory>
43 
44 #ifdef _MSC_VER
45 #pragma warning(push)
46 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
47 #pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list
48 #endif
49 
50 // Forward declarations
51 namespace geos {
52 namespace geom {
53 class Coordinate;
54 class CoordinateFilter;
55 class CoordinateSequence;
56 class CoordinateSequenceFilter;
57 class GeometryComponentFilter;
58 class GeometryFactory;
59 class GeometryFilter;
60 class PrecisionModel;
61 class Point;
62 class IntersectionMatrix;
63 }
64 namespace io { // geos.io
65 class Unload;
66 } // namespace geos.io
67 }
68 
69 namespace geos {
70 namespace geom { // geos::geom
71 
90 };
91 
92 enum GeometrySortIndex {
93  SORTINDEX_POINT = 0,
94  SORTINDEX_MULTIPOINT = 1,
95  SORTINDEX_LINESTRING = 2,
96  SORTINDEX_LINEARRING = 3,
97  SORTINDEX_MULTILINESTRING = 4,
98  SORTINDEX_POLYGON = 5,
99  SORTINDEX_MULTIPOLYGON = 6,
100  SORTINDEX_GEOMETRYCOLLECTION = 7
101 };
102 
186 class GEOS_DLL Geometry {
187 
188 public:
189 
190  friend class GeometryFactory;
191 
193  using ConstVect = std::vector<const Geometry*>;
194 
196  using NonConstVect = std::vector<Geometry*>;
197 
199  using Ptr = std::unique_ptr<Geometry> ;
200 
202  std::unique_ptr<Geometry> clone() const { return std::unique_ptr<Geometry>(cloneImpl()); }
203 
205  virtual ~Geometry();
206 
207 
215  const GeometryFactory*
216  getFactory() const
217  {
218  return _factory;
219  }
220 
234  void
235  setUserData(void* newUserData)
236  {
237  _userData = newUserData;
238  }
239 
246  void*
247  getUserData() const
248  {
249  return _userData;
250  }
251 
262  virtual int
263  getSRID() const
264  {
265  return SRID;
266  }
267 
271  virtual void
272  setSRID(int newSRID)
273  {
274  SRID = newSRID;
275  }
276 
281  const PrecisionModel* getPrecisionModel() const;
282 
284  virtual const Coordinate* getCoordinate() const = 0; //Abstract
285 
291  virtual std::unique_ptr<CoordinateSequence> getCoordinates() const = 0; //Abstract
292 
294  virtual std::size_t getNumPoints() const = 0; //Abstract
295 
297  virtual bool isSimple() const;
298 
300  virtual std::string getGeometryType() const = 0; //Abstract
301 
303  virtual GeometryTypeId getGeometryTypeId() const = 0; //Abstract
304 
307  virtual std::size_t
309  {
310  return 1;
311  }
312 
315  virtual const Geometry*
316  getGeometryN(std::size_t /*n*/) const
317  {
318  return this;
319  }
320 
330  virtual bool isValid() const;
331 
333  virtual bool isEmpty() const = 0; //Abstract
334 
336  virtual bool
337  isRectangle() const
338  {
339  return false;
340  }
341 
343  virtual Dimension::DimensionType getDimension() const = 0; //Abstract
344 
347  return d == getDimension();
348  }
349 
350  bool isPuntal() const {
351  return isDimensionStrict(Dimension::P);
352  }
353 
354  bool isLineal() const {
355  return isDimensionStrict(Dimension::L);
356  }
357 
358  bool isPolygonal() const {
359  return isDimensionStrict(Dimension::A);
360  }
361 
362  bool isCollection() const {
363  int t = getGeometryTypeId();
364  return t == GEOS_GEOMETRYCOLLECTION ||
365  t == GEOS_MULTIPOINT ||
366  t == GEOS_MULTILINESTRING ||
367  t == GEOS_MULTIPOLYGON;
368  }
369 
371  virtual uint8_t getCoordinateDimension() const = 0; //Abstract
372 
389  virtual std::unique_ptr<Geometry> getBoundary() const = 0; //Abstract
390 
392  virtual int getBoundaryDimension() const = 0; //Abstract
393 
395  virtual std::unique_ptr<Geometry> getEnvelope() const;
396 
401  virtual const Envelope* getEnvelopeInternal() const;
402 
419  virtual bool disjoint(const Geometry* other) const;
420 
425  virtual bool touches(const Geometry* other) const;
426 
428  virtual bool intersects(const Geometry* g) const;
429 
452  virtual bool crosses(const Geometry* g) const;
453 
458  virtual bool within(const Geometry* g) const;
459 
461  virtual bool contains(const Geometry* g) const;
462 
468  virtual bool overlaps(const Geometry* g) const;
469 
484  bool relate(const Geometry* g,
485  const std::string& intersectionPattern) const;
486 
487  bool
488  relate(const Geometry& g, const std::string& intersectionPattern) const
489  {
490  return relate(&g, intersectionPattern);
491  }
492 
494  std::unique_ptr<IntersectionMatrix> relate(const Geometry* g) const;
495 
496  std::unique_ptr<IntersectionMatrix> relate(const Geometry& g) const;
497 
503  virtual bool equals(const Geometry* g) const;
504 
543  bool covers(const Geometry* g) const;
544 
575  bool
576  coveredBy(const Geometry* g) const
577  {
578  return g->covers(this);
579  }
580 
581 
583  virtual std::string toString() const;
584 
585  virtual std::string toText() const;
586 
591  std::unique_ptr<Geometry> buffer(double distance) const;
592 
600  std::unique_ptr<Geometry> buffer(double distance, int quadrantSegments) const;
601 
638  std::unique_ptr<Geometry> buffer(double distance, int quadrantSegments,
639  int endCapStyle) const;
640 
644  virtual std::unique_ptr<Geometry> convexHull() const;
645 
652  std::unique_ptr<Geometry> reverse() const { return std::unique_ptr<Geometry>(reverseImpl()); }
653 
663  std::unique_ptr<Geometry> intersection(const Geometry* other) const;
664 
674  std::unique_ptr<Geometry> Union(const Geometry* other) const;
675  // throw(IllegalArgumentException *, TopologyException *);
676 
694  Ptr Union() const;
695  // throw(IllegalArgumentException *, TopologyException *);
696 
707  std::unique_ptr<Geometry> difference(const Geometry* other) const;
708 
718  std::unique_ptr<Geometry> symDifference(const Geometry* other) const;
719 
724  virtual bool equalsExact(const Geometry* other, double tolerance = 0)
725  const = 0; // Abstract
726 
727  virtual void apply_rw(const CoordinateFilter* filter) = 0; //Abstract
728  virtual void apply_ro(CoordinateFilter* filter) const = 0; //Abstract
729  virtual void apply_rw(GeometryFilter* filter);
730  virtual void apply_ro(GeometryFilter* filter) const;
731  virtual void apply_rw(GeometryComponentFilter* filter);
732  virtual void apply_ro(GeometryComponentFilter* filter) const;
733 
742  virtual void apply_rw(CoordinateSequenceFilter& filter) = 0;
743 
750  virtual void apply_ro(CoordinateSequenceFilter& filter) const = 0;
751 
761  template <class T>
762  void
764  {
765  for(std::size_t i = 0, n = getNumGeometries(); i < n; ++i) {
766  f.filter(getGeometryN(i));
767  }
768  }
769 
775  virtual void normalize() = 0; //Abstract
776 
778  virtual int compareTo(const Geometry* geom) const;
779 
784  virtual double distance(const Geometry* g) const;
785 
787  virtual double getArea() const;
788 
790  virtual double getLength() const;
791 
803  virtual bool isWithinDistance(const Geometry* geom,
804  double cDistance) const;
805 
815  virtual std::unique_ptr<Point> getCentroid() const;
816 
818  //
821  virtual bool getCentroid(Coordinate& ret) const;
822 
833  std::unique_ptr<Point> getInteriorPoint() const;
834 
840  virtual void geometryChanged();
841 
847  void geometryChangedAction();
848 
849 protected:
850 
852  mutable std::unique_ptr<Envelope> envelope;
853 
855  virtual Geometry* cloneImpl() const = 0;
856 
858  virtual Geometry* reverseImpl() const = 0;
859 
861  template<typename T>
862  static bool hasNonEmptyElements(const std::vector<T>* geometries) {
863  return std::any_of(geometries->begin(), geometries->end(), [](const T& g) { return !g->isEmpty(); });
864  }
865 
867  static bool hasNullElements(const CoordinateSequence* list);
868 
870  template<typename T>
871  static bool hasNullElements(const std::vector<T>* geometries) {
872  return std::any_of(geometries->begin(), geometries->end(), [](const T& g) { return g == nullptr; });
873  }
874 
875 // static void reversePointOrder(CoordinateSequence* coordinates);
876 // static Coordinate& minCoordinate(CoordinateSequence* coordinates);
877 // static void scroll(CoordinateSequence* coordinates,Coordinate* firstCoordinate);
878 // static int indexOf(Coordinate* coordinate,CoordinateSequence* coordinates);
879 //
884  virtual bool isEquivalentClass(const Geometry* other) const;
885 
886  static void checkNotGeometryCollection(const Geometry* g);
887  // throw(IllegalArgumentException *);
888 
889  //virtual void checkEqualSRID(Geometry *other);
890 
891  //virtual void checkEqualPrecisionModel(Geometry *other);
892 
893  virtual Envelope::Ptr computeEnvelopeInternal() const = 0; //Abstract
894 
895  virtual int compareToSameClass(const Geometry* geom) const = 0; //Abstract
896 
897  int compare(std::vector<Coordinate> a, std::vector<Coordinate> b) const;
898 
899  int compare(std::vector<Geometry*> a, std::vector<Geometry*> b) const;
900 
901  int compare(const std::vector<std::unique_ptr<Geometry>> & a, const std::vector<std::unique_ptr<Geometry>> & b) const;
902 
903  bool equal(const Coordinate& a, const Coordinate& b,
904  double tolerance) const;
905  int SRID;
906 
907  Geometry(const Geometry& geom);
908 
918  Geometry(const GeometryFactory* factory);
919 
920  template<typename T>
921  static std::vector<std::unique_ptr<Geometry>> toGeometryArray(std::vector<std::unique_ptr<T>> && v) {
922  static_assert(std::is_base_of<Geometry, T>::value, "");
923  std::vector<std::unique_ptr<Geometry>> gv(v.size());
924  for (std::size_t i = 0; i < v.size(); i++) {
925  gv[i] = std::move(v[i]);
926  }
927  return gv;
928  }
929 
930 protected:
931 
932  virtual int getSortIndex() const = 0;
933 
934 
935 private:
936 
937  class GEOS_DLL GeometryChangedFilter : public GeometryComponentFilter {
938  public:
939  void filter_rw(Geometry* geom) override;
940  };
941 
942  static GeometryChangedFilter geometryChangedFilter;
943 
948  const GeometryFactory* _factory;
949 
950  void* _userData;
951 };
952 
957 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Geometry& geom);
958 
959 struct GEOS_DLL GeometryGreaterThen {
960  bool operator()(const Geometry* first, const Geometry* second);
961 };
962 
963 
965 GEOS_DLL std::string geosversion();
966 
972 GEOS_DLL std::string jtsport();
973 
974 // We use this instead of std::pair<unique_ptr<Geometry>> because C++11
975 // forbids that construct:
976 // http://lwg.github.com/issues/lwg-closed.html#2068
977 struct GeomPtrPair {
978  typedef std::unique_ptr<Geometry> GeomPtr;
979  GeomPtr first;
980  GeomPtr second;
981 };
982 
983 } // namespace geos::geom
984 } // namespace geos
985 
986 #ifdef _MSC_VER
987 #pragma warning(pop)
988 #endif
989 
Geometry classes support the concept of applying a Geometry filter to the Geometry.
Definition: GeometryFilter.h:45
void * getUserData() const
Gets the user data object for this geometry, if any.
Definition: Geometry.h:247
a linestring
Definition: Geometry.h:77
a collection of heterogeneus geometries
Definition: Geometry.h:89
a collection of linestrings
Definition: Geometry.h:85
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
void applyComponentFilter(T &f) const
Apply a filter to each component of this geometry. The filter is expected to provide a ...
Definition: Geometry.h:763
std::unique_ptr< Geometry > clone() const
Make a deep-copy of this Geometry.
Definition: Geometry.h:202
Interface for classes which provide operations that can be applied to the coordinates in a Coordinate...
Definition: CoordinateSequenceFilter.h:55
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:90
const GeometryFactory * getFactory() const
Gets the factory which contains the context in which this geometry was created.
Definition: Geometry.h:216
std::string geosversion()
Return current GEOS version.
GeometryTypeId
Geometry types.
Definition: Geometry.h:73
virtual bool isDimensionStrict(Dimension::DimensionType d) const
Checks whether this Geometry consists only of components having dimension d.
Definition: Geometry.h:346
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Geometry classes support the concept of applying a coordinate filter to every coordinate in the Geome...
Definition: CoordinateFilter.h:41
a collection of points
Definition: Geometry.h:83
std::string jtsport()
Return the version of JTS this GEOS release has been ported from.
virtual int getSRID() const
Returns the ID of the Spatial Reference System used by the Geometry.
Definition: Geometry.h:263
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:235
Dimension value of a curve (1).
Definition: Dimension.h:43
Dimension value of a surface (2).
Definition: Dimension.h:46
virtual bool isRectangle() const
Polygon overrides to check for actual rectangle.
Definition: Geometry.h:337
std::unique_ptr< Geometry > reverse() const
Computes a new geometry which has all component coordinate sequences in reverse order (opposite orien...
Definition: Geometry.h:652
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:66
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:316
std::vector< Geometry * > NonConstVect
A vector of non-const Geometry pointers.
Definition: Geometry.h:196
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25
bool coveredBy(const Geometry *g) const
Tests whether this geometry is covered by the specified geometry.
Definition: Geometry.h:576
virtual void setSRID(int newSRID)
Sets the ID of the Spatial Reference System used by the Geometry.
Definition: Geometry.h:272
a polygon
Definition: Geometry.h:81
a linear ring (linestring with 1st point == last point)
Definition: Geometry.h:79
static bool hasNullElements(const std::vector< T > *geometries)
Returns true if the vector contains any null elements.
Definition: Geometry.h:871
a point
Definition: Geometry.h:75
Dimension value of a point (0).
Definition: Dimension.h:40
virtual std::size_t getNumGeometries() const
Definition: Geometry.h:308
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:44
static bool hasNonEmptyElements(const std::vector< T > *geometries)
Returns true if the array contains any non-empty Geometrys.
Definition: Geometry.h:862
bool covers(const Geometry *g) const
Returns true if this geometry covers the specified geometry.
std::unique_ptr< Geometry > Ptr
An unique_ptr of Geometry.
Definition: Geometry.h:199
a collection of polygons
Definition: Geometry.h:87
std::unique_ptr< Envelope > envelope
The bounding box of this Geometry.
Definition: Geometry.h:852
std::vector< const Geometry * > ConstVect
A vector of const Geometry pointers.
Definition: Geometry.h:193
DimensionType
Definition: Dimension.h:29
Definition: GeometryComponentFilter.h:41