GEOS  3.10.2
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 
159  std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
160 
165 
167  std::unique_ptr<Point> createPoint(std::size_t coordinateDimension = 2) 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  template<typename T>
189  std::unique_ptr<GeometryCollection> createGeometryCollection(
190  std::vector<std::unique_ptr<T>> && newGeoms) const {
191  // Can't use make_unique because constructor is protected
192  return std::unique_ptr<GeometryCollection>(new GeometryCollection(Geometry::toGeometryArray(std::move(newGeoms)), *this));
193  }
194 
197  const std::vector<const Geometry*>& newGeoms) const;
198 
200  std::unique_ptr<MultiLineString> createMultiLineString() const;
201 
204  std::vector<Geometry*>* newLines) const;
205 
208  const std::vector<const Geometry*>& fromLines) const;
209 
210  std::unique_ptr<MultiLineString> createMultiLineString(
211  std::vector<std::unique_ptr<LineString>> && fromLines) const;
212 
213  std::unique_ptr<MultiLineString> createMultiLineString(
214  std::vector<std::unique_ptr<Geometry>> && fromLines) const;
215 
217  std::unique_ptr<MultiPolygon> createMultiPolygon() const;
218 
220  MultiPolygon* createMultiPolygon(std::vector<Geometry*>* newPolys) const;
221 
224  const std::vector<const Geometry*>& fromPolys) const;
225 
226  std::unique_ptr<MultiPolygon> createMultiPolygon(
227  std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
228 
229  std::unique_ptr<MultiPolygon> createMultiPolygon(
230  std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
231 
233  std::unique_ptr<LinearRing> createLinearRing() const;
234 
237 
238  std::unique_ptr<LinearRing> createLinearRing(
239  std::unique_ptr<CoordinateSequence> && newCoords) const;
240 
243  const CoordinateSequence& coordinates) const;
244 
246  std::unique_ptr<MultiPoint> createMultiPoint() const;
247 
249  MultiPoint* createMultiPoint(std::vector<Geometry*>* newPoints) const;
250 
251  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
252 
253  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
254 
257  const std::vector<const Geometry*>& fromPoints) const;
258 
263  const CoordinateSequence& fromCoords) const;
264 
269  const std::vector<Coordinate>& fromCoords) const;
270 
272  std::unique_ptr<Polygon> createPolygon(std::size_t coordinateDimension = 2) const;
273 
276  std::vector<LinearRing*>* holes) const;
277 
278  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
279 
280  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
281  std::vector<std::unique_ptr<LinearRing>> && holes) const;
282 
284  std::unique_ptr<Polygon> createPolygon(std::vector<Coordinate> && coords) const;
285 
288  const std::vector<LinearRing*>& holes) const;
289 
291  std::unique_ptr<LineString> createLineString(std::size_t coordinateDimension = 2) const;
292 
294  std::unique_ptr<LineString> createLineString(const LineString& ls) const;
295 
298 
299  std::unique_ptr<LineString> createLineString(
300  std::unique_ptr<CoordinateSequence> && coordinates) const;
301 
304  const CoordinateSequence& coordinates) const;
305 
313  std::unique_ptr<Geometry> createEmpty(int dimension) const;
314 
345  Geometry* buildGeometry(std::vector<Geometry*>* geoms) const;
346 
347  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
348 
349  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
350 
351  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
352 
353  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
354 
356  //
363  template <class T>
364  std::unique_ptr<Geometry>
365  buildGeometry(T from, T toofar) const
366  {
367  bool isHeterogeneous = false;
368  std::size_t count = 0;
369  int geomClass = -1;
370  for(T i = from; i != toofar; ++i) {
371  ++count;
372  const Geometry* g = *i;
373  if(geomClass < 0) {
374  geomClass = g->getSortIndex();
375  }
376  else if(geomClass != g->getSortIndex()) {
377  isHeterogeneous = true;
378  }
379  }
380 
381  // for the empty geometry, return an empty GeometryCollection
382  if(count == 0) {
383  return std::unique_ptr<Geometry>(createGeometryCollection());
384  }
385 
386  // for the single geometry, return a clone
387  if(count == 1) {
388  return (*from)->clone();
389  }
390 
391  // Now we know it is a collection
392 
393  // FIXME:
394  // Until we tweak all the createMulti* interfaces
395  // to support taking iterators we'll have to build
396  // a custom vector here.
397  std::vector<std::unique_ptr<Geometry>> fromGeoms;
398  for(T i = from; i != toofar; ++i) {
399  fromGeoms.push_back((*i)->clone());
400  }
401 
402  // for an heterogeneous ...
403  if(isHeterogeneous) {
404  return createGeometryCollection(std::move(fromGeoms));
405  }
406 
407  // At this point we know the collection is not hetereogenous.
408  switch((*from)->getDimension()) {
409  case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
410  case Dimension::L: return createMultiLineString(std::move(fromGeoms));
411  case Dimension::P: return createMultiPoint(std::move(fromGeoms));
412  default:
413  throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
414  }
415  }
416 
424  Geometry* buildGeometry(const std::vector<const Geometry*>& geoms) const;
425 
426  int getSRID() const;
427 
432 
434  Geometry* createGeometry(const Geometry* g) const;
435 
437  void destroyGeometry(Geometry* g) const;
438 
445  void destroy();
446 
447 protected:
448 
455 
468  GeometryFactory(const PrecisionModel* pm, int newSRID,
469  CoordinateSequenceFactory* nCoordinateSequenceFactory);
470 
477  GeometryFactory(CoordinateSequenceFactory* nCoordinateSequenceFactory);
478 
488 
498  GeometryFactory(const PrecisionModel* pm, int newSRID);
499 
506 
508  virtual ~GeometryFactory();
509 
510 private:
511 
512  PrecisionModel precisionModel;
513  int SRID;
514  const CoordinateSequenceFactory* coordinateListFactory;
515 
516  mutable int _refCount;
517  bool _autoDestroy;
518 
519  friend class Geometry;
520 
521  void addRef() const;
522  void dropRef() const;
523 
524 };
525 
526 } // namespace geos::geom
527 } // namespace geos
528 
529 #ifdef GEOS_INLINE
530 # include "geos/geom/GeometryFactory.inl"
531 #endif
532 
533 #endif // ndef GEOS_GEOM_GEOMETRYFACTORY_H
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
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
@ A
Dimension value of a surface (2).
Definition: Dimension.h:48
@ L
Dimension value of a curve (1).
Definition: Dimension.h:45
@ P
Dimension value of a point (0).
Definition: Dimension.h:42
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:58
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:55
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:68
LineString * createLineString(const CoordinateSequence &coordinates) const
Construct a LineString with a deep-copy of given argument.
MultiPoint * createMultiPoint(const std::vector< const Geometry * > &fromPoints) const
Construct a MultiPoint with a deep-copy of given arguments.
static GeometryFactory::Ptr create(const PrecisionModel *pm)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and the defaul...
std::unique_ptr< MultiPolygon > createMultiPolygon() const
Construct an EMPTY MultiPolygon.
const CoordinateSequenceFactory * getCoordinateSequenceFactory() const
Returns the CoordinateSequenceFactory associated with this GeometryFactory.
GeometryCollection * createGeometryCollection(std::vector< Geometry * > *newGeoms) const
Construct a GeometryCollection taking ownership of given arguments.
static GeometryFactory::Ptr create()
Constructs a GeometryFactory that generates Geometries having a floating PrecisionModel and a spatial...
std::unique_ptr< LinearRing > createLinearRing() const
Construct an EMPTY LinearRing.
GeometryFactory(const PrecisionModel *pm)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and the defaul...
MultiPoint * createMultiPoint(const CoordinateSequence &fromCoords) const
Construct a MultiPoint containing a Point geometry for each Coordinate in the given list.
Point * createPoint(CoordinateSequence *coordinates) const
Creates a Point taking ownership of the given CoordinateSequence.
std::unique_ptr< LineString > createLineString(std::size_t coordinateDimension=2) const
Construct an EMPTY LineString.
Polygon * createPolygon(const LinearRing &shell, const std::vector< LinearRing * > &holes) const
Construct a Polygon with a deep-copy of given arguments.
LinearRing * createLinearRing(CoordinateSequence *newCoords) const
Construct a LinearRing taking ownership of given arguments.
MultiPolygon * createMultiPolygon(std::vector< Geometry * > *newPolys) const
Construct a MultiPolygon taking ownership of given arguments.
Geometry * createGeometry(const Geometry *g) const
Returns a clone of given Geometry.
std::unique_ptr< Geometry > createEmptyGeometry() const
Construct the EMPTY Geometry.
MultiLineString * createMultiLineString(std::vector< Geometry * > *newLines) const
Construct a MultiLineString taking ownership of given arguments.
GeometryCollection * createGeometryCollection(const std::vector< const Geometry * > &newGeoms) const
Constructs a GeometryCollection with a deep-copy of args.
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...
MultiPolygon * createMultiPolygon(const std::vector< const Geometry * > &fromPolys) const
Construct a MultiPolygon with a deep-copy of given arguments.
std::unique_ptr< MultiPoint > createMultiPoint() const
Constructs an EMPTY MultiPoint.
GeometryFactory(CoordinateSequenceFactory *nCoordinateSequenceFactory)
Constructs a GeometryFactory that generates Geometries having the given CoordinateSequence implementa...
std::unique_ptr< MultiLineString > createMultiLineString() const
Construct an EMPTY MultiLineString.
void destroyGeometry(Geometry *g) const
Destroy a Geometry, or release it.
static GeometryFactory::Ptr create(CoordinateSequenceFactory *nCoordinateSequenceFactory)
Constructs a GeometryFactory that generates Geometries having the given CoordinateSequence implementa...
std::unique_ptr< Polygon > createPolygon(std::size_t coordinateDimension=2) const
Construct an EMPTY Polygon.
static const GeometryFactory * getDefaultInstance()
Return a pointer to the default GeometryFactory. This is a global shared object instantiated using de...
const PrecisionModel * getPrecisionModel() const
Returns the PrecisionModel that Geometries created by this factory will be associated with.
std::unique_ptr< LineString > createLineString(const LineString &ls) const
Copy a LineString.
std::unique_ptr< Geometry > createEmpty(int dimension) const
GeometryFactory(const PrecisionModel *pm, int newSRID, CoordinateSequenceFactory *nCoordinateSequenceFactory)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel,...
std::unique_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector<Geometry *>&) for semantics.
Definition: GeometryFactory.h:365
LinearRing * createLinearRing(const CoordinateSequence &coordinates) const
Construct a LinearRing with a deep-copy of given arguments.
Polygon * createPolygon(LinearRing *shell, std::vector< LinearRing * > *holes) const
Construct a Polygon taking ownership of given arguments.
MultiPoint * createMultiPoint(const std::vector< Coordinate > &fromCoords) const
Construct a MultiPoint containing a Point geometry for each Coordinate in the given vector.
std::unique_ptr< Point > createPoint(std::size_t coordinateDimension=2) const
Creates an EMPTY Point.
std::unique_ptr< Geometry > toGeometry(const Envelope *envelope) const
GeometryFactory(const PrecisionModel *pm, int newSRID)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and spatial-re...
virtual ~GeometryFactory()
Destructor.
std::unique_ptr< GeometryCollection > createGeometryCollection() const
Construct an EMPTY GeometryCollection.
std::unique_ptr< Polygon > createPolygon(std::vector< Coordinate > &&coords) const
Construct a Polygon from a Coordinate vector, taking ownership of the vector.
static GeometryFactory::Ptr create(const GeometryFactory &gf)
Copy constructor.
Point * createPoint(const Coordinate &coordinate) const
Creates a Point using the given Coordinate.
MultiLineString * createMultiLineString(const std::vector< const Geometry * > &fromLines) const
Construct a MultiLineString with a deep-copy of given arguments.
GeometryFactory(const GeometryFactory &gf)
Copy constructor.
LineString * createLineString(CoordinateSequence *coordinates) const
Construct a LineString taking ownership of given argument.
GeometryFactory()
Constructs a GeometryFactory that generates Geometries having a floating PrecisionModel and a spatial...
Point * createPoint(const CoordinateSequence &coordinates) const
Creates a Point with a deep-copy of the given CoordinateSequence.
static GeometryFactory::Ptr create(const PrecisionModel *pm, int newSRID)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and spatial-re...
Geometry * buildGeometry(std::vector< Geometry * > *geoms) const
static GeometryFactory::Ptr create(const PrecisionModel *pm, int newSRID, CoordinateSequenceFactory *nCoordinateSequenceFactory)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel,...
MultiPoint * createMultiPoint(std::vector< Geometry * > *newPoints) const
Construct a MultiPoint taking ownership of given arguments.
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
Definition: LineString.h:68
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple.
Definition: LinearRing.h:57
Models a collection of LineStrings.
Definition: MultiLineString.h:51
Definition: MultiPoint.h:54
Definition: MultiPolygon.h:59
Definition: Point.h:66
Represents a linear polygon, which may include holes.
Definition: Polygon.h:64
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:87
Indicates one or more illegal arguments.
Definition: IllegalArgumentException.h:34
Basic namespace for all GEOS functionalities.
Definition: Angle.h:26