File: IsValidOp.cpp

package info (click to toggle)
geos 2.1.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,784 kB
  • ctags: 3,505
  • sloc: cpp: 24,991; sh: 8,431; xml: 6,597; makefile: 401; python: 77
file content (565 lines) | stat: -rw-r--r-- 17,017 bytes parent folder | download
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/**********************************************************************
 * $Id: IsValidOp.cpp,v 1.27 2004/12/08 13:54:44 strk Exp $
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.refractions.net
 *
 * 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.
 *
 **********************************************************************/

#include <geos/opValid.h>
#include <stdio.h>
#include <typeinfo>
#include <set>
#include <geos/util.h>

namespace geos {

const Coordinate& IsValidOp::findPtNotNode(const CoordinateSequence *testCoords,const LinearRing *searchRing, GeometryGraph *graph) {
	// find edge corresponding to searchRing.
	Edge *searchEdge=graph->findEdge(searchRing);
	// find a point in the testCoords which is not a node of the searchRing
	EdgeIntersectionList *eiList=searchEdge->getEdgeIntersectionList();
	// somewhat inefficient - is there a better way? (Use a node map, for instance?)
	for(int i=0;i<testCoords->getSize(); i++) {
		const Coordinate& pt=testCoords->getAt(i);
		if (!eiList->isIntersection(pt)) {
			return pt;
		}
	}
	return Coordinate::getNull();
}

IsValidOp::IsValidOp(const Geometry *geom){
	isChecked=false;
	validErr=NULL;
	parentGeometry=geom;
}

IsValidOp::~IsValidOp(){
	delete validErr;
}

bool IsValidOp::isValid() {
	checkValid(parentGeometry);
	return validErr==NULL;
}

/*
 * Checks whether a coordinate is valid for processing.
 * Coordinates are valid iff their x and y coordinates are in the
 * range of the floating point representation.
 *
 * @param coord the coordinate to validate
 * @return <code>true</code> if the coordinate is valid
 */
bool IsValidOp::isValid(const Coordinate &coord) {
	if (! finite(coord.x) ) return false;
	if (! finite(coord.y) ) return false;
	return true;
}

TopologyValidationError *
IsValidOp::getValidationError()
{
	checkValid(parentGeometry);
	return validErr;
}

void
IsValidOp::checkValid(const Geometry *g)
{
	const GeometryCollection *gc;
	if (isChecked) return;
	validErr=NULL;
	if (g->isEmpty()) return;
	if (typeid(*g)==typeid(Point)) checkValid((Point *)g);
	else if (typeid(*g)==typeid(LinearRing)) checkValid((LinearRing*)g);
	else if (typeid(*g)==typeid(LineString)) checkValid((LineString*)g);
	else if (typeid(*g)==typeid(Polygon)) checkValid((Polygon*)g);
	else if (typeid(*g)==typeid(MultiPolygon)) checkValid((MultiPolygon*)g);
	else if ((gc=dynamic_cast<const GeometryCollection *>(g)))
		checkValid(gc);
	else throw new UnsupportedOperationException();
}

/*
 * Checks validity of a Point.
 */
void IsValidOp::checkValid(const Point *g){
	CoordinateSequence *cs = g->getCoordinates();
	checkInvalidCoordinates(cs);
	delete cs;
}

/*
 * Checks validity of a LineString.  Almost anything goes for linestrings!
 */
void IsValidOp::checkValid(const LineString *g){
	checkInvalidCoordinates(g->getCoordinatesRO());
	if (validErr != NULL) return;
	GeometryGraph *graph=new GeometryGraph(0,g);
	checkTooFewPoints(graph);
	delete graph;
}

/**
* Checks validity of a LinearRing.
*/
void IsValidOp::checkValid(const LinearRing *g){
	checkInvalidCoordinates(g->getCoordinatesRO());
	if (validErr != NULL) return;
	GeometryGraph *graph = new GeometryGraph(0, g);
	checkTooFewPoints(graph);
	if (validErr!=NULL) {
		delete graph;
		return;
	}
	LineIntersector *li = new RobustLineIntersector();
	delete graph->computeSelfNodes(li, true);
	checkNoSelfIntersectingRings(graph);
	delete li;
	delete graph;
}

/**
* Checks the validity of a polygon.
* Sets the validErr flag.
*/
void IsValidOp::checkValid(const Polygon *g){
	checkInvalidCoordinates(g);
	if (validErr != NULL) return;
	auto_ptr<GeometryGraph> graph(new GeometryGraph(0,g));
	checkTooFewPoints(graph.get());
	if (validErr!=NULL) {
		return;
	}
	checkConsistentArea(graph.get());
	if (validErr!=NULL) {
		return;
	}
	checkNoSelfIntersectingRings(graph.get());
	if (validErr!=NULL) {
		return;
	}
	checkHolesInShell(g,graph.get());
	if (validErr!=NULL) {
		return;
	}
	//SLOWcheckHolesNotNested(g);
	checkHolesNotNested(g,graph.get());
	if (validErr!=NULL) {
		return;
	}
	checkConnectedInteriors(graph.get());
}

void
IsValidOp::checkValid(const MultiPolygon *g)
{
	for (int i=0; i<g->getNumGeometries(); i++)
	{
		checkInvalidCoordinates((const Polygon *)g->getGeometryN(i));
		if (validErr != NULL) return;
	}

	GeometryGraph *graph=new GeometryGraph(0,g);
	checkTooFewPoints(graph);
	if (validErr!=NULL) {
		delete graph;
		return;
	}
	checkConsistentArea(graph);
	if (validErr!=NULL) {
		delete graph;
		return;
	}
	checkNoSelfIntersectingRings(graph);
	if (validErr!=NULL) {
		delete graph;
		return;
	}

	for(int i=0;i<g->getNumGeometries();i++) {
		Polygon *p=(Polygon*)g->getGeometryN(i);
		checkHolesInShell(p,graph);
		if (validErr!=NULL) {
			delete graph;
			return;
		}
	}
	for(int i=0;i<g->getNumGeometries();i++) {
		Polygon *p=(Polygon*)g->getGeometryN(i);
		//checkDisjointHolesNotNested(p);
		checkHolesNotNested(p,graph);
		if (validErr!=NULL) {
			delete graph;
			return;
		}
	}
	checkShellsNotNested(g,graph);
	if (validErr!=NULL) {
		delete graph;
		return;
	}
	checkConnectedInteriors(graph);
	delete graph;
}

void IsValidOp::checkValid(const GeometryCollection *gc) {
	for(int i=0;i<gc->getNumGeometries();i++) {
		const Geometry *g=gc->getGeometryN(i);
		checkValid(g);
		if (validErr!=NULL) return;
	}
}

void IsValidOp::checkTooFewPoints(GeometryGraph *graph) {
	if (graph->hasTooFewPoints()) {
		validErr=new TopologyValidationError(
			TopologyValidationError::TOO_FEW_POINTS,
			graph->getInvalidPoint());
		return;
	}
}

void IsValidOp::checkConsistentArea(GeometryGraph *graph) {
	auto_ptr<ConsistentAreaTester> cat(new ConsistentAreaTester(graph));
	bool isValidArea=cat->isNodeConsistentArea();
	if (!isValidArea) {
		validErr=new TopologyValidationError(
			TopologyValidationError::SELF_INTERSECTION,
			cat->getInvalidPoint());
		return;
	}
	if (cat->hasDuplicateRings()) {
		validErr=new TopologyValidationError(
			TopologyValidationError::DUPLICATE_RINGS,
			cat->getInvalidPoint());
	}
}

void IsValidOp::checkNoSelfIntersectingRings(GeometryGraph *graph) {
	vector<Edge*> *edges=graph->getEdges();
	for(int i=0;i<(int)edges->size();i++) {
		Edge *e=(*edges)[i];
		checkSelfIntersectingRing(e->getEdgeIntersectionList());
		if(validErr!=NULL) return;
	}
}

/**
* check that a ring does not self-intersect, except at its endpoints.
* Algorithm is to count the number of times each node along edge occurs.
* If any occur more than once, that must be a self-intersection.
*/
void IsValidOp::checkSelfIntersectingRing(EdgeIntersectionList *eiList) {
	set<Coordinate,CoordLT> *nodeSet=new set<Coordinate,CoordLT>();
	bool isFirst=true;
	vector<EdgeIntersection*> *l=eiList->list;
	for(int i=0;i<(int)l->size();i++) {
		EdgeIntersection *ei=(*l)[i];
		if (isFirst) {
			isFirst=false;
			continue;
		}
		if (nodeSet->find(ei->coord)!=nodeSet->end()) {
			validErr=new TopologyValidationError(
				TopologyValidationError::RING_SELF_INTERSECTION,
				ei->coord);
			delete nodeSet;
			return;
		} else {
			nodeSet->insert(ei->coord);
		}
	}
	delete nodeSet;
}

/* NO LONGER NEEDED AS OF JTS Ver 1.2
void IsValidOp::checkNoRepeatedPoint(Geometry *g) {
	RepeatedPointTester *rpt=new RepeatedPointTester();
	if (rpt->hasRepeatedPoint(g)) {
		validErr=new TopologyValidationError(
			TopologyValidationError::REPEATED_POINT,
			rpt->getCoordinate());
	}
}
*/

/**
* Test that each hole is inside the polygon shell.
* This routine assumes that the holes have previously been tested
* to ensure that all vertices lie on the shell or inside it.
* A simple test of a single point in the hole can be used,
* provide the point is chosen such that it does not lie on the
* boundary of the shell.
* @param p the polygon to be tested for hole inclusion
* @param graph a GeometryGraph incorporating the polygon
*/
void IsValidOp::checkHolesInShell(const Polygon *p,GeometryGraph *graph) {
	LinearRing *shell=(LinearRing*) p->getExteriorRing();
	//const CoordinateSequence *shellPts=shell->getCoordinatesRO();
	//PointInRing pir=new SimplePointInRing(shell);
	//PointInRing pir=new SIRtreePointInRing(shell);
//	auto_ptr<PointInRing> pir(new MCPointInRing(shell));
	auto_ptr<PointInRing> pir(new MCPointInRing(shell));
	for(int i=0;i<p->getNumInteriorRing();i++) {
		LinearRing *hole=(LinearRing*) p->getInteriorRingN(i);
		const Coordinate& holePt=findPtNotNode(hole->getCoordinatesRO(),shell,graph);
		Assert::isTrue(!(holePt==Coordinate::getNull()), "Unable to find a hole point not a vertex of the shell");
		bool outside=!pir->isInside(holePt);
		if (outside) {
			validErr=new TopologyValidationError(
				TopologyValidationError::HOLE_OUTSIDE_SHELL,
				holePt);
			return;
		}
	}
}

/**
* Tests that no hole is nested inside another hole.
* This routine assumes that the holes are disjoint.
* To ensure this, holes have previously been tested
* to ensure that:
* <ul>
* <li>they do not partially overlap
* (checked by <code>checkRelateConsistency</code>)
* <li>they are not identical
* (checked by <code>checkRelateConsistency</code>)
* </ul>
*/
void IsValidOp::checkHolesNotNested(const Polygon *p,GeometryGraph *graph) {
	auto_ptr<QuadtreeNestedRingTester> nestedTester(new QuadtreeNestedRingTester(graph));
	//SimpleNestedRingTester nestedTester=new SimpleNestedRingTester(arg[0]);
	//SweeplineNestedRingTester nestedTester=new SweeplineNestedRingTester(arg[0]);
	for(int i=0;i<p->getNumInteriorRing();i++) {
		LinearRing *innerHole=(LinearRing*) p->getInteriorRingN(i);
		nestedTester->add(innerHole);
	}
	bool isNonNested=nestedTester->isNonNested();
	if (!isNonNested) {
		validErr=new TopologyValidationError(
			TopologyValidationError::NESTED_HOLES,
			nestedTester->getNestedPoint());
	}
}

/*
 * Tests that no element polygon is wholly in the interior of another
 * element polygon.
 * 
 * Preconditions:
 * 
 * - shells do not partially overlap
 * - shells do not touch along an edge
 * - no duplicate rings exist
 *
 * This routine relies on the fact that while polygon shells may touch at
 * one or more vertices, they cannot touch at ALL vertices.
 */
void
IsValidOp::checkShellsNotNested(const MultiPolygon *mp,GeometryGraph *graph)
{
	for(int i=0;i<mp->getNumGeometries();i++) {
		Polygon *p=(Polygon*)mp->getGeometryN(i);
		LinearRing *shell=(LinearRing*) p->getExteriorRing();
		for(int j=0;j<mp->getNumGeometries();j++) {
			if (i==j) continue;
			Polygon *p2=(Polygon*) mp->getGeometryN(j);
			checkShellNotNested(shell,p2,graph);
			if (validErr!=NULL) return;
		}
	}
}

/**
* Check if a shell is incorrectly nested within a polygon.  This is the case
* if the shell is inside the polygon shell, but not inside a polygon hole.
* (If the shell is inside a polygon hole, the nesting is valid.)
* 
* The algorithm used relies on the fact that the rings must be properly contained.
* E.g. they cannot partially overlap (this has been previously checked by
* <code>checkRelateConsistency</code>
*/
void IsValidOp::checkShellNotNested(const LinearRing *shell, const Polygon *p,GeometryGraph *graph) {
	const CoordinateSequence *shellPts=shell->getCoordinatesRO();
	// test if shell is inside polygon shell
	LinearRing *polyShell=(LinearRing*) p->getExteriorRing();
	const CoordinateSequence *polyPts=polyShell->getCoordinatesRO();
	const Coordinate& shellPt=findPtNotNode(shellPts,polyShell,graph);
	// if no point could be found, we can assume that the shell is outside the polygon
	if (shellPt==Coordinate::getNull())
		return;
	bool insidePolyShell=CGAlgorithms::isPointInRing(shellPt,polyPts);
	if (!insidePolyShell) return;
	// if no holes, this is an error!
	if (p->getNumInteriorRing()<=0) {
		validErr=new TopologyValidationError(
			TopologyValidationError::NESTED_SHELLS,
			shellPt);
		return;
	}
	
	/**
	 * Check if the shell is inside one of the holes.
	 * This is the case if one of the calls to checkShellInsideHole
	 * returns a null coordinate.
	 * Otherwise, the shell is not properly contained in a hole, which is
	 * an error.
	 */
	Coordinate& badNestedPt=Coordinate::getNull();
	for(int i=0; i<p->getNumInteriorRing(); i++) {
		LinearRing *hole=(LinearRing*) p->getInteriorRingN(i);
		badNestedPt = checkShellInsideHole(shell, hole, graph);
		if (badNestedPt==Coordinate::getNull()) return;
	}
	validErr=new TopologyValidationError(
		TopologyValidationError::NESTED_SHELLS, badNestedPt
	);
}

/*
 * This routine checks to see if a shell is properly contained in a hole.
 * It assumes that the edges of the shell and hole do not
 * properly intersect.
 *
 * @return <code>null</code> if the shell is properly contained, or
 *   a Coordinate which is not inside the hole if it is not
 *
 */
const Coordinate&
IsValidOp::checkShellInsideHole(const LinearRing *shell, const LinearRing *hole, GeometryGraph *graph)
{
	const CoordinateSequence *shellPts=shell->getCoordinatesRO();
	const CoordinateSequence *holePts=hole->getCoordinatesRO();
	// TODO: improve performance of this - by sorting pointlists for instance?
	const Coordinate& shellPt=findPtNotNode(shellPts,hole,graph);
	// if point is on shell but not hole, check that the shell is inside the hole
	if (!(shellPt==Coordinate::getNull())) {
		bool insideHole=CGAlgorithms::isPointInRing(shellPt,holePts);
		if (!insideHole)
			return shellPt;
	}
	const Coordinate& holePt=findPtNotNode(holePts,shell,graph);
	// if point is on hole but not shell, check that the hole is outside the shell
	if(!(holePt==Coordinate::getNull())) {
		bool insideShell=CGAlgorithms::isPointInRing(holePt,shellPts);
		if (insideShell) {
			return holePt;
		}
		return Coordinate::getNull();
	}
	Assert::shouldNeverReachHere("points in shell and hole appear to be equal");
	return Coordinate::getNull();
}

void IsValidOp::checkConnectedInteriors(GeometryGraph *graph) {
	auto_ptr<ConnectedInteriorTester> cit(new ConnectedInteriorTester(graph));
	if (!cit->isInteriorsConnected())
		validErr=new TopologyValidationError(
		TopologyValidationError::DISCONNECTED_INTERIOR,
		cit->getCoordinate());
}


void
IsValidOp::checkInvalidCoordinates(const CoordinateSequence *cs)
{
	for (int i = 0; i<cs->getSize(); i++)
	{
		if (! isValid(cs->getAt(i)) )
		{
			validErr = new TopologyValidationError(
				TopologyValidationError::INVALID_COORDINATE,
				cs->getAt(i));
			return;

		}
	}
}

void
IsValidOp::checkInvalidCoordinates(const Polygon *poly)
{
	checkInvalidCoordinates(poly->getExteriorRing()->getCoordinatesRO());
	if (validErr != NULL) return;
	for (int i=0; i<poly->getNumInteriorRing(); i++)
	{
		checkInvalidCoordinates(
			poly->getInteriorRingN(i)->getCoordinatesRO()
		);
		if (validErr != NULL) return;
	}
}

} // namespace geos

