Added by jgarnett, last edited by jgarnett on Aug 28, 2008  (view change)

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.

One common thing people want to do is grab data existing data and generate a shapefile (a lot of desktop and server applications work very well with shapefiles).

Related

Copying a MemoryDataStore to a Shapefile

When working on your own application you will often store your data in a memory datastore while it is under consturction:

public ShapefileDataStore exportToShapefile( MemoryDataStore memory, File directory ) throws IOException {
   FeatureSource featureSource= memory.getFeatureSource(); //existing feature source from MemoryDataStore
   FeatureType ft = featureSource.getSchema();
   String ftName = ft.getTypeName();
   CoordinateReferenceSystem crs = ft.getDefaultGeometry().getCoordinateReferenceSystem();
   
   File file = new File( directory, ftName+".shp" );
   
   Map params = new HashMap();
   params.put("url", file.toURI().toURL() );

   FileDataStoreFactorySpi factory = new IndexedShapefileDataStoreFactory();
   ShapefileDataStore dataStore = (ShapefileDataStore) factory.createNewDataStore(map);
   dataStore.createSchema(ft);
   dataStore.forceSchemaCRS( crs );
      
   FeatureStore featureStore = (FeatureStore) dataStore.getFeatureSource(typeName );

   Transaction t = new DefaultTransaction();
   try {
     FeatureCollection collection = featureSource.getFeatures(); // grab all features
     featureStore.addFeatures( collection );
     t.commit(); // write it out
   }
   catch( IOException eek){
       eek.printStrackTrace();
       try {
          t.rollback();
       }
       catch( IOException doubleEeek ){
          // rollback failed?
       }
   }
   finally {
      t.close();
   }
   return dataStore;
}

Copying WFS to a Shapefile

You can use the above technique to copy from a WFS to a Shapefile; please be advised that WFS TypeNames are not always valid filenames; you should take a moment to change non alphanumeric charactes to "_" before generating the filename.

You will need to select a single WFS FeatureType to export.

Copying PostGIS to a Shapefile

You will need to select a single PostGIS table to export.

Alternative - Using FeatureWriter

Thanks to gaby for the the following alternative. Basically FeatureSource and FeatureCollection are high level API, to get down and dirty you can use the low-level FeatureReader / FeatureWriter API.

public ShapefileDataStore exportToShapefile( MemoryDataStore ml, File f) throws IOException {
        FeatureSource fs = ml.getFeatureSource();
        FeatureType ft = fs.getSchema();
        CoordinateReferenceSystem currentCRS = ft.getDefaultGeometry().getCoordinateReferenceSystem();

        String ftName = ft.getTypeName();
        logMessage(Constants.INFO, "ftname " + ftName);
        try
        {
          logMessage(Constants.INFO, "instantiate datastore");
          IndexedShapefileDataStore dataStore =
             new IndexedShapefileDataStore(f.toURI().toURL());
          dataStore.createSchema(ft);
          dataStore.forceSchemaCRS(currentCRS);
          DefaultTransaction transaction = new DefaultTransaction("test");
          FeatureWriter aWriter = dataStore.getFeatureWriter(transaction);
          
          FeatureCollection fc = fs.getFeatures();
          Iterator iterator = fc.iterator();
          SimpleFeature feature;
          try
          {
            while(iterator.hasNext())
            {
              feature = (Feature) iterator.next();

              SimpleFeature aNewFeature = (SimpleFeature) aWriter.next();
              aNewFeature.setAttributes(feature.getAttributes());
              aWriter.write();
              transaction.commit();
            }
          } catch (Exception e) {
            transaction.rollback();
            logMessage(Constants.ERROR, e.getMessage());
          } finally {
            fc.close(iterator);
            transaction.close();
          }

        } catch (Exception e) {

        } 
}