Next you need to grab a FeatureStore and called addFeatures; there is an 
example of this kind of thing in the user guide. And we can add your 
example here when we are done with it....
>> Hello, I am wondering if there is a simple way of saving a FeatureSource
>> to a
>> shapefile. The FeatureSource is backed by an MStore. I can't quite see  
>> how to do this. I've gotten as far as this:
>>
>> FeatureSource fs = mapLayer.getFeatureSource(); //pre-existing
>> featursource
>> FileDataStoreFactorySpi factory = new IndexedShapefileDataStoreFactory();
>> Map map = Collections.singletonMap( "url", file.toURL());  //file from
>> chooser
>> DataStore myData = factory.createNewDataStore( map );
>> myData.createSchema(fs.getSchema());
>   
>String typeName = myData.getTypeNames[0];
>FeatureStore featureStore = (FeatureStore) myData.getFeatureSource( 
typeName );
>Transaction t = new DefaultTransaction();
>try {
>     FeatureCollection collection = fs.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();
>}
>
>Give that a go (in case I got something wrong) and send me back the 
>completed code example so I can dump it in the user guide :-)
It works! I tried to do the same thing with one difference:

String typeName = fs.getSchema().getTypeName();
FeatureStore featureStore = (FeatureStore) myData.getFeatureSource( 
typeName );

I assumed since I was using the same schema the type name would be the
same....Humm....

Anyways, here's the full final code, including the writing to a file part:

        FeatureSource fs = ml.getFeatureSource(); //existing map layer (in
memory data store)
        FeatureType ft = fs.getSchema(); 
        try
        {

          IndexedShapefileDataStore dataStore = new
IndexedShapefileDataStore(f.toURI().toURL());
          dataStore.createSchema(ft);
          dataStore.forceSchemaCRS(currentCRS);
          

          String typeName = dataStore.getTypeNames()[0];
          FeatureStore featureStore = (FeatureStore)
dataStore.getFeatureSource(typeName );
          Transaction t = new DefaultTransaction();
          try {
            FeatureCollection collection = fs.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();
          }
          DefaultTransaction transaction = new DefaultTransaction("test");
          FeatureWriter aWriter = dataStore.getFeatureWriter(transaction);
          try
          {
            while(aWriter.hasNext())
            {
              aWriter.next();
              aWriter.write();
              transaction.commit();
            }
          } catch (Exception e) {
            transaction.rollback();
            logMessage(Constants.ERROR, e.getMessage());
          } finally {
            transaction.close();
          }
        } catch (Exception e) {
        }


Finally note that since my goal was writing the data to a file, I was able
to bypass the stumbling block of the 
FeatureSource this way:

        FeatureSource fs = ml.getFeatureSource();
        FeatureType ft = fs.getSchema();
        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();
          Feature feature;
          try
          {
            while(iterator.hasNext())
            {
              feature = (Feature) iterator.next();
              SimpleFeature aNewFeature = (SimpleFeature)aWriter.next();
              aNewFeature.setAttributes(feature.getAttributes(null));
              aWriter.write();
              transaction.commit();
            }
          } catch (Exception e) {
            transaction.rollback();
            logMessage(Constants.ERROR, e.getMessage());
          } finally {
            transaction.close();
            fc.close(iterator);
          }

        } catch (Exception e) {

        } 



















-- 
View this message in context: 
http://www.nabble.com/Saving-FeatureSource-to-shapefile-tp14826700p14932272.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to