Creating a WFS Output Format
Adding a new GeoServer WFS output format.
This tutorial applies to GeoServer 1.7.0 and above.
This tutorial assumes you have set up a GeoServer development environment as described in the Maven Quickstart and Eclipse Quickstart
Introduction
Getting Started
Create a New Module
Import Module into Eclipse
Implement CSVOutputFormat
Test CSVOutputFormat
Install the Plug-in
Introduction
This tutorial walks through the process of creating a GeoServer plug-in for a WFS output format using the Comma Separated Value (CSV) file format as an example.
Getting Started
Do a full build from the root of the GeoServer source tree.
[geoserver]% mvn clean install
A successful build should result in teh following output:
[INFO] [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] ------------------------------------------------------------------------ [INFO] GeoServer ............................................. SUCCESS [4.985s] [INFO] GeoServer Maven Plugins ............................... SUCCESS [0.303s] [INFO] Plugins configuration files ........................... SUCCESS [1.453s] [INFO] Configuration Deployment PlugIn ....................... SUCCESS [1.659s] [INFO] GeoServer Maven Archetypes ............................ SUCCESS [0.101s] [INFO] GeoServer WFS Output Format Archetype ................. SUCCESS [0.198s] [INFO] Core Platform Module .................................. SUCCESS [1.209s] [INFO] Data Module ........................................... SUCCESS [4.198s] [INFO] Open Web Service Module ............................... SUCCESS [4.324s] [INFO] Main Module ........................................... SUCCESS [10.547s] [INFO] Web Coverage Service Module ........................... SUCCESS [1.645s] [INFO] Web Coverage Service 1.1.1 Module ..................... SUCCESS [19.973s] [INFO] Validation Module ..................................... SUCCESS [0.897s] [INFO] Web Feature Service Module ............................ SUCCESS [27.089s] [INFO] Web Feature Service Module ............................ SUCCESS [26.490s] [INFO] Versioned Web Feature Service Module .................. SUCCESS [2.749s] [INFO] Web Map Service Module ................................ SUCCESS [32.375s] [INFO] Web Application Module ................................ SUCCESS [19.315s] [INFO] Community Space ....................................... SUCCESS [0.126s] [INFO] GeoServer Extensions .................................. SUCCESS [0.120s] [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2 minutes 45 seconds [INFO] Finished at: Tue Aug 19 18:55:13 MDT 2008 [INFO] Final Memory: 40M/63M [INFO] ------------------------------------------------------------------------
Create a New Module
The first step is to create a new module for the output format. This is accomplished by using an GeoServer "archetype", a tool used to create the skeleton of a GeoServer plug-in.
Somewhere on the file system execute the following maven command line:
[/home/bob]% mvn archetype:create -DarchetypeGroupId=org.geoserver.maven \ -DarchetypeArtifactId=geoserver-archetype-wfsoutputformat \ -DarchetypeVersion=1.7.0-SNAPSHOT -DgroupId=org.geoserver \ -DartifactId=csv
The command will create a new directory named csv. The structure of the module should look like:
csv/
|--pom.xml
|--src/
|--main/
| |-java/
| |-org/
| |-geoserver/
| |-MyOutputFormat.java
|--test/
| |--java/
| |--org/
| |--geoserver
| |-MyOutputFormatTest.java
|--resources/
| |-applicationContext.xml
Import Module into Eclipse
- Change directory into the csv module create the Eclipse project files by executing the command line:
[csv]% mvn eclipse:eclipse
- Start Eclipse and import (File->Import...) the csv module. Full instructions are available in the Eclipse Quickstart.
Refactor
- Using the Eclipse Refactor tool:
- Rename the class MyOutputFormat to CSVOutputFormat
- Rename the test class MyOutputFormatTest to CSVOutputFormatTest


- Open the file src/main/resources/applicationContext.xml in the text editor and change:
<bean id="myOutputFormat" class="org.geoserver.MyOutputFormat"/>
to:
<bean id="csvOutputFormat" class="org.geoserver.CSVOutputFormat"/>

Implement CSVOutputFormat
- Open CSVOutputFormat in the text editor.
- Implement the constructor:
public CSVOutputFormat() { //this is the name of your output format, it is the string // that will be used when requesting the format in a // GEtFeature request: // ie ;.../geoserver/wfs?request=getfeature&outputFormat=myOutputFormat super("csv"); }
- Implement getMimeType():
@Override public String getMimeType(Object value, Operation operation) throws ServiceException { // return the mime type of the format here, the parent // class returns 'text/xml' return "text/csv"; }
- Implement write():
@Override protected void write(FeatureCollectionType featureCollection, OutputStream output, Operation getFeature) throws IOException, ServiceException { //write out content here //create a writer BufferedWriter w = new BufferedWriter( new OutputStreamWriter( output ) ); //get the feature collection FeatureCollection fc = (FeatureCollection) featureCollection.getFeature().get(0); //write out the header SimpleFeatureType ft = (SimpleFeatureType) fc.getSchema(); for ( int i = 0; i < ft.getAttributeCount(); i++ ) { AttributeDescriptor ad = ft.getDescriptor( i ); w.write( ad.getLocalName() ); if ( i < ft.getAttributeCount()-1 ) { w.write( "," ); } } w.write( "\n" ); //write out the features FeatureIterator i = fc.features(); try { while( i.hasNext() ) { SimpleFeature f = (SimpleFeature) i.next(); for ( int j = 0; j < f.getAttributeCount(); j++ ) { Object att = f.getAttribute( j ); if ( att != null ) { w.write( att.toString() ); } if ( j < f.getAttributeCount()-1 ) { w.write(","); } } w.write( "\n" ); } } finally { fc.close( i ); } w.flush(); }
- Save changes.
Test CSVOutputFormat
- Open CSVOutputFormatTest in the text editor.
- Modify testOutputFormat(), changing the value of the "outputFormat" parameter to "csv":
//execute a mock request using the output format public void testOutputFormat() throws Exception { InputStream in = get( "wfs?request=GetFeature&typeName=sf:PrimitiveGeoFeature" + "&outputFormat=myOutputFormat"); print( in ); }
- Save changes.
- Run the test case. This should result in the following output:
the_geom,FID,ADDRESS MULTIPOLYGON (((0.0008 0.0005, 0.0008 0.0007, 0.0012 0.0007, 0.0012 0.0005, 0.0008 0.0005))),113,123 Main Street MULTIPOLYGON (((0.002 0.0008, 0.002 0.001, 0.0024 0.001, 0.0024 0.0008, 0.002 0.0008))),114,215 Main Street
Install the Plug-in
- From the command line preform a build of the csv module:
[cvs]% mvn clean install
- Copy the file csv/target/csv-1.0-SNAPSHOT.jar into the webapps/geoserver/WEB-INF/lib directory of a GeoServer installation:
[csv]% cp target/csv-1.0-SNAPSHOT.jar /home/bob/geoserver/webapps/geoserver/WEB-INF/lib
- Restart GeoServer.