I encountered a very odd result while reprojecting simplefeatures with Geotools: I have data of Vienna (Austria) from Open Street Maps in SRID:4326 (WGS84) in a shapefile. I load the data into a DefaultFeatureCollection. I then perform a reprojection into SRID 3035 by loading the Data from the DeafultFeatureCollection into a ReprojectingFeatureCollection:
ReprojectingFeatureCollection reprojFeatures = new ReprojectingFeatureCollection(wienfeatures,CRS.decode("EPSG:4326"),CRS.decode("EPSG:3035"));
I was very suprised to find that the resulting geodata was now located in North America, after reprojection:
I tried to maintain the SRID in order to make sure no failure was created while exporting the shape:
ReprojectingFeatureCollection(wienfeatures,CRS.decode("EPSG:4326"),CRS.decode("EPSG:4326"));
The resulting shape is identical to the input, as expected:
I finally tried to transform the features in a own function which takes the SRID and the SimpleFeatureCollection as an input:
public static SimpleFeatureCollection transform(Integer targetSRID, SimpleFeatureCollection source) throws NoSuchAuthorityCodeException, FactoryException, MismatchedDimensionException, TransformException
{
SimpleFeatureType schema = source.getSchema();
CoordinateReferenceSystem dataCRS = schema.getCoordinateReferenceSystem();
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:"+targetSRID.toString());
boolean lenient = false; // allow for some error due to different datums
MathTransform transform = CRS.findMathTransform(dataCRS, targetCRS, lenient);
//Create new featuretype
SimpleFeatureType newfeatureType = SimpleFeatureTypeBuilder.retype(schema, targetCRS);
// create a builder for it...
SimpleFeatureBuilder featurebuilder = new SimpleFeatureBuilder(newfeatureType);
SimpleFeatureIterator featiter = source.features();
DefaultFeatureCollection projcollection = new DefaultFeatureCollection();
while(featiter.hasNext())
{
SimpleFeature feature = featiter.next();
Geometry geometry = (Geometry) feature.getDefaultGeometry();
Geometry geometry2 = JTS.transform(geometry, transform);
featurebuilder.addAll(feature.getAttributes());
SimpleFeature projfeature = featurebuilder.buildFeature(null);
projfeature.setDefaultGeometry(geometry2);
//add SimpleFeature to final collection...
projcollection.add(projfeature);
}
featiter.close();
return projcollection;
}
However, this gave me exactly the same erroneous results like in the beginning. What is the problem here? Could it be that Geotools has an issue transforming??
P.S.: I tried to project my input data with QGIS... there were no problems...