/** * Removes collinear vertices from the provided {@link Geometry}. * * <p> * For the moment this implementation only accepts, {@link Polygon}, {@link LinearRing} and {@link LineString}. * It return <code>null</code> in case the geometry is not of these types. * * @todo implement submethods for {@link GeometryCollection} sublcaases. * @param g the instance of a {@link Geometry} to remove collinear vertices from. * @return a new instance of the provided {@link Geometry} without collinear vertices. */ public static Geometry removeCollinearVertices(final Geometry g) { if (g == null) { throw new NullPointerException("The provided Geometry is null"); } if (g instanceof LineString) { return removeCollinearVertices((LineString) g); } if (g instanceof Polygon) { return removeCollinearVertices((Polygon) g); } throw new IllegalArgumentException("This method can work on LineString and Polygon."); } }
public LineString toLineString() { double[] ordinates = new double[idxLast - idxFirst]; System.arraycopy(this.ordinates, idxFirst, ordinates, 0, ordinates.length); PackedCoordinateSequence.Double cs = (Double) PackedCoordinateSequenceFactory.DOUBLE_FACTORY .create(ordinates, 2); return Utils.getGeometryFactory().createLineString(cs); }
Utils.setPrecision(100.0);
/** * Removes collinear vertices from the provided {@link Polygon}. * @param polygon the instance of a {@link Polygon} to remove collinear vertices from. * @return a new instance of the provided {@link Polygon} without collinear vertices. */ public static Polygon removeCollinearVertices(final Polygon polygon) { if (polygon == null) { throw new NullPointerException("The provided Polygon is null"); } // reuse existing factory final GeometryFactory gf = polygon.getFactory(); // work on the exterior ring LineString exterior = polygon.getExteriorRing(); LineString shell = removeCollinearVertices(exterior); if (shell == null || shell.isEmpty()) { return null; } // work on the holes List<LineString> holes = new ArrayList<LineString>(); final int size = polygon.getNumInteriorRing(); for (int i = 0; i < size; i++) { LineString hole = polygon.getInteriorRingN(i); hole = removeCollinearVertices(hole); if (hole != null && !hole.isEmpty()) { holes.add(hole); } } return gf.createPolygon((LinearRing) shell, (LinearRing[]) holes.toArray(new LinearRing[holes.size()])); }
LineSmoother smoother = new LineSmoother(Utils.getGeometryFactory()); smoother.setControl(smootherControl);
poly = Utils.removeCollinearVertices(poly);