org.locationtech.jts.geom.Geometry.isValid()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(428)

本文整理了Java中org.locationtech.jts.geom.Geometry.isValid()方法的一些代码示例,展示了Geometry.isValid()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Geometry.isValid()方法的具体详情如下:
包路径:org.locationtech.jts.geom.Geometry
类名称:Geometry
方法名:isValid

Geometry.isValid介绍

[英]Tests whether this Geometry is topologically valid, according to the OGC SFS specification.

For validity rules see the Javadoc for the specific Geometry subclass.
[中]根据OGC SFS规范,测试此Geometry是否在拓扑上有效。
有关有效性规则,请参阅特定几何体子类的Javadoc。

代码示例

代码示例来源:origin: geotools/geotools

  1. public static boolean isValid(Geometry arg0) {
  2. if (arg0 == null) return false;
  3. Geometry _this = arg0;
  4. return _this.isValid();
  5. }

代码示例来源:origin: geotools/geotools

  1. public boolean isValid() {
  2. return geometry.isValid();
  3. }

代码示例来源:origin: geotools/geotools

  1. @DescribeProcess(
  2. title = "Valid Test",
  3. description = "Tests if a geometry is topologically valid."
  4. )
  5. @DescribeResult(description = "True if the input is valid")
  6. public static boolean isValid(
  7. @DescribeParameter(name = "geom", description = "Input geometry") Geometry geom) {
  8. return geom.isValid();
  9. }

代码示例来源:origin: locationtech/spatial4j

  1. /**
  2. * This "pages" through standard geo boundaries offset by multiples of 360
  3. * longitudinally that intersect geom, and the intersecting results of a page
  4. * and the geom are shifted into the standard -180 to +180 and added to a new
  5. * geometry that is returned.
  6. */
  7. private static Geometry cutUnwrappedGeomInto360(Geometry geom) {
  8. Envelope geomEnv = geom.getEnvelopeInternal();
  9. if (geomEnv.getMinX() >= -180 && geomEnv.getMaxX() <= 180)
  10. return geom;
  11. assert geom.isValid() : "geom";
  12. //TODO opt: support geom's that start at negative pages --
  13. // ... will avoid need to previously shift in unwrapDateline(geom).
  14. List<Geometry> geomList = new ArrayList<Geometry>();
  15. //page 0 is the standard -180 to 180 range
  16. for (int page = 0; true; page++) {
  17. double minX = -180 + page * 360;
  18. if (geomEnv.getMaxX() <= minX)
  19. break;
  20. Geometry rect = geom.getFactory().toGeometry(new Envelope(minX, minX + 360, -90, 90));
  21. assert rect.isValid() : "rect";
  22. Geometry pageGeom = rect.intersection(geom);//JTS is doing some hard work
  23. assert pageGeom.isValid() : "pageGeom";
  24. shiftGeomByX(pageGeom, page * -360);
  25. geomList.add(pageGeom);
  26. }
  27. return UnaryUnionOp.union(geomList);
  28. }

