GEOS  3.6.1
GeometryFactory.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2011 Sandro Santilli <strk@keybit.net>
7  * Copyright (C) 2006 Refractions Research Inc.
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU Lesser General Public Licence as published
11  * by the Free Software Foundation.
12  * See the COPYING file for more information.
13  *
14  **********************************************************************
15  *
16  * Last port: geom/GeometryFactory.java r320 (JTS-1.12)
17  *
18  **********************************************************************/
19 
20 #ifndef GEOS_GEOM_GEOMETRYFACTORY_H
21 #define GEOS_GEOM_GEOMETRYFACTORY_H
22 
23 #include <geos/geom/Geometry.h>
24 #include <geos/geom/GeometryCollection.h>
25 #include <geos/geom/MultiPoint.h>
26 #include <geos/geom/MultiLineString.h>
27 #include <geos/geom/MultiPolygon.h>
28 #include <geos/export.h>
29 #include <geos/inline.h>
30 
31 #include <vector>
32 #include <memory>
33 #include <cassert>
34 
35 namespace geos {
36  namespace geom {
37  class CoordinateSequenceFactory;
38  class Coordinate;
39  class CoordinateSequence;
40  class Envelope;
41  class Geometry;
42  class GeometryCollection;
43  class LineString;
44  class LinearRing;
45  class MultiLineString;
46  class MultiPoint;
47  class MultiPolygon;
48  class Point;
49  class Polygon;
50  class PrecisionModel;
51  }
52 }
53 
54 namespace geos {
55 namespace geom { // geos::geom
56 
67 class GEOS_DLL GeometryFactory {
68 public:
69 
70  // TODO: typedef std::unique_ptr<GeometryFactory,destroy>
71  class unique_ptr {
72  mutable GeometryFactory *_f;
73  public:
74  // Copying an auto_unique_ptr transfers ownership
75  unique_ptr(const unique_ptr& o): _f(o.release()) {};
76  GeometryFactory* release() const { GeometryFactory *f = _f; _f=0; return f; }
77  void reset(GeometryFactory* f) { if ( _f ) _f->destroy(); _f = f; }
78  // assigning an auto_unique_ptr transfers ownership
79  unique_ptr& operator=(const unique_ptr& o) {
80  reset( o.release() );
81  return *this;
82  }
83  GeometryFactory* get() const { return _f; }
84  GeometryFactory* operator->() const { return _f; }
85  GeometryFactory& operator*() { return *_f; };
86  unique_ptr(): _f(0) {}
87  unique_ptr(GeometryFactory* f): _f(f) {}
88  ~unique_ptr() { reset(0); }
89  };
90 
96  static GeometryFactory::unique_ptr create();
97 
110  static GeometryFactory::unique_ptr create(const PrecisionModel *pm, int newSRID,
111  CoordinateSequenceFactory *nCoordinateSequenceFactory);
112 
119  static GeometryFactory::unique_ptr create(CoordinateSequenceFactory *nCoordinateSequenceFactory);
120 
129  static GeometryFactory::unique_ptr create(const PrecisionModel *pm);
130 
140  static GeometryFactory::unique_ptr create(const PrecisionModel* pm, int newSRID);
141 
147  static GeometryFactory::unique_ptr create(const GeometryFactory &gf);
148 
155  static const GeometryFactory*
156  getDefaultInstance();
157 
158 //Skipped a lot of list to array convertors
159 
160  Point* createPointFromInternalCoord(const Coordinate* coord,
161  const Geometry *exemplar) const;
162 
164  //
167  Geometry* toGeometry(const Envelope* envelope) const;
168 
172  const PrecisionModel* getPrecisionModel() const;
173 
175  Point* createPoint() const;
176 
178  Point* createPoint(const Coordinate& coordinate) const;
179 
181  Point* createPoint(CoordinateSequence *coordinates) const;
182 
184  Point* createPoint(const CoordinateSequence &coordinates) const;
185 
187  GeometryCollection* createGeometryCollection() const;
188 
190  Geometry* createEmptyGeometry() const;
191 
193  GeometryCollection* createGeometryCollection(
194  std::vector<Geometry *> *newGeoms) const;
195 
197  GeometryCollection* createGeometryCollection(
198  const std::vector<Geometry *> &newGeoms) const;
199 
201  MultiLineString* createMultiLineString() const;
202 
204  MultiLineString* createMultiLineString(
205  std::vector<Geometry *> *newLines) const;
206 
208  MultiLineString* createMultiLineString(
209  const std::vector<Geometry *> &fromLines) const;
210 
212  MultiPolygon* createMultiPolygon() const;
213 
215  MultiPolygon* createMultiPolygon(std::vector<Geometry *> *newPolys) const;
216 
218  MultiPolygon* createMultiPolygon(
219  const std::vector<Geometry *> &fromPolys) const;
220 
222  LinearRing* createLinearRing() const;
223 
225  LinearRing* createLinearRing(CoordinateSequence* newCoords) const;
226 
227  std::auto_ptr<Geometry> createLinearRing(
228  std::auto_ptr<CoordinateSequence> newCoords) const;
229 
231  LinearRing* createLinearRing(
232  const CoordinateSequence& coordinates) const;
233 
235  MultiPoint* createMultiPoint() const;
236 
238  MultiPoint* createMultiPoint(std::vector<Geometry *> *newPoints) const;
239 
241  MultiPoint* createMultiPoint(
242  const std::vector<Geometry *> &fromPoints) const;
243 
247  MultiPoint* createMultiPoint(
248  const CoordinateSequence &fromCoords) const;
249 
253  MultiPoint* createMultiPoint(
254  const std::vector<Coordinate> &fromCoords) const;
255 
257  Polygon* createPolygon() const;
258 
260  Polygon* createPolygon(LinearRing *shell,
261  std::vector<Geometry *> *holes) const;
262 
264  Polygon* createPolygon(const LinearRing &shell,
265  const std::vector<Geometry *> &holes) const;
266 
268  LineString* createLineString() const;
269 
271  std::auto_ptr<LineString> createLineString(const LineString& ls) const;
272 
274  LineString* createLineString(CoordinateSequence* coordinates) const;
275 
276  std::auto_ptr<Geometry> createLineString(
277  std::auto_ptr<CoordinateSequence> coordinates) const;
278 
280  LineString* createLineString(
281  const CoordinateSequence& coordinates) const;
282 
314  Geometry* buildGeometry(std::vector<Geometry *> *geoms) const;
315 
317  //
324  template <class T>
325  std::auto_ptr<Geometry> buildGeometry(T from, T toofar) const
326  {
327  bool isHeterogeneous = false;
328  size_t count = 0;
329  int geomClass = -1;
330  for (T i=from; i != toofar; ++i)
331  {
332  ++count;
333  const Geometry* g = *i;
334  if ( geomClass < 0 ) {
335  geomClass = g->getClassSortIndex();
336  }
337  else if ( geomClass != g->getClassSortIndex() ) {
338  isHeterogeneous = true;
339  }
340  }
341 
342  // for the empty geometry, return an empty GeometryCollection
343  if ( count == 0 ) {
344  return std::auto_ptr<Geometry>( createGeometryCollection() );
345  }
346 
347  // for the single geometry, return a clone
348  if ( count == 1 ) {
349  return std::auto_ptr<Geometry>( (*from)->clone() );
350  }
351 
352  // Now we know it is a collection
353 
354  // FIXME:
355  // Until we tweak all the createMulti* interfaces
356  // to support taking iterators we'll have to build
357  // a custom vector here.
358  std::vector<Geometry*> fromGeoms;
359  for (T i=from; i != toofar; ++i) {
360  const Geometry* g = *i;
361  fromGeoms.push_back(const_cast<Geometry*>(g));
362  }
363 
364 
365  // for an heterogeneous ...
366  if ( isHeterogeneous ) {
367  return std::auto_ptr<Geometry>( createGeometryCollection(fromGeoms) );
368  }
369 
370  // At this point we know the collection is not hetereogenous.
371  if ( dynamic_cast<const Polygon*>(*from) ) {
372  return std::auto_ptr<Geometry>( createMultiPolygon(fromGeoms) );
373  } else if ( dynamic_cast<const LineString*>(*from) ) {
374  return std::auto_ptr<Geometry>( createMultiLineString(fromGeoms) );
375  } else if ( dynamic_cast<const Point*>(*from) ) {
376  return std::auto_ptr<Geometry>( createMultiPoint(fromGeoms) );
377  }
378  // FIXME: Why not to throw an exception? --mloskot
379  assert(0); // buildGeomtry encountered an unkwnon geometry type
380  return std::auto_ptr<Geometry>(); // nullptr
381  }
382 
390  Geometry* buildGeometry(const std::vector<Geometry *> &geoms) const;
391 
392  int getSRID() const;
393 
397  const CoordinateSequenceFactory* getCoordinateSequenceFactory() const;
398 
400  Geometry* createGeometry(const Geometry *g) const;
401 
403  void destroyGeometry(Geometry *g) const;
404 
406  //
411  void destroy();
412 
413 protected:
414 
420  GeometryFactory();
421 
434  GeometryFactory(const PrecisionModel *pm, int newSRID,
435  CoordinateSequenceFactory *nCoordinateSequenceFactory);
436 
443  GeometryFactory(CoordinateSequenceFactory *nCoordinateSequenceFactory);
444 
453  GeometryFactory(const PrecisionModel *pm);
454 
464  GeometryFactory(const PrecisionModel* pm, int newSRID);
465 
471  GeometryFactory(const GeometryFactory &gf);
472 
474  virtual ~GeometryFactory();
475 
476 private:
477 
478  const PrecisionModel* precisionModel;
479  int SRID;
480  const CoordinateSequenceFactory *coordinateListFactory;
481 
482  mutable int _refCount;
483  bool _autoDestroy;
484 
485 friend class Geometry;
486 
487  void addRef() const;
488  void dropRef() const;
489 
490 };
491 
492 } // namespace geos::geom
493 } // namespace geos
494 
495 #ifdef GEOS_INLINE
496 # include "geos/geom/GeometryFactory.inl"
497 #endif
498 
499 #endif // ndef GEOS_GEOM_GEOMETRYFACTORY_H
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:53
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Models a collection of Polygons.
Definition: MultiPolygon.h:60
std::auto_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector<Geometry *>&) for semantics.
Definition: GeometryFactory.h:325
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:87
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:167
Definition: LineString.h:70
Represents a linear polygon, which may include holes.
Definition: Polygon.h:66
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:67
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:56
void destroy()
Request that the instance is deleted.
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
Definition: MultiPoint.h:55
Models an OGC SFS LinearRing.
Definition: LinearRing.h:57
A factory to create concrete instances of CoordinateSequences.
Definition: CoordinateSequenceFactory.h:47
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:59
Definition: Point.h:67
Models a collection of (}s.
Definition: MultiLineString.h:51