Hi all,

I'm getting the following error when I try to modify features in a  
shapefile I've created:
Exception in thread "main" java.io.IOException: No feature available  
to write
        at org.geotools.data.DiffFeatureWriter.write(DiffFeatureWriter.java: 
168)
        at org.geotools.data.InProcessLockingManager 
$1.write(InProcessLockingManager.java:330)
        at  
org 
.cecs 
.shapefileTools.ShapefileRandomiser.randomise(ShapefileRandomiser.java: 
86)
        at  
org 
.cecs.shapefileTools.ShapefileRandomiser.main(ShapefileRandomiser.java: 
22)

I've had a look through the source for DiffFeatureWriter, and I can't  
understand the meaning of the comments - it looks like it is supposed  
to refuse to write any features - is this correct?

My code is heavily based on example 5 in the docs, and the relevant  
bit is attached below.

So my question is what do I need to do to modify features and write  
them out. The slightly wider context is that I'm creating copies of  
shapefiles with randomised attributes based on the distributions in  
the original shapefile for anonymisation.

Thanks very much,
Dave Murray-Rust

---------------------------------

                DataStore data = getDataStore( filename );
                String typeName = data.getTypeNames()[0];
                FeatureCollection collection = getFeatureCollection( data );
                
<snip>

                DataStore newData = createDataStore( "data/output.shp" );
                newData.createSchema( collection.getSchema() );
                Transaction transaction = new DefaultTransaction();
                
                FeatureStore newStore =  
(FeatureStore)newData.getFeatureSource( typeName );
                features = getFeatures( data );
                newStore.setTransaction( transaction );
                try{
                        newStore.addFeatures( collection );
                        transaction.commit();
                }catch( Exception e )
                {
                        e.printStackTrace();
                        transaction.rollback();
                }
                FeatureWriter writer = newData.getFeatureWriter( typeName,  
transaction );
                int i = 0;
                System.out.println( "Got " +  
newData.getFeatureSource( typeName ).getFeatures().size() + " features  
" );
                while( writer.hasNext() )
                {
                        i++;
                        SimpleFeature f = (SimpleFeature)writer.next();
                        System.out.println( "feature " + i );
                        for( String prop : properties)
                        {
                                try
                                {
                                        Object newVal = freqs.sample( prop );
                                        System.out.println( "Setting " + prop + 
" to " + newVal );
                                        f.setAttribute( prop, freqs.sample( 
prop ) );
                                        transaction.commit();
                                        writer.write();
                                } catch (IllegalAttributeException e)
                                {
                                        System.err.println( "Could not set 
feature " + prop );
                                        e.printStackTrace();
                                        transaction.rollback();
                                }
                        }
                }
                transaction.commit();
                transaction.close();
                writer.close();



-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.


------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to