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
239 std::unique_ptr<LinearRing> createLinearRing(
240 const std::shared_ptr<const CoordinateSequence>& newCoords) const;
241
243 std::unique_ptr<LinearRing> createLinearRing(
244 const CoordinateSequence& coordinates) const;
245
247 std::unique_ptr<MultiPoint> createMultiPoint() const;
248
249 template<typename T>
250 std::unique_ptr<MultiPoint> createMultiPoint(const T& fromCoords) const
251 {
252 std::vector<std::unique_ptr<Geometry>> pts;
253 pts.reserve(fromCoords.size());
254 for (const auto& c : fromCoords) {
255 pts.emplace_back(createPoint(c));
256 }
257
258 return createMultiPoint(std::move(pts));
259 }
260
262 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
263
264 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
265
267 std::unique_ptr<MultiPoint> createMultiPoint(
268 const std::vector<const Geometry*>& fromPoints) const;
269
273 std::unique_ptr<MultiPoint> createMultiPoint(
274 const CoordinateSequence& fromCoords) const;
275
277 std::unique_ptr<Polygon> createPolygon(std::size_t coordinateDimension = 2) const;
278 std::unique_ptr<Polygon> createPolygon(bool hasZ, bool hasM) const;
279
281 std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
282
283 std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
284 std::vector<std::unique_ptr<LinearRing>> && holes) const;
285
287 std::unique_ptr<Polygon> createPolygon(CoordinateSequence && coords) const;
288
291 const std::vector<LinearRing*>& holes) const;
292
293
295 std::unique_ptr<CurvePolygon> createCurvePolygon(bool hasZ, bool hasM) const;
296
298 std::unique_ptr<CurvePolygon> createCurvePolygon(std::unique_ptr<Curve>&& shell) const;
299
300 std::unique_ptr<CurvePolygon> createCurvePolygon(std::unique_ptr<Curve>&& shell,
301 std::vector<std::unique_ptr<Curve>> && holes) const;
302
304 std::unique_ptr<LineString> createLineString(std::size_t coordinateDimension = 2) const;
305 std::unique_ptr<LineString> createLineString(bool hasZ, bool hasM) const;
306
308 std::unique_ptr<LineString> createLineString(const LineString& ls) const;
309
311 std::unique_ptr<LineString> createLineString(
312 std::unique_ptr<CoordinateSequence> && coordinates) const;
313
315 std::unique_ptr<LineString> createLineString(
316 const CoordinateSequence& coordinates) const;
317
319 std::unique_ptr<LineString> createLineString(
320 const std::shared_ptr<const CoordinateSequence>&) const;
321
323 std::unique_ptr<CircularString> createCircularString(bool hasZ, bool hasM) const;
324
326 std::unique_ptr<CircularString> createCircularString(const CircularString& ls) const;
327
329 std::unique_ptr<CircularString> createCircularString(
330 std::unique_ptr<CoordinateSequence> && coordinates) const;
331
333 std::unique_ptr<CircularString> createCircularString(
334 const std::shared_ptr<const CoordinateSequence>& coordinates) const;
335
337 std::unique_ptr<CircularString> createCircularString(
338 const CoordinateSequence& coordinates) const;
339
341 std::unique_ptr<CompoundCurve> createCompoundCurve() const;
342
344 std::unique_ptr<CompoundCurve> createCompoundCurve(std::vector<std::unique_ptr<SimpleCurve>>&&) const;
345
353 std::unique_ptr<Geometry> createEmpty(int dimension) const;
354
360 std::unique_ptr<Geometry> createEmpty(GeometryTypeId typeId) const;
361
362 std::unique_ptr<Geometry> createMulti(std::unique_ptr<Geometry> && geom) const;
363
394 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
395
396 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
397
398 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
399
400 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
401
403 //
410 template <class T>
411 std::unique_ptr<Geometry>
412 buildGeometry(T from, T toofar) const
413 {
414 bool isHeterogeneous = false;
415 std::size_t count = 0;
416 int geomClass = -1;
417 for(T i = from; i != toofar; ++i) {
418 ++count;
419 const Geometry* g = *i;
420 if(geomClass < 0) {
421 geomClass = g->getSortIndex();
422 }
423 else if(geomClass != g->getSortIndex()) {
424 isHeterogeneous = true;
425 }
426 }
427
428 // for the empty geometry, return an empty GeometryCollection
429 if(count == 0) {
430 return std::unique_ptr<Geometry>(createGeometryCollection());
431 }
432
433 // for the single geometry, return a clone
434 if(count == 1) {
435 return (*from)->clone();
436 }
437
438 // Now we know it is a collection
439
440 // FIXME:
441 // Until we tweak all the createMulti* interfaces
442 // to support taking iterators we'll have to build
443 // a custom vector here.
444 std::vector<std::unique_ptr<Geometry>> fromGeoms;
445 for(T i = from; i != toofar; ++i) {
446 fromGeoms.push_back((*i)->clone());
447 }
448
449 // for an heterogeneous ...
450 if(isHeterogeneous) {
451 return createGeometryCollection(std::move(fromGeoms));
452 }
453
454 // At this point we know the collection is not hetereogenous.
455 switch((*from)->getDimension()) {
456 case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
457 case Dimension::L: return createMultiLineString(std::move(fromGeoms));
458 case Dimension::P: return createMultiPoint(std::move(fromGeoms));
459 default:
460 throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
461 }
462 }
463
471 std::unique_ptr<Geometry> buildGeometry(const std::vector<const Geometry*>& geoms) const;
472
473 int getSRID() const
474 {
475 return SRID;
476 };
477
479 std::unique_ptr<Geometry> createGeometry(const Geometry* g) const;
480
482 void destroyGeometry(Geometry* g) const;
483
490 void destroy();
491
492protected:
493
500
510
520 GeometryFactory(const PrecisionModel* pm, int newSRID);
521
528
531
532private:
533
534 PrecisionModel precisionModel;
535 int SRID;
536
537 mutable std::atomic<int> _refCount;
538 bool _autoDestroy;
539
540 friend class Geometry;
541
542 void addRef() const;
543 void dropRef() const;
544
545};
546
547} // namespace geos::geom
548} // namespace geos
549
550
551
552
553
554
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< LineString > createLineString(const std::shared_ptr< const CoordinateSequence > &) const
Construct a LineString with a reference to shared coordinates.
std::unique_ptr< MultiLineString > createMultiLineString(std::vector< std::unique_ptr< LineString > > &&fromLines) const
Construct a MultiLineString taking ownership of given arguments.
std::unique_ptr< CircularString > createCircularString(const std::shared_ptr< const CoordinateSequence > &coordinates) const
Construct a CircularStrign with a reference to shared coordinates.
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:412
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