The shapefile coordinates are in EPSG3035, and I need to read them in regular long lat coordinates.
How can I do that with 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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.