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 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  void
72  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>
317  buildGeometry(T from, T toofar) const
318  {
319  bool isHeterogeneous = false;
320  size_t count = 0;
321  int geomClass = -1;
322  for(T i = from; i != toofar; ++i) {
323  ++count;
324  const Geometry* g = *i;
325  if(geomClass < 0) {
326  geomClass = g->getSortIndex();
327  }
328  else if(geomClass != g->getSortIndex()) {
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  }
365  else if(dynamic_cast<const LineString*>(*from)) {
366  return std::unique_ptr<Geometry>(createMultiLineString(fromGeoms));
367  }
368  else if(dynamic_cast<const Point*>(*from)) {
369  return std::unique_ptr<Geometry>(createMultiPoint(fromGeoms));
370  }
371  // FIXME: Why not to throw an exception? --mloskot
372  assert(0); // buildGeomtry encountered an unkwnon geometry type
373  return std::unique_ptr<Geometry>(); // nullptr
374  }
375 
383  Geometry* buildGeometry(const std::vector<Geometry*>& geoms) const;
384 
385  int getSRID() const;
386 
390  const CoordinateSequenceFactory* getCoordinateSequenceFactory() const;
391 
393  Geometry* createGeometry(const Geometry* g) const;
394 
396  void destroyGeometry(Geometry* g) const;
397 
399  //
404  void destroy();
405 
406 protected:
407 
413  GeometryFactory();
414 
427  GeometryFactory(const PrecisionModel* pm, int newSRID,
428  CoordinateSequenceFactory* nCoordinateSequenceFactory);
429 
436  GeometryFactory(CoordinateSequenceFactory* nCoordinateSequenceFactory);
437 
446  GeometryFactory(const PrecisionModel* pm);
447 
457  GeometryFactory(const PrecisionModel* pm, int newSRID);
458 
464  GeometryFactory(const GeometryFactory& gf);
465 
467  virtual ~GeometryFactory();
468 
469 private:
470 
471  const PrecisionModel* precisionModel;
472  int SRID;
473  const CoordinateSequenceFactory* coordinateListFactory;
474 
475  mutable int _refCount;
476  bool _autoDestroy;
477 
478  friend class Geometry;
479 
480  void addRef() const;
481  void dropRef() const;
482 
483 };
484 
485 } // namespace geos::geom
486 } // namespace geos
487 
488 #ifdef GEOS_INLINE
489 # include "geos/geom/GeometryFactory.inl"
490 #endif
491 
492 #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: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:187
Definition: LineString.h:69
Represents a linear polygon, which may include holes.
Definition: Polygon.h:65
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:55
void destroy()
Request that the instance is deleted.
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
Definition: MultiPoint.h:54
Models an OGC SFS LinearRing.
Definition: LinearRing.h:56
std::unique_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector&) for semantics.
Definition: GeometryFactory.h:317
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:58
Definition: Point.h:66
Models a collection of (}s.
Definition: MultiLineString.h:51