GEOS  3.8.0dev
LineSequencer.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: operation/linemerge/LineSequencer.java r378 (JTS-1.12)
17  *
18  **********************************************************************/
19 
20 #ifndef GEOS_OP_LINEMERGE_LINESEQUENCER_H
21 #define GEOS_OP_LINEMERGE_LINESEQUENCER_H
22 
23 #include <geos/export.h>
24 
25 #include <geos/operation/linemerge/LineMergeGraph.h> // for composition
26 #include <geos/geom/Geometry.h> // for inlines
27 #include <geos/geom/LineString.h> // for inlines
28 
29 #include <vector>
30 #include <list>
31 #include <memory> // for unique_ptr
32 
33 #ifdef _MSC_VER
34 #pragma warning(push)
35 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
36 #endif
37 
38 // Forward declarations
39 namespace geos {
40 namespace geom {
41 class GeometryFactory;
42 class Geometry;
43 class LineString;
44 }
45 namespace planargraph {
46 class DirectedEdge;
47 class Subgraph;
48 class Node;
49 }
50 }
51 
52 
53 namespace geos {
54 namespace operation { // geos::operation
55 namespace linemerge { // geos::operation::linemerge
56 
93 class GEOS_DLL LineSequencer {
94 
95 private:
96  typedef std::list<planargraph::DirectedEdge*> DirEdgeList;
97  typedef std::vector< DirEdgeList* > Sequences;
98 
99  LineMergeGraph graph;
100  const geom::GeometryFactory* factory;
101  unsigned int lineCount;
102  bool isRun;
103  std::unique_ptr<geom::Geometry> sequencedGeometry;
104  bool isSequenceableVar;
105 
106  void addLine(const geom::LineString* lineString);
107  void computeSequence();
108  Sequences* findSequences();
109  DirEdgeList* findSequence(planargraph::Subgraph& graph);
110 
111  void delAll(Sequences&);
112 
114  static geom::LineString* reverse(const geom::LineString* line);
115 
128  geom::Geometry* buildSequencedGeometry(const Sequences& sequences);
129 
130  static const planargraph::Node* findLowestDegreeNode(
131  const planargraph::Subgraph& graph);
132 
133  void addReverseSubpath(const planargraph::DirectedEdge* de,
134  DirEdgeList& deList,
135  DirEdgeList::iterator lit,
136  bool expectedClosed);
137 
146  static const planargraph::DirectedEdge* findUnvisitedBestOrientedDE(
147  const planargraph::Node* node);
148 
167  DirEdgeList* orient(DirEdgeList* seq);
168 
177  DirEdgeList* reverse(DirEdgeList& seq);
178 
186  bool hasSequence(planargraph::Subgraph& graph);
187 
188 public:
189 
190  static geom::Geometry*
191  sequence(const geom::Geometry& geom)
192  {
193  LineSequencer sequencer;
194  sequencer.add(geom);
195  return sequencer.getSequencedLineStrings();
196  }
197 
198  LineSequencer()
199  :
200  factory(nullptr),
201  lineCount(0),
202  isRun(false),
203  sequencedGeometry(nullptr),
204  isSequenceableVar(false)
205  {}
206 
218  static bool isSequenced(const geom::Geometry* geom);
219 
226  bool
228  {
229  computeSequence();
230  return isSequenceableVar;
231  }
232 
242  void
243  add(const geom::Geometry& geometry)
244  {
245  geometry.applyComponentFilter(*this);
246  }
247 
248  template <class TargetContainer>
249  void
250  add(TargetContainer& geoms)
251  {
252  for(typename TargetContainer::const_iterator i = geoms.begin(),
253  e = geoms.end(); i != e; ++i) {
254  const geom::Geometry* g = *i;
255  add(*g);
256  }
257  }
258 
263  void
265  {
266  if(const geom::LineString* ls = dynamic_cast<const geom::LineString*>(g)) {
267  addLine(ls);
268  }
269  }
270 
281  getSequencedLineStrings(bool release = 1)
282  {
283  computeSequence();
284  if(release) {
285  return sequencedGeometry.release();
286  }
287  else {
288  return sequencedGeometry.get();
289  }
290  }
291 };
292 
293 } // namespace geos::operation::linemerge
294 } // namespace geos::operation
295 } // namespace geos
296 
297 #ifdef _MSC_VER
298 #pragma warning(pop)
299 #endif
300 
301 #endif // GEOS_OP_LINEMERGE_LINESEQUENCER_H
Builds a sequence from a set of LineStrings so that they are ordered end to end.
Definition: LineSequencer.h:93
void filter(const geom::Geometry *g)
Act as a GeometryComponentFilter so to extract the linearworks.
Definition: LineSequencer.h:264
geom::Geometry * getSequencedLineStrings(bool release=1)
Returns the LineString or MultiLineString built by the sequencing process, if one exists...
Definition: LineSequencer.h:281
bool isSequenceable()
Tests whether the arrangement of linestrings has a valid sequence.
Definition: LineSequencer.h:227
Represents a directed edge in a PlanarGraph.
Definition: planargraph/DirectedEdge.h:46
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
Definition: LineString.h:68
A planar graph of edges that is analyzed to sew the edges together.
Definition: LineMergeGraph.h:59
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:66
void add(const geom::Geometry &geometry)
Adds a Geometry to be sequenced.
Definition: LineSequencer.h:243
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
A node in a PlanarGraph is a location where 0 or more Edge meet.
Definition: planargraph/Node.h:45
A subgraph of a PlanarGraph.
Definition: Subgraph.h:53
void applyComponentFilter(T &f) const
Apply a filter to each component of this geometry. The filter is expected to provide a ...
Definition: Geometry.h:760