0

I am creating a simple java program that will handle the response of my WFS OGC server and display the image behind a given link.

The link I use for generating an image:

http://localhost:8180/gss/native?service=map&method=map&ace_name=XXX&centre=XXX,XXX

The link given is an image layer in the returned xml and looks like this:

http://localhost:8180/gss/resource/map/mapXXX.jpeg?token=-XXX

I used a tutorial that seemed handy and it is from 25-SNAPSHOT, the same version I use: https://docs.geotools.org/latest/userguide/library/data/wfs-ng.html

The example class at the bottom of the page makes use of the DataStoreFinder which I found is deprecated. So the "new" thing is supposed to be the WFSDataStoreFactory and the WFSDataStore, but the error also occurs on these lines:

WFSDataStoreFactory factory = new WFSDataStoreFactory();
try{
    WFSDataStore dataStore = factory.createDataStore(connectionParameters);

Error: Exception in thread "main" java.lang.NoClassDefFoundError: org/geotools/factory/Factory

Pom:

<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>org.geotools</groupId>
    <artifactId>WFSProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-opengis</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-cql</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-xml</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-wfs</artifactId>
            <version>16.5</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-wfs-ng</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-metadata</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools.jdbc</groupId>
            <artifactId>gt-jdbc-postgis</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools.data</groupId>
            <artifactId>refdataset-1.0</artifactId>
            <version>1.0.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>osgeo</id>
            <name>OSGeo Release Repository</name>
            <url>https://repo.osgeo.org/repository/release/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
        <repository>
            <id>osgeo-snapshot</id>
            <name>OSGeo Snapshot Repository</name>
            <url>https://repo.osgeo.org/repository/snapshot/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>



</project>

Java class:

package org.geotools.WFSProject;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import org.geotools.data.*;
import org.geotools.data.DataAccessFactory.Param;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.Query;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.store.ContentDataStore;
//import org.geotools.data.AbstractDataStoreFactory;
//import org.geotools.tutorial.quickstart.AbstractDataStoreFactory;
import org.geotools.parameter.FloatParameter;
import org.geotools.data.wfs.impl.WFSDataAccessFactory;

import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.metadata.iso.extent.GeographicBoundingBoxImpl;
import org.geotools.parameter.DefaultParameterDescriptor;
import org.geotools.parameter.DefaultParameterDescriptorGroup;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.util.factory.GeoTools;
import org.geotools.xml.XMLSAXHandler;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.spatial.Intersects;
import org.opengis.geometry.BoundingBox;
import org.opengis.geometry.Envelope;
import org.opengis.geometry.coordinate.GeometryFactory;
import org.opengis.metadata.extent.GeographicBoundingBox;
import org.opengis.parameter.ParameterDescriptorGroup;
import org.opengis.parameter.ParameterValue;
import org.geotools.data.wfs.WFSDataStore;
import org.geotools.data.wfs.WFSDataStoreFactory;
import org.geotools.util.factory.Factory;
import org.geotools.util.factory.*;
import org.geotools.factory.*;
import org.geotools.util.factory.AbstractFactory;

import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.ReferencedEnvelope;

public class WFSConnector {

    public static void main(String[] args) {
        newTry();
    }



    public static void newTry() {
        String getCapabilities = "http://localhost:8180/gssogc/ogc?service=WFS&version=1.1.0&request=GetCapabilities";
        Map connectionParameters = new HashMap();
        // connectionParameters.put("url", getCapabilities );
        connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities);
        // connectionParameters.put("WFSDataStoreFactory:MAXFEATURES", 50);
        WFSDataStoreFactory factory = new WFSDataStoreFactory();
        try {
            WFSDataStore dataStore = factory.createDataStore(connectionParameters);
            SimpleFeatureSource source = dataStore.getFeatureSource("Name");
            SimpleFeatureCollection fc = source.getFeatures();
            while(fc.features().hasNext()){
                SimpleFeature sf = fc.features().next();
                System.out.println(sf);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        
        // Feature type: FeatureTypeList in getcapabilities
        /*
        String typeNames[] = data.getTypeNames();
        String typeName = typeNames[0];
        SimpleFeatureType schema = data.getSchema(typeName);

        // Step 4 - target
        FeatureSource<SimpleFeatureType, SimpleFeature> source = data.getFeatureSource(typeName);
        System.out.println("Metadata Bounds:" + source.getBounds());
        */
    }}

I found a couple issues that looked like mine, but no solutions have worked for me yet.

1 Answer 1

3

Your error is that you are trying to mix a (very) old module in with modern ones:

    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-wfs</artifactId>
        <version>16.5</version>
    </dependency>

will not work with 25-SNAPSHOT. You need to import gt-wfs-ng which replaced gt-wfs some time ago.

   <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-wfs-ng</artifactId>
        <version>${geotools.version}</version>
    </dependency>

I'm also not sure where you got the idea that DataStoreFinder.getDataStore(...) is deprecated, it's still the recommended way of finding a datastore as far as I know.

And, finally, your while loop should only fetch one iterator and make sure to close it:

    SimpleFeatureCollection fc = source.getFeatures(query);
    try (SimpleFeatureIterator itr = fc.features()) {
      while (itr.hasNext()) {
        SimpleFeature sf = itr.next();
        System.out.println(sf);
      }
    }
1
  • Thanks for reply. It was indeed the mixing of old and new. I looked at this before and tinkered with it but I was not aware I only needed the one. I will test out the other pointers, thank you so much!
    – ArthurP
    Oct 28, 2020 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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