代码示例来源:origin: mapsforge/mapsforge

  1. if (!geometry.isValid()) {
  2. if (!geometry.isValid()) {
  3. LOGGER.warning("invalid geometry even after attempt to fix " + way.getId());
  4. if ((ret instanceof Polygon || ret instanceof MultiPolygon) && !ret.isValid()) {
  5. LOGGER.warning("clipped way is not valid, trying to repair it: " + way.getId());
  6. ret = JTSUtils.repairInvalidPolygon(ret);

代码示例来源:origin: geotools/geotools

  1. private static Geometry pointInGeometry(Geometry g) {
  2. Point p = g.getCentroid();
  3. if (g instanceof Polygon) {
  4. // if the geometry is heavily generalized centroid computation may fail and return NaN
  5. if (Double.isNaN(p.getX()) || Double.isNaN(p.getY()))
  6. return g.getFactory().createPoint(g.getCoordinate());
  7. // otherwise let's check if the point is inside. Again, this check and
  8. // "getInteriorPoint"
  9. // will work only if the geometry is valid
  10. if (g.isValid() && !g.contains(p)) {
  11. try {
  12. p = g.getInteriorPoint();
  13. } catch (Exception e) {
  14. // generalized geometries might make interior point go bye bye
  15. return p;
  16. }
  17. } else {
  18. return p;
  19. }
  20. }
  21. return p;
  22. }

代码示例来源:origin: mapsforge/mapsforge

  1. @Test
  2. public void testBuildGeometryFromInValidPolygon() {
  3. // Some of these tests do not really make sense, as not everything that is a closed line
  4. // should be a polygon in OSM.
  5. String testfile = "invalid-polygon.wkt";
  6. // String expectedfile = "invalid-polygon-repaired.wkt";
  7. List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile);
  8. Geometry geometry = JTSUtils.toJtsGeometry(ways.get(0), ways.subList(1, ways.size()));
  9. Assert.isTrue(geometry instanceof LineString);
  10. Assert.isTrue(geometry.isValid());
  11. }

代码示例来源:origin: mapsforge/mapsforge

  1. @Test
  2. public void testBuildGeometryFromValidPolygon() {
  3. String testfile = "valid-polygon.wkt";
  4. List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile);
  5. Geometry geometry = JTSUtils.toJtsGeometry(ways.get(0), ways.subList(1, ways.size()));
  6. Assert.isTrue(geometry instanceof LineString);
  7. Assert.isTrue(geometry.isValid());
  8. }

代码示例来源:origin: mapsforge/mapsforge

  1. @Test
  2. public void testBuildGeometryFromInValidPolygonWithHoles() {
  3. String testfile = "invalid-polygon-2-inner-rings.wkt";
  4. String expectedfile = "invalid-polygon-2-inner-rings-repaired.wkt";
  5. List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile);
  6. Geometry geometry = JTSUtils.toJtsGeometry(ways.get(0), ways.subList(1, ways.size()));
  7. Assert.isTrue(geometry instanceof Polygon);
  8. Assert.isTrue(geometry.isValid());
  9. Geometry expected = MockingUtils.readWKTFile(expectedfile);
  10. Assert.equals(expected, geometry);
  11. }

