在Java中打开.shp文件
我正在尝试使用geotools打开从下载的.shp文件。我的代码如下所示:在Java中打开.shp文件,java,shapefile,geotools,Java,Shapefile,Geotools,我正在尝试使用geotools打开从下载的.shp文件。我的代码如下所示: File file = new File("ne_10m_admin_0_countries.shp"); try{ Map<String, URL> map = new HashMap<String, URL>(); map.put("url", file.toURI().toURL()); DataStore dataStore = Dat
File file = new File("ne_10m_admin_0_countries.shp");
try{
Map<String, URL> map = new HashMap<String, URL>();
map.put("url", file.toURI().toURL());
DataStore dataStore = DataStoreFinder.getDataStore(map);
System.out.println(DataStoreFinder.getDataStore(map));
System.out.println(map);
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();
System.out.println(left+" "+right);
}catch(Exception e){
System.out.println("ahoj" +e.getMessage());
}
File File文件=新文件(“ne_10m_admin_0_countries.shp”);
试一试{
Map Map=newhashmap();
map.put(“url”,file.toURI().toul());
DataStore DataStore=DataStoreFinder.getDataStore(map);
System.out.println(DataStoreFinder.getDataStore(map));
系统输出打印项次(map);
SimpleFeatureSource featureSource=dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
SimpleFeatureCollection集合=featureSource.getFeatures();
referenceDevelope env=collection.getBounds();
左双=env.getMinX();
double right=env.getMaxX();
double-top=env.getMaxY();
双底=env.getMinY();
System.out.println(左+“”+右);
}捕获(例外e){
System.out.println(“ahoj”+e.getMessage());
}
getDataStore()方法返回null,这就是问题所在。你知道我做错了什么吗
我想打开并使用这个shapefile来获得国家边界的坐标
问题是我没有包含其他需要的jar文件。它正在工作。类路径上是否有gt-shapefile.jar GeoTools使用SPI查找根据您提供的参数映射查找数据存储。另一种可能的检查方法是调用
DataStoreFinder.getAllDatastores()
检查可用性:
Iterator<DataStoreFactorySpi> it = DataStoreFinder.getAllDataStores();
while (it.hasNext()) {
DataStoreFactorySpi fac = it.next();
System.out.println(fac.getDisplayName());
}
Iterator it=DataStoreFinder.getAllDataStores();
while(it.hasNext()){
DataStoreFactorySpi fac=it.next();
System.out.println(fac.getDisplayName());
}
或者,如果您只想读取Shapefile,则直接创建ShapefileDataStore
另外,作为旁注,我建议使用DataUtilities.filetoul(file)
而不是file.toUri().toul()
,因为它可以更好地处理带有空格的窗口和路径