GEOS  3.8.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/geom/PrecisionModel.h>
29 #include <geos/export.h>
30 #include <geos/inline.h>
31 #include <geos/util.h>
32 
33 #include <vector>
34 #include <memory>
35 #include <cassert>
36 #include <geos/util/IllegalArgumentException.h>
37 
38 namespace geos {
39 namespace geom {
40 class CoordinateSequenceFactory;
41 class Coordinate;
42 class CoordinateSequence;
43 class Envelope;
44 class Geometry;
45 class GeometryCollection;
46 class LineString;
47 class LinearRing;
48 class MultiLineString;
49 class MultiPoint;
50 class MultiPolygon;
51 class Polygon;
52 }
53 }
54 
55 namespace geos {
56 namespace geom { // geos::geom
57 
68 class GEOS_DLL GeometryFactory {
69 private:
70 
71  struct GeometryFactoryDeleter {
72  void
73  operator()(GeometryFactory* p) const
74  {
75  p->destroy();
76  }
77  };
78 
79 public:
80 
81  using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
82 
88  static GeometryFactory::Ptr create();
89 
102  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID,
103  CoordinateSequenceFactory* nCoordinateSequenceFactory);
104 
111  static GeometryFactory::Ptr create(CoordinateSequenceFactory* nCoordinateSequenceFactory);
112 
121  static GeometryFactory::Ptr create(const PrecisionModel* pm);
122 
132  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
133 
139  static GeometryFactory::Ptr create(const GeometryFactory& gf);
140 
147  static const GeometryFactory*
149 
150 //Skipped a lot of list to array convertors
151 
152  Point* createPointFromInternalCoord(const Coordinate* coord,
153  const Geometry* exemplar) const;
154 
156  //
159  std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
160 
165 
167  std::unique_ptr<Point> createPoint() const;
168 
170  Point* createPoint(const Coordinate& coordinate) const;
171 
173  Point* createPoint(CoordinateSequence* coordinates) const;
174 
176  Point* createPoint(const CoordinateSequence& coordinates) const;
177 
179  std::unique_ptr<GeometryCollection> createGeometryCollection() const;
180 
182  std::unique_ptr<Geometry> createEmptyGeometry() const;
183 
186  std::vector<Geometry*>* newGeoms) const;
187 
188  std::unique_ptr<GeometryCollection> createGeometryCollection(
189  std::vector<std::unique_ptr<Geometry>> && newGeoms) const;
190 
193  const std::vector<const Geometry*>& newGeoms) const;
194 
196  std::unique_ptr<MultiLineString> createMultiLineString() const;
197 
200  std::vector<Geometry*>* newLines) const;
201 
204  const std::vector<const Geometry*>& fromLines) const;
205 
206  std::unique_ptr<MultiLineString> createMultiLineString(
207  std::vector<std::unique_ptr<LineString>> && fromLines) const;
208 
209  std::unique_ptr<MultiLineString> createMultiLineString(
210  std::vector<std::unique_ptr<Geometry>> && fromLines) const;
211 
213  std::unique_ptr<MultiPolygon> createMultiPolygon() const;
214 
216  MultiPolygon* createMultiPolygon(std::vector<Geometry*>* newPolys) const;
217 
220  const std::vector<const Geometry*>& fromPolys) const;
221 
222  std::unique_ptr<MultiPolygon> createMultiPolygon(
223  std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
224 
225  std::unique_ptr<MultiPolygon> createMultiPolygon(
226  std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
227 
229  std::unique_ptr<LinearRing> createLinearRing() const;
230 
233 
234  std::unique_ptr<LinearRing> createLinearRing(
235  std::unique_ptr<CoordinateSequence> && newCoords) const;
236 
239  const CoordinateSequence& coordinates) const;
240 
242  std::unique_ptr<MultiPoint> createMultiPoint() const;
243 
245  MultiPoint* createMultiPoint(std::vector<Geometry*>* newPoints) const;
246 
247  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
248 
249  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
250 
253  const std::vector<const Geometry*>& fromPoints) const;
254 
259  const CoordinateSequence& fromCoords) const;
260 
265  const std::vector<Coordinate>& fromCoords) const;
266 
268  std::unique_ptr<Polygon> createPolygon() const;
269 
272  std::vector<LinearRing*>* holes) const;
273 
274  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
275 
276  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
277  std::vector<std::unique_ptr<LinearRing>> && holes) const;
278 
281  const std::vector<LinearRing*>& holes) const;
282 
284  std::unique_ptr<LineString> createLineString() const;
285 
287  std::unique_ptr<LineString> createLineString(const LineString& ls) const;
288 
291 
292  std::unique_ptr<LineString> createLineString(
293  std::unique_ptr<CoordinateSequence> && coordinates) const;
294 
297  const CoordinateSequence& coordinates) const;
298 
329  Geometry* buildGeometry(std::vector<Geometry*>* geoms) const;
330 
331  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
332 
334  //
341  template <class T>
342  std::unique_ptr<Geometry>
343  buildGeometry(T from, T toofar) const
344  {
345  bool isHeterogeneous = false;
346  size_t count = 0;
347  int geomClass = -1;
348  for(T i = from; i != toofar; ++i) {
349  ++count;
350  const Geometry* g = *i;
351  if(geomClass < 0) {
352  geomClass = g->getSortIndex();
353  }
354  else if(geomClass != g->getSortIndex()) {
355  isHeterogeneous = true;
356  }
357  }
358 
359  // for the empty geometry, return an empty GeometryCollection
360  if(count == 0) {
361  return std::unique_ptr<Geometry>(createGeometryCollection());
362  }
363 
364  // for the single geometry, return a clone
365  if(count == 1) {
366  return (*from)->clone();
367  }
368 
369  // Now we know it is a collection
370 
371  // FIXME:
372  // Until we tweak all the createMulti* interfaces
373  // to support taking iterators we'll have to build
374  // a custom vector here.
375  std::vector<std::unique_ptr<Geometry>> fromGeoms;
376  for(T i = from; i != toofar; ++i) {
377  fromGeoms.push_back((*i)->clone());
378  }
379 
380  // for an heterogeneous ...
381  if(isHeterogeneous) {
382  return createGeometryCollection(std::move(fromGeoms));
383  }
384 
385  // At this point we know the collection is not hetereogenous.
386  switch((*from)->getDimension()) {
387  case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
388  case Dimension::L: return createMultiLineString(std::move(fromGeoms));
389  case Dimension::P: return createMultiPoint(std::move(fromGeoms));
390  default:
391  throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
392  }
393  }
394 
402  Geometry* buildGeometry(const std::vector<const Geometry*>& geoms) const;
403 
404  int getSRID() const;
405 
410 
412  Geometry* createGeometry(const Geometry* g) const;
413 
415  void destroyGeometry(Geometry* g) const;
416 
418  //
423  void destroy();
424 
425 protected:
426 
433 
446  GeometryFactory(const PrecisionModel* pm, int newSRID,
447  CoordinateSequenceFactory* nCoordinateSequenceFactory);
448 
455  GeometryFactory(CoordinateSequenceFactory* nCoordinateSequenceFactory);
456 
466 
476  GeometryFactory(const PrecisionModel* pm, int newSRID);
477 
484 
486  virtual ~GeometryFactory();
487 
488 private:
489 
490  PrecisionModel precisionModel;
491  int SRID;
492  const CoordinateSequenceFactory* coordinateListFactory;
493 
494  mutable int _refCount;
495  bool _autoDestroy;
496 
497  friend class Geometry;
498 
499  void addRef() const;
500  void dropRef() const;
501 
502 };
503 
504 } // namespace geos::geom
505 } // namespace geos
506 
507 #ifdef GEOS_INLINE
508 # include "geos/geom/GeometryFactory.inl"
509 #endif
510 
511 #endif // ndef GEOS_GEOM_GEOMETRYFACTORY_H
geos::geom::GeometryFactory::GeometryFactory
GeometryFactory(const PrecisionModel *pm, int newSRID, CoordinateSequenceFactory *nCoordinateSequenceFactory)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel,...
geos::geom::GeometryFactory::createLinearRing
LinearRing * createLinearRing(CoordinateSequence *newCoords) const
Construct a LinearRing taking ownership of given arguments.
geos::geom::GeometryFactory::buildGeometry
Geometry * buildGeometry(std::vector< Geometry * > *geoms) const
geos::geom::MultiPoint
Definition: MultiPoint.h:53
geos::geom::Dimension::A
@ A
Dimension value of a surface (2).
Definition: Dimension.h:96
geos::geom::GeometryFactory::createPolygon
Polygon * createPolygon(LinearRing *shell, std::vector< LinearRing * > *holes) const
Construct a Polygon taking ownership of given arguments.
geos::geom::GeometryFactory::createGeometry
Geometry * createGeometry(const Geometry *g) const
Returns a clone of given Geometry.
geos::geom::PrecisionModel
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:87
geos::geom::GeometryFactory::createGeometryCollection
GeometryCollection * createGeometryCollection(std::vector< Geometry * > *newGeoms) const
Construct a GeometryCollection taking ownership of given arguments.
geos::geom::GeometryFactory::create
static GeometryFactory::Ptr create(const PrecisionModel *pm)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and the defaul...
geos::geom::LinearRing
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple.
Definition: LinearRing.h:54
geos
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
geos::geom::GeometryFactory::createLineString
std::unique_ptr< LineString > createLineString(const LineString &ls) const
Copy a LineString.
geos::geom::GeometryFactory::createLinearRing
std::unique_ptr< LinearRing > createLinearRing() const
Construct an EMPTY LinearRing.
geos::geom::GeometryFactory::toGeometry
std::unique_ptr< Geometry > toGeometry(const Envelope *envelope) const
Converts an Envelope to a Geometry.
geos::geom::GeometryFactory::createMultiPolygon
std::unique_ptr< MultiPolygon > createMultiPolygon() const
Construct an EMPTY MultiPolygon.
geos::geom::GeometryFactory::createMultiPolygon
MultiPolygon * createMultiPolygon(std::vector< Geometry * > *newPolys) const
Construct a MultiPolygon taking ownership of given arguments.
geos::geom::GeometryFactory::createPolygon
Polygon * createPolygon(const LinearRing &shell, const std::vector< LinearRing * > &holes) const
Construct a Polygon with a deep-copy of given arguments.
geos::geom::GeometryFactory::createMultiPoint
MultiPoint * createMultiPoint(const CoordinateSequence &fromCoords) const
Construct a MultiPoint containing a Point geometry for each Coordinate in the given list.
geos::geom::GeometryFactory::create
static GeometryFactory::Ptr create(const PrecisionModel *pm, int newSRID)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and spatial-re...
geos::geom::GeometryFactory::create
static GeometryFactory::Ptr create(const PrecisionModel *pm, int newSRID, CoordinateSequenceFactory *nCoordinateSequenceFactory)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel,...
geos::geom::GeometryFactory::create
static GeometryFactory::Ptr create(const GeometryFactory &gf)
Copy constructor.
geos::geom::GeometryFactory::createPoint
std::unique_ptr< Point > createPoint() const
Creates an EMPTY Point.
geos::geom::GeometryFactory::createEmptyGeometry
std::unique_ptr< Geometry > createEmptyGeometry() const
Construct the EMPTY Geometry.
geos::geom::CoordinateSequenceFactory
A factory to create concrete instances of CoordinateSequences.
Definition: CoordinateSequenceFactory.h:48
geos::geom::MultiLineString
Models a collection of LineStrings.
Definition: MultiLineString.h:51
geos::geom::GeometryFactory::createPoint
Point * createPoint(const CoordinateSequence &coordinates) const
Creates a Point with a deep-copy of the given CoordinateSequence.
geos::geom::GeometryFactory::GeometryFactory
GeometryFactory(const PrecisionModel *pm)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and the defaul...
geos::util::IllegalArgumentException
Indicates one or more illegal arguments.
Definition: IllegalArgumentException.h:60
geos::geom::Polygon
Represents a linear polygon, which may include holes.
Definition: Polygon.h:64
geos::geom::GeometryFactory::createMultiPoint
MultiPoint * createMultiPoint(std::vector< Geometry * > *newPoints) const
Construct a MultiPoint taking ownership of given arguments.
geos::geom::GeometryFactory::createMultiPoint
MultiPoint * createMultiPoint(const std::vector< Coordinate > &fromCoords) const
Construct a MultiPoint containing a Point geometry for each Coordinate in the given vector.
geos::geom::Coordinate
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:84
geos::geom::GeometryFactory::~GeometryFactory
virtual ~GeometryFactory()
Destructor.
geos::geom::GeometryFactory::GeometryFactory
GeometryFactory(const PrecisionModel *pm, int newSRID)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and spatial-re...
geos::geom::Geometry
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
geos::geom::GeometryFactory::createMultiLineString
MultiLineString * createMultiLineString(std::vector< Geometry * > *newLines) const
Construct a MultiLineString taking ownership of given arguments.
geos::geom::Dimension::L
@ L
Dimension value of a curve (1).
Definition: Dimension.h:93
geos::geom::GeometryFactory::createMultiPoint
std::unique_ptr< MultiPoint > createMultiPoint() const
Constructs an EMPTY MultiPoint.
geos::geom::GeometryFactory::createLineString
std::unique_ptr< LineString > createLineString() const
Construct an EMPTY LineString.
geos::geom::GeometryCollection
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:55
geos::geom::GeometryFactory::createLineString
LineString * createLineString(const CoordinateSequence &coordinates) const
Construct a LineString with a deep-copy of given argument.
geos::geom::CoordinateSequence
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
geos::geom::GeometryFactory::create
static GeometryFactory::Ptr create(CoordinateSequenceFactory *nCoordinateSequenceFactory)
Constructs a GeometryFactory that generates Geometries having the given CoordinateSequence implementa...
geos::geom::GeometryFactory::createPoint
Point * createPoint(CoordinateSequence *coordinates) const
Creates a Point taking ownership of the given CoordinateSequence.
geos::geom::GeometryFactory::destroy
void destroy()
Request that the instance is deleted.
geos::geom::GeometryFactory::createLineString
LineString * createLineString(CoordinateSequence *coordinates) const
Construct a LineString taking ownership of given argument.
geos::geom::GeometryFactory::buildGeometry
Geometry * buildGeometry(const std::vector< const Geometry * > &geoms) const
This function does the same thing of the omonimouse function taking vector pointer instead of referen...
geos::geom::GeometryFactory::createPoint
Point * createPoint(const Coordinate &coordinate) const
Creates a Point using the given Coordinate.
geos::geom::GeometryFactory::createLinearRing
LinearRing * createLinearRing(const CoordinateSequence &coordinates) const
Construct a LinearRing with a deep-copy of given arguments.
geos::geom::GeometryFactory::GeometryFactory
GeometryFactory()
Constructs a GeometryFactory that generates Geometries having a floating PrecisionModel and a spatial...
geos::geom::GeometryFactory::createMultiPoint
MultiPoint * createMultiPoint(const std::vector< const Geometry * > &fromPoints) const
Construct a MultiPoint with a deep-copy of given arguments.
geos::geom::Dimension::P
@ P
Dimension value of a point (0).
Definition: Dimension.h:90
geos::geom::GeometryFactory::destroyGeometry
void destroyGeometry(Geometry *g) const
Destroy a Geometry, or release it.
geos::geom::Envelope
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:90
geos::geom::GeometryFactory::createGeometryCollection
std::unique_ptr< GeometryCollection > createGeometryCollection() const
Construct an EMPTY GeometryCollection.
geos::geom::GeometryFactory::buildGeometry
std::unique_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector<Geometry *>&) for semantics.
Definition: GeometryFactory.h:343
geos::geom::GeometryFactory
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:68
geos::geom::GeometryFactory::GeometryFactory
GeometryFactory(const GeometryFactory &gf)
Copy constructor.
geos::geom::MultiPolygon
Models a collection of Polygons.
Definition: MultiPolygon.h:59
geos::geom::GeometryFactory::getDefaultInstance
static const GeometryFactory * getDefaultInstance()
Return a pointer to the default GeometryFactory. This is a global shared object instantiated using de...
geos::geom::GeometryFactory::createPolygon
std::unique_ptr< Polygon > createPolygon() const
Construct an EMPTY Polygon.
geos::geom::GeometryFactory::createMultiLineString
MultiLineString * createMultiLineString(const std::vector< const Geometry * > &fromLines) const
Construct a MultiLineString with a deep-copy of given arguments.
geos::geom::GeometryFactory::getCoordinateSequenceFactory
const CoordinateSequenceFactory * getCoordinateSequenceFactory() const
Returns the CoordinateSequenceFactory associated with this GeometryFactory.
geos::geom::GeometryFactory::getPrecisionModel
const PrecisionModel * getPrecisionModel() const
Returns the PrecisionModel that Geometries created by this factory will be associated with.
geos::geom::GeometryFactory::createGeometryCollection
GeometryCollection * createGeometryCollection(const std::vector< const Geometry * > &newGeoms) const
Constructs a GeometryCollection with a deep-copy of args.
geos::geom::LineString
Definition: LineString.h:68
geos::geom::Point
Definition: Point.h:66
geos::geom::GeometryFactory::createMultiPolygon
MultiPolygon * createMultiPolygon(const std::vector< const Geometry * > &fromPolys) const
Construct a MultiPolygon with a deep-copy of given arguments.
geos::geom::GeometryFactory::GeometryFactory
GeometryFactory(CoordinateSequenceFactory *nCoordinateSequenceFactory)
Constructs a GeometryFactory that generates Geometries having the given CoordinateSequence implementa...
geos::geom::GeometryFactory::create
static GeometryFactory::Ptr create()
Constructs a GeometryFactory that generates Geometries having a floating PrecisionModel and a spatial...
geos::geom::GeometryFactory::createMultiLineString
std::unique_ptr< MultiLineString > createMultiLineString() const
Construct an EMPTY MultiLineString.