GEOS  3.9.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 #ifndef GEOS_GEOM_GEOMETRY_H
22 #define GEOS_GEOM_GEOMETRY_H
23 
24 #ifndef USE_UNSTABLE_GEOS_CPP_API
25 #ifndef _MSC_VER
26 # warning "The GEOS C++ API is unstable, please use the C API instead"
27 # warning "HINT: #include geos_c.h"
28 #else
29 #pragma message("The GEOS C++ API is unstable, please use the C API instead")
30 #pragma message("HINT: #include geos_c.h")
31 #endif
32 #endif
33 
34 #include <geos/export.h>
35 #include <geos/inline.h>
36 #include <geos/geom/Envelope.h>
37 #include <geos/geom/Dimension.h> // for Dimension::DimensionType
38 #include <geos/geom/GeometryComponentFilter.h> // for inheritance
39 #include <geos/geom/IntersectionMatrix.h>
40 
41 #include <algorithm>
42 #include <string>
43 #include <iostream>
44 #include <vector>
45 #include <memory>
46 
47 #ifdef _MSC_VER
48 #pragma warning(push)
49 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
50 #pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list
51 #endif
52 
53 // Forward declarations
54 namespace geos {
55 namespace geom {
56 class Coordinate;
57 class CoordinateFilter;
58 class CoordinateSequence;
59 class CoordinateSequenceFilter;
60 class GeometryComponentFilter;
61 class GeometryFactory;
62 class GeometryFilter;
63 class PrecisionModel;
64 class Point;
65 }
66 namespace io { // geos.io
67 class Unload;
68 } // namespace geos.io
69 }
70 
71 namespace geos {
72 namespace geom { // geos::geom
73 
92 };
93 
94 enum GeometrySortIndex {
95  SORTINDEX_POINT = 0,
96  SORTINDEX_MULTIPOINT = 1,
97  SORTINDEX_LINESTRING = 2,
98  SORTINDEX_LINEARRING = 3,
99  SORTINDEX_MULTILINESTRING = 4,
100  SORTINDEX_POLYGON = 5,
101  SORTINDEX_MULTIPOLYGON = 6,
102  SORTINDEX_GEOMETRYCOLLECTION = 7
103 };
104 
188 class GEOS_DLL Geometry {
189 
190 public:
191 
192  friend class GeometryFactory;
193 
195  using ConstVect = std::vector<const Geometry*>;
196 
198  using NonConstVect = std::vector<Geometry*>;
199 
201  using Ptr = std::unique_ptr<Geometry> ;
202 
204  virtual std::unique_ptr<Geometry> clone() const = 0;
205 
207  virtual ~Geometry();
208 
209 
217  const GeometryFactory*
218  getFactory() const
219  {
220  return _factory;
221  }
222 
236  void
237  setUserData(void* newUserData)
238  {
239  _userData = newUserData;
240  }
241 
248  void*
249  getUserData() const
250  {
251  return _userData;
252  }
253 
264  virtual int
265  getSRID() const
266  {
267  return SRID;
268  }
269 
273  virtual void
274  setSRID(int newSRID)
275  {
276  SRID = newSRID;
277  }
278 
283  const PrecisionModel* getPrecisionModel() const;
284 
286  virtual const Coordinate* getCoordinate() const = 0; //Abstract
287 
293  virtual std::unique_ptr<CoordinateSequence> getCoordinates() const = 0; //Abstract
294 
296  virtual std::size_t getNumPoints() const = 0; //Abstract
297 
299  virtual bool isSimple() const;
300 
302  virtual std::string getGeometryType() const = 0; //Abstract
303 
305  virtual GeometryTypeId getGeometryTypeId() const = 0; //Abstract
306 
309  virtual std::size_t
311  {
312  return 1;
313  }
314 
317  virtual const Geometry*
318  getGeometryN(std::size_t /*n*/) const
319  {
320  return this;
321  }
322 
332  virtual bool isValid() const;
333 
335  virtual bool isEmpty() const = 0; //Abstract
336 
338  virtual bool
339  isRectangle() const
340  {
341  return false;
342  }
343 
345  virtual Dimension::DimensionType getDimension() const = 0; //Abstract
346 
349  return d == getDimension();
350  }
351 
352  bool isPuntal() const {
353  return isDimensionStrict(Dimension::P);
354  }
355 
356  bool isLineal() const {
357  return isDimensionStrict(Dimension::L);
358  }
359 
360  bool isPolygonal() const {
361  return isDimensionStrict(Dimension::A);
362  }
363 
364  bool isCollection() const {
365  int t = getGeometryTypeId();
366  return t == GEOS_GEOMETRYCOLLECTION ||
367  t == GEOS_MULTIPOINT ||
368  t == GEOS_MULTILINESTRING ||
369  t == GEOS_MULTIPOLYGON;
370  }
371 
373  virtual int getCoordinateDimension() const = 0; //Abstract
374 
391  virtual std::unique_ptr<Geometry> getBoundary() const = 0; //Abstract
392 
394  virtual int getBoundaryDimension() const = 0; //Abstract
395 
397  virtual std::unique_ptr<Geometry> getEnvelope() const;
398 
403  virtual const Envelope* getEnvelopeInternal() const;
404 
421  virtual bool disjoint(const Geometry* other) const;
422 
427  virtual bool touches(const Geometry* other) const;
428 
430  virtual bool intersects(const Geometry* g) const;
431 
454  virtual bool crosses(const Geometry* g) const;
455 
460  virtual bool within(const Geometry* g) const;
461 
463  virtual bool contains(const Geometry* g) const;
464 
470  virtual bool overlaps(const Geometry* g) const;
471 
486  bool relate(const Geometry* g,
487  const std::string& intersectionPattern) const;
488 
489  bool
490  relate(const Geometry& g, const std::string& intersectionPattern) const
491  {
492  return relate(&g, intersectionPattern);
493  }
494 
496  std::unique_ptr<IntersectionMatrix> relate(const Geometry* g) const;
497 
498  std::unique_ptr<IntersectionMatrix> relate(const Geometry& g) const
499  {
500  return relate(&g);
501  }
502 
508  virtual bool equals(const Geometry* g) const;
509 
548  bool covers(const Geometry* g) const;
549 
580  bool
581  coveredBy(const Geometry* g) const
582  {
583  return g->covers(this);
584  }
585 
586 
588  virtual std::string toString() const;
589 
590  virtual std::string toText() const;
591 
596  std::unique_ptr<Geometry> buffer(double distance) const;
597 
605  std::unique_ptr<Geometry> buffer(double distance, int quadrantSegments) const;
606 
643  std::unique_ptr<Geometry> buffer(double distance, int quadrantSegments,
644  int endCapStyle) const;
645 
649  virtual std::unique_ptr<Geometry> convexHull() const;
650 
657  virtual std::unique_ptr<Geometry> reverse() const = 0;
658 
668  std::unique_ptr<Geometry> intersection(const Geometry* other) const;
669 
679  std::unique_ptr<Geometry> Union(const Geometry* other) const;
680  // throw(IllegalArgumentException *, TopologyException *);
681 
699  Ptr Union() const;
700  // throw(IllegalArgumentException *, TopologyException *);
701 
712  std::unique_ptr<Geometry> difference(const Geometry* other) const;
713 
723  std::unique_ptr<Geometry> symDifference(const Geometry* other) const;
724 
729  virtual bool equalsExact(const Geometry* other, double tolerance = 0)
730  const = 0; // Abstract
731 
732  virtual void apply_rw(const CoordinateFilter* filter) = 0; //Abstract
733  virtual void apply_ro(CoordinateFilter* filter) const = 0; //Abstract
734  virtual void apply_rw(GeometryFilter* filter);
735  virtual void apply_ro(GeometryFilter* filter) const;
736  virtual void apply_rw(GeometryComponentFilter* filter);
737  virtual void apply_ro(GeometryComponentFilter* filter) const;
738 
747  virtual void apply_rw(CoordinateSequenceFilter& filter) = 0;
748 
755  virtual void apply_ro(CoordinateSequenceFilter& filter) const = 0;
756 
766  template <class T>
767  void
769  {
770  for(std::size_t i = 0, n = getNumGeometries(); i < n; ++i) {
771  f.filter(getGeometryN(i));
772  }
773  }
774 
780  virtual void normalize() = 0; //Abstract
781 
783  virtual int compareTo(const Geometry* geom) const;
784 
789  virtual double distance(const Geometry* g) const;
790 
792  virtual double getArea() const;
793 
795  virtual double getLength() const;
796 
808  virtual bool isWithinDistance(const Geometry* geom,
809  double cDistance) const;
810 
820  virtual std::unique_ptr<Point> getCentroid() const;
821 
823  //
826  virtual bool getCentroid(Coordinate& ret) const;
827 
838  std::unique_ptr<Point> getInteriorPoint() const;
839 
845  virtual void geometryChanged();
846 
852  void geometryChangedAction();
853 
854 protected:
855 
857  mutable std::unique_ptr<Envelope> envelope;
858 
860  template<typename T>
861  static bool hasNonEmptyElements(const std::vector<T>* geometries) {
862  return std::any_of(geometries->begin(), geometries->end(), [](const T& g) { return !g->isEmpty(); });
863  }
864 
866  static bool hasNullElements(const CoordinateSequence* list);
867 
869  template<typename T>
870  static bool hasNullElements(const std::vector<T>* geometries) {
871  return std::any_of(geometries->begin(), geometries->end(), [](const T& g) { return g == nullptr; });
872  }
873 
874 // static void reversePointOrder(CoordinateSequence* coordinates);
875 // static Coordinate& minCoordinate(CoordinateSequence* coordinates);
876 // static void scroll(CoordinateSequence* coordinates,Coordinate* firstCoordinate);
877 // static int indexOf(Coordinate* coordinate,CoordinateSequence* coordinates);
878 //
883  virtual bool isEquivalentClass(const Geometry* other) const;
884 
885  static void checkNotGeometryCollection(const Geometry* g);
886  // throw(IllegalArgumentException *);
887 
888  //virtual void checkEqualSRID(Geometry *other);
889 
890  //virtual void checkEqualPrecisionModel(Geometry *other);
891 
892  virtual Envelope::Ptr computeEnvelopeInternal() const = 0; //Abstract
893 
894  virtual int compareToSameClass(const Geometry* geom) const = 0; //Abstract
895 
896  int compare(std::vector<Coordinate> a, std::vector<Coordinate> b) const;
897 
898  int compare(std::vector<Geometry*> a, std::vector<Geometry*> b) const;
899 
900  int compare(const std::vector<std::unique_ptr<Geometry>> & a, const std::vector<std::unique_ptr<Geometry>> & b) const;
901 
902  bool equal(const Coordinate& a, const Coordinate& b,
903  double tolerance) const;
904  int SRID;
905 
906  Geometry(const Geometry& geom);
907 
917  Geometry(const GeometryFactory* factory);
918 
919  template<typename T>
920  static std::vector<std::unique_ptr<Geometry>> toGeometryArray(std::vector<std::unique_ptr<T>> && v) {
921  static_assert(std::is_base_of<Geometry, T>::value, "");
922  std::vector<std::unique_ptr<Geometry>> gv(v.size());
923  for (size_t i = 0; i < v.size(); i++) {
924  gv[i] = std::move(v[i]);
925  }
926  return gv;
927  }
928 
929 protected:
930 
931  virtual int getSortIndex() const = 0;
932 
933 
934 private:
935 
936  class GEOS_DLL GeometryChangedFilter : public GeometryComponentFilter {
937  public:
938  void filter_rw(Geometry* geom) override;
939  };
940 
941  static GeometryChangedFilter geometryChangedFilter;
942 
947  const GeometryFactory* _factory;
948 
949  void* _userData;
950 };
951 
956 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Geometry& geom);
957 
958 struct GEOS_DLL GeometryGreaterThen {
959  bool operator()(const Geometry* first, const Geometry* second);
960 };
961 
962 
964 GEOS_DLL std::string geosversion();
965 
971 GEOS_DLL std::string jtsport();
972 
973 // We use this instead of std::pair<unique_ptr<Geometry>> because C++11
974 // forbids that construct:
975 // http://lwg.github.com/issues/lwg-closed.html#2068
976 struct GeomPtrPair {
977  typedef std::unique_ptr<Geometry> GeomPtr;
978  GeomPtr first;
979  GeomPtr second;
980 };
981 
982 } // namespace geos::geom
983 } // namespace geos
984 
985 #ifdef _MSC_VER
986 #pragma warning(pop)
987 #endif
988 
989 #endif // ndef GEOS_GEOM_GEOMETRY_H
Geometry classes support the concept of applying a Geometry filter to the Geometry.
Definition: GeometryFilter.h:47
virtual int getSRID() const
Returns the ID of the Spatial Reference System used by the Geometry.
Definition: Geometry.h:265
a linestring
Definition: Geometry.h:79
a collection of heterogeneus geometries
Definition: Geometry.h:91
a collection of linestrings
Definition: Geometry.h:87
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
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:318
virtual bool isRectangle() const
Polygon overrides to check for actual rectangle.
Definition: Geometry.h:339
Interface for classes which provide operations that can be applied to the coordinates in a Coordinate...
Definition: CoordinateSequenceFilter.h:57
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:87
virtual std::size_t getNumGeometries() const
Definition: Geometry.h:310
std::string geosversion()
Return current GEOS version.
GeometryTypeId
Geometry types.
Definition: Geometry.h:75
bool covers(const Geometry *g) const
Returns true if this geometry covers the specified geometry.
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
Geometry classes support the concept of applying a coordinate filter to every coordinate in the Geome...
Definition: CoordinateFilter.h:43
a collection of points
Definition: Geometry.h:85
std::string jtsport()
Return the version of JTS this GEOS release has been ported from.
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:237
Dimension value of a curve (1).
Definition: Dimension.h:45
Dimension value of a surface (2).
Definition: Dimension.h:48
void * getUserData() const
Gets the user data object for this geometry, if any.
Definition: Geometry.h:249
const GeometryFactory * getFactory() const
Gets the factory which contains the context in which this geometry was created.
Definition: Geometry.h:218
std::ostream & operator<<(std::ostream &os, const Coordinate &c)
Output function.
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:68
std::vector< Geometry * > NonConstVect
A vector of non-const Geometry pointers.
Definition: Geometry.h:198
virtual bool isDimensionStrict(Dimension::DimensionType d) const
Checks whether this Geometry consists only of components having dimension d.
Definition: Geometry.h:348
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:26
virtual void setSRID(int newSRID)
Sets the ID of the Spatial Reference System used by the Geometry.
Definition: Geometry.h:274
a polygon
Definition: Geometry.h:83
a linear ring (linestring with 1st point == last point)
Definition: Geometry.h:81
static bool hasNullElements(const std::vector< T > *geometries)
Returns true if the vector contains any null elements.
Definition: Geometry.h:870
a point
Definition: Geometry.h:77
Dimension value of a point (0).
Definition: Dimension.h:42
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
static bool hasNonEmptyElements(const std::vector< T > *geometries)
Returns true if the array contains any non-empty Geometrys.
Definition: Geometry.h:861
std::unique_ptr< Geometry > Ptr
An unique_ptr of Geometry.
Definition: Geometry.h:201
a collection of polygons
Definition: Geometry.h:89
std::unique_ptr< Envelope > envelope
The bounding box of this Geometry.
Definition: Geometry.h:857
std::vector< const Geometry * > ConstVect
A vector of const Geometry pointers.
Definition: Geometry.h:195
void applyComponentFilter(T &f) const
Apply a filter to each component of this geometry. The filter is expected to provide a ...
Definition: Geometry.h:768
DimensionType
Definition: Dimension.h:31
bool coveredBy(const Geometry *g) const
Tests whether this geometry is covered by the specified geometry.
Definition: Geometry.h:581
Definition: GeometryComponentFilter.h:43