GEOS 3.12.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
36namespace geos {
37namespace geom {
38class Coordinate;
39class CoordinateSequence;
40class Envelope;
41class Geometry;
42class GeometryCollection;
43class LineString;
44class LinearRing;
45class MultiLineString;
46class MultiPoint;
47class MultiPolygon;
48class Polygon;
49}
50}
51
52namespace geos {
53namespace geom { // geos::geom
54
65class GEOS_DLL GeometryFactory {
66private:
67
68 struct GeometryFactoryDeleter {
69 void
70 operator()(GeometryFactory* p) const
71 {
72 p->destroy();
73 }
74 };
75
76public:
77
78 using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
79
85 static GeometryFactory::Ptr create();
86
95 static GeometryFactory::Ptr create(const PrecisionModel* pm);
96
106 static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
107
113 static GeometryFactory::Ptr create(const GeometryFactory& gf);
114
121 static const GeometryFactory*
123
124//Skipped a lot of list to array convertors
125
126 static std::unique_ptr<Point> createPointFromInternalCoord(const Coordinate* coord,
127 const Geometry* exemplar);
128
133 std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
134
139 {
140 return &precisionModel;
141 };
142
144 std::unique_ptr<Point> createPoint(std::size_t coordinateDimension = 2) const;
145
147 std::unique_ptr<Point> createPoint(const Coordinate& coordinate) const;
148 std::unique_ptr<Point> createPoint(const CoordinateXY& coordinate) const;
149 std::unique_ptr<Point> createPoint(const CoordinateXYM& coordinate) const;
150 std::unique_ptr<Point> createPoint(const CoordinateXYZM& coordinate) const;
151
153 std::unique_ptr<Point> createPoint(std::unique_ptr<CoordinateSequence>&& coordinates) const;
154
156 std::unique_ptr<Point> createPoint(const CoordinateSequence& coordinates) const;
157
159 std::unique_ptr<GeometryCollection> createGeometryCollection() const;
160
162 std::unique_ptr<Geometry> createEmptyGeometry() const;
163
165 template<typename T>
166 std::unique_ptr<GeometryCollection> createGeometryCollection(
167 std::vector<std::unique_ptr<T>> && newGeoms) const {
168 // Can't use make_unique because constructor is protected
169 return std::unique_ptr<GeometryCollection>(new GeometryCollection(Geometry::toGeometryArray(std::move(newGeoms)), *this));
170 }
171
173 std::unique_ptr<GeometryCollection> createGeometryCollection(
174 const std::vector<const Geometry*>& newGeoms) const;
175
177 std::unique_ptr<MultiLineString> createMultiLineString() const;
178
180 std::unique_ptr<MultiLineString> createMultiLineString(
181 const std::vector<const Geometry*>& fromLines) const;
182
184 std::unique_ptr<MultiLineString> createMultiLineString(
185 std::vector<std::unique_ptr<LineString>> && fromLines) const;
186
187 std::unique_ptr<MultiLineString> createMultiLineString(
188 std::vector<std::unique_ptr<Geometry>> && fromLines) const;
189
191 std::unique_ptr<MultiPolygon> createMultiPolygon() const;
192
194 std::unique_ptr<MultiPolygon> createMultiPolygon(
195 const std::vector<const Geometry*>& fromPolys) const;
196
198 std::unique_ptr<MultiPolygon> createMultiPolygon(
199 std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
200
201 std::unique_ptr<MultiPolygon> createMultiPolygon(
202 std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
203
205 std::unique_ptr<LinearRing> createLinearRing(std::size_t coordinateDimension = 2) const;
206
208 std::unique_ptr<LinearRing> createLinearRing(
209 std::unique_ptr<CoordinateSequence> && newCoords) const;
210
212 std::unique_ptr<LinearRing> createLinearRing(
213 const CoordinateSequence& coordinates) const;
214
216 std::unique_ptr<MultiPoint> createMultiPoint() const;
217
218 template<typename T>
219 std::unique_ptr<MultiPoint> createMultiPoint(const T& fromCoords) const
220 {
221 std::vector<std::unique_ptr<Geometry>> pts;
222 pts.reserve(fromCoords.size());
223 for (const auto& c : fromCoords) {
224 pts.emplace_back(createPoint(c));
225 }
226
227 return createMultiPoint(std::move(pts));
228 }
229
231 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
232
233 std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
234
236 std::unique_ptr<MultiPoint> createMultiPoint(
237 const std::vector<const Geometry*>& fromPoints) const;
238
242 std::unique_ptr<MultiPoint> createMultiPoint(
243 const CoordinateSequence& fromCoords) const;
244
246 std::unique_ptr<Polygon> createPolygon(std::size_t coordinateDimension = 2) const;
247
249 std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
250
251 std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
252 std::vector<std::unique_ptr<LinearRing>> && holes) const;
253
255 std::unique_ptr<Polygon> createPolygon(CoordinateSequence && coords) const;
256
259 const std::vector<LinearRing*>& holes) const;
260
262 std::unique_ptr<LineString> createLineString(std::size_t coordinateDimension = 2) const;
263
265 std::unique_ptr<LineString> createLineString(const LineString& ls) const;
266
268 std::unique_ptr<LineString> createLineString(
269 std::unique_ptr<CoordinateSequence> && coordinates) const;
270
272 std::unique_ptr<LineString> createLineString(
273 const CoordinateSequence& coordinates) const;
274
282 std::unique_ptr<Geometry> createEmpty(int dimension) const;
283
289 std::unique_ptr<Geometry> createEmpty(GeometryTypeId typeId) const;
290
291 std::unique_ptr<Geometry> createMulti(std::unique_ptr<Geometry> && geom) const;
292
323 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
324
325 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
326
327 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
328
329 std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
330
332 //
339 template <class T>
340 std::unique_ptr<Geometry>
341 buildGeometry(T from, T toofar) const
342 {
343 bool isHeterogeneous = false;
344 std::size_t count = 0;
345 int geomClass = -1;
346 for(T i = from; i != toofar; ++i) {
347 ++count;
348 const Geometry* g = *i;
349 if(geomClass < 0) {
350 geomClass = g->getSortIndex();
351 }
352 else if(geomClass != g->getSortIndex()) {
353 isHeterogeneous = true;
354 }
355 }
356
357 // for the empty geometry, return an empty GeometryCollection
358 if(count == 0) {
359 return std::unique_ptr<Geometry>(createGeometryCollection());
360 }
361
362 // for the single geometry, return a clone
363 if(count == 1) {
364 return (*from)->clone();
365 }
366
367 // Now we know it is a collection
368
369 // FIXME:
370 // Until we tweak all the createMulti* interfaces
371 // to support taking iterators we'll have to build
372 // a custom vector here.
373 std::vector<std::unique_ptr<Geometry>> fromGeoms;
374 for(T i = from; i != toofar; ++i) {
375 fromGeoms.push_back((*i)->clone());
376 }
377
378 // for an heterogeneous ...
379 if(isHeterogeneous) {
380 return createGeometryCollection(std::move(fromGeoms));
381 }
382
383 // At this point we know the collection is not hetereogenous.
384 switch((*from)->getDimension()) {
385 case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
386 case Dimension::L: return createMultiLineString(std::move(fromGeoms));
387 case Dimension::P: return createMultiPoint(std::move(fromGeoms));
388 default:
389 throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
390 }
391 }
392
400 std::unique_ptr<Geometry> buildGeometry(const std::vector<const Geometry*>& geoms) const;
401
402 int getSRID() const
403 {
404 return SRID;
405 };
406
408 std::unique_ptr<Geometry> createGeometry(const Geometry* g) const;
409
411 void destroyGeometry(Geometry* g) const;
412
419 void destroy();
420
421protected:
422
429
439
449 GeometryFactory(const PrecisionModel* pm, int newSRID);
450
457
460
461private:
462
463 PrecisionModel precisionModel;
464 int SRID;
465
466 mutable int _refCount;
467 bool _autoDestroy;
468
469 friend class Geometry;
470
471 void addRef() const;
472 void dropRef() const;
473
474};
475
476} // namespace geos::geom
477} // namespace geos
478
479
480
481
482
483
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:216
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:51
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:65
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 > createEmptyGeometry() const
Construct the EMPTY Geometry.
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< 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.
const PrecisionModel * getPrecisionModel() const
Returns the PrecisionModel that Geometries created by this factory will be associated with.
Definition GeometryFactory.h:138
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< 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< 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< MultiLineString > createMultiLineString(std::vector< std::unique_ptr< LineString > > &&fromLines) const
Construct a MultiLineString taking ownership of given arguments.
std::unique_ptr< GeometryCollection > createGeometryCollection(std::vector< std::unique_ptr< T > > &&newGeoms) const
Construct a GeometryCollection taking ownership of given arguments.
Definition GeometryFactory.h:166
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< 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< 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< Polygon > createPolygon(std::size_t coordinateDimension=2) const
Construct an EMPTY Polygon.
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:341
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:186
Definition LineString.h:65
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:60
Specifies the precision model of the Coordinate in a Geometry.
Definition PrecisionModel.h:90
Indicates one or more illegal arguments.
Definition IllegalArgumentException.h:33
GeometryTypeId
Geometry types.
Definition Geometry.h:73
Basic namespace for all GEOS functionalities.
Definition geos.h:39