[英]Geotools solution to read shapefile in EPSG3035 to get long / lat in WGS84?
The shapefile coordinates are in EPSG3035, and I need to read them in regular long lat coordinates. shapefile 坐标位于 EPSG3035 中,我需要以常规的长纬度坐标读取它们。
How can I do that with Geotools?我怎样才能用 Geotools 做到这一点?
My code, without any conversion at the moment:我的代码,目前没有任何转换:
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...
}
Thanks!谢谢!
Not tested, but so it should work. 没有测试,但它应该工作。 See my comments inline: 查看我的评论内联:
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)
// .....
}
For more info see this tutorial: Geometry CRS Tutorial 有关详细信息,请参阅本教程: 几何CRS教程
ShapefileDataStore SHPdataStore = new ShapefileDataStore(shpFile.toURI().toURL());
ContentFeatureSource featureSource = SHPdataStore.getFeatureSource();
ContentFeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureType schema = featureSource.getSchema();
MathTransform transform = null;
if(schema.getCoordinateReferenceSystem() != null) {
CoordinateReferenceSystem sourceCRS = schema.getCoordinateReferenceSystem();
CoordinateReferenceSystem targetCRS = CRS.decode(Constants.EPSG4326);
transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
}
try (SimpleFeatureIterator iterator = featureCollection.features()) {
SimpleFeature sfeature;
if(schema.getCoordinateReferenceSystem() != null) {
while (iterator.hasNext()) {
sfeature = iterator.next();
System.out.println(sfeature.getDefaultGeometry());
sfeature.setDefaultGeometry(JTS.transform((org.locationtech.jts.geom.Geometry)sfeature.getDefaultGeometry(), transform));
features.add(sfeature);
}
}
} finally {
SHPdataStore.dispose();
}
Don't forgot to use try block for SimpleFeatureIterator and also to dispose the store otherwise you will face issue when running for multiple iteratble inputs.不要忘记对 SimpleFeatureIterator 使用 try 块并处理存储,否则在运行多个可迭代输入时会遇到问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.