public static boolean isValid(Geometry arg0) { if (arg0 == null) return false; Geometry _this = arg0; return _this.isValid(); }
public boolean isValid() { return geometry.isValid(); }
@DescribeProcess( title = "Valid Test", description = "Tests if a geometry is topologically valid." ) @DescribeResult(description = "True if the input is valid") public static boolean isValid( @DescribeParameter(name = "geom", description = "Input geometry") Geometry geom) { return geom.isValid(); }
/** * This "pages" through standard geo boundaries offset by multiples of 360 * longitudinally that intersect geom, and the intersecting results of a page * and the geom are shifted into the standard -180 to +180 and added to a new * geometry that is returned. */ private static Geometry cutUnwrappedGeomInto360(Geometry geom) { Envelope geomEnv = geom.getEnvelopeInternal(); if (geomEnv.getMinX() >= -180 && geomEnv.getMaxX() <= 180) return geom; assert geom.isValid() : "geom"; //TODO opt: support geom's that start at negative pages -- // ... will avoid need to previously shift in unwrapDateline(geom). List<Geometry> geomList = new ArrayList<Geometry>(); //page 0 is the standard -180 to 180 range for (int page = 0; true; page++) { double minX = -180 + page * 360; if (geomEnv.getMaxX() <= minX) break; Geometry rect = geom.getFactory().toGeometry(new Envelope(minX, minX + 360, -90, 90)); assert rect.isValid() : "rect"; Geometry pageGeom = rect.intersection(geom);//JTS is doing some hard work assert pageGeom.isValid() : "pageGeom"; shiftGeomByX(pageGeom, page * -360); geomList.add(pageGeom); } return UnaryUnionOp.union(geomList); }
if (!geometry.isValid()) { if (!geometry.isValid()) { LOGGER.warning("invalid geometry even after attempt to fix " + way.getId()); if ((ret instanceof Polygon || ret instanceof MultiPolygon) && !ret.isValid()) { LOGGER.warning("clipped way is not valid, trying to repair it: " + way.getId()); ret = JTSUtils.repairInvalidPolygon(ret);
private static Geometry pointInGeometry(Geometry g) { Point p = g.getCentroid(); if (g instanceof Polygon) { // if the geometry is heavily generalized centroid computation may fail and return NaN if (Double.isNaN(p.getX()) || Double.isNaN(p.getY())) return g.getFactory().createPoint(g.getCoordinate()); // otherwise let's check if the point is inside. Again, this check and // "getInteriorPoint" // will work only if the geometry is valid if (g.isValid() && !g.contains(p)) { try { p = g.getInteriorPoint(); } catch (Exception e) { // generalized geometries might make interior point go bye bye return p; } } else { return p; } } return p; }
@Test public void testBuildGeometryFromInValidPolygon() { // Some of these tests do not really make sense, as not everything that is a closed line // should be a polygon in OSM. String testfile = "invalid-polygon.wkt"; // String expectedfile = "invalid-polygon-repaired.wkt"; List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile); Geometry geometry = JTSUtils.toJtsGeometry(ways.get(0), ways.subList(1, ways.size())); Assert.isTrue(geometry instanceof LineString); Assert.isTrue(geometry.isValid()); }
@Test public void testBuildGeometryFromValidPolygon() { String testfile = "valid-polygon.wkt"; List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile); Geometry geometry = JTSUtils.toJtsGeometry(ways.get(0), ways.subList(1, ways.size())); Assert.isTrue(geometry instanceof LineString); Assert.isTrue(geometry.isValid()); }
@Test public void testBuildGeometryFromInValidPolygonWithHoles() { String testfile = "invalid-polygon-2-inner-rings.wkt"; String expectedfile = "invalid-polygon-2-inner-rings-repaired.wkt"; List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile); Geometry geometry = JTSUtils.toJtsGeometry(ways.get(0), ways.subList(1, ways.size())); Assert.isTrue(geometry instanceof Polygon); Assert.isTrue(geometry.isValid()); Geometry expected = MockingUtils.readWKTFile(expectedfile); Assert.equals(expected, geometry); }
if (!geom.isValid()) { String message = "Not a valid geometry. isValid() failed"; LOGGER.log(Level.FINEST, getName() + "(" + feature.getID() + "):" + message);
private Geometry simpleOffsetTest(Geometry geom, final double offsetDistance) { Geometry offset = offset(geom, offsetDistance); assertTrue(offset.isValid()); assertTrue(offset.getLength() > 0); assertEquals(abs(offsetDistance), offset.distance(geom), EPS * abs(offsetDistance));
@Test public void testSelfIntersectRight() throws Exception { Geometry geom = geometry("LINESTRING(0 0, 10 0, 10 -10, 3 -10, 3 3)"); Geometry offset = offset(geom, -1); assertTrue(offset.isValid()); assertTrue(offset.getLength() > 0); // the offset line intersects the original one, because it's also self intersecting, so we // cannot have this test // assertEquals(2, offset.distance(geom), EPS); assertEquals(geometry("LINESTRING (0 -1, 9 -1, 9 -9, 4 -9, 4 3)"), offset); }
} catch (TopologyException e) { try { if (((g instanceof Polygon) || (g instanceof MultiPolygon)) && (!g.isValid())) {
@Test public void testElongatedLoopGenerator() throws Exception { Geometry geom = geometry("LINESTRING(0 0, 5 0, 5 -10, 7 -10, 7 0, 10 0)"); Geometry offset = offset(geom, 1.5); assertTrue(offset.isValid()); assertTrue(offset.getLength() > 0); // this one "fails", but the output cannot be really called wrong anymore, if we are trying // to // offset a road at least Geometry expected = geometry( "LINESTRING (0 1.5, 5 1.5, 5.260472266500395 1.477211629518312, 5.513030214988503 1.4095389311788626, 5.75 1.299038105676658, 5.964181414529809 1.149066664678467, 6.149066664678467 0.9641814145298091, 6.299038105676658 0.7500000000000002, 6.409538931178862 0.5130302149885032, 6.477211629518312 0.2604722665003956, 6.5 0.0000000000000001, 6.5 -8.5, 5.5 -8.5, 5.5 0.0000000000000001, 5.522788370481688 0.2604722665003956, 5.590461068821138 0.5130302149885032, 5.700961894323342 0.7499999999999998, 5.850933335321533 0.9641814145298091, 6.035818585470191 1.149066664678467, 6.25 1.299038105676658, 6.486969785011497 1.4095389311788624, 6.739527733499605 1.477211629518312, 7 1.5, 10 1.5)"); assertTrue(expected.equalsExact(offset, 0.1)); }
@Test public void testSelfIntersectLeft() throws Exception { Geometry geom = geometry("LINESTRING(0 0, 10 0, 10 -10, 3 -10, 3 3)"); Geometry offset = offset(geom, 2); assertTrue(offset.isValid()); assertTrue(offset.getLength() > 0); // the offset line intersects the original one, because it's also self intersecting, so we // cannot have this test // assertEquals(2, offset.distance(geom), EPS); Geometry expected = geometry( "LINESTRING (0 2, 10 2, 10.34729635533386 1.969615506024416, 10.684040286651337 1.8793852415718169, 11 1.7320508075688774, 11.28557521937308 1.532088886237956, 11.532088886237956 1.2855752193730787, 11.732050807568877 1.0000000000000002, 11.879385241571816 0.6840402866513376, 11.969615506024416 0.3472963553338608, 12 0.0000000000000001, 12 -10, 11.969615506024416 -10.34729635533386, 11.879385241571818 -10.684040286651337, 11.732050807568877 -11, 11.532088886237956 -11.28557521937308, 11.28557521937308 -11.532088886237956, 11 -11.732050807568877, 10.684040286651339 -11.879385241571816, 10.34729635533386 -11.969615506024416, 10 -12, 2.9999999999999996 -12, 2.6527036446661394 -11.969615506024416, 2.3159597133486622 -11.879385241571816, 2 -11.732050807568877, 1.714424780626921 -11.532088886237956, 1.467911113762044 -11.28557521937308, 1.2679491924311228 -11, 1.1206147584281831 -10.684040286651337, 1.030384493975584 -10.34729635533386, 1 -10, 1 3)"); assertTrue(expected.equalsExact(offset, 0.1)); }
@Test public void testWrapGeometryLatLonMultipleTimes() throws Exception { ReferencedEnvelope renderingEnvelope = new ReferencedEnvelope(-90, 90, -580, 540, ED50_LATLON); // a geometry close to the dateline Geometry g = new WKTReader().read("POLYGON((-74 -33, -29 -33, -29 5, -74 5, -74 -33))"); // make sure the geometry is not wrapped, but it is preserved ProjectionHandler handler = ProjectionHandlerFinder.getHandler(renderingEnvelope, WGS84, true); assertTrue(handler.requiresProcessing(g)); Geometry preProcessed = handler.preProcess(g); MathTransform mt = handler.getRenderingTransform(CRS.findMathTransform(WGS84, ED50_LATLON)); Geometry transformed = JTS.transform(preProcessed, mt); // post process (provide identity transform to force wrap heuristic) Geometry postProcessed = handler.postProcess(mt, transformed); assertTrue(postProcessed.isValid()); // should have been replicated three times assertEquals(3, postProcessed.getNumGeometries()); }
@Test public void testWrapAnctartica() throws Exception { ReferencedEnvelope world = new ReferencedEnvelope(-80, 80, -180, 180, ED50_LATLON); // make sure the geometry is not wrapped, but it is preserved ProjectionHandler handler = ProjectionHandlerFinder.getHandler(world, WGS84, true); // a geometry that will cross the dateline and sitting in the same area as the // rendering envelope (with wgs84 lon/latcoordinates) String wkt = "POLYGON((180 -90, 180 90, -180 90, -180 -90, 180 -90))"; Geometry g = new WKTReader().read(wkt); MathTransform mt = CRS.findMathTransform(WGS84, ED50_LATLON); MathTransform prepared = handler.getRenderingTransform(mt); assertTrue(handler.requiresProcessing(g)); Geometry preProcessed = handler.preProcess(g); Geometry reprojected = JTS.transform(preProcessed, prepared); assertTrue(reprojected.isValid()); reprojected.apply( new CoordinateFilter() { @Override public void filter(Coordinate coord) { assertEquals(90.0, Math.abs(coord.getOrdinate(0)), 0.1); assertEquals(180.0, Math.abs(coord.getOrdinate(1)), 5); } }); // post process, this should wrap the geometry, make sure it's valid, and avoid large jumps // in its border Geometry postProcessed = handler.postProcess(prepared, reprojected); assertThat(postProcessed, CoreMatchers.instanceOf(MultiPolygon.class)); assertEquals(2, postProcessed.getNumGeometries()); }
public GeometryOperationValidator testValid() throws Exception { Assert.assertTrue("simplified geometry is not valid", ioGeometry[1] .isValid()); return this; }
public void checkValid(String name, Geometry g) { System.out.println("Running " + name); Stopwatch sw = new Stopwatch(); boolean isValid = g.isValid(); System.out.println("Is Valid = " + isValid + " Time: " + sw.getTimeString() ); }