Last updated January 07, 2011 01:40, by allaves
Feedicon  

Setting up a WPS

Setting up the unmodified WPS is simple, just download the latest version from here and deploy it to your tomcat. A few preselected algorithms (e.g. buffering and intersection) are shipped with the download version.

More information in this tutorial.

Implementing your own algorithm

The following tutorial is based on the one available here provided by 52North. It explains how to develop a convex hull algorithm and deploy it in our Envision WPS.

1. Create a new Java package:

 de.ifgi.envision.wps

2. Add a new Java class: ConvexHull

3. The new class must extend the class AbstractSelfDescribingAlgorithm.

By extending that class you need to fill 5 methods:

  • public List<String> getInputIdentifiers()
  • public List<String> getOutputIdentifiers()
  • public Class getInputDataType(String identifier)
  • public Class getOutputDataType(String identifier)
  • public Map<String, IData> run(Map<String, List<IData>> inputData)


public List<String> getInputIdentifiers()

This method lists all identifiers of the inputs you need. Since we want to develop a ConvexHull algorithm, we basically need only geometries. We give this identifier the name features. Therefore, return a list with a single string: features.

List<String> list = new ArrayList();
list.add("features");
return list;

public List<String> getOutputIdentifiers()

This method lists all identifiers of the produced outputs. A convex hull algorithm produces only polygons as outputs. Therefore we name the output: polygons. Return a list with a single string: polygons.

List<String> list = new ArrayList();
list.add("polygons");
return list;

public Class getInputDataType(String identifier)

This method lists all input datatypes for a given input data identifier. In our case, we only have one input identifier: features. Since we want to use a JTS algorithm which understands Geotools features, we will use a GTVectorDataBinding and therefore return:

if (identifier.equalsIgnoreCase("features")) {
    return GTVectorDataBinding.class;
}
return null;

public Class getOutputDataType(String identifier)

This method lists all output datatypes for a given output data identifier. In our case, we only have only one output identifier: polygons. Since JTS outputs Geotools features, we return:

if (identifier.equalsIgnoreCase("polygons")) {
    return GTVectorDataBinding.class;
} 
return null;

public Map<String, IData> run(Map<String, List<IData>> inputData)

This method holds the business logic. At first we check that all necessary inputdata is present.

if (inputData == null || !inputData.containsKey("FEATURES")) {
    throw new RuntimeException("Error while allocating input parameters");
}
List<IData> dataList = inputData.get("FEATURES");
if (dataList == null || dataList.size() != 1) {
    throw new RuntimeException("Error while allocating input parameters");
}

Next, we extract the Geotools featurecollection:

IData firstInputData = dataList.get(0);
FeatureCollection featureCollection = ((GTVectorDataBinding) firstInputData).getPayload();

And iterate over the features in order to extract all coordinates which will be stored in a list called coordinateList:

FeatureIterator iter = featureCollection.features();
List<Coordinate> coordinateList = new ArrayList<Coordinate>();
int counter = 0;
Geometry unifiedGeometry = null;
while (iter.hasNext()) {
    SimpleFeature feature = (SimpleFeature) iter.next();
    if (feature.getDefaultGeometry() == null) {
        throw new NullPointerException("defaultGeometry is null in feature id: " + feature.getID());
    }
    Geometry geom = (Geometry) feature.getDefaultGeometry();
    Coordinate[] coordinateArray = geom.getCoordinates();
    for(Coordinate coordinate : coordinateArray){
        coordinateList.add(coordinate);
    }
}

In the next step, we convert the coordinateList to an array, because this is required as input for the JTS function.

Coordinate[] coordinateArray = new Coordinate[coordinateList.size()];
for(int i = 0; i<coordinateList.size(); i++){
    coordinateArray[i] = coordinateList.get(i);
}

Now, we are ready to create a ConvexHull.

com.vividsolutions.jts.algorithm.ConvexHull convexHull = new com.vividsolutions.jts.algorithm.ConvexHull(coordinateArray, new GeometryFactory());
Geometry geometry = convexHull.getConvexHull();

And create a new Geotools feature with the output geometry via:

String uuid = UUID.randomUUID().toString();
SimpleFeatureType featureType = GTHelper.createFeatureType(geometry, uuid, featureCollection.getSchema().getCoordinateReferenceSystem());
GTHelper.createGML3SchemaForFeatureType(featureType);
Feature feature = GTHelper.createFeature("0", geometry, featureType);

We now create a feature collection and put the result feature in it.

FeatureCollection fOut = DefaultFeatureCollections.newCollection();
fOut.add(feature);

The last step is to create the standard output hashmap which holds the name of the output identifier and an IData object. In our case we create a GTVectordataBinding.

HashMap<String, IData> result = new HashMap<String, IData>();
result.put("polygons", new GTVectorDataBinding(fOut));
return result;

Save the source code and compile it.

4. Deploy custom process. In order to deploy the recently developed process, you need to:

4.1 Open your browser and access http://giv-wfs.uni-muenster.de/52n-wps-webapp-2.0-RC6-SNAPSHOT/webAdmin/index.jsp Login: admin/kv6iur

4.2 The Web Admin Console lets you change the basic configuration of the WPS and upload processes. Click on Upload Process.

4.3 Enter for the first text box the name of the class you developed: de.ifgi.envision.wps.ConvexHull

4.4 Specify the .java file for the process: ConvexHull.java

4.5 Click on Submit Query.

The source file gets now compiled at server side and added as a new process to the WPS.

  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close