GEOS  3.3.3
GeometryFactory.h
1 /**********************************************************************
2  * $Id: GeometryFactory.h 3471 2011-09-20 14:34:57Z strk $
3  *
4  * GEOS - Geometry Engine Open Source
5  * http://geos.refractions.net
6  *
7  * Copyright (C) 2011 Sandro Santilli <strk@keybit.net>
8  * Copyright (C) 2006 Refractions Research Inc.
9  *
10  * This is free software; you can redistribute and/or modify it under
11  * the terms of the GNU Lesser General Public Licence as published
12  * by the Free Software Foundation.
13  * See the COPYING file for more information.
14  *
15  **********************************************************************
16  *
17  * Last port: geom/GeometryFactory.java r320 (JTS-1.12)
18  *
19  **********************************************************************/
20 
21 #ifndef GEOS_GEOM_GEOMETRYFACTORY_H
22 #define GEOS_GEOM_GEOMETRYFACTORY_H
23 
24 #include <geos/geom/Geometry.h>
25 #include <geos/geom/GeometryCollection.h>
26 #include <geos/geom/MultiPoint.h>
27 #include <geos/geom/MultiLineString.h>
28 #include <geos/geom/MultiPolygon.h>
29 #include <geos/export.h>
30 #include <geos/inline.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 Point;
50  class Polygon;
51  class PrecisionModel;
52  }
53 }
54 
55 namespace geos {
56 namespace geom { // geos::geom
57 
68 class GEOS_DLL GeometryFactory {
69 public:
76 
89  GeometryFactory(const PrecisionModel *pm, int newSRID,
90  CoordinateSequenceFactory *nCoordinateSequenceFactory);
91 
98  GeometryFactory(CoordinateSequenceFactory *nCoordinateSequenceFactory);
99 
108  GeometryFactory(const PrecisionModel *pm);
109 
119  GeometryFactory(const PrecisionModel* pm, int newSRID);
120 
126  GeometryFactory(const GeometryFactory &gf);
127 
134  static const GeometryFactory*
135  getDefaultInstance();
136 
138  virtual ~GeometryFactory();
139 
140 //Skipped a lot of list to array convertors
141 
142  Point* createPointFromInternalCoord(const Coordinate* coord,
143  const Geometry *exemplar) const;
144 
146  //
149  Geometry* toGeometry(const Envelope* envelope) const;
150 
154  const PrecisionModel* getPrecisionModel() const;
155 
157  Point* createPoint() const;
158 
160  Point* createPoint(const Coordinate& coordinate) const;
161 
163  Point* createPoint(CoordinateSequence *coordinates) const;
164 
166  Point* createPoint(const CoordinateSequence &coordinates) const;
167 
169  GeometryCollection* createGeometryCollection() const;
170 
172  Geometry* createEmptyGeometry() const;
173 
175  GeometryCollection* createGeometryCollection(
176  std::vector<Geometry *> *newGeoms) const;
177 
179  GeometryCollection* createGeometryCollection(
180  const std::vector<Geometry *> &newGeoms) const;
181 
183  MultiLineString* createMultiLineString() const;
184 
186  MultiLineString* createMultiLineString(
187  std::vector<Geometry *> *newLines) const;
188 
190  MultiLineString* createMultiLineString(
191  const std::vector<Geometry *> &fromLines) const;
192 
194  MultiPolygon* createMultiPolygon() const;
195 
197  MultiPolygon* createMultiPolygon(std::vector<Geometry *> *newPolys) const;
198 
200  MultiPolygon* createMultiPolygon(
201  const std::vector<Geometry *> &fromPolys) const;
202 
204  LinearRing* createLinearRing() const;
205 
207  LinearRing* createLinearRing(CoordinateSequence* newCoords) const;
208 
209  std::auto_ptr<Geometry> createLinearRing(
210  std::auto_ptr<CoordinateSequence> newCoords) const;
211 
213  LinearRing* createLinearRing(
214  const CoordinateSequence& coordinates) const;
215 
217  MultiPoint* createMultiPoint() const;
218 
220  MultiPoint* createMultiPoint(std::vector<Geometry *> *newPoints) const;
221 
223  MultiPoint* createMultiPoint(
224  const std::vector<Geometry *> &fromPoints) const;
225 
229  MultiPoint* createMultiPoint(
230  const CoordinateSequence &fromCoords) const;
231 
235  MultiPoint* createMultiPoint(
236  const std::vector<Coordinate> &fromCoords) const;
237 
239  Polygon* createPolygon() const;
240 
242  Polygon* createPolygon(LinearRing *shell,
243  std::vector<Geometry *> *holes) const;
244 
246  Polygon* createPolygon(const LinearRing &shell,
247  const std::vector<Geometry *> &holes) const;
248 
250  LineString* createLineString() const;
251 
253  std::auto_ptr<LineString> createLineString(const LineString& ls) const;
254 
256  LineString* createLineString(CoordinateSequence* coordinates) const;
257 
258  std::auto_ptr<Geometry> createLineString(
259  std::auto_ptr<CoordinateSequence> coordinates) const;
260 
262  LineString* createLineString(
263  const CoordinateSequence& coordinates) const;
264 
296  Geometry* buildGeometry(std::vector<Geometry *> *geoms) const;
297 
299  //
306  template <class T>
307  std::auto_ptr<Geometry> buildGeometry(T from, T toofar) const
308  {
309  bool isHeterogeneous = false;
310  size_t count = 0;
311  int geomClass = -1;
312  for (T i=from; i != toofar; ++i)
313  {
314  ++count;
315  const Geometry* g = *i;
316  if ( geomClass < 0 ) {
317  geomClass = g->getClassSortIndex();
318  }
319  else if ( geomClass != g->getClassSortIndex() ) {
320  isHeterogeneous = true;
321  }
322  }
323 
324  // for the empty geometry, return an empty GeometryCollection
325  if ( count == 0 ) {
326  return std::auto_ptr<Geometry>( createGeometryCollection() );
327  }
328 
329  // for the single geometry, return a clone
330  if ( count == 1 ) {
331  return std::auto_ptr<Geometry>( (*from)->clone() );
332  }
333 
334  // Now we know it is a collection
335 
336  // FIXME:
337  // Until we tweak all the createMulti* interfaces
338  // to support taking iterators we'll have to build
339  // a custom vector here.
340  std::vector<Geometry*> fromGeoms;
341  for (T i=from; i != toofar; ++i) {
342  const Geometry* g = *i;
343  fromGeoms.push_back(const_cast<Geometry*>(g));
344  }
345 
346 
347  // for an heterogeneous ...
348  if ( isHeterogeneous ) {
349  return std::auto_ptr<Geometry>( createGeometryCollection(fromGeoms) );
350  }
351 
352  // At this point we know the collection is not hetereogenous.
353  if ( dynamic_cast<const Polygon*>(*from) ) {
354  return std::auto_ptr<Geometry>( createMultiPolygon(fromGeoms) );
355  } else if ( dynamic_cast<const LineString*>(*from) ) {
356  return std::auto_ptr<Geometry>( createMultiLineString(fromGeoms) );
357  } else if ( dynamic_cast<const Point*>(*from) ) {
358  return std::auto_ptr<Geometry>( createMultiPoint(fromGeoms) );
359  }
360  // FIXME: Why not to throw an exception? --mloskot
361  assert(0); // buildGeomtry encountered an unkwnon geometry type
362  return std::auto_ptr<Geometry>(); // nullptr
363  }
364 
372  Geometry* buildGeometry(const std::vector<Geometry *> &geoms) const;
373 
374  int getSRID() const;
375 
379  const CoordinateSequenceFactory* getCoordinateSequenceFactory() const;
380 
382  Geometry* createGeometry(const Geometry *g) const;
383 
385  void destroyGeometry(Geometry *g) const;
386 
387 private:
388  const PrecisionModel* precisionModel;
389  int SRID;
390  const CoordinateSequenceFactory *coordinateListFactory;
391 };
392 
393 } // namespace geos::geom
394 } // namespace geos
395 
396 #ifdef GEOS_INLINE
397 # include "geos/geom/GeometryFactory.inl"
398 #endif
399 
400 #endif // ndef GEOS_GEOM_GEOMETRYFACTORY_H
401 
402 /**********************************************************************
403  * $Log$
404  * Revision 1.11 2006/07/08 00:33:55 strk
405  * * configure.in: incremented CAPI minor version, to avoid falling behind any future version from the 2.2. branch.
406  * * source/geom/Geometry.cpp, source/geom/GeometryFactory.cpp,
407  * source/geomgraph/EdgeRing.cpp,
408  * source/headers/geos/geom/Geometry.h,
409  * source/headers/geos/geom/GeometryFactory.h,
410  * source/headers/geos/geom/GeometryFactory.inl,
411  * source/headers/geos/geomgraph/EdgeRing.h:
412  * updated doxygen comments (sync with JTS head).
413  * * source/headers/geos/platform.h.in: include <inttypes.h>
414  * rather then <stdint.h>
415  *
416  * Revision 1.10 2006/06/19 21:17:24 strk
417  * port info and doxygen dox.
418  *
419  * Revision 1.9 2006/04/28 11:56:52 strk
420  * * source/geom/GeometryFactory.cpp, source/headers/geos/geom/GeometryFactory.h: added LineString copy constructor.
421  * * source/geom/Polygon.cpp: fixed getBoundary method to always return a geometry composed by LineStrings (not LinearRings)
422  *
423  * Revision 1.8 2006/04/12 11:39:34 strk
424  * Removed Geometry.h and CoordinateSequence.h includes.
425  * The former created a circular dependency.
426  *
427  * Revision 1.7 2006/04/11 11:16:25 strk
428  * Added LineString and LinearRing constructors by auto_ptr
429  *
430  * Revision 1.6 2006/04/10 13:09:49 strk
431  * Added GeometryFactory::defaultInstance()
432  * Made Geometry::INTERNAL_GEOMETRY_FACTORY an alias for it
433  * removed last deletion from Unload::Release class
434  *
435  * Revision 1.5 2006/03/31 17:51:26 strk
436  * A few assertion checking, comments cleanup, use of initialization lists
437  * in constructors, handled NULL parameters.
438  *
439  * Revision 1.4 2006/03/28 16:33:14 strk
440  * Added note about args responsibility in GeometryFactory constructor
441  *
442  * Revision 1.3 2006/03/24 09:52:41 strk
443  * USE_INLINE => GEOS_INLINE
444  *
445  * Revision 1.2 2006/03/20 17:27:03 strk
446  * Bug #72 - Missing <vector> header
447  *
448  * Revision 1.1 2006/03/09 16:46:49 strk
449  * geos::geom namespace definition, first pass at headers split
450  *
451  **********************************************************************/
452