Java 如何在geoTools中使用GeometricShapeFactory在地图上创建圆
我目前正在使用下面的代码创建GeoJson多边形。这给了我一个无效的坏循环 在这种情况下,半径=1609.34,以米为单位为1英里Java 如何在geoTools中使用GeometricShapeFactory在地图上创建圆,java,latitude-longitude,geotools,Java,Latitude Longitude,Geotools,我目前正在使用下面的代码创建GeoJson多边形。这给了我一个无效的坏循环 在这种情况下,半径=1609.34,以米为单位为1英里 public GeoJsonPolygon createRadiusPolygon( Point point,double RADIUS) { GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
public GeoJsonPolygon createRadiusPolygon( Point point,double RADIUS) {
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(32);
shapeFactory.setCentre(new com.vividsolutions.jts.geom.Coordinate(point.getX(), point.getY()));
shapeFactory.setSize(RADIUS * 2);
com.vividsolutions.jts.geom.Geometry circle = shapeFactory.createCircle();
List<Point> points = new ArrayList<Point>();
for (com.vividsolutions.jts.geom.Coordinate coordinate : circle.getCoordinates()) {
Point lngLatAtl = new Point(coordinate.x, coordinate.y);
points.add(lngLatAtl);
}
Collections.reverse(points);
return new GeoJsonPolygon(points);
}
…您的输出圆是有效的,它恰好超过了地球表面的直径,因此您的GIS在绘制它时可能会遇到问题!问题是你不分青红皂白地混合了度数和米数,而GeoTools根本不知道你想要它做什么 您需要向程序中添加一些有关点坐标参考系的信息,如果该投影是地理投影,即以度为单位,则将问题转换为以米为单位的投影
public Geometry bufferPoint(Measure<Double, Length> distance, CoordinateReferenceSystem origCRS, Geometry geom) {
Geometry pGeom = geom;
MathTransform toTransform, fromTransform = null;
// reproject the geometry to a local projection
Unit<Length> unit = distance.getUnit();
if (!(origCRS instanceof ProjectedCRS)) {
double x = geom.getCoordinate().x;
double y = geom.getCoordinate().y;
String code = "AUTO:42001," + x + "," + y;
// System.out.println(code);
CoordinateReferenceSystem auto;
try {
auto = CRS.decode(code);
toTransform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
fromTransform = CRS.findMathTransform(auto, DefaultGeographicCRS.WGS84);
pGeom = JTS.transform(geom, toTransform);
unit = SI.METER;
} catch (MismatchedDimensionException | TransformException | FactoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
unit = (Unit<Length>) origCRS.getCoordinateSystem().getAxis(0).getUnit();
}
// buffer
Geometry out = pGeom.buffer(distance.doubleValue(unit));
Geometry retGeom = out;
// reproject the geometry to the original projection
if (!(origCRS instanceof ProjectedCRS)) {
try {
retGeom = JTS.transform(out, fromTransform);
} catch (MismatchedDimensionException | TransformException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return retGeom;
}
您的输出圆是有效的,它刚好超过地球表面的直径,因此您的GIS可能在绘制它时遇到问题!问题是你不分青红皂白地混合了度数和米数,而GeoTools根本不知道你想要它做什么 您需要向程序中添加一些有关点坐标参考系的信息,如果该投影是地理投影,即以度为单位,则将问题转换为以米为单位的投影
public Geometry bufferPoint(Measure<Double, Length> distance, CoordinateReferenceSystem origCRS, Geometry geom) {
Geometry pGeom = geom;
MathTransform toTransform, fromTransform = null;
// reproject the geometry to a local projection
Unit<Length> unit = distance.getUnit();
if (!(origCRS instanceof ProjectedCRS)) {
double x = geom.getCoordinate().x;
double y = geom.getCoordinate().y;
String code = "AUTO:42001," + x + "," + y;
// System.out.println(code);
CoordinateReferenceSystem auto;
try {
auto = CRS.decode(code);
toTransform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
fromTransform = CRS.findMathTransform(auto, DefaultGeographicCRS.WGS84);
pGeom = JTS.transform(geom, toTransform);
unit = SI.METER;
} catch (MismatchedDimensionException | TransformException | FactoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
unit = (Unit<Length>) origCRS.getCoordinateSystem().getAxis(0).getUnit();
}
// buffer
Geometry out = pGeom.buffer(distance.doubleValue(unit));
Geometry retGeom = out;
// reproject the geometry to the original projection
if (!(origCRS instanceof ProjectedCRS)) {
try {
retGeom = JTS.transform(out, fromTransform);
} catch (MismatchedDimensionException | TransformException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return retGeom;
}
谢谢@iant。。您用于获取输出的“距离”是否以米为单位?……在OP中。如果需要,该方法的实施要点是什么?JTS点@-73.87,40.84,CRS是CRS。DecodePSG:4326;对于Measure?,您是否可以剪切并粘贴此方法“调用”的代码行:Measure dist=Measure.value1.0,NonSI.MILE;版本18-SNAPSHOT,但应适用于所有最新版本谢谢@iant。。您用于获取输出的“距离”是否以米为单位?……在OP中。如果需要,该方法的实施要点是什么?JTS点@-73.87,40.84,CRS是CRS。DecodePSG:4326;对于Measure?,您是否可以剪切并粘贴此方法“调用”的代码行:Measure dist=Measure.value1.0,NonSI.MILE;版本18-SNAPSHOT,但应适用于所有最新版本。请注意,如果您使用的是来自JTS com.livitsolutions.JTS.geom或org.locationtech.JTS.geom的坐标类,那么X对应于经度,Y对应于纬度。在上面的帖子中,你需要像这样交换参数:新的坐标系,纬度这救了我的命!请注意,如果您使用的是来自JTS com.livitsolutions.JTS.geom或org.locationtech.JTS.geom的坐标类,那么X对应于经度,Y对应于纬度。在上面的帖子中,你需要像这样交换参数:新的坐标系,纬度这救了我的命!
//Measure<Double, Length> dist = Measure.valueOf(50.0, SI.KILOMETER);
Measure<Double, Length> dist = Measure.valueOf(1.0, NonSI.MILE);
GeometryFactory gf = new GeometryFactory();
Point p = gf.createPoint(new Coordinate(-73.87,40.84));
buf.bufferPoint(dist, DefaultGeographicCRS.WGS84, p);
double latitude = 40.689234d;
double longitude = -74.044598d;
double diameterInMeters = 2000d; //2km
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(64); // adjustable
shapeFactory.setCentre(new Coordinate(latitude, longitude));
// Length in meters of 1° of latitude = always 111.32 km
shapeFactory.setWidth(diameterInMeters/111320d);
// Length in meters of 1° of longitude = 40075 km * cos( latitude ) / 360
shapeFactory.setHeight(diameterInMeters / (40075000 * Math.cos(Math.toRadians(latitude)) / 360));
Polygon circle = shapeFactory.createEllipse();