:No feature available to write" error

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

:No feature available to write" error

Dave Murray-Rust-2
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
Reply | Threaded
Open this post in threaded view
|

Re: :No feature available to write" error

Dave Murray-Rust-2
On 6 Feb 2009, at 10:09, Dave Murray-Rust wrote:

> 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 found the problem, and it was due to my appalling misuse of  
transactions - I hadn't realised that after committing a transaction,  
it was necessary to create a new one, rather than keeping one  
transaction around and calling commit after each change. FWIW the  
error messages could be more useful - I'd have expected some kind of  
ClosedTransactionException or similar. But hey, it works!

Cheers,
dave

--
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
Reply | Threaded
Open this post in threaded view
|

Re: :No feature available to write" error

mbedward
Hi Dave,

Thanks for posting the solution to your problem - it's bound to be
handy to someone else in the future.

Michael

2009/2/11 Dave Murray-Rust <[hidden email]>:
> I've found the problem, and it was due to my appalling misuse of
> transactions - I hadn't realised that after committing a transaction,
> it was necessary to create a new one, rather than keeping one
> transaction around and calling commit after each change. FWIW the
> error messages could be more useful - I'd have expected some kind of
> ClosedTransactionException or similar. But hey, it works!
>
> Cheers,
> dave

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
Reply | Threaded
Open this post in threaded view
|

Re: :No feature available to write" error

jody.garnett
In reply to this post by Dave Murray-Rust-2
Glad it worked out for you :-)

The idea of command line tools is a good one; Andrea and myself have often thought of starting a "tools" module for GeoTools that gathered up some of the functionality of the library for command line use. Perhaps you would be interested in starting an unsupported module on this topic? It could be a lot of fun.

I made a general "reshape" operations for uDig that I would be willing to add to the mix:
- http://udig.refractions.net/confluence/display/EN/Processing+the+Geometry+in+a+Shapefile
- http://udig.refractions.net/confluence/display/EN/Adding+a+column+to+a+shapefile

It uses CQL to allow you to define a new shapefile in terms of the previous one :-)

Jody



On Wed, Feb 18, 2009 at 5:48 AM, Dave Murray-Rust <[hidden email]> wrote:
Hi Jody,



On 13 Feb 2009, at 05:26, Jody Garnett wrote:

I'm getting the following error when I try to modify features in a
shapefile I've created:

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?

DiffFeatureWriter stores the features you want to write into memory; waiting until transaction commit is called before writing the shapefile out.

Thanks for the followup! I figured out what my issue was, and put it on the list - basically misusing transactions - along with a suggestion that the error message could be made to more closely relate to the issue, i.e. some kind of TransactionClosedException or similar.




I have not made a feature writer example for the user guide yet; I do not use it much. Here is a good example of how to write a shapefile:
- http://docs.codehaus.org/display/GEOTDOC/06+CSV2SHP+Lab

And also this:
- http://docs.codehaus.org/display/GEOTDOC/05+SHP2SHP+Lab

Is this what were talking about as example 5?

Yup, that's exactly what I meant. They're good docs, I'd just failed to understand the transaction model.

I've now developed a relatively general shapefile converter class which handles some of the stuff around reading/writing shapefiles. Would you be interested in adding this kind of thing into Geotools? More generally, I'm slowly developing a bunch of simple command line tools for doing things like randomizing attributes, setting all attributes to certain values, taking the first x polygons from a shapefile etc. with the idea that they may be useful for people who aren't that big on GIS, but want to fiddle with their shapefiles programatically.

Cheers,
dave



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



------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users