简体   繁体   English

用Java打开.shp文件

[英]Opening .shp file in Java

I am trying to open .shp file downloaded from http://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/ using geotools. 我正在尝试使用geotools打开从http://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/下载的.shp文件。 My code looks like this: 我的代码如下所示:

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

DataStoreFinder.getDataStore() method returns null and that is the problem. DataStoreFinder.getDataStore()方法返回null,这就是问题所在。 Do you have some ideas what am I doing wrong? 你有什么主意我在做什么错吗?

I want to open and use this shapefile in order to get coordinates of country borders. 我想打开并使用此shapefile以获得国家边界的坐标。

The problem was that i didn't have include other needed jar files. 问题是我没有包含其他需要的jar文件。 It's working now. 现在正在工作。

do you have gt-shapefile.jar on the class path? 你在类路径上有gt-shapefile.jar吗?

GeoTools uses an SPI lookup to find datastores based on the parameter map you supply. GeoTools使用SPI查找根据您提供的参数图查找数据存储。 Another possible check is to call DataStoreFinder.getAllDatastores() to check availability: 另一种可能的检查是调用DataStoreFinder.getAllDatastores()以检查可用性:

Iterator<DataStoreFactorySpi> it = DataStoreFinder.getAllDataStores();
  while (it.hasNext()) {
    DataStoreFactorySpi fac = it.next();
    System.out.println(fac.getDisplayName());
  }

Or if you only ever want to read a Shapefile then create the ShapefileDataStore directly. 或者,如果您只想读取Shapefile,则直接创建ShapefileDataStore。

Also as a side note I'd recommend using DataUtilities.fileToURL(file) over file.toUri().toUrl() as it handles windows and paths with spaces better. 另外,我建议在file.toUri().toUrl()使用DataUtilities.fileToURL(file) ,因为它可以更好地处理带有空格的窗口和路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM