문제

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

도움이 되었습니까?

해결책 2

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();

다른 팁

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top