Search code examples
javashapefilegeotools

Read Shapefile extent with Geotools


Is there any possibility to get the extent of a Shapefile using the GeoTools library? I want to read the TOP, LEFT, BOTTOM, RIGHT coordinates from the SHP.

Seems like there is no kind of getExtent() method...


Solution

  • I haven't compiled the following but it should work.

    File file = new File("example.shp");
    Map map = new HashMap();
    map.put( "url", file.toURL() );
    DataStore dataStore = DataStoreFinder.getDataStore(map);
    
    SimpleFeatureSource featureSource = dataStore.getFeatureSource( typeName );
    SimpleFeatureCollection collection = featureSource.getFeatures();
    
    ReferencedEnvelope env = collection.getBounds();
    double left = env.getMinX();
    double right = env.getMaxX();
    double top = env.getMaxY();
    double bottom = env.getMinY();