Class RepeatedPointTester

 1  
 2  
 3 /*
 4  * Copyright (c) 2016 Vivid Solutions.
 5  *
 6  * All rights reserved. This program and the accompanying materials
 7  * are made available under the terms of the Eclipse Public License 2.0
 8  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 9  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
10  * and the Eclipse Distribution License is available at
11  *
12  * http://www.eclipse.org/org/documents/edl-v10.php.
13  */
14 package org.locationtech.jts.operation.valid;
15  
16 import org.locationtech.jts.geom.Coordinate;
17 import org.locationtech.jts.geom.Geometry;
18 import org.locationtech.jts.geom.GeometryCollection;
19 import org.locationtech.jts.geom.LineString;
20 import org.locationtech.jts.geom.MultiPoint;
21 import org.locationtech.jts.geom.Point;
22 import org.locationtech.jts.geom.Polygon;
23  
24 /**
25  * Implements the appropriate checks for repeated points
26  * (consecutive identical coordinates) as defined in the
27  * JTS spec.
28  *
29  * @version 1.7
30  */
31 public class RepeatedPointTester {
32  
33   // save the repeated coord found (if any)
34   private Coordinate repeatedCoord;
35  
36   public RepeatedPointTester() {
37   }
38  
39   public Coordinate getCoordinate() { return repeatedCoord; }
40  
41   public boolean hasRepeatedPoint(Geometry g)
42   {
43     if (g.isEmpty()) return false;
44     if (g instanceof Point)                   return false;
45     else if (g instanceof MultiPoint)         return false;
46                         // LineString also handles LinearRings
47     else if (g instanceof LineString)         return hasRepeatedPoint(((LineString) g).getCoordinates());
48     else if (g instanceof Polygon)            return hasRepeatedPoint((Polygon) g);
49     else if (g instanceof GeometryCollection) return hasRepeatedPoint((GeometryCollection) g);
50     else  throw new UnsupportedOperationException(g.getClass().getName());
51   }
52  
53   public boolean hasRepeatedPoint(Coordinate[] coord)
54   {
55     for (int i = 1; i < coord.length; i++) {
56       if (coord[i - 1].equals(coord[i]) ) {
57         repeatedCoord = coord[i];
58         return true;
59       }
60     }
61     return false;
62   }
63   private boolean hasRepeatedPoint(Polygon p)
64   {
65     if (hasRepeatedPoint(p.getExteriorRing().getCoordinates())) return true;
66     for (int i = 0; i < p.getNumInteriorRing(); i++) {
67       if (hasRepeatedPoint(p.getInteriorRingN(i).getCoordinates())) return true;
68     }
69     return false;
70   }
71   private boolean hasRepeatedPoint(GeometryCollection gc)
72   {
73     for (int i = 0; i < gc.getNumGeometries(); i++) {
74       Geometry g = gc.getGeometryN(i);
75       if (hasRepeatedPoint(g)) return true;
76     }
77     return false;
78   }
79  
80  
81 }
82