GEOS  3.10.1
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 
125  geom::Geometry* buildSequencedGeometry(const Sequences& sequences);
126 
127  static const planargraph::Node* findLowestDegreeNode(
128  const planargraph::Subgraph& graph);
129 
130  void addReverseSubpath(const planargraph::DirectedEdge* de,
131  DirEdgeList& deList,
132  DirEdgeList::iterator lit,
133  bool expectedClosed);
134 
143  static const planargraph::DirectedEdge* findUnvisitedBestOrientedDE(
144  const planargraph::Node* node);
145 
164  DirEdgeList* orient(DirEdgeList* seq);
165 
174  DirEdgeList* reverse(DirEdgeList& seq);
175 
183  bool hasSequence(planargraph::Subgraph& graph);
184 
185 public:
186 
187  static geom::Geometry*
188  sequence(const geom::Geometry& geom)
189  {
190  LineSequencer sequencer;
191  sequencer.add(geom);
192  return sequencer.getSequencedLineStrings();
193  }
194 
195  LineSequencer()
196  :
197  factory(nullptr),
198  lineCount(0),
199  isRun(false),
200  sequencedGeometry(nullptr),
201  isSequenceableVar(false)
202  {}
203 
215  static bool isSequenced(const geom::Geometry* geom);
216 
223  bool
225  {
226  computeSequence();
227  return isSequenceableVar;
228  }
229 
239  void
240  add(const geom::Geometry& geometry)
241  {
242  geometry.applyComponentFilter(*this);
243  }
244 
245  template <class TargetContainer>
246  void
247  add(TargetContainer& geoms)
248  {
249  for(typename TargetContainer::const_iterator i = geoms.begin(),
250  e = geoms.end(); i != e; ++i) {
251  const geom::Geometry* g = *i;
252  add(*g);
253  }
254  }
255 
260  void
262  {
263  if(const geom::LineString* ls = dynamic_cast<const geom::LineString*>(g)) {
264  addLine(ls);
265  }
266  }
267 
278  getSequencedLineStrings(bool release = 1)
279  {
280  computeSequence();
281  if(release) {
282  return sequencedGeometry.release();
283  }
284  else {
285  return sequencedGeometry.get();
286  }
287  }
288 };
289 
290 } // namespace geos::operation::linemerge
291 } // namespace geos::operation
292 } // namespace geos
293 
294 #ifdef _MSC_VER
295 #pragma warning(pop)
296 #endif
297 
298 #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:261
geom::Geometry * getSequencedLineStrings(bool release=1)
Returns the LineString or MultiLineString built by the sequencing process, if one exists...
Definition: LineSequencer.h:278
void applyComponentFilter(T &f) const
Apply a filter to each component of this geometry. The filter is expected to provide a ...
Definition: Geometry.h:768
bool isSequenceable()
Tests whether the arrangement of linestrings has a valid sequence.
Definition: LineSequencer.h:224
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:68
void add(const geom::Geometry &geometry)
Adds a Geometry to be sequenced.
Definition: LineSequencer.h:240
Basic namespace for all GEOS functionalities.
Definition: Angle.h:26
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