GEOS  3.11.0
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 #pragma once
21 
22 #include <geos/geom/Geometry.h>
23 #include <geos/geom/GeometryCollection.h>
24 #include <geos/geom/GeometryFactory.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/util/IllegalArgumentException.h>
30 #include <geos/export.h>
31 
32 #include <vector>
33 #include <memory>
34 #include <cassert>
35 
36 namespace geos {
37 namespace geom {
38 class CoordinateSequenceFactory;
39 class Coordinate;
40 class CoordinateSequence;
41 class Envelope;
42 class Geometry;
43 class GeometryCollection;
44 class LineString;
45 class LinearRing;
46 class MultiLineString;
47 class MultiPoint;
48 class MultiPolygon;
49 class Polygon;
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*
147 
148 //Skipped a lot of list to array convertors
149 
150  Point* createPointFromInternalCoord(const Coordinate* coord,
151  const Geometry* exemplar) const;
152 
157  std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
158 
163  {
164  return &precisionModel;
165  };
166 
168  std::unique_ptr<Point> createPoint(std::size_t coordinateDimension = 2) const;
169 
171  Point* createPoint(const Coordinate& coordinate) const;
172 
174  Point* createPoint(CoordinateSequence* coordinates) const;
175 
177  Point* createPoint(const CoordinateSequence& coordinates) const;
178 
180  std::unique_ptr<GeometryCollection> createGeometryCollection() const;
181 
183  std::unique_ptr<Geometry> createEmptyGeometry() const;
184 
187  std::vector<Geometry*>* newGeoms) const;
188 
189  template<typename T>
190  std::unique_ptr<GeometryCollection> createGeometryCollection(
191  std::vector<std::unique_ptr<T>> && newGeoms) const {
192  // Can't use make_unique because constructor is protected
193  return std::unique_ptr<GeometryCollection>(new GeometryCollection(Geometry::toGeometryArray(std::move(newGeoms)), *this));
194  }
195 
198  const std::vector<const Geometry*>& newGeoms) const;
199 
201  std::unique_ptr<MultiLineString> createMultiLineString() const;
202 
205  std::vector<Geometry*>* newLines) const;
206 
209  const std::vector<const Geometry*>& fromLines) const;
210 
211  std::unique_ptr<MultiLineString> createMultiLineString(
212  std::vector<std::unique_ptr<LineString>> && fromLines) const;
213 
214  std::unique_ptr<MultiLineString> createMultiLineString(
215  std::vector<std::unique_ptr<Geometry>> && fromLines) const;
216 
218  std::unique_ptr<MultiPolygon> createMultiPolygon() const;
219 
221  MultiPolygon* createMultiPolygon(std::vector<Geometry*>* newPolys) const;
222 
225  const std::vector<const Geometry*>& fromPolys) const;
226 
227  std::unique_ptr<MultiPolygon> createMultiPolygon(
228  std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
229 
230  std::unique_ptr<MultiPolygon> createMultiPolygon(
231  std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
232 
234  std::unique_ptr<LinearRing> createLinearRing() const;
235 
238 
239  std::unique_ptr<LinearRing> createLinearRing(
240  std::unique_ptr<CoordinateSequence> && newCoords) const;
241 
242  std::unique_ptr<LinearRing> createLinearRing(
243  std::vector<Coordinate> && coordinates) const;
244 
247  const CoordinateSequence& coordinates) const;
248 
250  std::unique_ptr<MultiPoint> createMultiPoint() const;
251 
253  MultiPoint* createMultiPoint(std::vector<Geometry*>* newPoints) const;
254 
255  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
256 
257  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
258 
261  const std::vector<const Geometry*>& fromPoints) const;
262 
267  const CoordinateSequence& fromCoords) const;
268 
273  const std::vector<Coordinate>& fromCoords) const;
274 
276  std::unique_ptr<Polygon> createPolygon(std::size_t coordinateDimension = 2) const;
277 
280  std::vector<LinearRing*>* holes) const;
281 
282  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
283 
284  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
285  std::vector<std::unique_ptr<LinearRing>> && holes) const;
286 
288  std::unique_ptr<Polygon> createPolygon(std::vector<Coordinate> && coords) const;
289 
292  const std::vector<LinearRing*>& holes) const;
293 
295  std::unique_ptr<LineString> createLineString(std::size_t coordinateDimension = 2) const;
296 
298  std::unique_ptr<LineString> createLineString(const LineString& ls) const;
299 
302 
303  std::unique_ptr<LineString> createLineString(
304  std::unique_ptr<CoordinateSequence> && coordinates) const;
305 
306  std::unique_ptr<LineString> createLineString(
307  std::vector<Coordinate> && coordinates) const;
308 
311  const CoordinateSequence& coordinates) const;
312 
320  std::unique_ptr<Geometry> createEmpty(int dimension) const;
321 
352  Geometry* buildGeometry(std::vector<Geometry*>* geoms) const;
353 
354  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
355 
356  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
357 
358  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
359 
360  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
361 
363  //
370  template <class T>
371  std::unique_ptr<Geometry>
372  buildGeometry(T from, T toofar) const
373  {
374  bool isHeterogeneous = false;
375  std::size_t count = 0;
376  int geomClass = -1;
377  for(T i = from; i != toofar; ++i) {
378  ++count;
379  const Geometry* g = *i;
380  if(geomClass < 0) {
381  geomClass = g->getSortIndex();
382  }
383  else if(geomClass != g->getSortIndex()) {
384  isHeterogeneous = true;
385  }
386  }
387 
388  // for the empty geometry, return an empty GeometryCollection
389  if(count == 0) {
390  return std::unique_ptr<Geometry>(createGeometryCollection());
391  }
392 
393  // for the single geometry, return a clone
394  if(count == 1) {
395  return (*from)->clone();
396  }
397 
398  // Now we know it is a collection
399 
400  // FIXME:
401  // Until we tweak all the createMulti* interfaces
402  // to support taking iterators we'll have to build
403  // a custom vector here.
404  std::vector<std::unique_ptr<Geometry>> fromGeoms;
405  for(T i = from; i != toofar; ++i) {
406  fromGeoms.push_back((*i)->clone());
407  }
408 
409  // for an heterogeneous ...
410  if(isHeterogeneous) {
411  return createGeometryCollection(std::move(fromGeoms));
412  }
413 
414  // At this point we know the collection is not hetereogenous.
415  switch((*from)->getDimension()) {
416  case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
417  case Dimension::L: return createMultiLineString(std::move(fromGeoms));
418  case Dimension::P: return createMultiPoint(std::move(fromGeoms));
419  default:
420  throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
421  }
422  }
423 
431  Geometry* buildGeometry(const std::vector<const Geometry*>& geoms) const;
432 
433  int getSRID() const
434  {
435  return SRID;
436  };
437 
442  {
443  return coordinateListFactory;
444  };
445 
447  Geometry* createGeometry(const Geometry* g) const;
448 
450  void destroyGeometry(Geometry* g) const;
451 
458  void destroy();
459 
460 protected:
461 
468 
481  GeometryFactory(const PrecisionModel* pm, int newSRID,
482  CoordinateSequenceFactory* nCoordinateSequenceFactory);
483 
490  GeometryFactory(CoordinateSequenceFactory* nCoordinateSequenceFactory);
491 
501 
511  GeometryFactory(const PrecisionModel* pm, int newSRID);
512 
519 
521  virtual ~GeometryFactory();
522 
523 private:
524 
525  PrecisionModel precisionModel;
526  int SRID;
527  const CoordinateSequenceFactory* coordinateListFactory;
528 
529  mutable int _refCount;
530  bool _autoDestroy;
531 
532  friend class Geometry;
533 
534  void addRef() const;
535  void dropRef() const;
536 
537 };
538 
539 } // namespace geos::geom
540 } // namespace geos
541 
542 
543 
544 
545 
546 
A factory to create concrete instances of CoordinateSequences.
Definition: CoordinateSequenceFactory.h:44
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:44
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
@ A
Dimension value of a surface (2).
Definition: Dimension.h:46
@ L
Dimension value of a curve (1).
Definition: Dimension.h:43
@ P
Dimension value of a point (0).
Definition: Dimension.h:40
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:52
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:66
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.
Definition: GeometryFactory.h:441
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.
Definition: GeometryFactory.h:162
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:372
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:186
Definition: LineString.h:66
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple.
Definition: LinearRing.h:55
Models a collection of LineStrings.
Definition: MultiLineString.h:50
Definition: MultiPoint.h:51
Definition: MultiPolygon.h:59
Definition: Point.h:63
Represents a linear polygon, which may include holes.
Definition: Polygon.h:61
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:90
Indicates one or more illegal arguments.
Definition: IllegalArgumentException.h:33
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25