1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2010      Sandro Santilli <strk@kbt.io>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU Lesser General Public Licence as published
10  * by the Free Software Foundation.
11  * See the COPYING file for more information.
12  *
13  **********************************************************************
14  *
15  * Last port: original work
16  *
17  * Developed by Sandro Santilli (strk@kbt.io)
18  * for Faunalia (http://www.faunalia.it)
19  * with funding from Regione Toscana - Settore SISTEMA INFORMATIVO
20  * TERRITORIALE ED AMBIENTALE - for the project: "Sviluppo strumenti
21  * software per il trattamento di dati geografici basati su QuantumGIS
22  * e Postgis (CIG 0494241492)"
23  *
24  **********************************************************************/
25 
26 #ifndef GEOS_OPERATION_SHAREDPATHSOP_H
27 #define GEOS_OPERATION_SHAREDPATHSOP_H
28 
29 #include <geos/export.h> // for GEOS_DLL
30 
31 #include <vector>
32 
33 // Forward declarations
34 namespace geos {
35 namespace geom {
36 class LineString;
37 class Geometry;
38 class GeometryFactory;
39 }
40 }
41 
42 
43 namespace geos {
44 namespace operation { // geos.operation
45 
46 /// Find shared paths among two linear Geometry objects.
47 namespace sharedpaths { // geos.operation.sharedpaths
48 
49 /** \brief
50  * Find shared paths among two linear Geometry objects
51  *
52  * For each shared path report if it direction is the same
53  * or opposite.
54  *
55  * Paths reported as shared are given in the direction they
56  * appear in the first geometry.
57  *
58  * \remark Developed by Sandro Santilli (strk@kbt.io)
59  * for Faunalia (http://www.faunalia.it)
60  * with funding from Regione Toscana - Settore SISTEMA INFORMATIVO
61  * TERRITORIALE ED AMBIENTALE - for the project: "Sviluppo strumenti
62  * software per il trattamento di dati geografici basati su QuantumGIS
63  * e Postgis (CIG 0494241492)"
64  *
65  */
66 class GEOS_DLL SharedPathsOp {
67 public:
68 
69     /// LineString vector (list of edges)
70     typedef std::vector<geom::LineString*> PathList;
71 
72     /// Find paths shared between two linear geometries
73     ///
74     /// @param g1
75     ///   First geometry. Must be linear.
76     ///
77     /// @param g2
78     ///   Second geometry. Must be linear.
79     ///
80     /// @param sameDirection
81     ///   Shared edges having the same direction are pushed
82     ///   onto this vector. They'll be of type LineString.
83     ///   Ownership of the edges is tranferred.
84     ///
85     /// @param oppositeDirection
86     ///   Shared edges having the opposite direction are pushed
87     ///   onto this vector. They'll be of type geom::LineString.
88     ///   Ownership of the edges is tranferred.
89     ///
90     static void sharedPathsOp(const geom::Geometry& g1,
91                               const geom::Geometry& g2,
92                               PathList& sameDirection,
93                               PathList& oppositeDirection);
94 
95     /// Constructor
96     ///
97     /// @param g1
98     ///   First geometry. Must be linear.
99     ///
100     /// @param g2
101     ///   Second geometry. Must be linear.
102     ///
103     SharedPathsOp(const geom::Geometry& g1, const geom::Geometry& g2);
104 
105     /// Get shared paths
106     ///
107     /// @param sameDirection
108     ///   Shared edges having the same direction are pushed
109     ///   onto this vector. They'll be of type geom::LineString.
110     ///   Ownership of the edges is tranferred.
111     ///
112     /// @param oppositeDirection
113     ///   Shared edges having the opposite direction are pushed
114     ///   onto this vector. They'll be of type geom::LineString.
115     ///   Ownership of the edges is tranferred.
116     ///
117     void getSharedPaths(PathList& sameDirection, PathList& oppositeDirection);
118 
119     /// Delete all edges in the list
120     static void clearEdges(PathList& from);
121 
122 private:
123 
124     /// Get all the linear intersections
125     ///
126     /// Ownership of linestring pushed to the given container
127     /// is transferred to caller. See clearEdges for a deep
128     /// release if you need one.
129     ///
130     void findLinearIntersections(PathList& to);
131 
132     /// Check if the given edge goes forward or backward on the given line.
133     ///
134     /// PRECONDITION: It is assumed the edge fully lays on the geometry
135     ///
136     bool isForward(const geom::LineString& edge,
137                    const geom::Geometry& geom);
138 
139     /// Check if the given edge goes in the same direction over
140     /// the two geometries.
141     bool
isSameDirection(const geom::LineString & edge)142     isSameDirection(const geom::LineString& edge)
143     {
144         return (isForward(edge, _g1) == isForward(edge, _g2));
145     }
146 
147     /// Throw an IllegalArgumentException if the geom is not linear
148     void checkLinealInput(const geom::Geometry& g);
149 
150     const geom::Geometry& _g1;
151     const geom::Geometry& _g2;
152     const geom::GeometryFactory& _gf;
153 
154     // Declare type as noncopyable
155     SharedPathsOp(const SharedPathsOp& other) = delete;
156     SharedPathsOp& operator=(const SharedPathsOp& rhs) = delete;
157 
158 };
159 
160 } // namespace geos.operation.sharedpaths
161 } // namespace geos.operation
162 } // namespace geos
163 
164 #endif
165 
166