Skip to main content
  1. Masters/

Software Development: Web Feature Service Connector

·358 words·2 mins·

The following showcases a Java program that retrieves and parses features from a Web Feature Service (WFS) using GeoTools. The chosen data source is the city of Vienna’s WFS for open data.

The first step is to create a WFSDataStoreFactory object. This factory is used to create a WFSDataStore object, which is the main class used to interact with the WFS service. In order to create the WFSDataStore object, a HashMap with the connection parameters is needed. These parameters are then passed to the createDataStore method of the WFSDataStoreFactory object. With a working WFSDataStore object, it is possible to retrieve and print all of the feature type names that are available from the WFS service.

Next all of the public transport stops for the bus line 99A (an arbitrary example) are to be selected. To do this the features of type ogdwien:OEFFHALTESTOGD are retrieved and parsed. The features are represented as SimpleFeature objects, which contain properties such as HLINIEN (bus line)and HTXTK (stop name).

The code can be found below:

import java.io.IOException;
import java.util.HashMap;

import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.wfs.WFSDataStore;
import org.geotools.data.wfs.WFSDataStoreFactory;
import org.opengis.feature.Feature;

public class WFSConnector {

    public static void main(String[] args)
    {
        // define the getCapabilities request
        String wfsGetCapabilitiesURL = "https://data.wien.gv.at/daten/geo?service=wfs&version=1.1.0";

        // create WFSDataStoreFactory object
        WFSDataStoreFactory wfsDSF = new WFSDataStoreFactory();

        // create HashMap and fill it with connection parameters
        HashMap connectionParameters = new HashMap();
        connectionParameters.put(WFSDataStoreFactory.URL.key, wfsGetCapabilitiesURL);
        connectionParameters.put(WFSDataStoreFactory.TIMEOUT.key, 10000);
        connectionParameters.put(WFSDataStoreFactory.ENCODING.key, "UTF-8");


        // create a DataStore object
        try {

            WFSDataStore wfsDS = wfsDSF.createDataStore(connectionParameters);

            String[] typeNames = wfsDS.getTypeNames();

            for(int i=0; i<typeNames.length; i++)
            {
                System.out.println("typeName[" + i + "]:" + typeNames[i]);
            }

            String typeName = "ogdwien:OEFFHALTESTOGD";

            // retrieve the features from the service
            SimpleFeatureSource featureSource = wfsDS.getFeatureSource(typeName);

            // parse the result/features
            SimpleFeatureCollection featureCollection = featureSource.getFeatures();

            SimpleFeatureIterator featureIterator = featureCollection.features();

            while(featureIterator.hasNext())
            {
                // print the features to the screen
                Feature currentFeature = featureIterator.next();

                //System.out.println(currentFeature);

                // for type name ogdwien:OEFFHALTESTOGD
                String line = currentFeature.getProperty("HLINIEN").getValue().toString();
                String stop = currentFeature.getProperty("HTXTK").getValue().toString();

                // print stops of desired line
                if (line.equals("99A"))
                {
                    System.out.println(line + ": " + stop);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }//main
}//class