使用Geotools读取Shapefile范围

时间:2013-06-02 23:50:50

标签: java shapefile geotools

是否有可能使用GeoTools库获取Shapefile的范围? 我想从SHP读取TOP,LEFT,BOTTOM,RIGHT坐标。

好像没有getExtent()方法......

2 个答案:

答案 0 :(得分:2)

我没有编译以下但它应该工作。

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

答案 1 :(得分:2)

谢谢,这正是我要找的!我只需要更改网址地图。这是工作代码:

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