All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vividsolutions.jtstest.clean.CleanDuplicatePoints Maven / Gradle / Ivy

The newest version!

/*
 * The JTS Topology Suite is a collection of Java classes that
 * implement the fundamental operations required to validate a given
 * geo-spatial data set to a known topological specification.
 *
 * Copyright (C) 2001 Vivid Solutions
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For more information, contact:
 *
 *     Vivid Solutions
 *     Suite #1A
 *     2328 Government Street
 *     Victoria BC  V8T 5G5
 *     Canada
 *
 *     (250)385-6040
 *     www.vividsolutions.com
 */
package com.vividsolutions.jtstest.clean;

import java.util.*;
import com.vividsolutions.jts.geom.*;


/**
 * @version 1.7
 */
public class CleanDuplicatePoints {

  public static Coordinate[] removeDuplicatePoints(Coordinate[] coord)
  {
    List uniqueCoords = new ArrayList();
    Coordinate lastPt = null;
    for (int i = 0; i < coord.length; i++) {
      if (lastPt == null || ! lastPt.equals(coord[i])) {
        lastPt = coord[i];
        uniqueCoords.add(new Coordinate(lastPt));
      }
    }
    return (Coordinate[]) uniqueCoords.toArray(new Coordinate[0]);
  }

  private GeometryFactory fact;

  public CleanDuplicatePoints() {
  }

  public Geometry clean(Geometry g)
  {
    fact = g.getFactory();
    if (g.isEmpty()) return g;
    if (g instanceof Point)                   return g;
    else if (g instanceof MultiPoint)         return g;
                        // LineString also handles LinearRings
    else if (g instanceof LinearRing)         return clean((LinearRing) g);
    else if (g instanceof LineString)         return clean((LineString) g);
    else if (g instanceof Polygon)            return clean((Polygon) g);
    else if (g instanceof MultiLineString)    return clean((MultiLineString) g);
    else if (g instanceof MultiPolygon)       return clean((MultiPolygon) g);
    else if (g instanceof GeometryCollection) return clean((GeometryCollection) g);
    else  throw new UnsupportedOperationException(g.getClass().getName());
  }

  private LinearRing clean(LinearRing g)
  {
    Coordinate[] coords = removeDuplicatePoints(g.getCoordinates());
    return fact.createLinearRing(coords);
  }

  private LineString clean(LineString g)
  {
    Coordinate[] coords = removeDuplicatePoints(g.getCoordinates());
    return fact.createLineString(coords);
  }

  private Polygon clean(Polygon poly)
  {
    Coordinate[] shellCoords = removeDuplicatePoints(poly.getExteriorRing().getCoordinates());
    LinearRing shell = fact.createLinearRing(shellCoords);
    List holes = new ArrayList();
    for (int i = 0; i < poly.getNumInteriorRing(); i++) {
      Coordinate[] holeCoords = removeDuplicatePoints(poly.getInteriorRingN(i).getCoordinates());
      holes.add(fact.createLinearRing(holeCoords));
    }
    return fact.createPolygon(shell, GeometryFactory.toLinearRingArray(holes));
  }

  private MultiPolygon clean(MultiPolygon g)
  {
    List polys = new ArrayList();
    for (int i = 0; i < g.getNumGeometries(); i++) {
      Polygon poly = (Polygon) g.getGeometryN(i);
      polys.add(clean(poly));
    }
    return fact.createMultiPolygon(GeometryFactory.toPolygonArray(polys));
  }
  private MultiLineString clean(MultiLineString g)
  {
    List lines = new ArrayList();
    for (int i = 0; i < g.getNumGeometries(); i++) {
      LineString line = (LineString) g.getGeometryN(i);
      lines.add(clean(line));
    }
    return fact.createMultiLineString(GeometryFactory.toLineStringArray(lines));
  }
  private GeometryCollection clean(GeometryCollection g)
  {
    List geoms = new ArrayList();
    for (int i = 0; i < g.getNumGeometries(); i++) {
      Geometry geom = g.getGeometryN(i);
      geoms.add(clean(geom));
    }
    return fact.createGeometryCollection(GeometryFactory.toGeometryArray(geoms));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy