0

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:

enter image description here

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:

enter image description here

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...

  • 1
    Your input data is almost certainly not in 4326 then. Try 3857 instead – Ian Turton Oct 10 '17 at 15:46
  • Negative, I checked the original dataset, it is in 4326. Btw, if it was not, it would not overlap perfeclty with the basemap in the second image. Thank you for the thought though! – Mr M Oct 10 '17 at 20:03
  • please add a link to the shapefile – Ian Turton Oct 11 '17 at 11:00
2

So, I figured out the issue. One has to define the order of the axis. This means that it has to be defined if the latitude or longitude appears first in a coordinate of the Well-known-text (WKT). This has to be set when defining the source Coordinate Reference System:

CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326",true);

This states that the longitude is followed by the latitude.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.