001:
002: /*
003: * The JTS Topology Suite is a collection of Java classes that
004: * implement the fundamental operations required to validate a given
005: * geo-spatial data set to a known topological specification.
006: *
007: * Copyright (C) 2001 Vivid Solutions
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: package com.vividsolutions.jts.operation.valid;
035:
036: import com.vividsolutions.jts.geom.*;
037:
038: /**
039: * Implements the appropriate checks for repeated points
040: * (consecutive identical coordinates) as defined in the
041: * JTS spec.
042: *
043: * @version 1.7
044: */
045: public class RepeatedPointTester {
046:
047: // save the repeated coord found (if any)
048: private Coordinate repeatedCoord;
049:
050: public RepeatedPointTester() {
051: }
052:
053: public Coordinate getCoordinate() {
054: return repeatedCoord;
055: }
056:
057: public boolean hasRepeatedPoint(Geometry g) {
058: if (g.isEmpty())
059: return false;
060: if (g instanceof Point)
061: return false;
062: else if (g instanceof MultiPoint)
063: return false;
064: // LineString also handles LinearRings
065: else if (g instanceof LineString)
066: return hasRepeatedPoint(((LineString) g).getCoordinates());
067: else if (g instanceof Polygon)
068: return hasRepeatedPoint((Polygon) g);
069: else if (g instanceof GeometryCollection)
070: return hasRepeatedPoint((GeometryCollection) g);
071: else
072: throw new UnsupportedOperationException(g.getClass()
073: .getName());
074: }
075:
076: public boolean hasRepeatedPoint(Coordinate[] coord) {
077: for (int i = 1; i < coord.length; i++) {
078: if (coord[i - 1].equals(coord[i])) {
079: repeatedCoord = coord[i];
080: return true;
081: }
082: }
083: return false;
084: }
085:
086: private boolean hasRepeatedPoint(Polygon p) {
087: if (hasRepeatedPoint(p.getExteriorRing().getCoordinates()))
088: return true;
089: for (int i = 0; i < p.getNumInteriorRing(); i++) {
090: if (hasRepeatedPoint(p.getInteriorRingN(i).getCoordinates()))
091: return true;
092: }
093: return false;
094: }
095:
096: private boolean hasRepeatedPoint(GeometryCollection gc) {
097: for (int i = 0; i < gc.getNumGeometries(); i++) {
098: Geometry g = gc.getGeometryN(i);
099: if (hasRepeatedPoint(g))
100: return true;
101: }
102: return false;
103: }
104:
105: }
|