GEOS  3.3.3
Geometry.h
1 /**********************************************************************
2  * $Id: Geometry.h 3538 2011-12-09 10:44:54Z strk $
3  *
4  * GEOS - Geometry Engine Open Source
5  * http://geos.refractions.net
6  *
7  * Copyright (C) 2009 2011 Sandro Santilli <strk@keybit.net>
8  * Copyright (C) 2005 2006 Refractions Research Inc.
9  * Copyright (C) 2001-2002 Vivid Solutions Inc.
10  *
11  * This is free software; you can redistribute and/or modify it under
12  * the terms of the GNU Lesser General Public Licence as published
13  * by the Free Software Foundation.
14  * See the COPYING file for more information.
15  *
16  **********************************************************************
17  *
18  * Last port: geom/Geometry.java rev. 1.112
19  *
20  **********************************************************************/
21 
22 #ifndef GEOS_GEOM_GEOMETRY_H
23 #define GEOS_GEOM_GEOMETRY_H
24 
25 #include <geos/export.h>
26 #include <geos/platform.h>
27 #include <geos/inline.h>
28 #include <geos/geom/Envelope.h>
29 #include <geos/geom/Dimension.h> // for Dimension::DimensionType
30 #include <geos/geom/GeometryComponentFilter.h> // for inheritance
31 
32 #include <string>
33 #include <iostream>
34 #include <vector>
35 #include <memory>
36 
37 #ifdef _MSC_VER
38 #pragma warning(push)
39 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
40 #pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list
41 #endif
42 
43 // Forward declarations
44 namespace geos {
45  namespace geom {
46  class Coordinate;
47  class CoordinateFilter;
48  class CoordinateSequence;
49  class CoordinateSequenceFilter;
50  class GeometryComponentFilter;
51  class GeometryFactory;
52  class GeometryFilter;
53  class IntersectionMatrix;
54  class PrecisionModel;
55  class Point;
56  }
57  namespace io { // geos.io
58  class Unload;
59  } // namespace geos.io
60 }
61 
62 namespace geos {
63 namespace geom { // geos::geom
64 
83 };
84 
168 class GEOS_DLL Geometry {
169 
170 public:
171 
172  friend class GeometryFactory;
173 
175  typedef std::vector<const Geometry *> ConstVect;
176 
178  typedef std::vector<Geometry *> NonConstVect;
179 
181  typedef std::auto_ptr<Geometry> AutoPtr;
182 
184  virtual Geometry* clone() const=0;
185 
187  virtual ~Geometry();
188 
189 
197  const GeometryFactory* getFactory() const { return factory; }
198 
212  void setUserData(void* newUserData) { userData=newUserData; }
213 
220  void* getUserData() { return userData; }
221 
222  /*
223  * \brief
224  * Returns the ID of the Spatial Reference System used by the
225  * <code>Geometry</code>.
226  *
227  * GEOS supports Spatial Reference System information in the simple way
228  * defined in the SFS. A Spatial Reference System ID (SRID) is present
229  * in each <code>Geometry</code> object. <code>Geometry</code>
230  * provides basic accessor operations for this field, but no others.
231  * The SRID is represented as an integer.
232  *
233  * @return the ID of the coordinate space in which the
234  * <code>Geometry</code> is defined.
235  *
236  */
237  virtual int getSRID() const { return SRID; }
238 
239  /*
240  * Sets the ID of the Spatial Reference System used by the
241  * <code>Geometry</code>.
242  */
243  virtual void setSRID(int newSRID) { SRID=newSRID; }
244 
249  const PrecisionModel* getPrecisionModel() const;
250 
255  virtual const Coordinate* getCoordinate() const=0; //Abstract
256 
262  virtual CoordinateSequence* getCoordinates() const=0; //Abstract
263 
265  virtual std::size_t getNumPoints() const=0; //Abstract
266 
268  virtual bool isSimple() const;
269 
271  virtual std::string getGeometryType() const=0; //Abstract
272 
274  virtual GeometryTypeId getGeometryTypeId() const=0; //Abstract
275 
278  virtual std::size_t getNumGeometries() const { return 1; }
279 
282  virtual const Geometry* getGeometryN(std::size_t /*n*/) const { return this; }
283 
293  virtual bool isValid() const;
294 
296  virtual bool isEmpty() const=0; //Abstract
297 
299  virtual bool isRectangle() const { return false; }
300 
302  virtual Dimension::DimensionType getDimension() const=0; //Abstract
303 
305  virtual int getCoordinateDimension() const=0; //Abstract
306 
323  virtual Geometry* getBoundary() const=0; //Abstract
324 
326  virtual int getBoundaryDimension() const=0; //Abstract
327 
329  virtual Geometry* getEnvelope() const;
330 
335  virtual const Envelope* getEnvelopeInternal() const;
336 
353  virtual bool disjoint(const Geometry *other) const;
354 
359  virtual bool touches(const Geometry *other) const;
360 
362  virtual bool intersects(const Geometry *g) const;
363 
386  virtual bool crosses(const Geometry *g) const;
387 
392  virtual bool within(const Geometry *g) const;
393 
395  virtual bool contains(const Geometry *g) const;
396 
402  virtual bool overlaps(const Geometry *g) const;
403 
418  virtual bool relate(const Geometry *g,
419  const std::string& intersectionPattern) const;
420 
421  bool relate(const Geometry& g, const std::string& intersectionPattern) const
422  {
423  return relate(&g, intersectionPattern);
424  }
425 
427  virtual IntersectionMatrix* relate(const Geometry *g) const;
428  IntersectionMatrix* relate(const Geometry &g) const {
429  return relate(&g);
430  }
431 
437  virtual bool equals(const Geometry *g) const;
438 
477  bool covers(const Geometry* g) const;
478 
509  bool coveredBy(const Geometry* g) const {
510  return g->covers(this);
511  }
512 
513 
515  virtual std::string toString() const;
516 
517  virtual std::string toText() const;
518 
520  //
523  virtual Geometry* buffer(double distance) const;
524 
529  //
532  virtual Geometry* buffer(double distance,int quadrantSegments) const;
533 
570  virtual Geometry* buffer(double distance, int quadrantSegments,
571  int endCapStyle) const;
572 
576  virtual Geometry* convexHull() const;
577 
587  virtual Geometry* intersection(const Geometry *other) const;
588 
598  Geometry* Union(const Geometry *other) const;
599  // throw(IllegalArgumentException *, TopologyException *);
600 
618  AutoPtr Union() const;
619  // throw(IllegalArgumentException *, TopologyException *);
620 
631  virtual Geometry* difference(const Geometry *other) const;
632 
642  virtual Geometry* symDifference(const Geometry *other) const;
643 
648  virtual bool equalsExact(const Geometry *other, double tolerance=0)
649  const=0; //Abstract
650 
651  virtual void apply_rw(const CoordinateFilter *filter)=0; //Abstract
652  virtual void apply_ro(CoordinateFilter *filter) const=0; //Abstract
653  virtual void apply_rw(GeometryFilter *filter);
654  virtual void apply_ro(GeometryFilter *filter) const;
655  virtual void apply_rw(GeometryComponentFilter *filter);
656  virtual void apply_ro(GeometryComponentFilter *filter) const;
657 
666  virtual void apply_rw(CoordinateSequenceFilter& filter)=0;
667 
674  virtual void apply_ro(CoordinateSequenceFilter& filter) const=0;
675 
685  template <class T>
686  void applyComponentFilter(T& f) const
687  {
688  for(std::size_t i=0, n=getNumGeometries(); i<n; ++i)
689  f.filter(getGeometryN(i));
690  }
691 
693  virtual void normalize()=0; //Abstract
694 
695  virtual int compareTo(const Geometry *geom) const;
696 
701  virtual double distance(const Geometry *g) const;
702 
704  virtual double getArea() const;
705 
707  virtual double getLength() const;
708 
720  virtual bool isWithinDistance(const Geometry *geom,
721  double cDistance) const;
722 
732  virtual Point* getCentroid() const;
733 
735  //
738  virtual bool getCentroid(Coordinate& ret) const;
739 
750  virtual Point* getInteriorPoint() const;
751 
752  /*
753  * \brief
754  * Notifies this Geometry that its Coordinates have been changed
755  * by an external party (using a CoordinateFilter, for example).
756  */
757  virtual void geometryChanged();
758 
764  void geometryChangedAction();
765 
766 protected:
767 
769  mutable std::auto_ptr<Envelope> envelope;
770 
772  static bool hasNonEmptyElements(const std::vector<Geometry *>* geometries);
773 
775  static bool hasNullElements(const CoordinateSequence* list);
776 
778  static bool hasNullElements(const std::vector<Geometry *>* lrs);
779 
780 // static void reversePointOrder(CoordinateSequence* coordinates);
781 // static Coordinate& minCoordinate(CoordinateSequence* coordinates);
782 // static void scroll(CoordinateSequence* coordinates,Coordinate* firstCoordinate);
783 // static int indexOf(Coordinate* coordinate,CoordinateSequence* coordinates);
784 //
789  virtual bool isEquivalentClass(const Geometry *other) const;
790 
791  static void checkNotGeometryCollection(const Geometry *g);
792  // throw(IllegalArgumentException *);
793 
794  //virtual void checkEqualSRID(Geometry *other);
795 
796  //virtual void checkEqualPrecisionModel(Geometry *other);
797 
798  virtual Envelope::AutoPtr computeEnvelopeInternal() const=0; //Abstract
799 
800  virtual int compareToSameClass(const Geometry *geom) const=0; //Abstract
801 
802  int compare(std::vector<Coordinate> a, std::vector<Coordinate> b) const;
803 
804  int compare(std::vector<Geometry *> a, std::vector<Geometry *> b) const;
805 
806  bool equal(const Coordinate& a, const Coordinate& b,
807  double tolerance) const;
808  int SRID;
809 
811  //Geometry* toInternalGeometry(const Geometry *g) const;
812 
814  //Geometry* fromInternalGeometry(const Geometry *g) const;
815 
817  //virtual bool isRectangle() const { return false; } -- moved to public
818 
819  Geometry(const Geometry &geom);
820 
830  Geometry(const GeometryFactory *factory);
831 
832 private:
833 
834  int getClassSortIndex() const;
835 
836  class GeometryChangedFilter : public GeometryComponentFilter
837  {
838  public:
839  void filter_rw(Geometry* geom)
840  {
841  geom->geometryChangedAction();
842  }
843  };
844 
845  static GeometryChangedFilter geometryChangedFilter;
846 
848  //
851  const GeometryFactory *factory;
852 
853  static const GeometryFactory* INTERNAL_GEOMETRY_FACTORY;
854 
855  void* userData;
856 };
857 
862 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Geometry& geom);
863 
864 struct GEOS_DLL GeometryGreaterThen {
865  bool operator()(const Geometry *first, const Geometry *second);
866 };
867 
868 
870 std::string geosversion();
871 
877 std::string jtsport();
878 
879 // We use this instead of std::pair<auto_ptr<Geometry>> because C++11
880 // forbids that construct:
881 // http://lwg.github.com/issues/lwg-closed.html#2068
882 struct GeomPtrPair {
883  typedef std::auto_ptr<Geometry> GeomPtr;
884  GeomPtr first;
885  GeomPtr second;
886 };
887 
888 } // namespace geos::geom
889 } // namespace geos
890 
891 #ifdef _MSC_VER
892 #pragma warning(pop)
893 #endif
894 
895 #endif // ndef GEOS_GEOM_GEOMETRY_H
896 
897 /**********************************************************************
898  * $Log$
899  * Revision 1.14 2006/07/08 00:33:55 strk
900  * * configure.in: incremented CAPI minor version, to avoid falling behind any future version from the 2.2. branch.
901  * * source/geom/Geometry.cpp, source/geom/GeometryFactory.cpp,
902  * source/geomgraph/EdgeRing.cpp,
903  * source/headers/geos/geom/Geometry.h,
904  * source/headers/geos/geom/GeometryFactory.h,
905  * source/headers/geos/geom/GeometryFactory.inl,
906  * source/headers/geos/geomgraph/EdgeRing.h:
907  * updated doxygen comments (sync with JTS head).
908  * * source/headers/geos/platform.h.in: include <inttypes.h>
909  * rather then <stdint.h>
910  *
911  * Revision 1.13 2006/06/12 10:10:39 strk
912  * Fixed getGeometryN() to take size_t rather then int, changed unsigned int parameters to size_t.
913  *
914  * Revision 1.12 2006/05/18 08:56:50 strk
915  * * source/geom/Geometry.cpp,
916  * source/headers/geos/geom/Geometry.h: added
917  * covers() and isCoveredBy() predicates.
918  * * tests/unit/Makefile.am,
919  * tests/unit/geom/Geometry/coversTest.cpp:
920  * added test for covers() predicates.
921  *
922  * Revision 1.11 2006/05/04 15:49:39 strk
923  * updated all Geometry::getDimension() methods to return Dimension::DimensionType (closes bug#93)
924  *
925  * Revision 1.10 2006/04/28 10:55:39 strk
926  * Geometry constructors made protected, to ensure all constructions use GeometryFactory,
927  * which has been made friend of all Geometry derivates. getNumPoints() changed to return
928  * size_t.
929  *
930  * Revision 1.9 2006/04/11 09:31:47 strk
931  * Added Geometry::AutoPtr typedef
932  *
933  * Revision 1.8 2006/04/10 18:15:09 strk
934  * Changed Geometry::envelope member to be of type auto_ptr<Envelope>.
935  * Changed computeEnvelopeInternal() signater to return auto_ptr<Envelope>
936  *
937  * Revision 1.7 2006/04/07 09:54:30 strk
938  * Geometry::getNumGeometries() changed to return 'unsigned int'
939  * rather then 'int'
940  *
941  * Revision 1.6 2006/03/31 16:53:53 strk
942  * Added comment about possible NULL return from getCoordinate()
943  *
944  * Revision 1.5 2006/03/24 09:52:41 strk
945  * USE_INLINE => GEOS_INLINE
946  *
947  * Revision 1.4 2006/03/23 15:10:29 strk
948  * Dropped by-pointer TopologyException constructor, various small cleanups
949  *
950  * Revision 1.3 2006/03/23 12:12:01 strk
951  * Fixes to allow build with -DGEOS_INLINE
952  *
953  * Revision 1.2 2006/03/20 12:03:25 strk
954  * Added operator<< for Geometry, writing HEXWKB
955  *
956  * Revision 1.1 2006/03/09 16:46:49 strk
957  * geos::geom namespace definition, first pass at headers split
958  *
959  **********************************************************************/