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

时间:2015-06-04 22:27:30

标签: 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...
}

谢谢!

1 个答案:

答案 0 :(得分:3)

未经测试,但应该可以使用。请参阅我的评论内联:

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