GEOS  3.8.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 
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
52 namespace geos {
53 namespace geom {
54 class Coordinate;
55 class CoordinateFilter;
56 class CoordinateSequence;
57 class CoordinateSequenceFilter;
58 class GeometryComponentFilter;
59 class GeometryFactory;
60 class GeometryFilter;
61 class IntersectionMatrix;
62 class PrecisionModel;
63 class Point;
64 }
65 namespace io { // geos.io
66 class Unload;
67 } // namespace geos.io
68 }
69 
70 namespace geos {
71 namespace geom { // geos::geom
72 
91 };
92 
93 enum GeometrySortIndex {
94  SORTINDEX_POINT = 0,
95  SORTINDEX_MULTIPOINT = 1,
96  SORTINDEX_LINESTRING = 2,
97  SORTINDEX_LINEARRING = 3,
98  SORTINDEX_MULTILINESTRING = 4,
99  SORTINDEX_POLYGON = 5,
100  SORTINDEX_MULTIPOLYGON = 6,
101  SORTINDEX_GEOMETRYCOLLECTION = 7
102 };
103 
187 class GEOS_DLL Geometry {
188 
189 public:
190 
191  friend class GeometryFactory;
192 
194  using ConstVect = std::vector<const Geometry*>;
195 
197  using NonConstVect = std::vector<Geometry*>;
198 
200  using Ptr = std::unique_ptr<Geometry> ;
201 
203  virtual Geometry* clone() const = 0;
204 
206  virtual ~Geometry();
207 
208 
216  const GeometryFactory*
217  getFactory() const
218  {
219  return _factory;
220  }
221 
235  void
236  setUserData(void* newUserData)
237  {
238  _userData = newUserData;
239  }
240 
247  void*
248  getUserData() const
249  {
250  return _userData;
251  }
252 
253  /*
254  * \brief
255  * Returns the ID of the Spatial Reference System used by the
256  * <code>Geometry</code>.
257  *
258  * GEOS supports Spatial Reference System information in the simple way
259  * defined in the SFS. A Spatial Reference System ID (SRID) is present
260  * in each <code>Geometry</code> object. <code>Geometry</code>
261  * provides basic accessor operations for this field, but no others.
262  * The SRID is represented as an integer.
263  *
264  * @return the ID of the coordinate space in which the
265  * <code>Geometry</code> is defined.
266  *
267  */
268  virtual int
269  getSRID() const
270  {
271  return SRID;
272  }
273 
274  /*
275  * Sets the ID of the Spatial Reference System used by the
276  * <code>Geometry</code>.
277  */
278  virtual void
279  setSRID(int newSRID)
280  {
281  SRID = newSRID;
282  }
283 
288  const PrecisionModel* getPrecisionModel() const;
289 
294  virtual const Coordinate* getCoordinate() const = 0; //Abstract
295 
301  virtual CoordinateSequence* getCoordinates() const = 0; //Abstract
302 
304  virtual std::size_t getNumPoints() const = 0; //Abstract
305 
307  virtual bool isSimple() const;
308 
310  virtual std::string getGeometryType() const = 0; //Abstract
311 
313  virtual GeometryTypeId getGeometryTypeId() const = 0; //Abstract
314 
317  virtual std::size_t
319  {
320  return 1;
321  }
322 
325  virtual const Geometry*
326  getGeometryN(std::size_t /*n*/) const
327  {
328  return this;
329  }
330 
340  virtual bool isValid() const;
341 
343  virtual bool isEmpty() const = 0; //Abstract
344 
346  virtual bool
347  isRectangle() const
348  {
349  return false;
350  }
351 
353  virtual Dimension::DimensionType getDimension() const = 0; //Abstract
354 
356  virtual int getCoordinateDimension() const = 0; //Abstract
357 
374  virtual Geometry* getBoundary() const = 0; //Abstract
375 
377  virtual int getBoundaryDimension() const = 0; //Abstract
378 
380  virtual Geometry* getEnvelope() const;
381 
386  virtual const Envelope* getEnvelopeInternal() const;
387 
404  virtual bool disjoint(const Geometry* other) const;
405 
410  virtual bool touches(const Geometry* other) const;
411 
413  virtual bool intersects(const Geometry* g) const;
414 
437  virtual bool crosses(const Geometry* g) const;
438 
443  virtual bool within(const Geometry* g) const;
444 
446  virtual bool contains(const Geometry* g) const;
447 
453  virtual bool overlaps(const Geometry* g) const;
454 
469  virtual bool relate(const Geometry* g,
470  const std::string& intersectionPattern) const;
471 
472  bool
473  relate(const Geometry& g, const std::string& intersectionPattern) const
474  {
475  return relate(&g, intersectionPattern);
476  }
477 
479  virtual IntersectionMatrix* relate(const Geometry* g) const;
480  IntersectionMatrix*
481  relate(const Geometry& g) const
482  {
483  return relate(&g);
484  }
485 
491  virtual bool equals(const Geometry* g) const;
492 
531  bool covers(const Geometry* g) const;
532 
563  bool
564  coveredBy(const Geometry* g) const
565  {
566  return g->covers(this);
567  }
568 
569 
571  virtual std::string toString() const;
572 
573  virtual std::string toText() const;
574 
576  //
579  virtual Geometry* buffer(double distance) const;
580 
585  //
588  virtual Geometry* buffer(double distance, int quadrantSegments) const;
589 
626  virtual Geometry* buffer(double distance, int quadrantSegments,
627  int endCapStyle) const;
628 
632  virtual Geometry* convexHull() const;
633 
640  virtual Geometry* reverse() const = 0;
641 
651  virtual Geometry* intersection(const Geometry* other) const;
652 
662  Geometry* Union(const Geometry* other) const;
663  // throw(IllegalArgumentException *, TopologyException *);
664 
682  Ptr Union() const;
683  // throw(IllegalArgumentException *, TopologyException *);
684 
695  virtual Geometry* difference(const Geometry* other) const;
696 
706  virtual Geometry* symDifference(const Geometry* other) const;
707 
712  virtual bool equalsExact(const Geometry* other, double tolerance = 0)
713  const = 0; //Abstract
714 
715  virtual void apply_rw(const CoordinateFilter* filter) = 0; //Abstract
716  virtual void apply_ro(CoordinateFilter* filter) const = 0; //Abstract
717  virtual void apply_rw(GeometryFilter* filter);
718  virtual void apply_ro(GeometryFilter* filter) const;
719  virtual void apply_rw(GeometryComponentFilter* filter);
720  virtual void apply_ro(GeometryComponentFilter* filter) const;
721 
730  virtual void apply_rw(CoordinateSequenceFilter& filter) = 0;
731 
738  virtual void apply_ro(CoordinateSequenceFilter& filter) const = 0;
739 
749  template <class T>
750  void
752  {
753  for(std::size_t i = 0, n = getNumGeometries(); i < n; ++i) {
754  f.filter(getGeometryN(i));
755  }
756  }
757 
759  virtual void normalize() = 0; //Abstract
760 
761  virtual int compareTo(const Geometry* geom) const;
762 
767  virtual double distance(const Geometry* g) const;
768 
770  virtual double getArea() const;
771 
773  virtual double getLength() const;
774 
786  virtual bool isWithinDistance(const Geometry* geom,
787  double cDistance) const;
788 
798  virtual Point* getCentroid() const;
799 
801  //
804  virtual bool getCentroid(Coordinate& ret) const;
805 
816  virtual Point* getInteriorPoint() const;
817 
818  /*
819  * \brief
820  * Notifies this Geometry that its Coordinates have been changed
821  * by an external party (using a CoordinateFilter, for example).
822  */
823  virtual void geometryChanged();
824 
830  void geometryChangedAction();
831 
832 protected:
833 
835  mutable std::unique_ptr<Envelope> envelope;
836 
838  static bool hasNonEmptyElements(const std::vector<Geometry*>* geometries);
839 
841  static bool hasNullElements(const CoordinateSequence* list);
842 
844  static bool hasNullElements(const std::vector<Geometry*>* lrs);
845 
846 // static void reversePointOrder(CoordinateSequence* coordinates);
847 // static Coordinate& minCoordinate(CoordinateSequence* coordinates);
848 // static void scroll(CoordinateSequence* coordinates,Coordinate* firstCoordinate);
849 // static int indexOf(Coordinate* coordinate,CoordinateSequence* coordinates);
850 //
855  virtual bool isEquivalentClass(const Geometry* other) const;
856 
857  static void checkNotGeometryCollection(const Geometry* g);
858  // throw(IllegalArgumentException *);
859 
860  //virtual void checkEqualSRID(Geometry *other);
861 
862  //virtual void checkEqualPrecisionModel(Geometry *other);
863 
864  virtual Envelope::Ptr computeEnvelopeInternal() const = 0; //Abstract
865 
866  virtual int compareToSameClass(const Geometry* geom) const = 0; //Abstract
867 
868  int compare(std::vector<Coordinate> a, std::vector<Coordinate> b) const;
869 
870  int compare(std::vector<Geometry*> a, std::vector<Geometry*> b) const;
871 
872  bool equal(const Coordinate& a, const Coordinate& b,
873  double tolerance) const;
874  int SRID;
875 
877  //Geometry* toInternalGeometry(const Geometry *g) const;
878 
880  //Geometry* fromInternalGeometry(const Geometry *g) const;
881 
883  //virtual bool isRectangle() const { return false; } -- moved to public
884 
885  Geometry(const Geometry& geom);
886 
896  Geometry(const GeometryFactory* factory);
897 
898 
899 protected:
900 
901  virtual int getSortIndex() const = 0;
902 
903 
904 private:
905 
906  class GEOS_DLL GeometryChangedFilter : public GeometryComponentFilter {
907  public:
908  void filter_rw(Geometry* geom) override;
909  };
910 
911  static GeometryChangedFilter geometryChangedFilter;
912 
914  //
917  const GeometryFactory* _factory;
918 
919  void* _userData;
920 };
921 
926 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Geometry& geom);
927 
928 struct GEOS_DLL GeometryGreaterThen {
929  bool operator()(const Geometry* first, const Geometry* second);
930 };
931 
932 
934 std::string geosversion();
935 
941 std::string jtsport();
942 
943 // We use this instead of std::pair<unique_ptr<Geometry>> because C++11
944 // forbids that construct:
945 // http://lwg.github.com/issues/lwg-closed.html#2068
946 struct GeomPtrPair {
947  typedef std::unique_ptr<Geometry> GeomPtr;
948  GeomPtr first;
949  GeomPtr second;
950 };
951 
952 } // namespace geos::geom
953 } // namespace geos
954 
955 #ifdef _MSC_VER
956 #pragma warning(pop)
957 #endif
958 
959 #endif // ndef GEOS_GEOM_GEOMETRY_H
Geometry classes support the concept of applying a Geometry filter to the Geometry.
Definition: GeometryFilter.h:47
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:59
a linestring
Definition: Geometry.h:78
a collection of heterogeneus geometries
Definition: Geometry.h:90
a collection of linestrings
Definition: Geometry.h:86
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
virtual const Geometry * getGeometryN(std::size_t) const
Definition: Geometry.h:326
GEOS_DLL std::ostream & operator<<(std::ostream &os, const Coordinate &c)
Output function.
virtual bool isRectangle() const
Polygon overrides to check for actual rectangle.
Definition: Geometry.h:347
Definition: CoordinateSequenceFilter.h:58
virtual std::size_t getNumGeometries() const
Definition: Geometry.h:318
std::string geosversion()
Return current GEOS version.
GeometryTypeId
Geometry types.
Definition: Geometry.h:74
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:187
Definition: CoordinateFilter.h:43
a collection of points
Definition: Geometry.h:84
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:236
void * getUserData() const
Gets the user data object for this geometry, if any.
Definition: Geometry.h:248
const GeometryFactory * getFactory() const
Gets the factory which contains the context in which this geometry was created.
Definition: Geometry.h:217
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:67
std::vector< Geometry * > NonConstVect
A vector of non-const Geometry pointers.
Definition: Geometry.h:197
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
a polygon
Definition: Geometry.h:82
a linear ring (linestring with 1st point == last point)
Definition: Geometry.h:80
a point
Definition: Geometry.h:76
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
std::unique_ptr< Geometry > Ptr
An unique_ptr of Geometry.
Definition: Geometry.h:200
Definition: Point.h:66
a collection of polygons
Definition: Geometry.h:88
std::unique_ptr< Envelope > envelope
The bounding box of this Geometry.
Definition: Geometry.h:835
std::vector< const Geometry * > ConstVect
A vector of const Geometry pointers.
Definition: Geometry.h:194
void applyComponentFilter(T &f) const
Apply a fiter to each component of this geometry. The filter is expected to provide a ...
Definition: Geometry.h:751
DimensionType
Definition: Dimension.h:31
bool coveredBy(const Geometry *g) const
Tests whether this geometry is covered by the specified geometry.
Definition: Geometry.h:564
Definition: GeometryComponentFilter.h:43