4

is there a way to reproject a feature collection into a new one with another coordinate reference system? I am trying to achieve this by using geotools's reprojector:

SimpleFeatureSource featureSource = ...
SimpleFeatureCollection sfc = new ReprojectingFeatureCollection(featureSource.getFeatures(),
    org.geotools.referencing.CRS.decode("EPSG:4326"));

sfc presents another schema's CRS but its geometries looks the same before reprojecting. What am I missing? Do I have to manually create a new geometry with desired CRS and coordinates for each geometry in featureSource or geotools offer a more elegant and efficient way to do this? I am using geotools version 17.2.

1 Answer 1

5

This code works for me:

public static void main(String[] args) throws IOException, NoSuchAuthorityCodeException, FactoryException {
     if(args.length==0) {
       System.err.println("usage: Reprojector shapefile.shp");
       System.exit(1);
     }
     FileDataStore ds = FileDataStoreFinder.getDataStore(new File(args[0]));
     SimpleFeatureCollection features = ds.getFeatureSource().getFeatures();
     try(SimpleFeatureIterator itr=features.features()){
       int count=0;
       while(itr.hasNext()&&count++<10) {
         System.out.println(((Geometry) itr.next().getDefaultGeometry()).getCentroid());
       }
     }
     System.out.println("");
     ReprojectingFeatureCollection rfc = new ReprojectingFeatureCollection(features, CRS.decode("epsg:3875"));
     try(SimpleFeatureIterator itr=rfc.features()){
       int count=0;
       while(itr.hasNext()&&count++<10) {
         System.out.println(((Geometry) itr.next().getDefaultGeometry()).getCentroid());
       }
     }
  }

giving this output:

POINT (-69.97420075028728 12.516935269076281)
POINT (66.00844744953935 33.836266910437075)

POINT (7865991.086526802 4801048.897930129)
POINT (4827402.208410819 25805775.61452194)
6
  • Hello, I want to reproject EPSG:32632 features in EPSG:4326. As I run your code I obtain this point, POINT (626913.7451504259 5058702.588350934) correspond to this one: POINT (45.67025422985437 10.629381224435772) I think that point should looks like this: POINT (10.629381224435772 45.67025422985437) Am I wrong?
    – Francesco
    Commented Sep 19, 2017 at 16:22
  • that is correct - see docs.geotools.org/stable/userguide/library/referencing/…
    – Ian Turton
    Commented Sep 19, 2017 at 16:24
  • That was. Hours wasted because I forgot a system property.. Could be useful to mention that system property directly in your answer. I'll accept it.
    – Francesco
    Commented Sep 19, 2017 at 16:39
  • that system property is only relevant if you have swapped your axes by mistake in general learn to like lat,lon :-)
    – Ian Turton
    Commented Sep 19, 2017 at 16:47
  • Doesn't changing the wrapped Geometries in a collection work as well? So you don't have the need to create a new collection nor layer.
    – Campa
    Commented Aug 30, 2018 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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