Good work!

Updating the page with your example here:
- http://docs.codehaus.org/display/GEOTDOC/11+Exporting+to+A+Shapefile
>> 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 am confused? Oh wait I get it ... the reason I did not do this is 
because sometimes the typeName will
be different (often for a shapefile it is the same as the filename - no 
matter what you asked for!).

> 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();
>           }
>   
Cool.

So what is the following? It looks scary with a commit after each call 
to write() ?
>           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:
>   
See comment inline!
>         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 = (SimpleFeature) 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 {
>             fc.close(iterator); // close iterator first
>             transaction.close();
>           }
>         } catch (Exception e) {
>
>         } 
>   
I did not know you could do: feature.getAttributes( null ) - suppose 
that represents the lack of an xpath expression?
I expected you to use feature.getAttributes().

Thanks for the great code example; please review the page to make sure I 
did not kill anything important.
Jody

-------------------------------------------------------------------------
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