Table Of Contents

Previous topic


Next topic

ArcSDE Plugin

This Page


One common thing people want to do is grab data existing data and export to a shapefile or PostGIS. A lot of desktop and server applications work very well with shapefiles.

There are a few tricks to keep in mind when taking information between different formats and we cover a few examples on this page.


  • doc:/tutorial/geometry/geometrycrs tutorial covers transforming a shapefile


When working on your own application you will often store your data in a MemoryDataStore it is under construction.

  • The following example shows how to export out a MemoryDataStore as a single shapefile:

    DataStore exportToShapefile(MemoryDataStore memory, String typeName, File directory)
            throws IOException {
        // existing feature source from MemoryDataStore
        SimpleFeatureSource featureSource = memory.getFeatureSource(typeName);
        SimpleFeatureType ft = featureSource.getSchema();
        String fileName = ft.getTypeName();
        File file = new File(directory, fileName + ".shp");
        Map<String,> creationParams = new HashMap<>();
        creationParams.put("url", DataUtilities.fileToURL(file));
        FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
        DataStore dataStore = factory.createNewDataStore(creationParams);
        // The following workaround to write out the prj is no longer needed
        // ((ShapefileDataStore)dataStore).forceSchemaCRS(ft.getCoordinateReferenceSystem());
        SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
        Transaction t = new DefaultTransaction();
        try {
            SimpleFeatureCollection collection = featureSource.getFeatures(); // grab all features
            t.commit(); // write it out
        } catch (IOException eek) {
            try {
            } catch (IOException doubleEeek) {
                // rollback failed?
        } finally {
        return dataStore;
  • We also have an alternative example (thanks gaby) using FeatureWriter.

    FeatureSource and FeatureCollection are high level API, to get down and dirty you can use the low-level FeatureReader / FeatureWriter API.

A couple of hints:

  • Shapefile does note support “Geometry” so if you are working with mixed content you will need to export out three shapefiles, one for point, line and polygon.


You can use the above technique to copy from a WFS to a Shapefile.

A couple of hints:

  • Please be advised that WFS TypeNames are not always valid filenames; you should take a moment to change non alphanumeric characters to “_” before generating the filename.:

    .. literalinclude:: /../src/main/java/org/geotools/data/
       :language: java
       :start-after: // fixWFSTypeName start
       :end-before: // fixWFSTypeName end
  • The WFS-T protocol does not allow us to implement createSchema so creating a new featureType will need to be done according to the procedure for your WFS prior to calling addFeature

    As an example GeoServer supports the use of a REST API for this purpose.


To copy information into PostGIS:

  • You can export from PostGIS as expected using the examples above
  • PostGIS also supports createSchema allowing you to create a new table to hold the content
  • You may wish to adjust the featureType information, especially the lengths of strings prior to calling createSchema