0

I am new to geotools and my java knowledge is rusty. I'm trying to input many shapefile into java so that I can calculate the areas of all polygons in them and add a separate attribute to the respective shapefile. I tried using the code given in the link Does anyone know of a library in Java that can parse ESRI Shapefiles? . But I'm getting exception thrown

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.access$100(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$1.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.access$100(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$1.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

The error is probabaly at the line

DataStore dataStore = DataStoreFinder.getDataStore(connect);

How do I resolve it? Should I change any of the java arguments? Should the file be full name?

Community
  • 1
  • 1

3 Answers3

0

For loading shapefiles you can use this code:

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

Make sure the path is correct.

Lars
  • 2,315
  • 2
  • 24
  • 29
0

I resolved this by changing the code in the while loop to the following

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

Please work through (at least) the GeoTools' quickstart tutorial to find out what jars you will need to use a ShapeFile DataStore.

TBH I wouldn't try to build even a simple GeoTools project without using a build manager, life is too short to worry about dependencies.

If you must push on alone then you need to include all of the following jars:

➜  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
Ian Turton
  • 10,018
  • 1
  • 28
  • 47