java - Geotools 解决方案读取 EPSG3035 中的 shapefile 以在 WGS84 中获取长/纬度?

标签 java coordinates gis shapefile geotools

shapefile 坐标在 EPSG3035 中,我需要在常规长纬度坐标中读取它们。

我如何使用 Geotools 做到这一点?

我的代码,目前没有任何转换:

ShapefileDataStore dataStore = new ShapefileDataStore(file.toURL());
ContentFeatureSource featureSource = dataStore.getFeatureSource();
ContentFeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureIterator iterator = featureCollection.features();

while (iterator.hasNext()) {
   SimpleFeature feature = iterator.next();
   Collection<Property> properties = feature.getProperties();
   etc...
}

谢谢!

最佳答案

未测试,但应该可以。查看我的内联评论:

ShapefileDataStore dataStore = new ShapefileDataStore(file.toURL());
ContentFeatureSource featureSource = dataStore.getFeatureSource();
ContentFeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureIterator iterator = featureCollection.features();

// get dynamically the CRS of your data:
SimpleFeatureType schema = featureSource.getSchema();
CoordinateReferenceSystem sourceCRS = schema.getCoordinateReferenceSystem();

// OR fallback to hardcoded 3035 if the above fails:
// CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:3035")

CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326") // the coordinates system you want to reproject the data to
// define a MathTransform object
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);


while (iterator.hasNext()) {
   SimpleFeature feature = iterator.next();
   Collection<Property> properties = feature.getProperties();

   // get the geometry of the actual feature
   Geometry sourceGeometry = feature.getDefaultGeometry()
   // transform the geometry and save it in a new variable
   Geometry reprojectedGeometry = JTS.transform(sourceGeometry, transform)
   // set the reprojected geometry as the geometry of the actual feature
   feature.setDefaultGeometry(reprojectedGeometry)
   // .....
}

有关详细信息,请参阅本教程:Geometry CRS Tutorial

关于java - Geotools 解决方案读取 EPSG3035 中的 shapefile 以在 WGS84 中获取长/纬度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30655286/

相关文章:

python - 使用Sqlite函数InitSpatialMetaData()通过Python创建Spatialite数据库的性能问题

java - 创建文本/纯 Jersey 响应

java - xpath表达式中如何使用逻辑运算符

java - 为什么我用的是? : conditional operator incorrect?

java - android中的屏幕镜像

java - 坐标 - 查看给定的 X、Y 是否在正方形区域内,如果不在,则找到最近的位置

mysql - 为什么使用 "Great-circle distance"公式和 Google 地球工具计算空间中两点之间的距离时会得到不同的值?

python - 测量纬度/经度坐标和 utm 坐标之间的距离

Java:坐标变换 - 旋转和缩放

mapping - 在英国,如何根据 GPS 坐标找到地址?