4

I am trying to use geotools to retrieve the list of features served by a WFS. I am following the tutorial provided at http://docs.geotools.org/latest/userguide/library/data/wfs.html.

The code that I have is as follows:

// Step 1 - connection parameters
//
Map connectionParameters = new HashMap();
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", capabilitiesUrl.toString() );

// Step 2 - connection
DataStore data = DataStoreFinder.getDataStore( connectionParameters );

// Step 3 - discouvery
String typeNames[] = data.getTypeNames();
String typeName = typeNames[0];
SimpleFeatureType schema = data.getSchema( typeName );
System.out.println( "Schema Attributes:"+schema.getAttributeCount() );

// Step 4 - target
SimpleFeatureSource source = data.getFeatureSource( typeName );
System.out.println( "Metadata Bounds:"+ source.getBounds() );

SimpleFeatureCollection features = source.getFeatures();
SimpleFeatureIterator iterator = features.features();
while( iterator.hasNext() ){
    Feature feature = (Feature) iterator.next();
    System.out.println(feature.getName());
}

However, my DataStore object is always null. I have tried this using geotools version 9.1, version 10-SNAPSHOT and version 11-SNAPSHOT. I am using Maven to manage the dependancies. When I use version 11-SNAPSHOT, I keep getting a DataStoreFinder NoClassDefFound on running the above code. When using version 11-snapshot, I cannot see any of the geotools jars in the maven local repository which is probably why this error occurs. However, I have not yet managed to resolve it. I know that geotools can be used to retrieve features as shown above and would appreciate any help in pointing out what I could possibly be doing wrong. Thanks!

UPDATE: I have included the pom files below. The first POM results in the following error on running mvn clean install:

Exception in thread "main" java.lang.NoClassDefFoundError: org/geotools/data/DataStoreFinder
    at com.envitia.geopackage.geopackagecreator.dataharvestor.WFSDataHarvestor.getFeatureGeometries(WFSDataHarvestor.java:295)
    at com.envitia.geopackage.geopackagecreator.dataharvestor.WFSDataHarvestor.getFeatureType(WFSDataHarvestor.java:274)
    at com.envitia.geopackage.geopackagecreator.dataharvestor.WFSDataHarvestor.readData(WFSDataHarvestor.java:176)
    at com.envitia.geopackage.geopackagecreator.App.main(App.java:22)

POM 1:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample.harvestor</groupId>
    <artifactId>Creator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Creator</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>11-SNAPSHOT</geotools.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>${geotools.version}</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-opengis</artifactId>
            <version>${geotools.version}</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-api</artifactId>
            <version>${geotools.version}</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>com.sample.harvestor.creator.App</mainClass>
                </manifest>
              </archive>
            </configuration>
          </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository> <!--Add the snapshot repository here-->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <id>opengeo</id>
            <name>OpenGeo Maven Repository</name>
            <url>http://repo.opengeo.org</url>
        </repository>
    </repositories>
</project>

The next POM does not return any errors on mvn install, but on running the application, I get the following error:

Exception in thread "main" java.lang.NullPointerException
    at com.envitia.geopackage.geopackagecreator.dataharvestor.WFSDataHarvestor.getFeatureGeometries(WFSDataHarvestor.java:298)
    at com.envitia.geopackage.geopackagecreator.dataharvestor.WFSDataHarvestor.getFeatureType(WFSDataHarvestor.java:274)
    at com.envitia.geopackage.geopackagecreator.dataharvestor.WFSDataHarvestor.readData(WFSDataHarvestor.java:176)
    at com.envitia.geopackage.geopackagecreator.App.main(App.java:22)

This NullPointer is due to the data object being null as mentioned earlier.

POM2:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample.harvestor</groupId>
    <artifactId>Harvestor</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Harvestor</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>10-SNAPSHOT</geotools.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>${geotools.version}</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-opengis</artifactId>
            <version>${geotools.version}</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-api</artifactId>
            <version>${geotools.version}</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>com.sample.temp.harvestor.App</mainClass>
                </manifest>
              </archive>
            </configuration>
          </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository> <!--Add the snapshot repository here-->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <id>opengeo</id>
            <name>OpenGeo Maven Repository</name>
            <url>http://repo.opengeo.org</url>
        </repository>
    </repositories>
</project>
1

DataStoreFinder is depricated in new versions. And the tutorial you mentioned is an old tutorial.
You can code in the following manner:

String getCapabilities = "http://localhost:8081/geoserver/wfs?REQUEST=GetCapabilities&version=1.0.0";
Map connectionParameters = new HashMap();
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities);
WFSDataStoreFactory  dsf = new WFSDataStoreFactory();
try {
    WFSDataStore dataStore = dsf.createDataStore(connectionParameters);
    SimpleFeatureSource source = dataStore.getFeatureSource("ali:Manategh_Tehran");
    SimpleFeatureCollection fc = source.getFeatures();
    while(fc.features().hasNext()){
        SimpleFeature sf = fc.features().next();
        System.out.println(sf.getAttribute("myname"));
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

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.