Read Shapefile extent with Geotools

manuSO picture manuSO · Jun 3, 2013 · Viewed 7.1k times · Source

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

Answer

manuSO picture manuSO · Jun 18, 2013

thank you, that's exactly what I was looking for! I only had to change the url map. this is the working code:

File file = new File(shpFilePath);
Map<String, URL> map = new HashMap<String, URL>();      
map.put("url", file.toURI().toURL());

DataStore dataStore = DataStoreFinder.getDataStore(map);

// SimpleFeatureSource featureSource = dataStore.getFeatureSource(shpName); this works too
SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);        
SimpleFeatureCollection collection = featureSource.getFeatures();

ReferencedEnvelope env = collection.getBounds();
double left = env.getMinX();
double right = env.getMaxX();
double top = env.getMaxY();
double bottom = env.getMinY();