2

Trying to make a simple class that will read shapefiles and eventually be able to pull out the coordinates/attributes for each feature (working with points here). But can't quite figure out geotools.

File file = new File(filepath);
if(!file.exists() || !filepath.endsWith(".shp")) {
    throw new Exception("Invalid shapefile filepath: " + filepath);
}
ShapefileDataStore dataStore = new ShapefileDataStore(file.toURL());
ContentFeatureSource featureSource = dataStore.getFeatureSource();
ContentFeatureCollection featureCollection = featureSource.getFeatures();

SimpleFeatureIterator iterator = featureCollection.features();
while(iterator.hasNext()) {
    SimpleFeature feature = iterator.next();
    List attributes = feature.getAttributes();
    Collection properties = feature.getProperties();
}

Fails at dataStore.getFeatureSource() with the error: java.lang.NoSuchMethodError: org.geotools.data.shapefile.ShapefileDataStore.getFeatureSource() Lorg/geotools/data/store/ContentFeatureSource;

The imports for every geotools class mentioned above are there. As some of you know, geotools comes with a crapton of jars. As I'm unfamiliar with Maven and this is an existing project that I don't know how to convert over, I'm trying to do the library imports the old fashioned way. Right now my imports are: gt-api-11.1.jar, gt-data-11.1.jar, gt-main-11.1.jar, gt-opengis-11.1.jar, and gt-shapefile-11.1.jar. I figure it may be a missing jar that could be the problem but I'm not sure which as every imported class is in one of these jars.

1

Please try to use Maven when using GeoTools - to be honest trying to build any medium sized Java project without is going to be much harder work than you really want.

But if you must plead ignorance then at least install it and learn to use

mvn dependency:list 

which for the Shapefile module in GeoTools gives the following dependencies (on trunk so your version numbers may vary):

[INFO]    org.hsqldb:hsqldb:jar:2.2.8:test
[INFO]    org.jdom:jdom:jar:1.1.3:compile
[INFO]    junit:junit:jar:4.11:test
[INFO]    java3d:vecmath:jar:1.3.2:compile
[INFO]    org.geotools:gt-referencing:jar:12-SNAPSHOT:compile
[INFO]    com.vividsolutions:jts:jar:1.13:compile
[INFO]    org.geotools:gt-api:jar:12-SNAPSHOT:compile
[INFO]    net.java.dev.jsr-275:jsr-275:jar:1.0-beta-2:compile
[INFO]    org.geotools:gt-metadata:jar:12-SNAPSHOT:compile
[INFO]    commons-pool:commons-pool:jar:1.5.4:compile
[INFO]    org.geotools:gt-opengis:jar:12-SNAPSHOT:compile
[INFO]    org.hamcrest:hamcrest-core:jar:1.3:test
[INFO]    org.geotools:gt-data:jar:12-SNAPSHOT:compile
[INFO]    org.geotools:gt-main:jar:12-SNAPSHOT:compile
[INFO]    jgridshift:jgridshift:jar:1.0:compile
[INFO]    javax.media:jai_core:jar:1.1.3:compile
[INFO]    org.geotools:gt-sample-data:jar:12-SNAPSHOT:test
[INFO]    org.geotools:gt-epsg-hsql:jar:12-SNAPSHOT:test
  • I'm sorry for "pleading ignorance" (no need for condescension), I've built this project up traditionally and while I am planning on learning my way around Maven now, I don't have time at the moment to learn a whole new thing just to get a simple thing done. I understand geotools is designed for Maven, but all my other dependencies are much simpler and that's what I originally built this project dealing with. I'm not trying to make a huge project here, just a simple thing. Anyways I added every single jar in the geotools folder and still come up with this error. – wowohweewah May 30 '14 at 17:39
  • 1
    I was not being condescending just practical - life is too short to waste on dependency management. Did you try just adding the files in the list above? – Ian Turton May 30 '14 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.