我需要一个Java函数,它将在缓冲区周围生成一个边界框(矩形)。缓冲区由中心点(WGS84坐标)和半径(以米为单位)定义。
在JTS中获取缓冲区的边界框似乎很简单:
Point center = ....
Geometry boundingBox = center.buffer(...).getEnvelope();
然而,这是纯平面几何。有没有办法使用坐标参考系统以米为单位给出距离?
最好使用Geotools,但其他Java解决方案也可以工作......
答案 0 :(得分:2)
虽然你已经以另一种方式接近它,但我还有另一种解决方案。结果将比您提出的解决方案更精确。
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);
}
这是带有结果的地图,红色的缓冲区,黑色的信封。
答案 1 :(得分:1)
我最终使用GeodeticCalculator
手动查找方框的角落。坦率地说,结果并不十分准确,但这是我迄今为止找到的最佳解决方案:
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);
答案 2 :(得分:0)
这是一个简单的解决方案,我用它来生成与GeoNames citieJSON API一起使用的边界框坐标,以便从gps十进制坐标获取附近的大城市。
这是我的GitHub存储库中的Java方法:FusionTableModifyJava
我有一个十进制GPS位置,我需要找到该位置附近最大的城市/州。我需要一个相对准确的边界框来传递到citiesJSON GeoNames webservice以获取该边界框中最大的城市。我传递了我感兴趣的位置和“半径”(以km为单位),它返回了传递给citiesJSON所需的北,南,东,西十进制坐标。
(我发现这些资源对我的研究很有用:
Calculate distance, bearing and more between Latitude/Longitude points.
它不是超级准确,但足够准确,我正在使用它:
// Compute bounding Box coordinates for use with Geonames API.
class BoundingBox
{
public double north, south, east, west;
public BoundingBox(String location, float km)
{
//System.out.println(location + " : "+ km);
String[] parts = location.replaceAll("\\s","").split(","); //remove spaces and split on ,
double lat = Double.parseDouble(parts[0]);
double lng = Double.parseDouble(parts[1]);
double adjust = .008983112; // 1km in degrees at equator.
//adjust = 0.008983152770714983; // 1km in degrees at equator.
//System.out.println("deg: "+(1.0/40075.017)*360.0);
north = lat + ( km * adjust);
south = lat - ( km * adjust);
double lngRatio = 1/Math.cos(Math.toRadians(lat)); //ratio for lng size
//System.out.println("lngRatio: "+lngRatio);
east = lng + (km * adjust) * lngRatio;
west = lng - (km * adjust) * lngRatio;
}
}