Geotools:wgs84 中缓冲区的边界框

dap*_*hez 5 java jts geospatial geotools

我需要一个 Java 函数来在缓冲区周围生成一个边界框(矩形)。缓冲区由中心点(WGS84 坐标)和半径(以米为单位)定义。

在 JTS 中获取缓冲区的边界框似乎很简单:

Point center = ....
Geometry boundingBox = center.buffer(...).getEnvelope();
Run Code Online (Sandbox Code Playgroud)

然而,这是纯平面几何。有没有办法使用以米为单位给出距离的坐标参考系统来做到这一点?

最好使用 Geotools,但其他 Java 解决方案也可以使用...

Edu*_*rdo 5

尽管您以另一种方式处理它,但我有另一种解决方案。结果将比您提出的解决方案更加精确。

GeometryFactory GEOMETRY_FACTORY = JTSFactoryFinder.getGeometryFactory();

// Remember, order is (longitude, latitude)
Coordinate center = Coordinate(2.29443, 48.85816);
Point point = GEOMETRY_FACTORY.createPoint(center);

// Buffer 50KM around the point, then get the envelope
Envelope envelopeInternal = buffer(point, 50000).getEnvelopeInternal();

// Then you can play with the envelope, e.g.,
double minX = envelopeInternal.getMinX();
double maxX = envelopeInternal.getMaxX();

// The buffer using distanceInMeters
private Geometry buffer(Geometry geometry, double distanceInMeters) throws FactoryException, TransformException {
     String code = "AUTO:42001," + geometry.getCentroid().getCoordinate().x + "," + geometry.getCentroid().getCoordinate().y;
     CoordinateReferenceSystem auto = CRS.decode(code);

     MathTransform toTransform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
     MathTransform fromTransform = CRS.findMathTransform(auto, DefaultGeographicCRS.WGS84);

     Geometry pGeom = JTS.transform(geometry, toTransform);
     Geometry pBufferedGeom = pGeom.buffer(distanceInMeters);
     return JTS.transform(pBufferedGeom, fromTransform);
}
Run Code Online (Sandbox Code Playgroud)

这是结果图,缓冲区为红色,信封为黑色。

缓冲区和信封


dap*_*hez 1

我最终使用 aGeodeticCalculator手动找到盒子的角点。坦率地说,结果不是很精确,但这是我迄今为止找到的最佳解决方案:

 GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
 CoordinateReferenceSystem wgs84 = DefaultGeographicCRS.WGS84;
 GeodeticCalculator geodeticCalculator = new GeodeticCalculator(wgs84);
 geodeticCalculator.setStartingGeographicPoint(center.getX(), center.getY());
 Coordinate[] coordinates = new Coordinate[5];
 for (int i = 0; i < 4; i++) {
    geodeticCalculator.setDirection(-180 + i * 90 + 45, bufferRadiusMeters * Math.sqrt(2));
    Point2D point2D = geodeticCalculator.getDestinationGeographicPoint();
    coordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
 }
 coordinates[4] = coordinates[0];
 Polygon box = geometryFactory.createPolygon(coordinates);
Run Code Online (Sandbox Code Playgroud)