2

I have created a function that reads a shapefile file and I get its geographic coordinates well. This works fine. My problem is that I need those coordinates in wgs84 format (latitude, longitude), but I cannot get it to work.

I used a 'Query' object, but its setCoordinateSystem method does not do anything.

This is my code:

public Geometry getGeometryDistrict(String refCatDistrict, String shapeFileDistrict){

                Geometry union = null;

                File shapeFile = new File (shapeFileDistrict);

                try {

                   Query query = new Query();
                   query.setCoordinateSystem(DefaultGeographicCRS.WGS84);

                   Filter filter = CQL.toFilter("obj_co_id = '"+refCatDistrict+"'");
                   query.setFilter(filter);

                   FileDataStore storeDistrict = FileDataStoreFinder.getDataStore(shapeFile);

                   SimpleFeatureSource featureSource = storeDistrict.getFeatureSource();

                   SimpleFeatureCollection collection = featureSource.getFeatures(query);
                   SimpleFeature fDistrict = null;

                   ArrayList<Geometry> geometries = new ArrayList<>();

                   SimpleFeatureIterator itrDistrict = collection.features();

                   while(itrDistrict.hasNext()){

                      fDistrict = itrDistrict.next();

                      Geometry geomDistrict = (Geometry)fDistrict.getDefaultGeometry();
                      geometries.add(geomDistrict);

                   }

                   GeometryFactory factory = new GeometryFactory();

                   GeometryCollection geometryCollection = (GeometryCollection) factory.buildGeometry( geometries );

                   union = geometryCollection.union();

                   itrDistrict.close();
                   storeDistrict.dispose();

               }catch(IOException | NoSuchElementException | CQLException e){
                   Throwable cause = e.getCause();
                   System.out.println(cause);
               }

                return union;
            }

What should I change?

  • @RichardMorgan that is a good answer but not actually needed here. – Ian Turton Jun 12 at 15:03
1

You need to use Query.setCoordinateSystemReproject(CRS) not Query.setCoordinateSystem(CRS). One changes the output coordinates and the other tells the system what CRS the coordinates of any geometries in the query are in.

This code fetches the boundary of Pennsylvania in EPSG:2272.

public static void main(String[] args)
      throws IOException, CQLException, NoSuchAuthorityCodeException, FactoryException {
    File states = new File("/home/ian/Data/states/states.shp");
    FileDataStore ds = FileDataStoreFinder.getDataStore(states);
    Query query = new Query();
    query.setCoordinateSystemReproject(CRS.decode("EPSG:2272"));

    Filter filter = CQL.toFilter("STATE_ABBR = 'PA'");
    query.setFilter(filter);
    SimpleFeatureCollection collection = ds.getFeatureSource().getFeatures(query);
    try (SimpleFeatureIterator itr = collection.features()) {
      while (itr.hasNext()) {
        SimpleFeature feature = itr.next();
        System.out.println(feature.getDefaultGeometry());

      }
    }
  }

giving

MULTIPOLYGON (((2045557.7266281387 140806.06587454624, 1871110.379503949 143004.87968818948, 1803993.1961730379...

despite the input file being in WGS84 degrees.

  • Thanks for answering. I also tried with the setCoordinateSystemReproject method, but it shows me this runtime error:" java.io.IOException: Can not reproject data, the source CRS is not available". – Mimmetico Jun 12 at 15:29
  • then you need to fix your shapefile (make sure there is a .prj file), if you know the projection you can download the contents at epsg.io – Ian Turton Jun 12 at 16:13
  • I think that is precisely my problem. In the file structure there is no .prj file (in other projects i worked it does exist), but not in this one. My file structure had only three files extensions: .dbf, .shp, .shx... but no .prj – Mimmetico Jun 12 at 16:17
  • In case of not having a .prj file it could be generated in some way, or how could it be converted geometries from utm to wgs84 without that .prj file? – Mimmetico Jun 12 at 16:57
  • as I said you can generate a .prj file by going to epsg.io and looking for the UTM zone your data is in and downloading the OGC WKT file - for example if you need UTM 30N you would get epsg.io/32630.wkt and save it as "filename".prj – Ian Turton Jun 13 at 7:28

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.