GEOS
3.4.2
|
00001 /********************************************************************** 00002 * 00003 * GEOS - Geometry Engine Open Source 00004 * http://geos.osgeo.org 00005 * 00006 * Copyright (C) 2011 Sandro Santilli <strk@keybit.net> 00007 * Copyright (C) 2006 Refractions Research Inc. 00008 * 00009 * This is free software; you can redistribute and/or modify it under 00010 * the terms of the GNU Lesser General Public Licence as published 00011 * by the Free Software Foundation. 00012 * See the COPYING file for more information. 00013 * 00014 ********************************************************************** 00015 * 00016 * Last port: geom/GeometryFactory.java r320 (JTS-1.12) 00017 * 00018 **********************************************************************/ 00019 00020 #ifndef GEOS_GEOM_GEOMETRYFACTORY_H 00021 #define GEOS_GEOM_GEOMETRYFACTORY_H 00022 00023 #include <geos/geom/Geometry.h> 00024 #include <geos/geom/GeometryCollection.h> 00025 #include <geos/geom/MultiPoint.h> 00026 #include <geos/geom/MultiLineString.h> 00027 #include <geos/geom/MultiPolygon.h> 00028 #include <geos/export.h> 00029 #include <geos/inline.h> 00030 00031 #include <vector> 00032 #include <memory> 00033 #include <cassert> 00034 00035 namespace geos { 00036 namespace geom { 00037 class CoordinateSequenceFactory; 00038 class Coordinate; 00039 class CoordinateSequence; 00040 class Envelope; 00041 class Geometry; 00042 class GeometryCollection; 00043 class LineString; 00044 class LinearRing; 00045 class MultiLineString; 00046 class MultiPoint; 00047 class MultiPolygon; 00048 class Point; 00049 class Polygon; 00050 class PrecisionModel; 00051 } 00052 } 00053 00054 namespace geos { 00055 namespace geom { // geos::geom 00056 00067 class GEOS_DLL GeometryFactory { 00068 public: 00074 GeometryFactory(); 00075 00088 GeometryFactory(const PrecisionModel *pm, int newSRID, 00089 CoordinateSequenceFactory *nCoordinateSequenceFactory); 00090 00097 GeometryFactory(CoordinateSequenceFactory *nCoordinateSequenceFactory); 00098 00107 GeometryFactory(const PrecisionModel *pm); 00108 00118 GeometryFactory(const PrecisionModel* pm, int newSRID); 00119 00125 GeometryFactory(const GeometryFactory &gf); 00126 00133 static const GeometryFactory* 00134 getDefaultInstance(); 00135 00137 virtual ~GeometryFactory(); 00138 00139 //Skipped a lot of list to array convertors 00140 00141 Point* createPointFromInternalCoord(const Coordinate* coord, 00142 const Geometry *exemplar) const; 00143 00145 // 00148 Geometry* toGeometry(const Envelope* envelope) const; 00149 00153 const PrecisionModel* getPrecisionModel() const; 00154 00156 Point* createPoint() const; 00157 00159 Point* createPoint(const Coordinate& coordinate) const; 00160 00162 Point* createPoint(CoordinateSequence *coordinates) const; 00163 00165 Point* createPoint(const CoordinateSequence &coordinates) const; 00166 00168 GeometryCollection* createGeometryCollection() const; 00169 00171 Geometry* createEmptyGeometry() const; 00172 00174 GeometryCollection* createGeometryCollection( 00175 std::vector<Geometry *> *newGeoms) const; 00176 00178 GeometryCollection* createGeometryCollection( 00179 const std::vector<Geometry *> &newGeoms) const; 00180 00182 MultiLineString* createMultiLineString() const; 00183 00185 MultiLineString* createMultiLineString( 00186 std::vector<Geometry *> *newLines) const; 00187 00189 MultiLineString* createMultiLineString( 00190 const std::vector<Geometry *> &fromLines) const; 00191 00193 MultiPolygon* createMultiPolygon() const; 00194 00196 MultiPolygon* createMultiPolygon(std::vector<Geometry *> *newPolys) const; 00197 00199 MultiPolygon* createMultiPolygon( 00200 const std::vector<Geometry *> &fromPolys) const; 00201 00203 LinearRing* createLinearRing() const; 00204 00206 LinearRing* createLinearRing(CoordinateSequence* newCoords) const; 00207 00208 std::auto_ptr<Geometry> createLinearRing( 00209 std::auto_ptr<CoordinateSequence> newCoords) const; 00210 00212 LinearRing* createLinearRing( 00213 const CoordinateSequence& coordinates) const; 00214 00216 MultiPoint* createMultiPoint() const; 00217 00219 MultiPoint* createMultiPoint(std::vector<Geometry *> *newPoints) const; 00220 00222 MultiPoint* createMultiPoint( 00223 const std::vector<Geometry *> &fromPoints) const; 00224 00228 MultiPoint* createMultiPoint( 00229 const CoordinateSequence &fromCoords) const; 00230 00234 MultiPoint* createMultiPoint( 00235 const std::vector<Coordinate> &fromCoords) const; 00236 00238 Polygon* createPolygon() const; 00239 00241 Polygon* createPolygon(LinearRing *shell, 00242 std::vector<Geometry *> *holes) const; 00243 00245 Polygon* createPolygon(const LinearRing &shell, 00246 const std::vector<Geometry *> &holes) const; 00247 00249 LineString* createLineString() const; 00250 00252 std::auto_ptr<LineString> createLineString(const LineString& ls) const; 00253 00255 LineString* createLineString(CoordinateSequence* coordinates) const; 00256 00257 std::auto_ptr<Geometry> createLineString( 00258 std::auto_ptr<CoordinateSequence> coordinates) const; 00259 00261 LineString* createLineString( 00262 const CoordinateSequence& coordinates) const; 00263 00295 Geometry* buildGeometry(std::vector<Geometry *> *geoms) const; 00296 00298 // 00305 template <class T> 00306 std::auto_ptr<Geometry> buildGeometry(T from, T toofar) const 00307 { 00308 bool isHeterogeneous = false; 00309 size_t count = 0; 00310 int geomClass = -1; 00311 for (T i=from; i != toofar; ++i) 00312 { 00313 ++count; 00314 const Geometry* g = *i; 00315 if ( geomClass < 0 ) { 00316 geomClass = g->getClassSortIndex(); 00317 } 00318 else if ( geomClass != g->getClassSortIndex() ) { 00319 isHeterogeneous = true; 00320 } 00321 } 00322 00323 // for the empty geometry, return an empty GeometryCollection 00324 if ( count == 0 ) { 00325 return std::auto_ptr<Geometry>( createGeometryCollection() ); 00326 } 00327 00328 // for the single geometry, return a clone 00329 if ( count == 1 ) { 00330 return std::auto_ptr<Geometry>( (*from)->clone() ); 00331 } 00332 00333 // Now we know it is a collection 00334 00335 // FIXME: 00336 // Until we tweak all the createMulti* interfaces 00337 // to support taking iterators we'll have to build 00338 // a custom vector here. 00339 std::vector<Geometry*> fromGeoms; 00340 for (T i=from; i != toofar; ++i) { 00341 const Geometry* g = *i; 00342 fromGeoms.push_back(const_cast<Geometry*>(g)); 00343 } 00344 00345 00346 // for an heterogeneous ... 00347 if ( isHeterogeneous ) { 00348 return std::auto_ptr<Geometry>( createGeometryCollection(fromGeoms) ); 00349 } 00350 00351 // At this point we know the collection is not hetereogenous. 00352 if ( dynamic_cast<const Polygon*>(*from) ) { 00353 return std::auto_ptr<Geometry>( createMultiPolygon(fromGeoms) ); 00354 } else if ( dynamic_cast<const LineString*>(*from) ) { 00355 return std::auto_ptr<Geometry>( createMultiLineString(fromGeoms) ); 00356 } else if ( dynamic_cast<const Point*>(*from) ) { 00357 return std::auto_ptr<Geometry>( createMultiPoint(fromGeoms) ); 00358 } 00359 // FIXME: Why not to throw an exception? --mloskot 00360 assert(0); // buildGeomtry encountered an unkwnon geometry type 00361 return std::auto_ptr<Geometry>(); // nullptr 00362 } 00363 00371 Geometry* buildGeometry(const std::vector<Geometry *> &geoms) const; 00372 00373 int getSRID() const; 00374 00378 const CoordinateSequenceFactory* getCoordinateSequenceFactory() const; 00379 00381 Geometry* createGeometry(const Geometry *g) const; 00382 00384 void destroyGeometry(Geometry *g) const; 00385 00386 private: 00387 const PrecisionModel* precisionModel; 00388 int SRID; 00389 const CoordinateSequenceFactory *coordinateListFactory; 00390 }; 00391 00392 } // namespace geos::geom 00393 } // namespace geos 00394 00395 #ifdef GEOS_INLINE 00396 # include "geos/geom/GeometryFactory.inl" 00397 #endif 00398 00399 #endif // ndef GEOS_GEOM_GEOMETRYFACTORY_H