@Test public void testMakeValid() throws Exception { final int[] xPoints = {0, 5, 5, 5, 10, 10, 5, 0}; final int[] yPoints = {0, 0, 5, 10, 10, 5, 5, 5}; final int nPoints = xPoints.length; final Shape shape = new java.awt.Polygon(xPoints, yPoints, nPoints); final LinearRing geom = (LinearRing) JTS.toGeometry(shape); final GeometryFactory factory = new GeometryFactory(); final org.locationtech.jts.geom.Polygon polygon = factory.createPolygon(geom); assertFalse(polygon.isValid()); final List<org.locationtech.jts.geom.Polygon> validPols = JTS.makeValid(polygon, false); assertEquals(2, validPols.size()); org.locationtech.jts.geom.Polygon polygon1 = validPols.get(0); org.locationtech.jts.geom.Polygon polygon2 = validPols.get(1); assertEquals(5, polygon1.getNumPoints()); assertEquals(5, polygon2.getNumPoints()); }
public static List<Polygon> makeValid(Polygon polygon, boolean removeHoles) { final Polygonizer p = new Polygonizer(); polygon.apply( new CoordinateSequenceFilter() { public boolean isGeometryChanged() { return false; } public boolean isDone() { return false; } public void filter(CoordinateSequence seq, int i) { if (i == 0) { return; } p.add( new GeometryFactory() .createLineString( new Coordinate[] { seq.getCoordinate(i - 1), seq.getCoordinate(i) })); } }); List<Polygon> result = new ArrayList<Polygon>(p.getPolygons()); if (removeHoles) { for (int i = 0; i < result.size(); i++) { Polygon item = result.get(i); if (item.getNumInteriorRing() > 0) { GeometryFactory factory = item.getFactory(); Polygon noHoles = factory.createPolygon((LinearRing) item.getExteriorRing(), null); result.set(i, noHoles); } } } return result; }
JTS { public static List<Polygon> makeValid(Polygon polygon, boolean removeHoles) { final Polygonizer p = new Polygonizer(); polygon.apply( new CoordinateSequenceFilter() { public boolean isGeometryChanged() { return false; } public boolean isDone() { return false; } public void filter(CoordinateSequence seq, int i) { if (i == 0) { return; } p.add( new GeometryFactory() .createLineString( new Coordinate[] { seq.getCoordinate(i - 1), seq.getCoordinate(i) })); } }); List<Polygon> result = new ArrayList<Polygon>(p.getPolygons()); if (removeHoles) { for (int i = 0; i < result.size(); i++) { Polygon item = result.get(i); if (item.getNumInteriorRing() > 0) { GeometryFactory factory = item.getFactory(); Polygon noHoles = factory.createPolygon((LinearRing) item.getExteriorRing(), null); result.set(i, noHoles); } } } return result; } }
JTS { public static List<Polygon> makeValid(Polygon polygon, boolean removeHoles) { final Polygonizer p = new Polygonizer(); polygon.apply( new CoordinateSequenceFilter() { public boolean isGeometryChanged() { return false; } public boolean isDone() { return false; } public void filter(CoordinateSequence seq, int i) { if (i == 0) { return; } p.add( new GeometryFactory() .createLineString( new Coordinate[] { seq.getCoordinate(i - 1), seq.getCoordinate(i) })); } }); List<Polygon> result = new ArrayList<Polygon>(p.getPolygons()); if (removeHoles) { for (int i = 0; i < result.size(); i++) { Polygon item = result.get(i); if (item.getNumInteriorRing() > 0) { GeometryFactory factory = item.getFactory(); Polygon noHoles = factory.createPolygon((LinearRing) item.getExteriorRing(), null); result.set(i, noHoles); } } } return result; } private  JTS(); }
JTS { public static List<Polygon> makeValid(Polygon polygon, boolean removeHoles) { final Polygonizer p = new Polygonizer(); polygon.apply( new CoordinateSequenceFilter() { public boolean isGeometryChanged() { return false; } public boolean isDone() { return false; } public void filter(CoordinateSequence seq, int i) { if (i == 0) { return; } p.add( new GeometryFactory() .createLineString( new Coordinate[] { seq.getCoordinate(i - 1), seq.getCoordinate(i) })); } }); List<Polygon> result = new ArrayList<Polygon>(p.getPolygons()); if (removeHoles) { for (int i = 0; i < result.size(); i++) { Polygon item = result.get(i); if (item.getNumInteriorRing() > 0) { GeometryFactory factory = item.getFactory(); Polygon noHoles = factory.createPolygon((LinearRing) item.getExteriorRing(), null); result.set(i, noHoles); } } } return result; } private  JTS(); static Envelope transform(final Envelope envelope, final MathTransform transform); static Envelope transform(
            final Envelope sourceEnvelope,
            Envelope targetEnvelope,
            final MathTransform transform,
            int npoints); static ReferencedEnvelope3D transformTo3D(
            final ReferencedEnvelope sourceEnvelope,
            CoordinateReferenceSystem targetCRS,
            boolean lenient,
            int npoints); static ReferencedEnvelope transformTo2D(
            final ReferencedEnvelope sourceEnvelope,
            CoordinateReferenceSystem targetCRS,
            boolean lenient,
            int npoints); static Geometry transform(final Geometry geom, final MathTransform transform); static Coordinate transform(
            final Coordinate source, Coordinate dest, final MathTransform transform); static Envelope toGeographic(
            final Envelope envelope, final CoordinateReferenceSystem crs); static ReferencedEnvelope toGeographic(final ReferencedEnvelope envelope); static void xform(final MathTransform transform, final double[] src, final double[] dest); static synchronized double orthodromicDistance(
            final Coordinate p1, final Coordinate p2, final CoordinateReferenceSystem crs); static DirectPosition toDirectPosition(
            final Coordinate point, final CoordinateReferenceSystem crs); static void copy(final Coordinate point, final double[] ordinates); static Geometry toGeometry(final Shape shape); static Geometry toGeometry(final Shape shape, final GeometryFactory factory); static Envelope2D getEnvelope2D(
            final Envelope envelope, final CoordinateReferenceSystem crs); static Point toGeometry(DirectPosition position); static Point toGeometry(DirectPosition position, GeometryFactory factory); static Polygon toGeometry(Envelope envelope); static Polygon toGeometry(final Envelope envelope, GeometryFactory factory); static ReferencedEnvelope toEnvelope(Geometry geom); static Polygon toGeometry(ReferencedEnvelope bbox); static Polygon toGeometry(BoundingBox bbox, GeometryFactory factory, int npoints); static Geometry toGeographic(Geometry geom, final CoordinateReferenceSystem crs); static Polygon toGeometry(BoundingBox bbox); static Polygon toGeometry(BoundingBox bbox, final GeometryFactory factory); static void checkCoordinatesRange(Geometry geom, CoordinateReferenceSystem crs); static Geometry smooth(final Geometry geom, double fit); static Geometry smooth(final Geometry geom, double fit, final GeometryFactory factory); static CoordinateSequence createCS(
            CoordinateSequenceFactory csFactory, int size, int dimension); static CoordinateSequence createCS(
            CoordinateSequenceFactory csFactory, int size, int dimension, int measures); static ReferencedEnvelope bounds(Geometry geometry, CoordinateReferenceSystem crs); static Geometry removeCollinearVertices(final Geometry g); static Geometry removeCollinearVertices(final Geometry geometry, int minPoints); static List<Polygon> makeValid(Polygon polygon, boolean removeHoles); static Rectangle2D toRectangle2D(Envelope envelope); static Envelope toEnvelope(Rectangle2D rectangle); static Polygon toPolygon(java.awt.Polygon polygon); static Polygon toPolygon(java.awt.Rectangle rectangle); static Polygon toPolygon(Rectangle2D rectangle); static boolean equals(Envelope e1, Envelope e2, double tolerance); static boolean equals(BoundingBox a, BoundingBox b, double tolerance); }
