GEOS  3.6.2
Geometry.h
00001 /**********************************************************************
00002  *
00003  * GEOS - Geometry Engine Open Source
00004  * http://geos.osgeo.org
00005  *
00006  * Copyright (C) 2009 2011 Sandro Santilli <strk@keybit.net>
00007  * Copyright (C) 2005 2006 Refractions Research Inc.
00008  * Copyright (C) 2001-2002 Vivid Solutions Inc.
00009  *
00010  * This is free software; you can redistribute and/or modify it under
00011  * the terms of the GNU Lesser General Public Licence as published
00012  * by the Free Software Foundation.
00013  * See the COPYING file for more information.
00014  *
00015  **********************************************************************
00016  *
00017  * Last port: geom/Geometry.java rev. 1.112
00018  *
00019  **********************************************************************/
00020 
00021 #ifndef GEOS_GEOM_GEOMETRY_H
00022 #define GEOS_GEOM_GEOMETRY_H
00023 
00024 #include <geos/export.h>
00025 #include <geos/platform.h>
00026 #include <geos/inline.h>
00027 #include <geos/geom/Envelope.h>
00028 #include <geos/geom/Dimension.h> // for Dimension::DimensionType
00029 #include <geos/geom/GeometryComponentFilter.h> // for inheritance
00030 
00031 #include <string>
00032 #include <iostream>
00033 #include <vector>
00034 #include <memory>
00035 
00036 #ifdef _MSC_VER
00037 #pragma warning(push)
00038 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
00039 #pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list
00040 #endif
00041 
00042 // Forward declarations
00043 namespace geos {
00044         namespace geom {
00045                 class Coordinate;
00046                 class CoordinateFilter;
00047                 class CoordinateSequence;
00048                 class CoordinateSequenceFilter;
00049                 class GeometryComponentFilter;
00050                 class GeometryFactory;
00051                 class GeometryFilter;
00052                 class IntersectionMatrix;
00053                 class PrecisionModel;
00054                 class Point;
00055         }
00056         namespace io { // geos.io
00057                 class Unload;
00058         } // namespace geos.io
00059 }
00060 
00061 namespace geos {
00062 namespace geom { // geos::geom
00063 
00065 enum GeometryTypeId {
00067         GEOS_POINT,
00069         GEOS_LINESTRING,
00071         GEOS_LINEARRING,
00073         GEOS_POLYGON,
00075         GEOS_MULTIPOINT,
00077         GEOS_MULTILINESTRING,
00079         GEOS_MULTIPOLYGON,
00081         GEOS_GEOMETRYCOLLECTION
00082 };
00083 
00167 class GEOS_DLL Geometry {
00168 
00169 public:
00170 
00171         friend class GeometryFactory;
00172 
00174         typedef std::vector<const Geometry *> ConstVect;
00175 
00177         typedef std::vector<Geometry *> NonConstVect;
00178 
00180         typedef std::auto_ptr<Geometry> AutoPtr;
00181 
00183         virtual Geometry* clone() const=0;
00184 
00186         virtual ~Geometry();
00187 
00188 
00196         const GeometryFactory* getFactory() const { return _factory; }
00197 
00211         void setUserData(void* newUserData) { _userData=newUserData; }
00212 
00219         void* getUserData() const { return _userData; }
00220 
00221         /*
00222          * \brief
00223          * Returns the ID of the Spatial Reference System used by the
00224          * <code>Geometry</code>.
00225          *
00226          * GEOS supports Spatial Reference System information in the simple way
00227          * defined in the SFS. A Spatial Reference System ID (SRID) is present
00228          * in each <code>Geometry</code> object. <code>Geometry</code>
00229          * provides basic accessor operations for this field, but no others.
00230          * The SRID is represented as an integer.
00231          *
00232          * @return the ID of the coordinate space in which the
00233          * <code>Geometry</code> is defined.
00234          *
00235          */
00236         virtual int getSRID() const { return SRID; }
00237 
00238         /*
00239          * Sets the ID of the Spatial Reference System used by the
00240          * <code>Geometry</code>.
00241          */
00242         virtual void setSRID(int newSRID) { SRID=newSRID; }
00243 
00248         const PrecisionModel* getPrecisionModel() const;
00249 
00254         virtual const Coordinate* getCoordinate() const=0; //Abstract
00255 
00261         virtual CoordinateSequence* getCoordinates() const=0; //Abstract
00262 
00264         virtual std::size_t getNumPoints() const=0; //Abstract
00265 
00267         virtual bool isSimple() const;
00268 
00270         virtual std::string getGeometryType() const=0; //Abstract
00271 
00273         virtual GeometryTypeId getGeometryTypeId() const=0; //Abstract
00274 
00277         virtual std::size_t getNumGeometries() const { return 1; }
00278 
00281         virtual const Geometry* getGeometryN(std::size_t /*n*/) const { return this; }
00282 
00292         virtual bool isValid() const;
00293 
00295         virtual bool isEmpty() const=0; //Abstract
00296 
00298         virtual bool isRectangle() const { return false; }
00299 
00301         virtual Dimension::DimensionType getDimension() const=0; //Abstract
00302 
00304         virtual int getCoordinateDimension() const=0; //Abstract
00305 
00322         virtual Geometry* getBoundary() const=0; //Abstract
00323 
00325         virtual int getBoundaryDimension() const=0; //Abstract
00326 
00328         virtual Geometry* getEnvelope() const;
00329 
00334         virtual const Envelope* getEnvelopeInternal() const;
00335 
00352         virtual bool disjoint(const Geometry *other) const;
00353 
00358         virtual bool touches(const Geometry *other) const;
00359 
00361         virtual bool intersects(const Geometry *g) const;
00362 
00385         virtual bool crosses(const Geometry *g) const;
00386 
00391         virtual bool within(const Geometry *g) const;
00392 
00394         virtual bool contains(const Geometry *g) const;
00395 
00401         virtual bool overlaps(const Geometry *g) const;
00402 
00417         virtual bool relate(const Geometry *g,
00418                         const std::string& intersectionPattern) const;
00419 
00420         bool relate(const Geometry& g, const std::string& intersectionPattern) const
00421         {
00422                 return relate(&g, intersectionPattern);
00423         }
00424 
00426         virtual IntersectionMatrix* relate(const Geometry *g) const;
00427         IntersectionMatrix* relate(const Geometry &g) const {
00428                 return relate(&g);
00429         }
00430 
00436         virtual bool equals(const Geometry *g) const;
00437 
00476         bool covers(const Geometry* g) const;
00477 
00508         bool coveredBy(const Geometry* g) const {
00509           return g->covers(this);
00510         }
00511 
00512 
00514         virtual std::string toString() const;
00515 
00516         virtual std::string toText() const;
00517 
00519         //
00522         virtual Geometry* buffer(double distance) const;
00523 
00528         //
00531         virtual Geometry* buffer(double distance,int quadrantSegments) const;
00532 
00569         virtual Geometry* buffer(double distance, int quadrantSegments,
00570                         int endCapStyle) const;
00571 
00575         virtual Geometry* convexHull() const;
00576 
00586         virtual Geometry* intersection(const Geometry *other) const;
00587 
00597         Geometry* Union(const Geometry *other) const;
00598                 // throw(IllegalArgumentException *, TopologyException *);
00599 
00617   AutoPtr Union() const;
00618                 // throw(IllegalArgumentException *, TopologyException *);
00619 
00630         virtual Geometry* difference(const Geometry *other) const;
00631 
00641         virtual Geometry* symDifference(const Geometry *other) const;
00642 
00647         virtual bool equalsExact(const Geometry *other, double tolerance=0)
00648                 const=0; //Abstract
00649 
00650         virtual void apply_rw(const CoordinateFilter *filter)=0; //Abstract
00651         virtual void apply_ro(CoordinateFilter *filter) const=0; //Abstract
00652         virtual void apply_rw(GeometryFilter *filter);
00653         virtual void apply_ro(GeometryFilter *filter) const;
00654         virtual void apply_rw(GeometryComponentFilter *filter);
00655         virtual void apply_ro(GeometryComponentFilter *filter) const;
00656 
00665         virtual void apply_rw(CoordinateSequenceFilter& filter)=0;
00666 
00673         virtual void apply_ro(CoordinateSequenceFilter& filter) const=0;
00674 
00684         template <class T>
00685         void applyComponentFilter(T& f) const
00686         {
00687                 for(std::size_t i=0, n=getNumGeometries(); i<n; ++i)
00688                         f.filter(getGeometryN(i));
00689         }
00690 
00692         virtual void normalize()=0; //Abstract
00693 
00694         virtual int compareTo(const Geometry *geom) const;
00695 
00700         virtual double distance(const Geometry *g) const;
00701 
00703         virtual double getArea() const;
00704 
00706         virtual double getLength() const;
00707 
00719         virtual bool isWithinDistance(const Geometry *geom,
00720                         double cDistance) const;
00721 
00731         virtual Point* getCentroid() const;
00732 
00734         //
00737         virtual bool getCentroid(Coordinate& ret) const;
00738 
00749         virtual Point* getInteriorPoint() const;
00750 
00751         /*
00752          * \brief
00753          * Notifies this Geometry that its Coordinates have been changed
00754          * by an external party (using a CoordinateFilter, for example).
00755          */
00756         virtual void geometryChanged();
00757 
00763         void geometryChangedAction();
00764 
00765 protected:
00766 
00768         mutable std::auto_ptr<Envelope> envelope;
00769 
00771         static bool hasNonEmptyElements(const std::vector<Geometry *>* geometries);
00772 
00774         static bool hasNullElements(const CoordinateSequence* list);
00775 
00777         static bool hasNullElements(const std::vector<Geometry *>* lrs);
00778 
00779 //      static void reversePointOrder(CoordinateSequence* coordinates);
00780 //      static Coordinate& minCoordinate(CoordinateSequence* coordinates);
00781 //      static void scroll(CoordinateSequence* coordinates,Coordinate* firstCoordinate);
00782 //      static int indexOf(Coordinate* coordinate,CoordinateSequence* coordinates);
00783 //
00788         virtual bool isEquivalentClass(const Geometry *other) const;
00789 
00790         static void checkNotGeometryCollection(const Geometry *g);
00791                         // throw(IllegalArgumentException *);
00792 
00793         //virtual void checkEqualSRID(Geometry *other);
00794 
00795         //virtual void checkEqualPrecisionModel(Geometry *other);
00796 
00797         virtual Envelope::AutoPtr computeEnvelopeInternal() const=0; //Abstract
00798 
00799         virtual int compareToSameClass(const Geometry *geom) const=0; //Abstract
00800 
00801         int compare(std::vector<Coordinate> a, std::vector<Coordinate> b) const;
00802 
00803         int compare(std::vector<Geometry *> a, std::vector<Geometry *> b) const;
00804 
00805         bool equal(const Coordinate& a, const Coordinate& b,
00806                         double tolerance) const;
00807         int SRID;
00808 
00810         //Geometry* toInternalGeometry(const Geometry *g) const;
00811 
00813         //Geometry* fromInternalGeometry(const Geometry *g) const;
00814 
00816         //virtual bool isRectangle() const { return false; } -- moved to public
00817 
00818         Geometry(const Geometry &geom);
00819 
00829         Geometry(const GeometryFactory *factory);
00830 
00831 private:
00832 
00833         int getClassSortIndex() const;
00834 
00835         class GEOS_DLL GeometryChangedFilter : public GeometryComponentFilter
00836         {
00837         public:
00838                 void filter_rw(Geometry* geom);
00839         };
00840 
00841         static GeometryChangedFilter geometryChangedFilter;
00842 
00844         //
00847         const GeometryFactory* _factory;
00848 
00849         void* _userData;
00850 };
00851 
00856 GEOS_DLL std::ostream& operator<< (std::ostream& os, const Geometry& geom);
00857 
00858 struct GEOS_DLL GeometryGreaterThen {
00859         bool operator()(const Geometry *first, const Geometry *second);
00860 };
00861 
00862 
00864 std::string geosversion();
00865 
00871 std::string jtsport();
00872 
00873 // We use this instead of std::pair<auto_ptr<Geometry>> because C++11
00874 // forbids that construct:
00875 // http://lwg.github.com/issues/lwg-closed.html#2068
00876 struct GeomPtrPair {
00877         typedef std::auto_ptr<Geometry> GeomPtr;
00878         GeomPtr first;
00879         GeomPtr second;
00880 };
00881 
00882 } // namespace geos::geom
00883 } // namespace geos
00884 
00885 #ifdef _MSC_VER
00886 #pragma warning(pop)
00887 #endif
00888 
00889 #endif // ndef GEOS_GEOM_GEOMETRY_H