输入 ESRI shapefile 到 java

Input ESRI shapefile to java

我是 geotools 的新手,我的 java 知识很生疏。我正在尝试将许多 shapefile 输入到 java 中,以便我可以计算其中所有多边形的面积,并向相应的 shapefile 添加一个单独的属性。我尝试使用 link 中给出的代码 Does anyone know of a library in Java that can parse ESRI Shapefiles?。 但是我抛出异常

Oct 21, 2016 12:39:08 PM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "DataStoreFactorySpi". Cause is "ServiceConfigurationError: org.geotools.data.DataStoreFactorySpi: Provider org.geotools.data.shapefile.ShapefileDataStoreFactory could not be instantiated".
java.util.ServiceConfigurationError: org.geotools.data.DataStoreFactorySpi: Provider org.geotools.data.shapefile.ShapefileDataStoreFactory could not be instantiated
at java.util.ServiceLoader.fail(ServiceLoader.java:232)
at java.util.ServiceLoader.access0(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader.next(ServiceLoader.java:480)
at org.geotools.factory.FactoryRegistry.register(FactoryRegistry.java:826)
at org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:772)
at org.geotools.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:805)
at org.geotools.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:196)
at org.geotools.data.DataAccessFinder.getAvailableDataStores(DataAccessFinder.java:207)
at org.geotools.data.DataStoreFinder.getAvailableDataStores(DataStoreFinder.java:116)
at org.geotools.data.DataStoreFinder.getDataStore(DataStoreFinder.java:87)
at shapefile.ReadFile.main(ReadFile.java:24)
Caused by: java.lang.NoClassDefFoundError: org/geotools/data/directory/FileStoreFactory
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.newInstance(Class.java:412)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
... 10 more
Caused by: java.lang.ClassNotFoundException: org.geotools.data.directory.FileStoreFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 15 more

Oct 21, 2016 12:39:08 PM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "DataStoreFactorySpi". Cause is "ServiceConfigurationError: org.geotools.data.DataStoreFactorySpi: Provider org.geotools.data.shapefile.ShapefileDirectoryFactory could not be instantiated".
java.util.ServiceConfigurationError: org.geotools.data.DataStoreFactorySpi: Provider org.geotools.data.shapefile.ShapefileDirectoryFactory could not be instantiated
at java.util.ServiceLoader.fail(ServiceLoader.java:232)
at java.util.ServiceLoader.access0(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader.next(ServiceLoader.java:480)
at org.geotools.factory.FactoryRegistry.register(FactoryRegistry.java:826)
at org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:772)
at org.geotools.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:805)
at org.geotools.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:196)
at org.geotools.data.DataAccessFinder.getAvailableDataStores(DataAccessFinder.java:207)
at org.geotools.data.DataStoreFinder.getAvailableDataStores(DataStoreFinder.java:116)
at org.geotools.data.DataStoreFinder.getDataStore(DataStoreFinder.java:87)
at shapefile.ReadFile.main(ReadFile.java:24)
Caused by: java.lang.NoClassDefFoundError: org/geotools/data/directory/FileStoreFactory
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.newInstance(Class.java:412)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
... 10 more
Caused by: java.lang.ClassNotFoundException: org.geotools.data.directory.FileStoreFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 15 more

java.lang.NullPointerException

错误可能在行

DataStore dataStore = DataStoreFinder.getDataStore(connect);

如何解决?我应该更改任何 java 参数吗?文件应该是全名吗?

要加载 shapefile,您可以使用此代码:

ShapefileDataStore myShape = new ShapefileDataStore(new File("path/to/shapefile.shp").toURL());

确保路径正确。

我通过将 while 循环中的代码更改为以下代码解决了这个问题

while(iterator.hasNext()){
                Feature feature = iterator.next();
                SimpleFeature simple = (SimpleFeature)feature;
                Geometry sourcegeometry = (Geometry) simple.getDefaultGeometry();
}

请(至少)完成 GeoTools' quickstart tutorial 以找出使用 ShapeFile DataStore 需要哪些 jar。

TBH 我不会尝试在不使用构建管理器的情况下构建一个简单的 GeoTools 项目,生命太短暂而不必担心依赖性。

如果您必须独自推进,那么您需要包含以下所有 jar:

➜  shapefile git:(master) ✗ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Shapefile module 17-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ gt-shapefile ---
[INFO] org.geotools:gt-shapefile:jar:17-SNAPSHOT
[INFO] +- org.geotools:gt-data:jar:17-SNAPSHOT:compile
[INFO] |  \- org.geotools:gt-main:jar:17-SNAPSHOT:compile
[INFO] |     +- org.geotools:gt-api:jar:17-SNAPSHOT:compile
[INFO] |     \- com.vividsolutions:jts:jar:1.13:compile
[INFO] +- org.jdom:jdom:jar:1.1.3:compile
[INFO] +- org.geotools:gt-sample-data:jar:17-SNAPSHOT:test
[INFO] +- org.geotools:gt-epsg-hsql:jar:17-SNAPSHOT:test
[INFO] |  +- org.geotools:gt-referencing:jar:17-SNAPSHOT:compile
[INFO] |  |  +- com.googlecode.efficient-java-matrix-library:core:jar:0.26:compile
[INFO] |  |  +- commons-pool:commons-pool:jar:1.5.4:compile
[INFO] |  |  +- org.geotools:gt-metadata:jar:17-SNAPSHOT:compile
[INFO] |  |  |  \- org.geotools:gt-opengis:jar:17-SNAPSHOT:compile
[INFO] |  |  |     \- net.java.dev.jsr-275:jsr-275:jar:1.0-beta-2:compile
[INFO] |  |  +- jgridshift:jgridshift:jar:1.0:compile
[INFO] |  |  \- net.sf.geographiclib:GeographicLib-Java:jar:1.44:compile
[INFO] |  \- org.hsqldb:hsqldb:jar:2.3.0:test
[INFO] +- org.geotools:gt-property:jar:17-SNAPSHOT:test
[INFO] +- commons-io:commons-io:jar:2.1:test
[INFO] +- javax.media:jai_core:jar:1.1.3:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test