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 Curve;
45class Envelope;
46class Geometry;
47class GeometryCollection;
48class LineString;
49class LinearRing;
50class MultiLineString;
51class MultiCurve;
52class MultiPoint;
53class MultiPolygon;
54class MultiSurface;
55class CurvePolygon;
56}
57}
58
59namespace geos {
60namespace geom { // geos::geom
61
72class GEOS_DLL GeometryFactory {
73private:
74
75 struct GeometryFactoryDeleter {
76 void
77 operator()(GeometryFactory* p) const
78 {
79 p->destroy();
80 }
81 };
82
83public:
84
85 using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
86
92 static GeometryFactory::Ptr create();
93
102 static GeometryFactory::Ptr create(const PrecisionModel* pm);
103
113 static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
114
120 static GeometryFactory::Ptr create(const GeometryFactory& gf);
121
128 static const GeometryFactory*
130
131//Skipped a lot of list to array converters
132
133 static std::unique_ptr<Point> createPointFromInternalCoord(const Coordinate* coord,
134 const Geometry* exemplar);
135
140 std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
141
146 {
147 return &precisionModel;
148 };
149
151 std::unique_ptr<Point> createPoint(std::size_t coordinateDimension = 2) const;
152 std::unique_ptr<Point> createPoint(bool hasZ, bool hasM) const;
153
155 std::unique_ptr<Point> createPoint(const Coordinate& coordinate) const;
156 std::unique_ptr<Point> createPoint(const CoordinateXY& coordinate) const;
157 std::unique_ptr<Point> createPoint(const CoordinateXYM& coordinate) const;
158 std::unique_ptr<Point> createPoint(const CoordinateXYZM& coordinate) const;
159
161 std::unique_ptr<Point> createPoint(std::unique_ptr<CoordinateSequence>&& coordinates) const;
162
164 std::unique_ptr<Point> createPoint(const CoordinateSequence& coordinates) const;
165
167 std::unique_ptr<GeometryCollection> createGeometryCollection() const;
168
170 std::unique_ptr<Geometry> createEmptyGeometry(GeometryTypeId type = GEOS_GEOMETRYCOLLECTION, bool hasZ=false, bool hasM=false) const;
171
173 template<typename T>
174 std::unique_ptr<GeometryCollection> createGeometryCollection(
175 std::vector<std::unique_ptr<T>> && newGeoms) const {
176 // Can't use make_unique because constructor is protected
177 return std::unique_ptr<GeometryCollection>(new GeometryCollection(Geometry::toGeometryArray(std::move(newGeoms)), *this));
178 }
179
181 std::unique_ptr<GeometryCollection> createGeometryCollection(
182 const std::vector<const Geometry*>& newGeoms) const;
183
185 std::unique_ptr<MultiLineString> createMultiLineString() const;
186
188 std::unique_ptr<MultiLineString> createMultiLineString(
189 const std::vector<const Geometry*>& fromLines) const;
190
192 std::unique_ptr<MultiLineString> createMultiLineString(
193 std::vector<std::unique_ptr<LineString>> && fromLines) const;
194
195 std::unique_ptr<MultiLineString> createMultiLineString(
196 std::vector<std::unique_ptr<Curve>> && fromLines) const;
197
198 std::unique_ptr<MultiLineString> createMultiLineString(
199 std::vector<std::unique_ptr<Geometry>> && fromLines) const;
200
202 std::unique_ptr<MultiCurve> createMultiCurve() const;
203
205 std::unique_ptr<MultiCurve> createMultiCurve(
206 const std::vector<const Geometry*>& from) const;
207
209 std::unique_ptr<MultiCurve> createMultiCurve(
210 std::vector<std::unique_ptr<Geometry>> && fromCurves) const;
211
212 std::unique_ptr<MultiCurve> createMultiCurve(
213 std::vector<std::unique_ptr<Curve>> && fromCurves) const;
214
216 std::unique_ptr<MultiPolygon> createMultiPolygon() const;
217
219 std::unique_ptr<MultiPolygon> createMultiPolygon(
220 const std::vector<const Geometry*>& fromPolys) const;
221
223 std::unique_ptr<MultiPolygon> createMultiPolygon(
224 std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
225
226 std::unique_ptr<MultiPolygon> createMultiPolygon(
227 std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
228
230 std::unique_ptr<MultiSurface> createMultiSurface() const;
231
233 std::unique_ptr<MultiSurface> createMultiSurface(
234 const std::vector<const Geometry*>& from) const;
235
237 std::unique_ptr<MultiSurface> createMultiSurface(
238 std::vector<std::unique_ptr<Geometry>> && from) const;
239
240 std::unique_ptr<MultiSurface> createMultiSurface(
241 std::vector<std::unique_ptr<Surface>> && from) const;
242
244 std::unique_ptr<LinearRing> createLinearRing(std::size_t coordinateDimension = 2) const;
245 std::unique_ptr<LinearRing> createLinearRing(bool hasZ, bool hasM) const;
246
248 std::unique_ptr<LinearRing> createLinearRing(
249 std::unique_ptr<CoordinateSequence> && newCoords) const;
250
251 std::unique_ptr<LinearRing> createLinearRing(
252 const std::shared_ptr<const CoordinateSequence>& newCoords) const;
253
255 std::unique_ptr<LinearRing> createLinearRing(
256 const CoordinateSequence& coordinates) const;
257
259 std::unique_ptr<MultiPoint> createMultiPoint() const;
260
261 template<typename T>
262 std::unique_ptr<MultiPoint> createMultiPoint(const T& fromCoords) const
263 {
264 std::vector<std::unique_ptr<Geometry>> pts;
265 pts.reserve(fromCoords.size());
266 for (const auto& c : fromCoords) {
267 pts.emplace_back(createPoint(c));
268 }
269
270 return createMultiPoint(std::move(pts));
271 }
272
274 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
275
276 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
277
279 std::unique_ptr<MultiPoint> createMultiPoint(
280 const std::vector<const Geometry*>& fromPoints) const;
281
285 std::unique_ptr<MultiPoint> createMultiPoint(
286 const CoordinateSequence& fromCoords) const;
287
289 std::unique_ptr<Polygon> createPolygon(std::size_t coordinateDimension = 2) const;
290 std::unique_ptr<Polygon> createPolygon(bool hasZ, bool hasM) const;
291
293 std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
294
295 std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
296 std::vector<std::unique_ptr<LinearRing>> && holes) const;
297
299 std::unique_ptr<Polygon> createPolygon(CoordinateSequence && coords) const;
300
303 const std::vector<LinearRing*>& holes) const;
304
305
307 std::unique_ptr<CurvePolygon> createCurvePolygon(bool hasZ, bool hasM) const;
308
310 std::unique_ptr<CurvePolygon> createCurvePolygon(std::unique_ptr<Curve>&& shell) const;
311
312 std::unique_ptr<CurvePolygon> createCurvePolygon(std::unique_ptr<Curve>&& shell,
313 std::vector<std::unique_ptr<Curve>> && holes) const;
314
315
319 std::unique_ptr<Surface> createSurface(std::unique_ptr<Curve>&& shell) const;
320
321 std::unique_ptr<Surface> createSurface(std::unique_ptr<Curve>&& shell,
322 std::vector<std::unique_ptr<Curve>> && holes) const;
323
325 std::unique_ptr<LineString> createLineString(std::size_t coordinateDimension = 2) const;
326 std::unique_ptr<LineString> createLineString(bool hasZ, bool hasM) const;
327
329 std::unique_ptr<LineString> createLineString(const LineString& ls) const;
330
332 std::unique_ptr<LineString> createLineString(
333 std::unique_ptr<CoordinateSequence> && coordinates) const;
334
336 std::unique_ptr<LineString> createLineString(
337 const CoordinateSequence& coordinates) const;
338
340 std::unique_ptr<LineString> createLineString(
341 const std::shared_ptr<const CoordinateSequence>&) const;
342
344 std::unique_ptr<CircularString> createCircularString(bool hasZ, bool hasM) const;
345
347 std::unique_ptr<CircularString> createCircularString(const CircularString& ls) const;
348
350 std::unique_ptr<CircularString> createCircularString(
351 std::unique_ptr<CoordinateSequence> && coordinates) const;
352
354 std::unique_ptr<CircularString> createCircularString(
355 const std::shared_ptr<const CoordinateSequence>& coordinates) const;
356
358 std::unique_ptr<CircularString> createCircularString(
359 const CoordinateSequence& coordinates) const;
360
362 std::unique_ptr<CompoundCurve> createCompoundCurve() const;
363
365 std::unique_ptr<CompoundCurve> createCompoundCurve(std::vector<std::unique_ptr<SimpleCurve>>&&) const;
366
374 std::unique_ptr<Geometry> createEmpty(int dimension) const;
375
381 std::unique_ptr<Geometry> createEmpty(GeometryTypeId typeId) const;
382
383 std::unique_ptr<Geometry> createMulti(std::unique_ptr<Geometry> && geom) const;
384
415 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
416
417 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
418
419 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
420
421 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
422
423 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Curve>> && geoms) const;
424
426 //
433 template <class T>
434 std::unique_ptr<Geometry>
435 buildGeometry(T from, T toofar) const
436 {
437 bool isHeterogeneous = false;
438 std::size_t count = 0;
439 int geomClass = -1;
440 for(T i = from; i != toofar; ++i) {
441 ++count;
442 const Geometry* g = *i;
443 if(geomClass < 0) {
444 geomClass = g->getSortIndex();
445 }
446 else if(geomClass != g->getSortIndex()) {
447 isHeterogeneous = true;
448 }
449 }
450
451 // for the empty geometry, return an empty GeometryCollection
452 if(count == 0) {
453 return std::unique_ptr<Geometry>(createGeometryCollection());
454 }
455
456 // for the single geometry, return a clone
457 if(count == 1) {
458 return (*from)->clone();
459 }
460
461 // Now we know it is a collection
462
463 // FIXME:
464 // Until we tweak all the createMulti* interfaces
465 // to support taking iterators we'll have to build
466 // a custom vector here.
467 std::vector<std::unique_ptr<Geometry>> fromGeoms;
468 for(T i = from; i != toofar; ++i) {
469 fromGeoms.push_back((*i)->clone());
470 }
471
472 // for an heterogeneous ...
473 if(isHeterogeneous) {
474 return createGeometryCollection(std::move(fromGeoms));
475 }
476
477 // At this point we know the collection is not hetereogenous.
478 switch((*from)->getDimension()) {
479 case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
480 case Dimension::L: return createMultiLineString(std::move(fromGeoms));
481 case Dimension::P: return createMultiPoint(std::move(fromGeoms));
482 default:
483 throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
484 }
485 }
486
494 std::unique_ptr<Geometry> buildGeometry(const std::vector<const Geometry*>& geoms) const;
495
496 int getSRID() const
497 {
498 return SRID;
499 };
500
502 std::unique_ptr<Geometry> createGeometry(const Geometry* g) const;
503
505 void destroyGeometry(Geometry* g) const;
506
513 void destroy();
514
515protected:
516
523
533
543 GeometryFactory(const PrecisionModel* pm, int newSRID);
544
551
554
555private:
556
557 PrecisionModel precisionModel;
558 int SRID;
559
560 mutable std::atomic<int> _refCount;
561 bool _autoDestroy;
562
563 friend class Geometry;
564
565 void addRef() const;
566 void dropRef() const;
567
568};
569
570} // namespace geos::geom
571} // namespace geos
572
573
574
575
576
577
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:220
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:72
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< MultiCurve > createMultiCurve(const std::vector< const Geometry * > &from) const
Construct a MultiCurve with a deep-copy of given arguments.
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:145
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< MultiSurface > createMultiSurface(const std::vector< const Geometry * > &from) const
Construct a MultiSurface with a deep-copy of given arguments.
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:174
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< Surface > createSurface(std::unique_ptr< Curve > &&shell) const
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:435
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