GEOS 3.14.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
37namespace geos {
38namespace geom {
39class Coordinate;
40class CircularString;
41class CompoundCurve;
42class CoordinateSequence;
43class Envelope;
44class Geometry;
45class GeometryCollection;
46class LineString;
47class LinearRing;
48class MultiLineString;
49class MultiCurve;
50class MultiPoint;
51class MultiPolygon;
52class MultiSurface;
53class CurvePolygon;
54}
55}
56
57namespace geos {
58namespace geom { // geos::geom
59
70class GEOS_DLL GeometryFactory {
71private:
72
73 struct GeometryFactoryDeleter {
74 void
75 operator()(GeometryFactory* p) const
76 {
77 p->destroy();
78 }
79 };
80
81public:
82
83 using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
84
90 static GeometryFactory::Ptr create();
91
100 static GeometryFactory::Ptr create(const PrecisionModel* pm);
101
111 static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
112
118 static GeometryFactory::Ptr create(const GeometryFactory& gf);
119
126 static const GeometryFactory*
128
129//Skipped a lot of list to array converters
130
131 static std::unique_ptr<Point> createPointFromInternalCoord(const Coordinate* coord,
132 const Geometry* exemplar);
133
138 std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
139
144 {
145 return &precisionModel;
146 };
147
149 std::unique_ptr<Point> createPoint(std::size_t coordinateDimension = 2) const;
150 std::unique_ptr<Point> createPoint(bool hasZ, bool hasM) const;
151
153 std::unique_ptr<Point> createPoint(const Coordinate& coordinate) const;
154 std::unique_ptr<Point> createPoint(const CoordinateXY& coordinate) const;
155 std::unique_ptr<Point> createPoint(const CoordinateXYM& coordinate) const;
156 std::unique_ptr<Point> createPoint(const CoordinateXYZM& coordinate) const;
157
159 std::unique_ptr<Point> createPoint(std::unique_ptr<CoordinateSequence>&& coordinates) const;
160
162 std::unique_ptr<Point> createPoint(const CoordinateSequence& coordinates) const;
163
165 std::unique_ptr<GeometryCollection> createGeometryCollection() const;
166
168 std::unique_ptr<Geometry> createEmptyGeometry(GeometryTypeId type = GEOS_GEOMETRYCOLLECTION, bool hasZ=false, bool hasM=false) const;
169
171 template<typename T>
172 std::unique_ptr<GeometryCollection> createGeometryCollection(
173 std::vector<std::unique_ptr<T>> && newGeoms) const {
174 // Can't use make_unique because constructor is protected
175 return std::unique_ptr<GeometryCollection>(new GeometryCollection(Geometry::toGeometryArray(std::move(newGeoms)), *this));
176 }
177
179 std::unique_ptr<GeometryCollection> createGeometryCollection(
180 const std::vector<const Geometry*>& newGeoms) const;
181
183 std::unique_ptr<MultiLineString> createMultiLineString() const;
184
186 std::unique_ptr<MultiLineString> createMultiLineString(
187 const std::vector<const Geometry*>& fromLines) const;
188
190 std::unique_ptr<MultiLineString> createMultiLineString(
191 std::vector<std::unique_ptr<LineString>> && fromLines) const;
192
193 std::unique_ptr<MultiLineString> createMultiLineString(
194 std::vector<std::unique_ptr<Geometry>> && fromLines) const;
195
197 std::unique_ptr<MultiCurve> createMultiCurve() const;
198
200 std::unique_ptr<MultiCurve> createMultiCurve(
201 std::vector<std::unique_ptr<Geometry>> && fromCurves) const;
202
203 std::unique_ptr<MultiCurve> createMultiCurve(
204 std::vector<std::unique_ptr<Curve>> && fromCurves) const;
205
207 std::unique_ptr<MultiPolygon> createMultiPolygon() const;
208
210 std::unique_ptr<MultiPolygon> createMultiPolygon(
211 const std::vector<const Geometry*>& fromPolys) const;
212
214 std::unique_ptr<MultiPolygon> createMultiPolygon(
215 std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
216
217 std::unique_ptr<MultiPolygon> createMultiPolygon(
218 std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
219
221 std::unique_ptr<MultiSurface> createMultiSurface() const;
222
224 std::unique_ptr<MultiSurface> createMultiSurface(
225 std::vector<std::unique_ptr<Geometry>> && from) const;
226
227 std::unique_ptr<MultiSurface> createMultiSurface(
228 std::vector<std::unique_ptr<Surface>> && from) const;
229
231 std::unique_ptr<LinearRing> createLinearRing(std::size_t coordinateDimension = 2) const;
232 std::unique_ptr<LinearRing> createLinearRing(bool hasZ, bool hasM) const;
233
235 std::unique_ptr<LinearRing> createLinearRing(
236 std::unique_ptr<CoordinateSequence> && newCoords) const;
237
239 std::unique_ptr<LinearRing> createLinearRing(
240 const CoordinateSequence& coordinates) const;
241
243 std::unique_ptr<MultiPoint> createMultiPoint() const;
244
245 template<typename T>
246 std::unique_ptr<MultiPoint> createMultiPoint(const T& fromCoords) const
247 {
248 std::vector<std::unique_ptr<Geometry>> pts;
249 pts.reserve(fromCoords.size());
250 for (const auto& c : fromCoords) {
251 pts.emplace_back(createPoint(c));
252 }
253
254 return createMultiPoint(std::move(pts));
255 }
256
258 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
259
260 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
261
263 std::unique_ptr<MultiPoint> createMultiPoint(
264 const std::vector<const Geometry*>& fromPoints) const;
265
269 std::unique_ptr<MultiPoint> createMultiPoint(
270 const CoordinateSequence& fromCoords) const;
271
273 std::unique_ptr<Polygon> createPolygon(std::size_t coordinateDimension = 2) const;
274 std::unique_ptr<Polygon> createPolygon(bool hasZ, bool hasM) const;
275
277 std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
278
279 std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
280 std::vector<std::unique_ptr<LinearRing>> && holes) const;
281
283 std::unique_ptr<Polygon> createPolygon(CoordinateSequence && coords) const;
284
287 const std::vector<LinearRing*>& holes) const;
288
289
291 std::unique_ptr<CurvePolygon> createCurvePolygon(bool hasZ, bool hasM) const;
292
294 std::unique_ptr<CurvePolygon> createCurvePolygon(std::unique_ptr<Curve>&& shell) const;
295
296 std::unique_ptr<CurvePolygon> createCurvePolygon(std::unique_ptr<Curve>&& shell,
297 std::vector<std::unique_ptr<Curve>> && holes) const;
298
300 std::unique_ptr<LineString> createLineString(std::size_t coordinateDimension = 2) const;
301 std::unique_ptr<LineString> createLineString(bool hasZ, bool hasM) const;
302
304 std::unique_ptr<LineString> createLineString(const LineString& ls) const;
305
307 std::unique_ptr<LineString> createLineString(
308 std::unique_ptr<CoordinateSequence> && coordinates) const;
309
311 std::unique_ptr<LineString> createLineString(
312 const CoordinateSequence& coordinates) const;
313
315 std::unique_ptr<CircularString> createCircularString(bool hasZ, bool hasM) const;
316
318 std::unique_ptr<CircularString> createCircularString(const CircularString& ls) const;
319
321 std::unique_ptr<CircularString> createCircularString(
322 std::unique_ptr<CoordinateSequence> && coordinates) const;
323
325 std::unique_ptr<CircularString> createCircularString(
326 const CoordinateSequence& coordinates) const;
327
329 std::unique_ptr<CompoundCurve> createCompoundCurve() const;
330
332 std::unique_ptr<CompoundCurve> createCompoundCurve(std::vector<std::unique_ptr<SimpleCurve>>&&) const;
333
341 std::unique_ptr<Geometry> createEmpty(int dimension) const;
342
348 std::unique_ptr<Geometry> createEmpty(GeometryTypeId typeId) const;
349
350 std::unique_ptr<Geometry> createMulti(std::unique_ptr<Geometry> && geom) const;
351
382 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
383
384 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
385
386 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
387
388 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
389
391 //
398 template <class T>
399 std::unique_ptr<Geometry>
400 buildGeometry(T from, T toofar) const
401 {
402 bool isHeterogeneous = false;
403 std::size_t count = 0;
404 int geomClass = -1;
405 for(T i = from; i != toofar; ++i) {
406 ++count;
407 const Geometry* g = *i;
408 if(geomClass < 0) {
409 geomClass = g->getSortIndex();
410 }
411 else if(geomClass != g->getSortIndex()) {
412 isHeterogeneous = true;
413 }
414 }
415
416 // for the empty geometry, return an empty GeometryCollection
417 if(count == 0) {
418 return std::unique_ptr<Geometry>(createGeometryCollection());
419 }
420
421 // for the single geometry, return a clone
422 if(count == 1) {
423 return (*from)->clone();
424 }
425
426 // Now we know it is a collection
427
428 // FIXME:
429 // Until we tweak all the createMulti* interfaces
430 // to support taking iterators we'll have to build
431 // a custom vector here.
432 std::vector<std::unique_ptr<Geometry>> fromGeoms;
433 for(T i = from; i != toofar; ++i) {
434 fromGeoms.push_back((*i)->clone());
435 }
436
437 // for an heterogeneous ...
438 if(isHeterogeneous) {
439 return createGeometryCollection(std::move(fromGeoms));
440 }
441
442 // At this point we know the collection is not hetereogenous.
443 switch((*from)->getDimension()) {
444 case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
445 case Dimension::L: return createMultiLineString(std::move(fromGeoms));
446 case Dimension::P: return createMultiPoint(std::move(fromGeoms));
447 default:
448 throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
449 }
450 }
451
459 std::unique_ptr<Geometry> buildGeometry(const std::vector<const Geometry*>& geoms) const;
460
461 int getSRID() const
462 {
463 return SRID;
464 };
465
467 std::unique_ptr<Geometry> createGeometry(const Geometry* g) const;
468
470 void destroyGeometry(Geometry* g) const;
471
478 void destroy();
479
480protected:
481
488
498
508 GeometryFactory(const PrecisionModel* pm, int newSRID);
509
516
519
520private:
521
522 PrecisionModel precisionModel;
523 int SRID;
524
525 mutable int _refCount;
526 bool _autoDestroy;
527
528 friend class Geometry;
529
530 void addRef() const;
531 void dropRef() const;
532
533};
534
535} // namespace geos::geom
536} // namespace geos
537
538
539
540
541
542
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:70
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:143
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:172
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:400
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:197
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:88
Indicates one or more illegal arguments.
Definition IllegalArgumentException.h:33
@ GEOS_GEOMETRYCOLLECTION
Definition geos_c.h:214
GeometryTypeId
Geometry types.
Definition Geometry.h:74
Basic namespace for all GEOS functionalities.
Definition geos.h:39