1 2 /* 3 * Copyright (c) 2016 Vivid Solutions. 4 * 5 * All rights reserved. This program and the accompanying materials 6 * are made available under the terms of the Eclipse Public License 2.0 7 * and Eclipse Distribution License v. 1.0 which accompanies this distribution. 8 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html 9 * and the Eclipse Distribution License is available at 10 * 11 * http://www.eclipse.org/org/documents/edl-v10.php. 12 */ 13 package org.locationtech.jtstest.clean; 14 15 import java.util.*; 16 17 import org.locationtech.jts.geom.*; 18 19 20 /** 21 * @version 1.7 22 */ 23 public class CleanDuplicatePoints { 24 removeDuplicatePoints(Coordinate[] coord)25 public static Coordinate[] removeDuplicatePoints(Coordinate[] coord) 26 { 27 List uniqueCoords = new ArrayList(); 28 Coordinate lastPt = null; 29 for (int i = 0; i < coord.length; i++) { 30 if (lastPt == null || ! lastPt.equals(coord[i])) { 31 lastPt = coord[i]; 32 uniqueCoords.add(new Coordinate(lastPt)); 33 } 34 } 35 return (Coordinate[]) uniqueCoords.toArray(new Coordinate[0]); 36 } 37 38 private GeometryFactory fact; 39 CleanDuplicatePoints()40 public CleanDuplicatePoints() { 41 } 42 clean(Geometry g)43 public Geometry clean(Geometry g) 44 { 45 fact = g.getFactory(); 46 if (g.isEmpty()) return g; 47 if (g instanceof Point) return g; 48 else if (g instanceof MultiPoint) return g; 49 // LineString also handles LinearRings 50 else if (g instanceof LinearRing) return clean((LinearRing) g); 51 else if (g instanceof LineString) return clean((LineString) g); 52 else if (g instanceof Polygon) return clean((Polygon) g); 53 else if (g instanceof MultiLineString) return clean((MultiLineString) g); 54 else if (g instanceof MultiPolygon) return clean((MultiPolygon) g); 55 else if (g instanceof GeometryCollection) return clean((GeometryCollection) g); 56 else throw new UnsupportedOperationException(g.getClass().getName()); 57 } 58 clean(LinearRing g)59 private LinearRing clean(LinearRing g) 60 { 61 Coordinate[] coords = removeDuplicatePoints(g.getCoordinates()); 62 return fact.createLinearRing(coords); 63 } 64 clean(LineString g)65 private LineString clean(LineString g) 66 { 67 Coordinate[] coords = removeDuplicatePoints(g.getCoordinates()); 68 return fact.createLineString(coords); 69 } 70 clean(Polygon poly)71 private Polygon clean(Polygon poly) 72 { 73 Coordinate[] shellCoords = removeDuplicatePoints(poly.getExteriorRing().getCoordinates()); 74 LinearRing shell = fact.createLinearRing(shellCoords); 75 List holes = new ArrayList(); 76 for (int i = 0; i < poly.getNumInteriorRing(); i++) { 77 Coordinate[] holeCoords = removeDuplicatePoints(poly.getInteriorRingN(i).getCoordinates()); 78 holes.add(fact.createLinearRing(holeCoords)); 79 } 80 return fact.createPolygon(shell, GeometryFactory.toLinearRingArray(holes)); 81 } 82 clean(MultiPolygon g)83 private MultiPolygon clean(MultiPolygon g) 84 { 85 List polys = new ArrayList(); 86 for (int i = 0; i < g.getNumGeometries(); i++) { 87 Polygon poly = (Polygon) g.getGeometryN(i); 88 polys.add(clean(poly)); 89 } 90 return fact.createMultiPolygon(GeometryFactory.toPolygonArray(polys)); 91 } clean(MultiLineString g)92 private MultiLineString clean(MultiLineString g) 93 { 94 List lines = new ArrayList(); 95 for (int i = 0; i < g.getNumGeometries(); i++) { 96 LineString line = (LineString) g.getGeometryN(i); 97 lines.add(clean(line)); 98 } 99 return fact.createMultiLineString(GeometryFactory.toLineStringArray(lines)); 100 } clean(GeometryCollection g)101 private GeometryCollection clean(GeometryCollection g) 102 { 103 List geoms = new ArrayList(); 104 for (int i = 0; i < g.getNumGeometries(); i++) { 105 Geometry geom = g.getGeometryN(i); 106 geoms.add(clean(geom)); 107 } 108 return fact.createGeometryCollection(GeometryFactory.toGeometryArray(geoms)); 109 } 110 111 } 112