gpt4 book ai didi

java - 使用JAVA中的geotools在定义的距离(km)内从一条线(GPS坐标)生成多边形

转载 作者:行者123 更新时间:2023-11-30 03:25:12 27 4
gpt4 key购买 nike

使用地理工具是否可以在定义的距离内从一条线(坐标[])生成多边形?例如。 (100,100)、(101,100)、(102,100),距离 1 公里。因此,它从每个点生成一个圆并成为某物。像:

--------------之前

(========)之后

或者我必须首先将 GPS 坐标转换为以公里为单位的正交坐标,然后使用三角函数生成多边形,最后将其转换回 GPS?

最佳答案

最简单的是,您只需调用底层 JTS 几何图形上的 buffer(distance) 方法即可。棘手的一点是处理以米为单位的投影的重投影(因此您可以以米或公里为单位指定缓冲区)。

public SimpleFeature bufferFeature(SimpleFeature feature,
Measure<Double, Length> distance) {
// extract the geometry
GeometryAttribute gProp = feature.getDefaultGeometryProperty();
CoordinateReferenceSystem origCRS = gProp.getDescriptor()
.getCoordinateReferenceSystem();

Geometry geom = (Geometry) feature.getDefaultGeometry();
Geometry pGeom = geom;
MathTransform toTransform,fromTransform = null;
// reproject the geometry to a local projection
if (!(origCRS instanceof ProjectedCRS)) {

Point c = geom.getCentroid();
double x = c.getCoordinate().x;
double y = c.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);
} catch (MismatchedDimensionException | TransformException
| FactoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

// buffer
Geometry out = buffer(pGeom, distance.doubleValue(SI.METER));
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 a new feature containing the geom
SimpleFeatureType schema = feature.getFeatureType();
SimpleFeatureTypeBuilder ftBuilder = new SimpleFeatureTypeBuilder();
ftBuilder.setCRS(origCRS);
//ftBuilder.setDefaultGeometry("buffer");
ftBuilder.addAll(schema.getAttributeDescriptors());
ftBuilder.setName(schema.getName());

SimpleFeatureType nSchema = ftBuilder.buildFeatureType();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(nSchema);
List<Object> atts = feature.getAttributes();
for(int i=0;i<atts.size();i++) {
if(atts.get(i) instanceof Geometry) {
atts.set(i, retGeom);
}
}
SimpleFeature nFeature = builder.buildFeature(null, atts.toArray() );
return nFeature;
}

完整代码位于https://gist.github.com/ianturton/9a7cfee378e7072ec3cd它展示了如何处理整个事情。

关于java - 使用JAVA中的geotools在定义的距离(km)内从一条线(GPS坐标)生成多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30426901/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com