GEOS 3.15.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#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/geom/Polygon.h>
30#include <geos/util/IllegalArgumentException.h>
31#include <geos/export.h>
32
33#include <vector>
34#include <memory>
35#include <cassert>
36#include <atomic>
37
38namespace geos {
39namespace geom {
40class Coordinate;
41class CircularString;
42class CompoundCurve;
43class CoordinateSequence;
44class Envelope;
45class Geometry;
46class GeometryCollection;
47class LineString;
48class LinearRing;
49class MultiLineString;
50class MultiCurve;
51class MultiPoint;
52class MultiPolygon;
53class MultiSurface;
54class CurvePolygon;
55}
56}
57
58namespace geos {
59namespace geom { // geos::geom
60
71class GEOS_DLL GeometryFactory {
72private:
73
74 struct GeometryFactoryDeleter {
75 void
76 operator()(GeometryFactory* p) const
77 {
78 p->destroy();
79 }
80 };
81
82public:
83
84 using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
85
91 static GeometryFactory::Ptr create();
92
101 static GeometryFactory::Ptr create(const PrecisionModel* pm);
102
112 static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
113
119 static GeometryFactory::Ptr create(const GeometryFactory& gf);
120
127 static const GeometryFactory*
129
130//Skipped a lot of list to array converters
131
132 static std::unique_ptr<Point> createPointFromInternalCoord(const Coordinate* coord,
133 const Geometry* exemplar);
134
139 std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
140
145 {
146 return &precisionModel;
147 };
148
150 std::unique_ptr<Point> createPoint(std::size_t coordinateDimension = 2) const;
151 std::unique_ptr<Point> createPoint(bool hasZ, bool hasM) const;
152
154 std::unique_ptr<Point> createPoint(const Coordinate& coordinate) const;
155 std::unique_ptr<Point> createPoint(const CoordinateXY& coordinate) const;
156 std::unique_ptr<Point> createPoint(const CoordinateXYM& coordinate) const;
157 std::unique_ptr<Point> createPoint(const CoordinateXYZM& coordinate) const;
158
160 std::unique_ptr<Point> createPoint(std::unique_ptr<CoordinateSequence>&& coordinates) const;
161
163 std::unique_ptr<Point> createPoint(const CoordinateSequence& coordinates) const;
164
166 std::unique_ptr<GeometryCollection> createGeometryCollection() const;
167
169 std::unique_ptr<Geometry> createEmptyGeometry(GeometryTypeId type = GEOS_GEOMETRYCOLLECTION, bool hasZ=false, bool hasM=false) const;
170
172 template<typename T>
173 std::unique_ptr<GeometryCollection> createGeometryCollection(
174 std::vector<std::unique_ptr<T>> && newGeoms) const {
175 // Can't use make_unique because constructor is protected
176 return std::unique_ptr<GeometryCollection>(new GeometryCollection(Geometry::toGeometryArray(std::move(newGeoms)), *this));
177 }
178
180 std::unique_ptr<GeometryCollection> createGeometryCollection(
181 const std::vector<const Geometry*>& newGeoms) const;
182
184 std::unique_ptr<MultiLineString> createMultiLineString() const;
185
187 std::unique_ptr<MultiLineString> createMultiLineString(
188 const std::vector<const Geometry*>& fromLines) const;
189
191 std::unique_ptr<MultiLineString> createMultiLineString(
192 std::vector<std::unique_ptr<LineString>> && fromLines) const;
193
194 std::unique_ptr<MultiLineString> createMultiLineString(
195 std::vector<std::unique_ptr<Geometry>> && fromLines) const;
196
198 std::unique_ptr<MultiCurve> createMultiCurve() const;
199
201 std::unique_ptr<MultiCurve> createMultiCurve(
202 std::vector<std::unique_ptr<Geometry>> && fromCurves) const;
203
204 std::unique_ptr<MultiCurve> createMultiCurve(
205 std::vector<std::unique_ptr<Curve>> && fromCurves) const;
206
208 std::unique_ptr<MultiPolygon> createMultiPolygon() const;
209
211 std::unique_ptr<MultiPolygon> createMultiPolygon(
212 const std::vector<const Geometry*>& fromPolys) const;
213
215 std::unique_ptr<MultiPolygon> createMultiPolygon(
216 std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
217
218 std::unique_ptr<MultiPolygon> createMultiPolygon(
219 std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
220
222 std::unique_ptr<MultiSurface> createMultiSurface() const;
223
225 std::unique_ptr<MultiSurface> createMultiSurface(
226 std::vector<std::unique_ptr<Geometry>> && from) const;
227
228 std::unique_ptr<MultiSurface> createMultiSurface(
229 std::vector<std::unique_ptr<Surface>> && from) const;
230
232 std::unique_ptr<LinearRing> createLinearRing(std::size_t coordinateDimension = 2) const;
233 std::unique_ptr<LinearRing> createLinearRing(bool hasZ, bool hasM) const;
234
236 std::unique_ptr<LinearRing> createLinearRing(
237 std::unique_ptr<CoordinateSequence> && newCoords) const;
238
240 std::unique_ptr<LinearRing> createLinearRing(
241 const CoordinateSequence& coordinates) const;
242
244 std::unique_ptr<MultiPoint> createMultiPoint() const;
245
246 template<typename T>
247 std::unique_ptr<MultiPoint> createMultiPoint(const T& fromCoords) const
248 {
249 std::vector<std::unique_ptr<Geometry>> pts;
250 pts.reserve(fromCoords.size());
251 for (const auto& c : fromCoords) {
252 pts.emplace_back(createPoint(c));
253 }
254
255 return createMultiPoint(std::move(pts));
256 }
257
259 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
260
261 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
262
264 std::unique_ptr<MultiPoint> createMultiPoint(
265 const std::vector<const Geometry*>& fromPoints) const;
266
270 std::unique_ptr<MultiPoint> createMultiPoint(
271 const CoordinateSequence& fromCoords) const;
272
274 std::unique_ptr<Polygon> createPolygon(std::size_t coordinateDimension = 2) const;
275 std::unique_ptr<Polygon> createPolygon(bool hasZ, bool hasM) const;
276
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(CoordinateSequence && coords) const;
285
288 const std::vector<LinearRing*>& holes) const;
289
290
292 std::unique_ptr<CurvePolygon> createCurvePolygon(bool hasZ, bool hasM) const;
293
295 std::unique_ptr<CurvePolygon> createCurvePolygon(std::unique_ptr<Curve>&& shell) const;
296
297 std::unique_ptr<CurvePolygon> createCurvePolygon(std::unique_ptr<Curve>&& shell,
298 std::vector<std::unique_ptr<Curve>> && holes) const;
299
301 std::unique_ptr<LineString> createLineString(std::size_t coordinateDimension = 2) const;
302 std::unique_ptr<LineString> createLineString(bool hasZ, bool hasM) const;
303
305 std::unique_ptr<LineString> createLineString(const LineString& ls) const;
306
308 std::unique_ptr<LineString> createLineString(
309 std::unique_ptr<CoordinateSequence> && coordinates) const;
310
312 std::unique_ptr<LineString> createLineString(
313 const CoordinateSequence& coordinates) const;
314
316 std::unique_ptr<CircularString> createCircularString(bool hasZ, bool hasM) const;
317
319 std::unique_ptr<CircularString> createCircularString(const CircularString& ls) const;
320
322 std::unique_ptr<CircularString> createCircularString(
323 std::unique_ptr<CoordinateSequence> && coordinates) const;
324
326 std::unique_ptr<CircularString> createCircularString(
327 const CoordinateSequence& coordinates) const;
328
330 std::unique_ptr<CompoundCurve> createCompoundCurve() const;
331
333 std::unique_ptr<CompoundCurve> createCompoundCurve(std::vector<std::unique_ptr<SimpleCurve>>&&) const;
334
342 std::unique_ptr<Geometry> createEmpty(int dimension) const;
343
349 std::unique_ptr<Geometry> createEmpty(GeometryTypeId typeId) const;
350
351 std::unique_ptr<Geometry> createMulti(std::unique_ptr<Geometry> && geom) const;
352
383 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
384
385 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
386
387 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
388
389 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
390
392 //
399 template <class T>
400 std::unique_ptr<Geometry>
401 buildGeometry(T from, T toofar) const
402 {
403 bool isHeterogeneous = false;
404 std::size_t count = 0;
405 int geomClass = -1;
406 for(T i = from; i != toofar; ++i) {
407 ++count;
408 const Geometry* g = *i;
409 if(geomClass < 0) {
410 geomClass = g->getSortIndex();
411 }
412 else if(geomClass != g->getSortIndex()) {
413 isHeterogeneous = true;
414 }
415 }
416
417 // for the empty geometry, return an empty GeometryCollection
418 if(count == 0) {
419 return std::unique_ptr<Geometry>(createGeometryCollection());
420 }
421
422 // for the single geometry, return a clone
423 if(count == 1) {
424 return (*from)->clone();
425 }
426
427 // Now we know it is a collection
428
429 // FIXME:
430 // Until we tweak all the createMulti* interfaces
431 // to support taking iterators we'll have to build
432 // a custom vector here.
433 std::vector<std::unique_ptr<Geometry>> fromGeoms;
434 for(T i = from; i != toofar; ++i) {
435 fromGeoms.push_back((*i)->clone());
436 }
437
438 // for an heterogeneous ...
439 if(isHeterogeneous) {
440 return createGeometryCollection(std::move(fromGeoms));
441 }
442
443 // At this point we know the collection is not hetereogenous.
444 switch((*from)->getDimension()) {
445 case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
446 case Dimension::L: return createMultiLineString(std::move(fromGeoms));
447 case Dimension::P: return createMultiPoint(std::move(fromGeoms));
448 default:
449 throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
450 }
451 }
452
460 std::unique_ptr<Geometry> buildGeometry(const std::vector<const Geometry*>& geoms) const;
461
462 int getSRID() const
463 {
464 return SRID;
465 };
466
468 std::unique_ptr<Geometry> createGeometry(const Geometry* g) const;
469
471 void destroyGeometry(Geometry* g) const;
472
479 void destroy();
480
481protected:
482
489
499
509 GeometryFactory(const PrecisionModel* pm, int newSRID);
510
517
520
521private:
522
523 PrecisionModel precisionModel;
524 int SRID;
525
526 mutable std::atomic<int> _refCount;
527 bool _autoDestroy;
528
529 friend class Geometry;
530
531 void addRef() const;
532 void dropRef() const;
533
534};
535
536} // namespace geos::geom
537} // namespace geos
538
539
540
541
542
543
The internal representation of a list of coordinates inside a Geometry.
Definition CoordinateSequence.h:56
Coordinate is the lightweight class used to store coordinates.
Definition Coordinate.h:217
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition Envelope.h:59
Represents a collection of heterogeneous Geometry objects.
Definition GeometryCollection.h:51
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:71
std::unique_ptr< MultiPolygon > createMultiPolygon(std::vector< std::unique_ptr< Polygon > > &&fromPolys) const
Construct a MultiPolygon taking ownership of given arguments.
static GeometryFactory::Ptr create(const PrecisionModel *pm)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and the defaul...
static const GeometryFactory * getDefaultInstance()
Return a pointer to the default GeometryFactory. This is a global shared object instantiated using de...
std::unique_ptr< Geometry > createGeometry(const Geometry *g) const
Returns a clone of given Geometry.
std::unique_ptr< Point > createPoint(const CoordinateSequence &coordinates) const
Creates a Point with a deep-copy of the given CoordinateSequence.
static GeometryFactory::Ptr create()
Constructs a GeometryFactory that generates Geometries having a floating PrecisionModel and a spatial...
GeometryFactory(const PrecisionModel *pm)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and the defaul...
std::unique_ptr< MultiPoint > createMultiPoint() const
Constructs an EMPTY MultiPoint.
std::unique_ptr< Point > createPoint(std::size_t coordinateDimension=2) const
Creates an EMPTY Point.
std::unique_ptr< MultiSurface > createMultiSurface(std::vector< std::unique_ptr< Geometry > > &&from) const
Construct a MultiSurface taking ownership of given arguments.
std::unique_ptr< CircularString > createCircularString(std::unique_ptr< CoordinateSequence > &&coordinates) const
Construct a CircularString taking ownership of given argument.
std::unique_ptr< MultiPolygon > createMultiPolygon() const
Construct an EMPTY MultiPolygon.
std::unique_ptr< LineString > createLineString(const CoordinateSequence &coordinates) const
Construct a LineString with a deep-copy of given argument.
std::unique_ptr< MultiSurface > createMultiSurface() const
Construct an EMPTY MultiSurface.
const PrecisionModel * getPrecisionModel() const
Returns the PrecisionModel that Geometries created by this factory will be associated with.
Definition GeometryFactory.h:144
std::unique_ptr< GeometryCollection > createGeometryCollection() const
Construct an EMPTY GeometryCollection.
std::unique_ptr< GeometryCollection > createGeometryCollection(const std::vector< const Geometry * > &newGeoms) const
Constructs a GeometryCollection with a deep-copy of args.
std::unique_ptr< LineString > createLineString(const LineString &ls) const
Copy a LineString.
std::unique_ptr< CircularString > createCircularString(const CoordinateSequence &coordinates) const
Construct a CircularString with a deep-copy of given argument.
std::unique_ptr< LineString > createLineString(std::size_t coordinateDimension=2) const
Construct an EMPTY LineString.
std::unique_ptr< LineString > createLineString(std::unique_ptr< CoordinateSequence > &&coordinates) const
Construct a LineString taking ownership of given argument.
std::unique_ptr< Geometry > createEmptyGeometry(GeometryTypeId type=GEOS_GEOMETRYCOLLECTION, bool hasZ=false, bool hasM=false) const
Construct the EMPTY Geometry.
std::unique_ptr< MultiLineString > createMultiLineString(const std::vector< const Geometry * > &fromLines) const
Construct a MultiLineString with a deep-copy of given arguments.
void destroyGeometry(Geometry *g) const
Destroy a Geometry, or release it.
std::unique_ptr< MultiPoint > createMultiPoint(const CoordinateSequence &fromCoords) const
Construct a MultiPoint containing a Point geometry for each Coordinate in the given list.
std::unique_ptr< CompoundCurve > createCompoundCurve(std::vector< std::unique_ptr< SimpleCurve > > &&) const
Construct a CompoundCurve taking ownership of given argument.
std::unique_ptr< MultiLineString > createMultiLineString(std::vector< std::unique_ptr< LineString > > &&fromLines) const
Construct a MultiLineString taking ownership of given arguments.
std::unique_ptr< MultiCurve > createMultiCurve() const
Construct an EMPTY MultiCurve.
std::unique_ptr< GeometryCollection > createGeometryCollection(std::vector< std::unique_ptr< T > > &&newGeoms) const
Construct a GeometryCollection taking ownership of given arguments.
Definition GeometryFactory.h:173
std::unique_ptr< MultiCurve > createMultiCurve(std::vector< std::unique_ptr< Geometry > > &&fromCurves) const
Construct a MultiCurve taking ownership of given arguments.
std::unique_ptr< LinearRing > createLinearRing(std::size_t coordinateDimension=2) const
Construct an EMPTY LinearRing.
Polygon * createPolygon(const LinearRing &shell, const std::vector< LinearRing * > &holes) const
Construct a Polygon with a deep-copy of given arguments.
std::unique_ptr< Geometry > createEmpty(int dimension) const
std::unique_ptr< MultiLineString > createMultiLineString() const
Construct an EMPTY MultiLineString.
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< CurvePolygon > createCurvePolygon(std::unique_ptr< Curve > &&shell) const
Construct a CurvePolygon taking ownership of given arguments.
std::unique_ptr< Point > createPoint(std::unique_ptr< CoordinateSequence > &&coordinates) const
Creates a Point taking ownership of the given CoordinateSequence.
std::unique_ptr< MultiPoint > createMultiPoint(const std::vector< const Geometry * > &fromPoints) const
Construct a MultiPoint with a deep-copy of given arguments.
std::unique_ptr< Point > createPoint(const Coordinate &coordinate) const
Creates a Point using the given Coordinate.
std::unique_ptr< Polygon > createPolygon(std::unique_ptr< LinearRing > &&shell) const
Construct a Polygon taking ownership of given arguments.
std::unique_ptr< Geometry > buildGeometry(std::vector< std::unique_ptr< Geometry > > &&geoms) const
static GeometryFactory::Ptr create(const GeometryFactory &gf)
Copy constructor.
std::unique_ptr< Polygon > createPolygon(CoordinateSequence &&coords) const
Construct a Polygon from a Coordinate vector, taking ownership of the vector.
std::unique_ptr< CompoundCurve > createCompoundCurve() const
Construct an EMPTY CompoundCurve.
std::unique_ptr< CircularString > createCircularString(const CircularString &ls) const
Copy a CircularString.
std::unique_ptr< 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...
std::unique_ptr< CurvePolygon > createCurvePolygon(bool hasZ, bool hasM) const
Construct an EMPTY CurvePolygon.
std::unique_ptr< Polygon > createPolygon(std::size_t coordinateDimension=2) const
Construct an EMPTY Polygon.
std::unique_ptr< CircularString > createCircularString(bool hasZ, bool hasM) const
Construct an EMPTY CircularString.
std::unique_ptr< LinearRing > createLinearRing(std::unique_ptr< CoordinateSequence > &&newCoords) const
Construct a LinearRing taking ownership of given arguments.
GeometryFactory(const GeometryFactory &gf)
Copy constructor.
GeometryFactory()
Constructs a GeometryFactory that generates Geometries having a floating PrecisionModel and a spatial...
static GeometryFactory::Ptr create(const PrecisionModel *pm, int newSRID)
Constructs a GeometryFactory that generates Geometries having the given PrecisionModel and spatial-re...
std::unique_ptr< Geometry > createEmpty(GeometryTypeId typeId) const
std::unique_ptr< MultiPoint > createMultiPoint(std::vector< std::unique_ptr< Point > > &&newPoints) const
Construct a MultiPoint taking ownership of given arguments.
std::unique_ptr< LinearRing > createLinearRing(const CoordinateSequence &coordinates) const
Construct a LinearRing with a deep-copy of given arguments.
std::unique_ptr< MultiPolygon > createMultiPolygon(const std::vector< const Geometry * > &fromPolys) const
Construct a MultiPolygon with a deep-copy of given arguments.
std::unique_ptr< Geometry > toGeometry(const Envelope *envelope) const
std::unique_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector<Geometry *>&) for semantics.
Definition GeometryFactory.h:401
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:196
Definition LineString.h:66
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple.
Definition LinearRing.h:54
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:87
Indicates one or more illegal arguments.
Definition IllegalArgumentException.h:33
@ GEOS_GEOMETRYCOLLECTION
Definition geos_c.h:227
GeometryTypeId
Geometry types.
Definition Geometry.h:73
Basic namespace for all GEOS functionalities.
Definition geos.h:38