/**********************************************************************
 * $Log: IsValidOp.cpp,v $
 * Revision 1.27  2004/12/08 13:54:44  strk
 * gcc warnings checked and fixed, general cleanups.
 *
 * Revision 1.26  2004/11/06 08:16:46  strk
 * Fixed CGAlgorithms::isCCW from JTS port.
 * Code cleanup in IsValidOp.
 *
 * Revision 1.25  2004/11/05 11:41:57  strk
 * Made IsValidOp handle IllegalArgumentException throw from GeometryGraph
 * as a sign of invalidity (just for Polygon geometries).
 * Removed leaks generated by this specific exception.
 *
 * Revision 1.24  2004/10/19 19:51:14  strk
 * Fixed many leaks and bugs in Polygonizer.
 * Output still bogus.
 *
 * Revision 1.23  2004/09/13 12:50:11  strk
 * comments cleanup
 *
 * Revision 1.22  2004/09/13 12:39:14  strk
 * Made Point and MultiPoint subject to Validity tests.
 *
 * Revision 1.21  2004/09/13 10:12:49  strk
 * Added invalid coordinates checks in IsValidOp.
 * Cleanups.
 *
 * Revision 1.20  2004/09/13 09:18:10  strk
 * Added IsValidOp::isValid(Coordinate &)
 *
 * Revision 1.19  2004/07/08 19:34:50  strk
 * Mirrored JTS interface of CoordinateSequence, factory and
 * default implementations.
 * Added DefaultCoordinateSequenceFactory::instance() function.
 *
 * Revision 1.18  2004/07/02 13:28:29  strk
 * Fixed all #include lines to reflect headers layout change.
 * Added client application build tips in README.
 *
 * Revision 1.17  2004/05/18 00:02:37  ybychkov
 * IsValidOp::checkShellNotNested() bugfix from JTS 1.4.1 (not released yet) has been added.
 *
 * Revision 1.16  2004/03/29 06:59:25  ybychkov
 * "noding/snapround" package ported (JTS 1.4);
 * "operation", "operation/valid", "operation/relate" and "operation/overlay" upgraded to JTS 1.4;
 * "geom" partially upgraded.
 *
 * Revision 1.15  2003/11/07 01:23:42  pramsey
 * Add standard CVS headers licence notices and copyrights to all cpp and h
 * files.
 *
 * Revision 1.14  2003/10/15 11:24:28  strk
 * Use getCoordinatesRO() introduced.
 *
 * Revision 1.13  2003/10/13 17:54:40  strk
 * IsValidOp constructor used same name for the arg and a private 
 * element. Fixed.
 *
 **********************************************************************/