-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathRelateComputer.cpp
516 lines (463 loc) · 16 KB
/
RelateComputer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2011 Sandro Santilli <[email protected]>
* Copyright (C) 2005 Refractions Research Inc.
* Copyright (C) 2001-2002 Vivid Solutions Inc.
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************
*
* Last port: operation/relate/RelateComputer.java rev. 1.24 (JTS-1.10)
*
**********************************************************************/
#include <geos/operation/relate/RelateComputer.h>
#include <geos/operation/relate/RelateNodeFactory.h>
#include <geos/operation/relate/RelateNode.h>
#include <geos/operation/relate/EdgeEndBuilder.h>
#include <geos/algorithm/BoundaryNodeRule.h>
#include <geos/algorithm/LineIntersector.h>
#include <geos/algorithm/PointLocator.h>
#include <geos/geom/IntersectionMatrix.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/Envelope.h>
#include <geos/geomgraph/index/SegmentIntersector.h>
#include <geos/geomgraph/GeometryGraph.h>
#include <geos/geomgraph/Label.h>
#include <geos/geomgraph/Edge.h>
#include <geos/geomgraph/EdgeEndStar.h>
#include <geos/geomgraph/Node.h>
#include <geos/geomgraph/EdgeIntersectionList.h>
#include <geos/geomgraph/EdgeIntersection.h>
#include <geos/operation/BoundaryOp.h>
#include <geos/util/Interrupt.h>
#include <geos/util.h>
#include <vector>
#include <cassert>
#ifndef GEOS_DEBUG
#define GEOS_DEBUG 0
#endif
#if GEOS_DEBUG
# include <iostream>
#endif
using namespace geos::geom;
using namespace geos::geomgraph;
using namespace geos::geomgraph::index;
using namespace geos::algorithm;
namespace geos {
namespace operation { // geos.operation
namespace relate { // geos.operation.relate
RelateComputer::RelateComputer(std::vector<std::unique_ptr<GeometryGraph>>& newArg):
arg(newArg),
nodes(RelateNodeFactory::instance()),
im(new IntersectionMatrix())
{
}
std::unique_ptr<IntersectionMatrix>
RelateComputer::computeIM()
{
// since Geometries are finite and embedded in a 2-D space, the EE element must always be 2
im->set(Location::EXTERIOR, Location::EXTERIOR, 2);
// if the Geometries don't overlap there is nothing to do
const Envelope* e1 = arg[0]->getGeometry()->getEnvelopeInternal();
const Envelope* e2 = arg[1]->getGeometry()->getEnvelopeInternal();
if(!e1->intersects(e2)) {
computeDisjointIM(im.get(), arg[0]->getBoundaryNodeRule());
return std::move(im);
}
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "computing self nodes 1"
<< std::endl;
#endif
std::unique_ptr<SegmentIntersector> si1(
arg[0]->computeSelfNodes(&li, false)
);
GEOS_CHECK_FOR_INTERRUPTS();
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "computing self nodes 2"
<< std::endl;
#endif
std::unique_ptr<SegmentIntersector> si2(
arg[1]->computeSelfNodes(&li, false)
);
GEOS_CHECK_FOR_INTERRUPTS();
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "computing edge intersections"
<< std::endl;
#endif
// compute intersections between edges of the two input geometries
std::unique_ptr< SegmentIntersector> intersector(
arg[0]->computeEdgeIntersections(arg[1].get(), &li, false)
);
GEOS_CHECK_FOR_INTERRUPTS();
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "copying intersection nodes"
<< std::endl;
#endif
computeIntersectionNodes(0);
computeIntersectionNodes(1);
GEOS_CHECK_FOR_INTERRUPTS();
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "copying nodes and labels"
<< std::endl;
#endif
GEOS_CHECK_FOR_INTERRUPTS();
/*
* Copy the labelling for the nodes in the parent Geometries.
* These override any labels determined by intersections
* between the geometries.
*/
copyNodesAndLabels(0);
copyNodesAndLabels(1);
GEOS_CHECK_FOR_INTERRUPTS();
/*
* complete the labelling for any nodes which only have a
* label for a single geometry
*/
//Debug.addWatch(nodes.find(new Coordinate(110, 200)));
//Debug.printWatch();
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "labeling isolated nodes"
<< std::endl;
#endif
labelIsolatedNodes();
//Debug.printWatch();
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "computing proper intersection matrix"
<< std::endl;
#endif
/*
* If a proper intersection was found, we can set a lower bound
* on the IM.
*/
computeProperIntersectionIM(intersector.get(), im.get());
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "computing improper intersections"
<< std::endl;
#endif
/*
* Now process improper intersections
* (eg where one or other of the geometries has a vertex at the
* intersection point)
* We need to compute the edge graph at all nodes to determine
* the IM.
*/
// build EdgeEnds for all intersections
EdgeEndBuilder eeBuilder;
auto&& ee0 = eeBuilder.computeEdgeEnds(arg[0]->getEdges());
insertEdgeEnds(ee0);
auto&& ee1 = eeBuilder.computeEdgeEnds(arg[1]->getEdges());
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "inserting edge ends"
<< std::endl;
#endif
insertEdgeEnds(ee1);
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "labeling node edges"
<< std::endl;
#endif
labelNodeEdges();
/*
* Compute the labeling for isolated components.
* Isolated components are components that do not touch any
* other components in the graph.
* They can be identified by the fact that they will
* contain labels containing ONLY a single element, the one for
* their parent geometry.
* We only need to check components contained in the input graphs,
* since isolated components will not have been replaced by new
* components formed by intersections.
*/
#if GEOS_DEBUG
std::cerr << "RelateComputer::computeIM: "
<< "computing labeling for isolated components"
<< std::endl;
#endif
//debugPrintln("Graph A isolated edges - ");
labelIsolatedEdges(0, 1);
//debugPrintln("Graph B isolated edges - ");
labelIsolatedEdges(1, 0);
// update the IM from all components
updateIM(*im);
return std::move(im);
}
void
RelateComputer::insertEdgeEnds(std::vector<std::unique_ptr<EdgeEnd>>& ee)
{
for(auto& e : ee) {
nodes.add(std::move(e));
}
}
/* private */
void
RelateComputer::computeProperIntersectionIM(SegmentIntersector* intersector, IntersectionMatrix* imX)
{
// If a proper intersection is found, we can set a lower bound on the IM.
int dimA = arg[0]->getGeometry()->getDimension();
int dimB = arg[1]->getGeometry()->getDimension();
bool hasProper = intersector->hasProperIntersection();
bool hasProperInterior = intersector->hasProperInteriorIntersection();
// For Geometry's of dim 0 there can never be proper intersections.
/*
* If edge segments of Areas properly intersect, the areas must properly overlap.
*/
if(dimA == 2 && dimB == 2) {
if(hasProper) {
imX->setAtLeast("212101212");
}
}
/*
* If an Line segment properly intersects an edge segment of an Area,
* it follows that the Interior of the Line intersects the Boundary of the Area.
* If the intersection is a proper *interior* intersection, then
* there is an Interior-Interior intersection too.
* Note that it does not follow that the Interior of the Line intersects the Exterior
* of the Area, since there may be another Area component which contains the rest of the Line.
*/
else if(dimA == 2 && dimB == 1) {
if(hasProper) {
imX->setAtLeast("FFF0FFFF2");
}
if(hasProperInterior) {
imX->setAtLeast("1FFFFF1FF");
}
}
else if(dimA == 1 && dimB == 2) {
if(hasProper) {
imX->setAtLeast("F0FFFFFF2");
}
if(hasProperInterior) {
imX->setAtLeast("1F1FFFFFF");
}
}
/* If edges of LineStrings properly intersect *in an interior point*, all
we can deduce is that
the interiors intersect. (We can NOT deduce that the exteriors intersect,
since some other segments in the geometries might cover the points in the
neighbourhood of the intersection.)
It is important that the point be known to be an interior point of
both Geometries, since it is possible in a self-intersecting geometry to
have a proper intersection on one segment that is also a boundary point of another segment.
*/
else if(dimA == 1 && dimB == 1) {
if(hasProperInterior) {
imX->setAtLeast("0FFFFFFFF");
}
}
}
/**
* Copy all nodes from an arg geometry into this graph.
* The node label in the arg geometry overrides any previously computed
* label for that argIndex.
* (E.g. a node may be an intersection node with
* a computed label of BOUNDARY,
* but in the original arg Geometry it is actually
* in the interior due to the Boundary Determination Rule)
*/
void
RelateComputer::copyNodesAndLabels(uint8_t argIndex)
{
const NodeMap* nm = arg[argIndex]->getNodeMap();
for(const auto& it: *nm) {
const Node* graphNode = it.second.get();
Node* newNode = nodes.addNode(graphNode->getCoordinate());
newNode->setLabel(argIndex,
graphNode->getLabel().getLocation(argIndex));
//node.print(System.out);
}
}
/**
* Insert nodes for all intersections on the edges of a Geometry.
* Label the created nodes the same as the edge label if they do not
* already have a label.
* This allows nodes created by either self-intersections or
* mutual intersections to be labelled.
* Endpoint nodes will already be labelled from when they were inserted.
*/
void
RelateComputer::computeIntersectionNodes(uint8_t argIndex)
{
std::vector<Edge*>* edges = arg[argIndex]->getEdges();
for(Edge* e: *edges) {
Location eLoc = e->getLabel().getLocation(argIndex);
const EdgeIntersectionList& eiL = e->getEdgeIntersectionList();
for(const EdgeIntersection & ei : eiL) {
RelateNode* n = detail::down_cast<RelateNode*>(nodes.addNode(ei.coord));
if(eLoc == Location::BOUNDARY) {
n->setLabelBoundary(argIndex);
}
else {
if(n->getLabel().isNull(argIndex)) {
n->setLabel(argIndex, Location::INTERIOR);
}
}
}
}
}
/*
* For all intersections on the edges of a Geometry,
* label the corresponding node IF it doesn't already have a label.
* This allows nodes created by either self-intersections or
* mutual intersections to be labelled.
* Endpoint nodes will already be labelled from when they were inserted.
*/
void
RelateComputer::labelIntersectionNodes(uint8_t argIndex)
{
const std::vector<Edge*>* edges = arg[argIndex]->getEdges();
for(const Edge* e: *edges) {
Location eLoc = e->getLabel().getLocation(argIndex);
const EdgeIntersectionList& eiL = e->getEdgeIntersectionList();
for(const EdgeIntersection& ei : eiL) {
RelateNode* n = static_cast<RelateNode*>(nodes.find(ei.coord));
if(n->getLabel().isNull(argIndex)) {
if(eLoc == Location::BOUNDARY) {
n->setLabelBoundary(argIndex);
}
else {
n->setLabel(argIndex, Location::INTERIOR);
}
}
}
}
}
/* private */
void
RelateComputer::computeDisjointIM(IntersectionMatrix* imX, const algorithm::BoundaryNodeRule& boundaryNodeRule)
{
const Geometry* ga = arg[0]->getGeometry();
if(!ga->isEmpty()) {
imX->set(Location::INTERIOR, Location::EXTERIOR, ga->getDimension());
imX->set(Location::BOUNDARY, Location::EXTERIOR, getBoundaryDim(*ga, boundaryNodeRule));
}
const Geometry* gb = arg[1]->getGeometry();
if(!gb->isEmpty()) {
imX->set(Location::EXTERIOR, Location::INTERIOR, gb->getDimension());
imX->set(Location::EXTERIOR, Location::BOUNDARY, getBoundaryDim(*gb, boundaryNodeRule));
}
}
int
RelateComputer::getBoundaryDim(const Geometry& geom, const algorithm::BoundaryNodeRule& boundaryNodeRule)
{
// If the geometry has a non-empty boundary
// the intersection is the nominal dimension.
if (BoundaryOp::hasBoundary(geom, boundaryNodeRule)) {
/**
* special case for lines, since Geometry.getBoundaryDimension is not aware
* of Boundary Node Rule.
*/
if (geom.getDimension() == 1)
return Dimension::P;
return geom.getBoundaryDimension();
}
// Otherwise intersection is F
return Dimension::False;
}
void
RelateComputer::labelNodeEdges()
{
const auto& nMap = nodes.nodeMap;
for(const auto& entry : nMap) {
RelateNode* node = detail::down_cast<RelateNode*>(entry.second.get());
#if GEOS_DEBUG
std::cerr << "RelateComputer::labelNodeEdges: "
<< "node edges: " << *(node->getEdges())
<< std::endl;
#endif
node->getEdges()->computeLabelling(arg);
//Debug.print(node.getEdges());
//node.print(System.out);
}
}
/*private*/
void
RelateComputer::updateIM(IntersectionMatrix& imX)
{
std::vector<Edge*>::iterator ei = isolatedEdges.begin();
for(; ei < isolatedEdges.end(); ++ei) {
Edge* e = *ei;
e->GraphComponent::updateIM(imX);
}
const auto& nMap = nodes.nodeMap;
for(const auto& entry : nMap) {
RelateNode* node = detail::down_cast<RelateNode*>(entry.second.get());
node->updateIM(imX);
node->updateIMFromEdges(imX);
}
}
/*private*/
void
RelateComputer::labelIsolatedEdges(uint8_t thisIndex, uint8_t targetIndex)
{
std::vector<Edge*>* edges = arg[thisIndex]->getEdges();
for(Edge* e: *edges) {
if(e->isIsolated()) {
labelIsolatedEdge(e, targetIndex, arg[targetIndex]->getGeometry());
isolatedEdges.push_back(e);
}
}
}
/* private */
void
RelateComputer::labelIsolatedEdge(Edge* e, uint8_t targetIndex, const Geometry* target)
{
// this won't work for GeometryCollections with both dim 2 and 1 geoms
if(target->getDimension() > 0) {
// since edge is not in boundary, may not need the full generality of PointLocator?
// Possibly should use ptInArea locator instead? We probably know here
// that the edge does not touch the bdy of the target Geometry
Location loc = ptLocator.locate(e->getCoordinate(), target);
e->getLabel().setAllLocations(targetIndex, loc);
}
else {
e->getLabel().setAllLocations(targetIndex, Location::EXTERIOR);
}
//System.out.println(e.getLabel());
}
/*private*/
void
RelateComputer::labelIsolatedNodes()
{
for(const auto& it: nodes) {
Node* n = it.second.get();
const Label& label = n->getLabel();
// isolated nodes should always have at least one geometry in their label
assert(label.getGeometryCount() > 0); // node with empty label found
if(n->isIsolated()) {
if(label.isNull(0)) {
labelIsolatedNode(n, 0);
}
else {
labelIsolatedNode(n, 1);
}
}
}
}
/* private */
void
RelateComputer::labelIsolatedNode(Node* n, uint8_t targetIndex)
{
Location loc = ptLocator.locate(n->getCoordinate(),
arg[targetIndex]->getGeometry());
n->getLabel().setAllLocations(targetIndex, loc);
//debugPrintln(n.getLabel());
}
} // namespace geos.operation.relate
} // namespace geos.operation
} // namespace geos