GEOS  3.8.0dev
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 Polygon;
49 class PrecisionModel;
50 }
51 }
52 
53 namespace geos {
54 namespace geom { // geos::geom
55 
66 class GEOS_DLL GeometryFactory {
67 private:
68 
69  struct GeometryFactoryDeleter {
70  void
71  operator()(GeometryFactory* p) const
72  {
73  p->destroy();
74  }
75  };
76 
77 public:
78 
79  using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
80 
86  static GeometryFactory::Ptr create();
87 
100  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID,
101  CoordinateSequenceFactory* nCoordinateSequenceFactory);
102 
109  static GeometryFactory::Ptr create(CoordinateSequenceFactory* nCoordinateSequenceFactory);
110 
119  static GeometryFactory::Ptr create(const PrecisionModel* pm);
120 
130  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
131 
137  static GeometryFactory::Ptr create(const GeometryFactory& gf);
138 
145  static const GeometryFactory*
146  getDefaultInstance();
147 
148 //Skipped a lot of list to array convertors
149 
150  Point* createPointFromInternalCoord(const Coordinate* coord,
151  const Geometry* exemplar) const;
152 
154  //
157  Geometry* toGeometry(const Envelope* envelope) const;
158 
162  const PrecisionModel* getPrecisionModel() const;
163 
165  Point* createPoint() const;
166 
168  Point* createPoint(const Coordinate& coordinate) const;
169 
171  Point* createPoint(CoordinateSequence* coordinates) const;
172 
174  Point* createPoint(const CoordinateSequence& coordinates) const;
175 
177  GeometryCollection* createGeometryCollection() const;
178 
180  Geometry* createEmptyGeometry() const;
181 
183  GeometryCollection* createGeometryCollection(
184  std::vector<Geometry*>* newGeoms) const;
185 
186  std::unique_ptr<GeometryCollection> createGeometryCollection(
187  std::vector<std::unique_ptr<Geometry>> && newGeoms) const;
188 
190  GeometryCollection* createGeometryCollection(
191  const std::vector<Geometry*>& newGeoms) const;
192 
194  MultiLineString* createMultiLineString() const;
195 
197  MultiLineString* createMultiLineString(
198  std::vector<Geometry*>* newLines) const;
199 
201  MultiLineString* createMultiLineString(
202  const std::vector<Geometry*>& fromLines) const;
203 
204  std::unique_ptr<MultiLineString> createMultiLineString(
205  std::vector<std::unique_ptr<LineString>> && fromLines) const;
206 
207  std::unique_ptr<MultiLineString> createMultiLineString(
208  std::vector<std::unique_ptr<Geometry>> && fromLines) const;
209 
211  MultiPolygon* createMultiPolygon() const;
212 
214  MultiPolygon* createMultiPolygon(std::vector<Geometry*>* newPolys) const;
215 
217  MultiPolygon* createMultiPolygon(
218  const std::vector<Geometry*>& fromPolys) const;
219 
220  std::unique_ptr<MultiPolygon> createMultiPolygon(
221  std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
222 
223  std::unique_ptr<MultiPolygon> createMultiPolygon(
224  std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
225 
227  LinearRing* createLinearRing() const;
228 
230  LinearRing* createLinearRing(CoordinateSequence* newCoords) const;
231 
232  std::unique_ptr<LinearRing> createLinearRing(
233  std::unique_ptr<CoordinateSequence> && newCoords) const;
234 
236  LinearRing* createLinearRing(
237  const CoordinateSequence& coordinates) const;
238 
240  MultiPoint* createMultiPoint() const;
241 
243  MultiPoint* createMultiPoint(std::vector<Geometry*>* newPoints) const;
244 
245  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
246 
247  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
248 
250  MultiPoint* createMultiPoint(
251  const std::vector<Geometry*>& fromPoints) const;
252 
256  MultiPoint* createMultiPoint(
257  const CoordinateSequence& fromCoords) const;
258 
262  MultiPoint* createMultiPoint(
263  const std::vector<Coordinate>& fromCoords) const;
264 
266  Polygon* createPolygon() const;
267 
269  Polygon* createPolygon(LinearRing* shell,
270  std::vector<LinearRing*>* holes) const;
271 
272  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
273 
274  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
275  std::vector<std::unique_ptr<LinearRing>> && holes) const;
276 
278  Polygon* createPolygon(const LinearRing& shell,
279  const std::vector<LinearRing*>& holes) const;
280 
282  LineString* createLineString() const;
283 
285  std::unique_ptr<LineString> createLineString(const LineString& ls) const;
286 
288  LineString* createLineString(CoordinateSequence* coordinates) const;
289 
290  std::unique_ptr<LineString> createLineString(
291  std::unique_ptr<CoordinateSequence> && coordinates) const;
292 
294  LineString* createLineString(
295  const CoordinateSequence& coordinates) const;
296 
327  Geometry* buildGeometry(std::vector<Geometry*>* geoms) const;
328 
330  //
337  template <class T>
338  std::unique_ptr<Geometry>
339  buildGeometry(T from, T toofar) const
340  {
341  bool isHeterogeneous = false;
342  size_t count = 0;
343  int geomClass = -1;
344  for(T i = from; i != toofar; ++i) {
345  ++count;
346  const Geometry* g = *i;
347  if(geomClass < 0) {
348  geomClass = g->getSortIndex();
349  }
350  else if(geomClass != g->getSortIndex()) {
351  isHeterogeneous = true;
352  }
353  }
354 
355  // for the empty geometry, return an empty GeometryCollection
356  if(count == 0) {
357  return std::unique_ptr<Geometry>(createGeometryCollection());
358  }
359 
360  // for the single geometry, return a clone
361  if(count == 1) {
362  return std::unique_ptr<Geometry>((*from)->clone());
363  }
364 
365  // Now we know it is a collection
366 
367  // FIXME:
368  // Until we tweak all the createMulti* interfaces
369  // to support taking iterators we'll have to build
370  // a custom vector here.
371  std::vector<Geometry*> fromGeoms;
372  for(T i = from; i != toofar; ++i) {
373  const Geometry* g = *i;
374  fromGeoms.push_back(const_cast<Geometry*>(g));
375  }
376 
377 
378  // for an heterogeneous ...
379  if(isHeterogeneous) {
380  return std::unique_ptr<Geometry>(createGeometryCollection(fromGeoms));
381  }
382 
383  // At this point we know the collection is not hetereogenous.
384  if(dynamic_cast<const Polygon*>(*from)) {
385  return std::unique_ptr<Geometry>(createMultiPolygon(fromGeoms));
386  }
387  else if(dynamic_cast<const LineString*>(*from)) {
388  return std::unique_ptr<Geometry>(createMultiLineString(fromGeoms));
389  }
390  else if(dynamic_cast<const Point*>(*from)) {
391  return std::unique_ptr<Geometry>(createMultiPoint(fromGeoms));
392  }
393  // FIXME: Why not to throw an exception? --mloskot
394  assert(0); // buildGeomtry encountered an unkwnon geometry type
395  return std::unique_ptr<Geometry>(); // nullptr
396  }
397 
405  Geometry* buildGeometry(const std::vector<Geometry*>& geoms) const;
406 
407  int getSRID() const;
408 
412  const CoordinateSequenceFactory* getCoordinateSequenceFactory() const;
413 
415  Geometry* createGeometry(const Geometry* g) const;
416 
418  void destroyGeometry(Geometry* g) const;
419 
421  //
426  void destroy();
427 
428 protected:
429 
435  GeometryFactory();
436 
449  GeometryFactory(const PrecisionModel* pm, int newSRID,
450  CoordinateSequenceFactory* nCoordinateSequenceFactory);
451 
458  GeometryFactory(CoordinateSequenceFactory* nCoordinateSequenceFactory);
459 
468  GeometryFactory(const PrecisionModel* pm);
469 
479  GeometryFactory(const PrecisionModel* pm, int newSRID);
480 
486  GeometryFactory(const GeometryFactory& gf);
487 
489  virtual ~GeometryFactory();
490 
491 private:
492 
493  const PrecisionModel* precisionModel;
494  int SRID;
495  const CoordinateSequenceFactory* coordinateListFactory;
496 
497  mutable int _refCount;
498  bool _autoDestroy;
499 
500  friend class Geometry;
501 
502  void addRef() const;
503  void dropRef() const;
504 
505 };
506 
507 } // namespace geos::geom
508 } // namespace geos
509 
510 #ifdef GEOS_INLINE
511 # include "geos/geom/GeometryFactory.inl"
512 #endif
513 
514 #endif // ndef GEOS_GEOM_GEOMETRYFACTORY_H
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:58
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Models a collection of Polygons.
Definition: MultiPolygon.h:59
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:188
Definition: LineString.h:68
Represents a linear polygon, which may include holes.
Definition: Polygon.h:64
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:66
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:55
void destroy()
Request that the instance is deleted.
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
Definition: MultiPoint.h:53
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple...
Definition: LinearRing.h:54
std::unique_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector&) for semantics.
Definition: GeometryFactory.h:339
A factory to create concrete instances of CoordinateSequences.
Definition: CoordinateSequenceFactory.h:48
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
Definition: Point.h:65
Models a collection of LineStrings.
Definition: MultiLineString.h:51