GEOS  3.7.1
GeometryFactory.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2011 Sandro Santilli <strk@kbt.io>
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 private:
69 
70  struct GeometryFactoryDeleter
71  {
72  void operator()(GeometryFactory* p) const
73  {
74  p->destroy();
75  }
76  };
77 
78 public:
79 
80  using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
81 
87  static GeometryFactory::Ptr create();
88 
101  static GeometryFactory::Ptr create(const PrecisionModel *pm, int newSRID,
102  CoordinateSequenceFactory *nCoordinateSequenceFactory);
103 
110  static GeometryFactory::Ptr create(CoordinateSequenceFactory *nCoordinateSequenceFactory);
111 
120  static GeometryFactory::Ptr create(const PrecisionModel *pm);
121 
131  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
132 
138  static GeometryFactory::Ptr create(const GeometryFactory &gf);
139 
146  static const GeometryFactory*
147  getDefaultInstance();
148 
149 //Skipped a lot of list to array convertors
150 
151  Point* createPointFromInternalCoord(const Coordinate* coord,
152  const Geometry *exemplar) const;
153 
155  //
158  Geometry* toGeometry(const Envelope* envelope) const;
159 
163  const PrecisionModel* getPrecisionModel() const;
164 
166  Point* createPoint() const;
167 
169  Point* createPoint(const Coordinate& coordinate) const;
170 
172  Point* createPoint(CoordinateSequence *coordinates) const;
173 
175  Point* createPoint(const CoordinateSequence &coordinates) const;
176 
178  GeometryCollection* createGeometryCollection() const;
179 
181  Geometry* createEmptyGeometry() const;
182 
184  GeometryCollection* createGeometryCollection(
185  std::vector<Geometry *> *newGeoms) const;
186 
188  GeometryCollection* createGeometryCollection(
189  const std::vector<Geometry *> &newGeoms) const;
190 
192  MultiLineString* createMultiLineString() const;
193 
195  MultiLineString* createMultiLineString(
196  std::vector<Geometry *> *newLines) const;
197 
199  MultiLineString* createMultiLineString(
200  const std::vector<Geometry *> &fromLines) const;
201 
203  MultiPolygon* createMultiPolygon() const;
204 
206  MultiPolygon* createMultiPolygon(std::vector<Geometry *> *newPolys) const;
207 
209  MultiPolygon* createMultiPolygon(
210  const std::vector<Geometry *> &fromPolys) const;
211 
213  LinearRing* createLinearRing() const;
214 
216  LinearRing* createLinearRing(CoordinateSequence* newCoords) const;
217 
218  std::unique_ptr<Geometry> createLinearRing(
219  std::unique_ptr<CoordinateSequence> newCoords) const;
220 
222  LinearRing* createLinearRing(
223  const CoordinateSequence& coordinates) const;
224 
226  MultiPoint* createMultiPoint() const;
227 
229  MultiPoint* createMultiPoint(std::vector<Geometry *> *newPoints) const;
230 
232  MultiPoint* createMultiPoint(
233  const std::vector<Geometry *> &fromPoints) const;
234 
238  MultiPoint* createMultiPoint(
239  const CoordinateSequence &fromCoords) const;
240 
244  MultiPoint* createMultiPoint(
245  const std::vector<Coordinate> &fromCoords) const;
246 
248  Polygon* createPolygon() const;
249 
251  Polygon* createPolygon(LinearRing *shell,
252  std::vector<Geometry *> *holes) const;
253 
255  Polygon* createPolygon(const LinearRing &shell,
256  const std::vector<Geometry *> &holes) const;
257 
259  LineString* createLineString() const;
260 
262  std::unique_ptr<LineString> createLineString(const LineString& ls) const;
263 
265  LineString* createLineString(CoordinateSequence* coordinates) const;
266 
267  std::unique_ptr<Geometry> createLineString(
268  std::unique_ptr<CoordinateSequence> coordinates) const;
269 
271  LineString* createLineString(
272  const CoordinateSequence& coordinates) const;
273 
305  Geometry* buildGeometry(std::vector<Geometry *> *geoms) const;
306 
308  //
315  template <class T>
316  std::unique_ptr<Geometry> buildGeometry(T from, T toofar) const
317  {
318  bool isHeterogeneous = false;
319  size_t count = 0;
320  int geomClass = -1;
321  for (T i=from; i != toofar; ++i)
322  {
323  ++count;
324  const Geometry* g = *i;
325  if ( geomClass < 0 ) {
326  geomClass = g->getClassSortIndex();
327  }
328  else if ( geomClass != g->getClassSortIndex() ) {
329  isHeterogeneous = true;
330  }
331  }
332 
333  // for the empty geometry, return an empty GeometryCollection
334  if ( count == 0 ) {
335  return std::unique_ptr<Geometry>( createGeometryCollection() );
336  }
337 
338  // for the single geometry, return a clone
339  if ( count == 1 ) {
340  return std::unique_ptr<Geometry>( (*from)->clone() );
341  }
342 
343  // Now we know it is a collection
344 
345  // FIXME:
346  // Until we tweak all the createMulti* interfaces
347  // to support taking iterators we'll have to build
348  // a custom vector here.
349  std::vector<Geometry*> fromGeoms;
350  for (T i=from; i != toofar; ++i) {
351  const Geometry* g = *i;
352  fromGeoms.push_back(const_cast<Geometry*>(g));
353  }
354 
355 
356  // for an heterogeneous ...
357  if ( isHeterogeneous ) {
358  return std::unique_ptr<Geometry>( createGeometryCollection(fromGeoms) );
359  }
360 
361  // At this point we know the collection is not hetereogenous.
362  if ( dynamic_cast<const Polygon*>(*from) ) {
363  return std::unique_ptr<Geometry>( createMultiPolygon(fromGeoms) );
364  } else if ( dynamic_cast<const LineString*>(*from) ) {
365  return std::unique_ptr<Geometry>( createMultiLineString(fromGeoms) );
366  } else if ( dynamic_cast<const Point*>(*from) ) {
367  return std::unique_ptr<Geometry>( createMultiPoint(fromGeoms) );
368  }
369  // FIXME: Why not to throw an exception? --mloskot
370  assert(0); // buildGeomtry encountered an unkwnon geometry type
371  return std::unique_ptr<Geometry>(); // nullptr
372  }
373 
381  Geometry* buildGeometry(const std::vector<Geometry *> &geoms) const;
382 
383  int getSRID() const;
384 
388  const CoordinateSequenceFactory* getCoordinateSequenceFactory() const;
389 
391  Geometry* createGeometry(const Geometry *g) const;
392 
394  void destroyGeometry(Geometry *g) const;
395 
397  //
402  void destroy();
403 
404 protected:
405 
411  GeometryFactory();
412 
425  GeometryFactory(const PrecisionModel *pm, int newSRID,
426  CoordinateSequenceFactory *nCoordinateSequenceFactory);
427 
434  GeometryFactory(CoordinateSequenceFactory *nCoordinateSequenceFactory);
435 
444  GeometryFactory(const PrecisionModel *pm);
445 
455  GeometryFactory(const PrecisionModel* pm, int newSRID);
456 
462  GeometryFactory(const GeometryFactory &gf);
463 
465  virtual ~GeometryFactory();
466 
467 private:
468 
469  const PrecisionModel* precisionModel;
470  int SRID;
471  const CoordinateSequenceFactory *coordinateListFactory;
472 
473  mutable int _refCount;
474  bool _autoDestroy;
475 
476 friend class Geometry;
477 
478  void addRef() const;
479  void dropRef() const;
480 
481 };
482 
483 } // namespace geos::geom
484 } // namespace geos
485 
486 #ifdef GEOS_INLINE
487 # include "geos/geom/GeometryFactory.inl"
488 #endif
489 
490 #endif // ndef GEOS_GEOM_GEOMETRYFACTORY_H
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:59
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Models a collection of Polygons.
Definition: MultiPolygon.h:60
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:177
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
std::unique_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector<Geometry *>&) for semantics.
Definition: GeometryFactory.h:316