代码示例来源:origin: geotools/geotools

  1. if (!geom.isValid()) {
  2. String message = "Not a valid geometry. isValid() failed";
  3. LOGGER.log(Level.FINEST, getName() + "(" + feature.getID() + "):" + message);

代码示例来源:origin: geotools/geotools

  1. boolean isValid = g.isValid();
  2. } else if (roi instanceof ROIGeometry) {
  3. g = ((ROIGeometry) roi).getAsGeometry();
  4. isValid = g.isValid();
  5. if (!isValid) {
  6. double tol = GeometrySnapper.computeOverlaySnapTolerance(g);

代码示例来源:origin: geotools/geotools

  1. private Geometry simpleOffsetTest(Geometry geom, final double offsetDistance) {
  2. Geometry offset = offset(geom, offsetDistance);
  3. assertTrue(offset.isValid());
  4. assertTrue(offset.getLength() > 0);
  5. assertEquals(abs(offsetDistance), offset.distance(geom), EPS * abs(offsetDistance));

代码示例来源:origin: geotools/geotools

  1. @Test
  2. public void testSelfIntersectRight() throws Exception {
  3. Geometry geom = geometry("LINESTRING(0 0, 10 0, 10 -10, 3 -10, 3 3)");
  4. Geometry offset = offset(geom, -1);
  5. assertTrue(offset.isValid());
  6. assertTrue(offset.getLength() > 0);
  7. // the offset line intersects the original one, because it's also self intersecting, so we
  8. // cannot have this test
  9. // assertEquals(2, offset.distance(geom), EPS);
  10. assertEquals(geometry("LINESTRING (0 -1, 9 -1, 9 -9, 4 -9, 4 3)"), offset);
  11. }

代码示例来源:origin: geotools/geotools

  1. } catch (TopologyException e) {
  2. try {
  3. if (((g instanceof Polygon) || (g instanceof MultiPolygon)) && (!g.isValid())) {

代码示例来源:origin: geotools/geotools

  1. @Test
  2. public void testElongatedLoopGenerator() throws Exception {
  3. Geometry geom = geometry("LINESTRING(0 0, 5 0, 5 -10, 7 -10, 7 0, 10 0)");
  4. Geometry offset = offset(geom, 1.5);
  5. assertTrue(offset.isValid());
  6. assertTrue(offset.getLength() > 0);
  7. // this one "fails", but the output cannot be really called wrong anymore, if we are trying
  8. // to
  9. // offset a road at least
  10. Geometry expected =
  11. geometry(
  12. "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)");
  13. assertTrue(expected.equalsExact(offset, 0.1));
  14. }

代码示例来源:origin: geotools/geotools

  1. @Test
  2. public void testSelfIntersectLeft() throws Exception {
  3. Geometry geom = geometry("LINESTRING(0 0, 10 0, 10 -10, 3 -10, 3 3)");
  4. Geometry offset = offset(geom, 2);
  5. assertTrue(offset.isValid());
  6. assertTrue(offset.getLength() > 0);
  7. // the offset line intersects the original one, because it's also self intersecting, so we
  8. // cannot have this test
  9. // assertEquals(2, offset.distance(geom), EPS);
  10. Geometry expected =
  11. geometry(
  12. "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)");
  13. assertTrue(expected.equalsExact(offset, 0.1));
  14. }

代码示例来源:origin: geotools/geotools

  1. @Test
  2. public void testWrapGeometryLatLonMultipleTimes() throws Exception {
  3. ReferencedEnvelope renderingEnvelope =
  4. new ReferencedEnvelope(-90, 90, -580, 540, ED50_LATLON);
  5. // a geometry close to the dateline
  6. Geometry g = new WKTReader().read("POLYGON((-74 -33, -29 -33, -29 5, -74 5, -74 -33))");
  7. // make sure the geometry is not wrapped, but it is preserved
  8. ProjectionHandler handler =
  9. ProjectionHandlerFinder.getHandler(renderingEnvelope, WGS84, true);
  10. assertTrue(handler.requiresProcessing(g));
  11. Geometry preProcessed = handler.preProcess(g);
  12. MathTransform mt = handler.getRenderingTransform(CRS.findMathTransform(WGS84, ED50_LATLON));
  13. Geometry transformed = JTS.transform(preProcessed, mt);
  14. // post process (provide identity transform to force wrap heuristic)
  15. Geometry postProcessed = handler.postProcess(mt, transformed);
  16. assertTrue(postProcessed.isValid());
  17. // should have been replicated three times
  18. assertEquals(3, postProcessed.getNumGeometries());
  19. }

代码示例来源:origin: geotools/geotools

  1. @Test
  2. public void testWrapAnctartica() throws Exception {
  3. ReferencedEnvelope world = new ReferencedEnvelope(-80, 80, -180, 180, ED50_LATLON);
  4. // make sure the geometry is not wrapped, but it is preserved
  5. ProjectionHandler handler = ProjectionHandlerFinder.getHandler(world, WGS84, true);
  6. // a geometry that will cross the dateline and sitting in the same area as the
  7. // rendering envelope (with wgs84 lon/latcoordinates)
  8. String wkt = "POLYGON((180 -90, 180 90, -180 90, -180 -90, 180 -90))";
  9. Geometry g = new WKTReader().read(wkt);
  10. MathTransform mt = CRS.findMathTransform(WGS84, ED50_LATLON);
  11. MathTransform prepared = handler.getRenderingTransform(mt);
  12. assertTrue(handler.requiresProcessing(g));
  13. Geometry preProcessed = handler.preProcess(g);
  14. Geometry reprojected = JTS.transform(preProcessed, prepared);
  15. assertTrue(reprojected.isValid());
  16. reprojected.apply(
  17. new CoordinateFilter() {
  18. @Override
  19. public void filter(Coordinate coord) {
  20. assertEquals(90.0, Math.abs(coord.getOrdinate(0)), 0.1);
  21. assertEquals(180.0, Math.abs(coord.getOrdinate(1)), 5);
  22. }
  23. });
  24. // post process, this should wrap the geometry, make sure it's valid, and avoid large jumps
  25. // in its border
  26. Geometry postProcessed = handler.postProcess(prepared, reprojected);
  27. assertThat(postProcessed, CoreMatchers.instanceOf(MultiPolygon.class));
  28. assertEquals(2, postProcessed.getNumGeometries());
  29. }

代码示例来源:origin: locationtech/jts

  1. public GeometryOperationValidator testValid() throws Exception {
  2. Assert.assertTrue("simplified geometry is not valid", ioGeometry[1]
  3. .isValid());
  4. return this;
  5. }

代码示例来源:origin: locationtech/jts

  1. public void checkValid(String name, Geometry g)
  2. {
  3. System.out.println("Running " + name);
  4. Stopwatch sw = new Stopwatch();
  5. boolean isValid = g.isValid();
  6. System.out.println("Is Valid = " + isValid
  7. + " Time: " + sw.getTimeString() );
  8. }

相关文章