/src/geos/src/operation/relateng/NodeSections.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (c) 2024 Martin Davis |
7 | | * Copyright (C) 2024 Paul Ramsey <pramsey@cleverelephant.ca> |
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 | | #include <geos/operation/relateng/NodeSections.h> |
17 | | #include <geos/operation/relateng/RelateNode.h> |
18 | | #include <geos/operation/relateng/PolygonNodeConverter.h> |
19 | | #include <algorithm> |
20 | | |
21 | | using geos::geom::CoordinateXY; |
22 | | using geos::geom::Geometry; |
23 | | |
24 | | |
25 | | namespace geos { // geos |
26 | | namespace operation { // geos.operation |
27 | | namespace relateng { // geos.operation.relateng |
28 | | |
29 | | /* public */ |
30 | | const CoordinateXY* |
31 | | NodeSections::getCoordinate() const |
32 | 0 | { |
33 | 0 | return nodePt; |
34 | 0 | } |
35 | | |
36 | | |
37 | | /* public */ |
38 | | void |
39 | | NodeSections::addNodeSection(NodeSection* e) |
40 | 0 | { |
41 | | //System.out.println(e); |
42 | 0 | sections.emplace_back(e); |
43 | 0 | } |
44 | | |
45 | | |
46 | | /* public */ |
47 | | bool |
48 | | NodeSections::hasInteractionAB() const |
49 | 0 | { |
50 | 0 | bool isA = false; |
51 | 0 | bool isB = false; |
52 | 0 | for (const std::unique_ptr<NodeSection>& ns : sections) { |
53 | 0 | if (ns->isA()) |
54 | 0 | isA = true; |
55 | 0 | else |
56 | 0 | isB = true; |
57 | |
|
58 | 0 | if (isA && isB) |
59 | 0 | return true; |
60 | 0 | } |
61 | 0 | return false; |
62 | 0 | } |
63 | | |
64 | | |
65 | | /* public */ |
66 | | const Geometry* |
67 | | NodeSections::getPolygonal(bool isA) const |
68 | 0 | { |
69 | 0 | for (const std::unique_ptr<NodeSection>& ns : sections) { |
70 | 0 | if (ns->isA() == isA) { |
71 | 0 | const Geometry* poly = ns->getPolygonal(); |
72 | 0 | if (poly != nullptr) |
73 | 0 | return poly; |
74 | 0 | } |
75 | 0 | } |
76 | 0 | return nullptr; |
77 | 0 | } |
78 | | |
79 | | |
80 | | /* public */ |
81 | | std::unique_ptr<RelateNode> |
82 | | NodeSections::createNode() |
83 | 0 | { |
84 | 0 | prepareSections(); |
85 | |
|
86 | 0 | std::unique_ptr<RelateNode> node(new RelateNode(nodePt)); |
87 | 0 | std::size_t i = 0; |
88 | 0 | while (i < sections.size()) { |
89 | 0 | const std::unique_ptr<NodeSection>& ns = sections[i]; |
90 | | //-- if there multiple polygon sections incident at node convert them to maximal-ring structure |
91 | 0 | if (ns->isArea() && hasMultiplePolygonSections(sections, i)) { |
92 | 0 | std::vector<const NodeSection*> polySections = collectPolygonSections(sections, i); |
93 | 0 | std::vector<std::unique_ptr<NodeSection>> nsConvert = PolygonNodeConverter::convert(polySections); |
94 | 0 | node->addEdges(nsConvert); |
95 | 0 | i += polySections.size(); |
96 | 0 | } |
97 | 0 | else { |
98 | | //-- the most common case is a line or a single polygon ring section |
99 | 0 | node->addEdges(ns.get()); |
100 | 0 | i += 1; |
101 | 0 | } |
102 | 0 | } |
103 | 0 | return node; |
104 | 0 | } |
105 | | |
106 | | |
107 | | /* private */ |
108 | | void |
109 | | NodeSections::prepareSections() |
110 | 0 | { |
111 | | // Comparator lambda for sort support |
112 | 0 | auto comparator = []( |
113 | 0 | const std::unique_ptr<NodeSection>& a, |
114 | 0 | const std::unique_ptr<NodeSection>& b) |
115 | 0 | { |
116 | 0 | return a->compareTo(*b) < 0; |
117 | 0 | }; |
118 | |
|
119 | 0 | std::sort(sections.begin(), sections.end(), comparator); |
120 | | //TODO: remove duplicate sections |
121 | 0 | } |
122 | | |
123 | | |
124 | | /* private static */ |
125 | | bool |
126 | | NodeSections::hasMultiplePolygonSections( |
127 | | std::vector<std::unique_ptr<NodeSection>>& sections, |
128 | | std::size_t i) |
129 | 0 | { |
130 | | //-- if last section can only be one |
131 | 0 | if (i >= sections.size() - 1) |
132 | 0 | return false; |
133 | | //-- check if there are at least two sections for same polygon |
134 | 0 | std::unique_ptr<NodeSection>& ns = sections[i]; |
135 | 0 | std::unique_ptr<NodeSection>& nsNext = sections[i + 1]; |
136 | 0 | return ns->isSamePolygon(*nsNext); |
137 | 0 | } |
138 | | |
139 | | |
140 | | /* private static */ |
141 | | std::vector<const NodeSection*> |
142 | | NodeSections::collectPolygonSections( |
143 | | std::vector<std::unique_ptr<NodeSection>>& sections, |
144 | | std::size_t i) |
145 | 0 | { |
146 | 0 | std::vector<const NodeSection*> polySections; |
147 | | //-- note ids are only unique to a geometry |
148 | 0 | std::unique_ptr<NodeSection>& polySection = sections[i]; |
149 | 0 | while (i < sections.size() && |
150 | 0 | polySection->isSamePolygon(*(sections[i]))) |
151 | 0 | { |
152 | 0 | polySections.push_back(sections[i].get()); |
153 | 0 | i++; |
154 | 0 | } |
155 | 0 | return polySections; |
156 | 0 | } |
157 | | |
158 | | |
159 | | } // namespace geos.operation.overlayng |
160 | | } // namespace geos.operation |
161 | | } // namespace geos |
162 | | |
163 | | |