[udig-devel] new not existing shapefile opened for reading

Jody Garnett jgarnett at refractions.net
Mon Mar 10 13:49:47 PDT 2008


Hello andrea, lets see if we can help. First of all what version of udig 
are you using? Shapefile got a lot of attention on trunk and we may of 
introduced a bug...it looks like you are using the createNewDataStore 
method so you are doing it right. I am available on the IRC channel if 
needed; and please let me know which version you are using. The 
tutorials folder has an example of writing a shapefile that you can run 
if needed.

I *have* had some problem with file.toURI() vs file.toURL().toURI() 
previously - perhaps that is it?
Jody

> I think I'm gone mad, since I am no more able to write a shapefile...
> Following Jody's/Jesse's tutorials about writing shapefiles from
> featurecollections properly, things that used to work in the past do
> not work for me any longer.
>
> The problem seems to be that it always tries to open the file as a
> reading channel, supposing that it exists already.
>
> java.io.FileNotFoundException: /home/moovida/TMP/brenta_net.shp
> 	at org.geotools.data.shapefile.ShapefileDataStore.getReadChannel(ShapefileDataStore.java:293)
> 	at org.geotools.data.shapefile.ShapefileDataStore.openShapeReader(ShapefileDataStore.java:448)
> 	at org.geotools.data.shapefile.ShapefileDataStore.readAttributes(ShapefileDataStore.java:659)
> 	at org.geotools.data.shapefile.ShapefileDataStore.getSchema(ShapefileDataStore.java:615)
> 	at org.geotools.data.AbstractFileDataStore.getFeatureSource(AbstractFileDataStore.java:70)
>
> Am I missing some change in the api?
>
> Shouldn't it be something like:
>
>             // Create the DataStoreFactory
>             ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
>
>             // Create the file you want to write to
>             File file = null;
>             if (shapeFilePath.toLowerCase().endsWith(".shp")) {
>                 file = new File(shapeFilePath);
>             } else {
>                 file = new File(shapeFilePath + ".shp");
>             }
>             // Create a Map object used by our DataStore Factory
>             // NOTE: file.toURI().toURL() is used because file.toURL()
> is deprecated
>             Map<String, URL> map = Collections.singletonMap("url",
> file.toURI().toURL());
>
>             // Create the ShapefileDataStore from our factory based on
> our Map object
>             ShapefileDataStore dStore = (ShapefileDataStore)
> factory.createNewDataStore(map);
>
>             dStore.createSchema(fet.getSchema());
>             dStore.forceSchemaCRS(crs);
>
> [...]
>
>         String featureName = dStore.getTypeNames()[0];
>         DefaultTransaction transaction = null;
>         FeatureStore store = null;
>
>
>             // Create the DefaultTransaction Object
>             transaction = new DefaultTransaction();
>
>             // Tell it the name of the shapefile it should look for in
> our DataStore
>             store = (FeatureStore) data.getFeatureSource(featureName);
>
>             // Then set the transaction for that FeatureStore
>             store.setTransaction(transaction);
>             store.addFeatures(collection);
>             transaction.commit();
>             transaction.close();
>             return true;
>
>
> ???????
>
> Thanks for any help,
> Andrea
> _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel
>   



More information about the udig-devel mailing list