java-Geotools:wgs84中缓冲区的边界框
作者:互联网
我需要一个Java函数,它将在缓冲区周围生成一个边界框(矩形).缓冲区由中心点(WGS84坐标)和半径(以米为单位)定义.
在JTS中为缓冲区获取边界框似乎非常简单:
Point center = ....
Geometry boundingBox = center.buffer(...).getEnvelope();
但是,这是纯平面几何形状.有没有一种方法可以使用以米为单位的距离的坐标参考系?
最佳搭配Geotools使用,但其他Java解决方案也可以使用…
解决方法:
尽管您以其他方式进行了处理,但我对此有另一种解决方案.结果将比您提出的解决方案更加精确.
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);
}
这是结果的地图,红色为缓冲区,黑色为信封.
标签:geospatial,jts,geotools,java 来源: https://codeday.me/bug/20191118/2031216.html