GEOS  3.11.1
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 
36 namespace geos {
37 namespace geom {
38 class CoordinateSequenceFactory;
39 class Coordinate;
40 class CoordinateSequence;
41 class Envelope;
42 class Geometry;
43 class GeometryCollection;
44 class LineString;
45 class LinearRing;
46 class MultiLineString;
47 class MultiPoint;
48 class MultiPolygon;
49 class Polygon;
50 }
51 }
52 
53 namespace geos {
54 namespace geom { // geos::geom
55 
66 class GEOS_DLL GeometryFactory {
67 private:
68 
69  struct GeometryFactoryDeleter {
70  void
71  operator()(GeometryFactory* p) const
72  {
73  p->destroy();
74  }
75  };
76 
77 public:
78 
79  using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
80 
86  static GeometryFactory::Ptr create();
87 
100  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID,
101  CoordinateSequenceFactory* nCoordinateSequenceFactory);
102 
109  static GeometryFactory::Ptr create(CoordinateSequenceFactory* nCoordinateSequenceFactory);
110 
119  static GeometryFactory::Ptr create(const PrecisionModel* pm);
120 
130  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
131 
137  static GeometryFactory::Ptr create(const GeometryFactory& gf);
138 
145  static const GeometryFactory*
146  getDefaultInstance();
147 
148 //Skipped a lot of list to array convertors
149 
150  Point* createPointFromInternalCoord(const Coordinate* coord,
151  const Geometry* exemplar) const;
152 
157  std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
158 
163  {
164  return &precisionModel;
165  };
166 
168  std::unique_ptr<Point> createPoint(std::size_t coordinateDimension = 2) const;
169 
171  Point* createPoint(const Coordinate& coordinate) const;
172 
174  Point* createPoint(CoordinateSequence* coordinates) const;
175 
177  Point* createPoint(const CoordinateSequence& coordinates) const;
178 
180  std::unique_ptr<GeometryCollection> createGeometryCollection() const;
181 
183  std::unique_ptr<Geometry> createEmptyGeometry() const;
184 
186  GeometryCollection* createGeometryCollection(
187  std::vector<Geometry*>* newGeoms) const;
188 
189  template<typename T>
190  std::unique_ptr<GeometryCollection> createGeometryCollection(
191  std::vector<std::unique_ptr<T>> && newGeoms) const {
192  // Can't use make_unique because constructor is protected
193  return std::unique_ptr<GeometryCollection>(new GeometryCollection(Geometry::toGeometryArray(std::move(newGeoms)), *this));
194  }
195 
197  GeometryCollection* createGeometryCollection(
198  const std::vector<const Geometry*>& newGeoms) const;
199 
201  std::unique_ptr<MultiLineString> createMultiLineString() const;
202 
204  MultiLineString* createMultiLineString(
205  std::vector<Geometry*>* newLines) const;
206 
208  MultiLineString* createMultiLineString(
209  const std::vector<const Geometry*>& fromLines) const;
210 
211  std::unique_ptr<MultiLineString> createMultiLineString(
212  std::vector<std::unique_ptr<LineString>> && fromLines) const;
213 
214  std::unique_ptr<MultiLineString> createMultiLineString(
215  std::vector<std::unique_ptr<Geometry>> && fromLines) const;
216 
218  std::unique_ptr<MultiPolygon> createMultiPolygon() const;
219 
221  MultiPolygon* createMultiPolygon(std::vector<Geometry*>* newPolys) const;
222 
224  MultiPolygon* createMultiPolygon(
225  const std::vector<const Geometry*>& fromPolys) const;
226 
227  std::unique_ptr<MultiPolygon> createMultiPolygon(
228  std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
229 
230  std::unique_ptr<MultiPolygon> createMultiPolygon(
231  std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
232 
234  std::unique_ptr<LinearRing> createLinearRing() const;
235 
237  LinearRing* createLinearRing(CoordinateSequence* newCoords) const;
238 
239  std::unique_ptr<LinearRing> createLinearRing(
240  std::unique_ptr<CoordinateSequence> && newCoords) const;
241 
242  std::unique_ptr<LinearRing> createLinearRing(
243  std::vector<Coordinate> && coordinates) const;
244 
246  LinearRing* createLinearRing(
247  const CoordinateSequence& coordinates) const;
248 
250  std::unique_ptr<MultiPoint> createMultiPoint() const;
251 
253  MultiPoint* createMultiPoint(std::vector<Geometry*>* newPoints) const;
254 
255  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<Coordinate> && newPoints) const;
256 
257  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
258 
259  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
260 
262  MultiPoint* createMultiPoint(
263  const std::vector<const Geometry*>& fromPoints) const;
264 
268  MultiPoint* createMultiPoint(
269  const CoordinateSequence& fromCoords) const;
270 
274  MultiPoint* createMultiPoint(
275  const std::vector<Coordinate>& fromCoords) const;
276 
278  std::unique_ptr<Polygon> createPolygon(std::size_t coordinateDimension = 2) const;
279 
281  Polygon* createPolygon(LinearRing* shell,
282  std::vector<LinearRing*>* holes) const;
283 
284  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
285 
286  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
287  std::vector<std::unique_ptr<LinearRing>> && holes) const;
288 
290  std::unique_ptr<Polygon> createPolygon(std::vector<Coordinate> && coords) const;
291 
293  Polygon* createPolygon(const LinearRing& shell,
294  const std::vector<LinearRing*>& holes) const;
295 
297  std::unique_ptr<LineString> createLineString(std::size_t coordinateDimension = 2) const;
298 
300  std::unique_ptr<LineString> createLineString(const LineString& ls) const;
301 
303  LineString* createLineString(CoordinateSequence* coordinates) const;
304 
305  std::unique_ptr<LineString> createLineString(
306  std::unique_ptr<CoordinateSequence> && coordinates) const;
307 
308  std::unique_ptr<LineString> createLineString(
309  std::vector<Coordinate> && coordinates) const;
310 
312  LineString* createLineString(
313  const CoordinateSequence& coordinates) const;
314 
322  std::unique_ptr<Geometry> createEmpty(int dimension) const;
323 
354  Geometry* buildGeometry(std::vector<Geometry*>* geoms) const;
355 
356  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
357 
358  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
359 
360  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
361 
362  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
363 
365  //
372  template <class T>
373  std::unique_ptr<Geometry>
374  buildGeometry(T from, T toofar) const
375  {
376  bool isHeterogeneous = false;
377  std::size_t count = 0;
378  int geomClass = -1;
379  for(T i = from; i != toofar; ++i) {
380  ++count;
381  const Geometry* g = *i;
382  if(geomClass < 0) {
383  geomClass = g->getSortIndex();
384  }
385  else if(geomClass != g->getSortIndex()) {
386  isHeterogeneous = true;
387  }
388  }
389 
390  // for the empty geometry, return an empty GeometryCollection
391  if(count == 0) {
392  return std::unique_ptr<Geometry>(createGeometryCollection());
393  }
394 
395  // for the single geometry, return a clone
396  if(count == 1) {
397  return (*from)->clone();
398  }
399 
400  // Now we know it is a collection
401 
402  // FIXME:
403  // Until we tweak all the createMulti* interfaces
404  // to support taking iterators we'll have to build
405  // a custom vector here.
406  std::vector<std::unique_ptr<Geometry>> fromGeoms;
407  for(T i = from; i != toofar; ++i) {
408  fromGeoms.push_back((*i)->clone());
409  }
410 
411  // for an heterogeneous ...
412  if(isHeterogeneous) {
413  return createGeometryCollection(std::move(fromGeoms));
414  }
415 
416  // At this point we know the collection is not hetereogenous.
417  switch((*from)->getDimension()) {
418  case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
419  case Dimension::L: return createMultiLineString(std::move(fromGeoms));
420  case Dimension::P: return createMultiPoint(std::move(fromGeoms));
421  default:
422  throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
423  }
424  }
425 
433  Geometry* buildGeometry(const std::vector<const Geometry*>& geoms) const;
434 
435  int getSRID() const
436  {
437  return SRID;
438  };
439 
444  {
445  return coordinateListFactory;
446  };
447 
449  Geometry* createGeometry(const Geometry* g) const;
450 
452  void destroyGeometry(Geometry* g) const;
453 
460  void destroy();
461 
462 protected:
463 
469  GeometryFactory();
470 
483  GeometryFactory(const PrecisionModel* pm, int newSRID,
484  CoordinateSequenceFactory* nCoordinateSequenceFactory);
485 
492  GeometryFactory(CoordinateSequenceFactory* nCoordinateSequenceFactory);
493 
502  GeometryFactory(const PrecisionModel* pm);
503 
513  GeometryFactory(const PrecisionModel* pm, int newSRID);
514 
520  GeometryFactory(const GeometryFactory& gf);
521 
523  virtual ~GeometryFactory();
524 
525 private:
526 
527  PrecisionModel precisionModel;
528  int SRID;
529  const CoordinateSequenceFactory* coordinateListFactory;
530 
531  mutable int _refCount;
532  bool _autoDestroy;
533 
534  friend class Geometry;
535 
536  void addRef() const;
537  void dropRef() const;
538 
539 };
540 
541 } // namespace geos::geom
542 } // namespace geos
543 
544 
545 
546 
547 
548 
const PrecisionModel * getPrecisionModel() const
Returns the PrecisionModel that Geometries created by this factory will be associated with...
Definition: GeometryFactory.h:162
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:58
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:90
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Indicates one or more illegal arguments.
Definition: IllegalArgumentException.h:33
Dimension value of a curve (1).
Definition: Dimension.h:43
Dimension value of a surface (2).
Definition: Dimension.h:46
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:66
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:52
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25
const CoordinateSequenceFactory * getCoordinateSequenceFactory() const
Returns the CoordinateSequenceFactory associated with this GeometryFactory.
Definition: GeometryFactory.h:443
Dimension value of a point (0).
Definition: Dimension.h:40
A factory to create concrete instances of CoordinateSequences.
Definition: CoordinateSequenceFactory.h:44
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:44
Definition: Point.h:63
std::unique_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector<Geometry *>&) for semantics.
Definition: GeometryFactory.h:374