GEOS  3.9.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 #ifndef GEOS_GEOM_GEOMETRYFACTORY_H
21 #define GEOS_GEOM_GEOMETRYFACTORY_H
22 
23 #include <geos/geom/Geometry.h>
24 #include <geos/geom/GeometryCollection.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/export.h>
30 #include <geos/inline.h>
31 #include <geos/util.h>
32 
33 #include <vector>
34 #include <memory>
35 #include <cassert>
36 #include <geos/util/IllegalArgumentException.h>
37 
38 namespace geos {
39 namespace geom {
40 class CoordinateSequenceFactory;
41 class Coordinate;
42 class CoordinateSequence;
43 class Envelope;
44 class Geometry;
45 class GeometryCollection;
46 class LineString;
47 class LinearRing;
48 class MultiLineString;
49 class MultiPoint;
50 class MultiPolygon;
51 class Polygon;
52 }
53 }
54 
55 namespace geos {
56 namespace geom { // geos::geom
57 
68 class GEOS_DLL GeometryFactory {
69 private:
70 
71  struct GeometryFactoryDeleter {
72  void
73  operator()(GeometryFactory* p) const
74  {
75  p->destroy();
76  }
77  };
78 
79 public:
80 
81  using Ptr = std::unique_ptr<GeometryFactory, GeometryFactoryDeleter>;
82 
88  static GeometryFactory::Ptr create();
89 
102  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID,
103  CoordinateSequenceFactory* nCoordinateSequenceFactory);
104 
111  static GeometryFactory::Ptr create(CoordinateSequenceFactory* nCoordinateSequenceFactory);
112 
121  static GeometryFactory::Ptr create(const PrecisionModel* pm);
122 
132  static GeometryFactory::Ptr create(const PrecisionModel* pm, int newSRID);
133 
139  static GeometryFactory::Ptr create(const GeometryFactory& gf);
140 
147  static const GeometryFactory*
148  getDefaultInstance();
149 
150 //Skipped a lot of list to array convertors
151 
152  Point* createPointFromInternalCoord(const Coordinate* coord,
153  const Geometry* exemplar) const;
154 
159  std::unique_ptr<Geometry> toGeometry(const Envelope* envelope) const;
160 
164  const PrecisionModel* getPrecisionModel() const;
165 
167  std::unique_ptr<Point> createPoint() const;
168 
170  Point* createPoint(const Coordinate& coordinate) const;
171 
173  Point* createPoint(CoordinateSequence* coordinates) const;
174 
176  Point* createPoint(const CoordinateSequence& coordinates) const;
177 
179  std::unique_ptr<GeometryCollection> createGeometryCollection() const;
180 
182  std::unique_ptr<Geometry> createEmptyGeometry() const;
183 
185  GeometryCollection* createGeometryCollection(
186  std::vector<Geometry*>* newGeoms) const;
187 
188  template<typename T>
189  std::unique_ptr<GeometryCollection> createGeometryCollection(
190  std::vector<std::unique_ptr<T>> && newGeoms) const {
191  // Can't use make_unique because constructor is protected
192  return std::unique_ptr<GeometryCollection>(new GeometryCollection(Geometry::toGeometryArray(std::move(newGeoms)), *this));
193  }
194 
196  GeometryCollection* createGeometryCollection(
197  const std::vector<const Geometry*>& newGeoms) const;
198 
200  std::unique_ptr<MultiLineString> createMultiLineString() const;
201 
203  MultiLineString* createMultiLineString(
204  std::vector<Geometry*>* newLines) const;
205 
207  MultiLineString* createMultiLineString(
208  const std::vector<const Geometry*>& fromLines) const;
209 
210  std::unique_ptr<MultiLineString> createMultiLineString(
211  std::vector<std::unique_ptr<LineString>> && fromLines) const;
212 
213  std::unique_ptr<MultiLineString> createMultiLineString(
214  std::vector<std::unique_ptr<Geometry>> && fromLines) const;
215 
217  std::unique_ptr<MultiPolygon> createMultiPolygon() const;
218 
220  MultiPolygon* createMultiPolygon(std::vector<Geometry*>* newPolys) const;
221 
223  MultiPolygon* createMultiPolygon(
224  const std::vector<const Geometry*>& fromPolys) const;
225 
226  std::unique_ptr<MultiPolygon> createMultiPolygon(
227  std::vector<std::unique_ptr<Polygon>> && fromPolys) const;
228 
229  std::unique_ptr<MultiPolygon> createMultiPolygon(
230  std::vector<std::unique_ptr<Geometry>> && fromPolys) const;
231 
233  std::unique_ptr<LinearRing> createLinearRing() const;
234 
236  LinearRing* createLinearRing(CoordinateSequence* newCoords) const;
237 
238  std::unique_ptr<LinearRing> createLinearRing(
239  std::unique_ptr<CoordinateSequence> && newCoords) const;
240 
242  LinearRing* createLinearRing(
243  const CoordinateSequence& coordinates) const;
244 
246  std::unique_ptr<MultiPoint> createMultiPoint() const;
247 
249  MultiPoint* createMultiPoint(std::vector<Geometry*>* newPoints) const;
250 
251  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Point>> && newPoints) const;
252 
253  std::unique_ptr<MultiPoint> createMultiPoint(std::vector<std::unique_ptr<Geometry>> && newPoints) const;
254 
256  MultiPoint* createMultiPoint(
257  const std::vector<const Geometry*>& fromPoints) const;
258 
262  MultiPoint* createMultiPoint(
263  const CoordinateSequence& fromCoords) const;
264 
268  MultiPoint* createMultiPoint(
269  const std::vector<Coordinate>& fromCoords) const;
270 
272  std::unique_ptr<Polygon> createPolygon() const;
273 
275  Polygon* createPolygon(LinearRing* shell,
276  std::vector<LinearRing*>* holes) const;
277 
278  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell) const;
279 
280  std::unique_ptr<Polygon> createPolygon(std::unique_ptr<LinearRing> && shell,
281  std::vector<std::unique_ptr<LinearRing>> && holes) const;
282 
284  Polygon* createPolygon(const LinearRing& shell,
285  const std::vector<LinearRing*>& holes) const;
286 
288  std::unique_ptr<LineString> createLineString() const;
289 
291  std::unique_ptr<LineString> createLineString(const LineString& ls) const;
292 
294  LineString* createLineString(CoordinateSequence* coordinates) const;
295 
296  std::unique_ptr<LineString> createLineString(
297  std::unique_ptr<CoordinateSequence> && coordinates) const;
298 
300  LineString* createLineString(
301  const CoordinateSequence& coordinates) const;
302 
333  Geometry* buildGeometry(std::vector<Geometry*>* geoms) const;
334 
335  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Geometry>> && geoms) const;
336 
337  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Point>> && geoms) const;
338 
339  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<LineString>> && geoms) const;
340 
341  std::unique_ptr<Geometry> buildGeometry(std::vector<std::unique_ptr<Polygon>> && geoms) const;
342 
344  //
351  template <class T>
352  std::unique_ptr<Geometry>
353  buildGeometry(T from, T toofar) const
354  {
355  bool isHeterogeneous = false;
356  size_t count = 0;
357  int geomClass = -1;
358  for(T i = from; i != toofar; ++i) {
359  ++count;
360  const Geometry* g = *i;
361  if(geomClass < 0) {
362  geomClass = g->getSortIndex();
363  }
364  else if(geomClass != g->getSortIndex()) {
365  isHeterogeneous = true;
366  }
367  }
368 
369  // for the empty geometry, return an empty GeometryCollection
370  if(count == 0) {
371  return std::unique_ptr<Geometry>(createGeometryCollection());
372  }
373 
374  // for the single geometry, return a clone
375  if(count == 1) {
376  return (*from)->clone();
377  }
378 
379  // Now we know it is a collection
380 
381  // FIXME:
382  // Until we tweak all the createMulti* interfaces
383  // to support taking iterators we'll have to build
384  // a custom vector here.
385  std::vector<std::unique_ptr<Geometry>> fromGeoms;
386  for(T i = from; i != toofar; ++i) {
387  fromGeoms.push_back((*i)->clone());
388  }
389 
390  // for an heterogeneous ...
391  if(isHeterogeneous) {
392  return createGeometryCollection(std::move(fromGeoms));
393  }
394 
395  // At this point we know the collection is not hetereogenous.
396  switch((*from)->getDimension()) {
397  case Dimension::A: return createMultiPolygon(std::move(fromGeoms));
398  case Dimension::L: return createMultiLineString(std::move(fromGeoms));
399  case Dimension::P: return createMultiPoint(std::move(fromGeoms));
400  default:
401  throw geos::util::IllegalArgumentException(std::string("Invalid geometry type."));
402  }
403  }
404 
412  Geometry* buildGeometry(const std::vector<const Geometry*>& geoms) const;
413 
414  int getSRID() const;
415 
419  const CoordinateSequenceFactory* getCoordinateSequenceFactory() const;
420 
422  Geometry* createGeometry(const Geometry* g) const;
423 
425  void destroyGeometry(Geometry* g) const;
426 
433  void destroy();
434 
435 protected:
436 
442  GeometryFactory();
443 
456  GeometryFactory(const PrecisionModel* pm, int newSRID,
457  CoordinateSequenceFactory* nCoordinateSequenceFactory);
458 
465  GeometryFactory(CoordinateSequenceFactory* nCoordinateSequenceFactory);
466 
475  GeometryFactory(const PrecisionModel* pm);
476 
486  GeometryFactory(const PrecisionModel* pm, int newSRID);
487 
493  GeometryFactory(const GeometryFactory& gf);
494 
496  virtual ~GeometryFactory();
497 
498 private:
499 
500  PrecisionModel precisionModel;
501  int SRID;
502  const CoordinateSequenceFactory* coordinateListFactory;
503 
504  mutable int _refCount;
505  bool _autoDestroy;
506 
507  friend class Geometry;
508 
509  void addRef() const;
510  void dropRef() const;
511 
512 };
513 
514 } // namespace geos::geom
515 } // namespace geos
516 
517 #ifdef GEOS_INLINE
518 # include "geos/geom/GeometryFactory.inl"
519 #endif
520 
521 #endif // ndef GEOS_GEOM_GEOMETRYFACTORY_H
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:60
Definition: MultiPolygon.h:59
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:87
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
Indicates one or more illegal arguments.
Definition: IllegalArgumentException.h:34
Definition: LineString.h:68
Represents a linear polygon, which may include holes.
Definition: Polygon.h:64
Dimension value of a curve (1).
Definition: Dimension.h:45
Dimension value of a surface (2).
Definition: Dimension.h:48
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:68
Represents a collection of heterogeneous Geometry objects.
Definition: GeometryCollection.h:55
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:26
Definition: MultiPoint.h:54
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple...
Definition: LinearRing.h:54
std::unique_ptr< Geometry > buildGeometry(T from, T toofar) const
See buildGeometry(std::vector&) for semantics.
Definition: GeometryFactory.h:353
Dimension value of a point (0).
Definition: Dimension.h:42
A factory to create concrete instances of CoordinateSequences.
Definition: CoordinateSequenceFactory.h:48
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
Definition: Point.h:66
Models a collection of LineStrings.
Definition: MultiLineString.h:51