JTS { public static List<Polygon> makeValid(Polygon polygon, boolean removeHoles) { final Polygonizer p = new Polygonizer(); polygon.apply( new CoordinateSequenceFilter() { public boolean isGeometryChanged() { return false; } public boolean isDone() { return false; } public void filter(CoordinateSequence seq, int i) { if (i == 0) { return; } p.add( new GeometryFactory() .createLineString( new Coordinate[] { seq.getCoordinate(i - 1), seq.getCoordinate(i) })); } }); List<Polygon> result = new ArrayList<Polygon>(p.getPolygons()); if (removeHoles) { for (int i = 0; i < result.size(); i++) { Polygon item = result.get(i); if (item.getNumInteriorRing() > 0) { GeometryFactory factory = item.getFactory(); Polygon noHoles = factory.createPolygon((LinearRing) item.getExteriorRing(), null); result.set(i, noHoles); } } } return result; } private  JTS(); static Envelope transform(final Envelope envelope, final MathTransform transform); static Envelope transform(
            final Envelope sourceEnvelope,
            Envelope targetEnvelope,
            final MathTransform transform,
            int npoints); static ReferencedEnvelope3D transformTo3D(
            final ReferencedEnvelope sourceEnvelope,
            CoordinateReferenceSystem targetCRS,
            boolean lenient,
            int npoints); static ReferencedEnvelope transformTo2D(
            final ReferencedEnvelope sourceEnvelope,
            CoordinateReferenceSystem targetCRS,
            boolean lenient,
            int npoints); static Geometry transform(final Geometry geom, final MathTransform transform); static Coordinate transform(
            final Coordinate source, Coordinate dest, final MathTransform transform); static Envelope toGeographic(
            final Envelope envelope, final CoordinateReferenceSystem crs); static ReferencedEnvelope toGeographic(final ReferencedEnvelope envelope); static void xform(final MathTransform transform, final double[] src, final double[] dest); static synchronized double orthodromicDistance(
            final Coordinate p1, final Coordinate p2, final CoordinateReferenceSystem crs); static DirectPosition toDirectPosition(
            final Coordinate point, final CoordinateReferenceSystem crs); static void copy(final Coordinate point, final double[] ordinates); static Geometry toGeometry(final Shape shape); static Geometry toGeometry(final Shape shape, final GeometryFactory factory); static Envelope2D getEnvelope2D(
            final Envelope envelope, final CoordinateReferenceSystem crs); static Point toGeometry(DirectPosition position); static Point toGeometry(DirectPosition position, GeometryFactory factory); static Polygon toGeometry(Envelope envelope); static Polygon toGeometry(final Envelope envelope, GeometryFactory factory); static ReferencedEnvelope toEnvelope(Geometry geom); static Polygon toGeometry(ReferencedEnvelope bbox); static Polygon toGeometry(BoundingBox bbox, GeometryFactory factory, int npoints); static Geometry toGeographic(Geometry geom, final CoordinateReferenceSystem crs); static Polygon toGeometry(BoundingBox bbox); static Polygon toGeometry(BoundingBox bbox, final GeometryFactory factory); static void checkCoordinatesRange(Geometry geom, CoordinateReferenceSystem crs); static Geometry smooth(final Geometry geom, double fit); static Geometry smooth(final Geometry geom, double fit, final GeometryFactory factory); static CoordinateSequence createCS(
            CoordinateSequenceFactory csFactory, int size, int dimension); static CoordinateSequence createCS(
            CoordinateSequenceFactory csFactory, int size, int dimension, int measures); static ReferencedEnvelope bounds(Geometry geometry, CoordinateReferenceSystem crs); static Geometry removeCollinearVertices(final Geometry g); static Geometry removeCollinearVertices(final Geometry geometry, int minPoints); static List<Polygon> makeValid(Polygon polygon, boolean removeHoles); static Rectangle2D toRectangle2D(Envelope envelope); static Envelope toEnvelope(Rectangle2D rectangle); static Polygon toPolygon(java.awt.Polygon polygon); static Polygon toPolygon(java.awt.Rectangle rectangle); static Polygon toPolygon(Rectangle2D rectangle); static boolean equals(Envelope e1, Envelope e2, double tolerance); static boolean equals(BoundingBox a, BoundingBox b, double tolerance); static final AffineTransformation Y_INVERSION; }