I'm using ShapefileDataStore (GT2.3) and I have realized that it doesn't
store null geometries in the way it should (well, or the way I think it
should). If I set the default geometry of a feature to null, I write the
shapefile and I read it again I get an empty geometry instead of a null. I
have read in the shapefile specification the following:

"A PolyLine is an ordered set of vertices that consists of one or more
parts. A part is a
connected sequence of two or more points. Parts may or may not be connected
to one
another. Parts may or may not intersect one another.
Because this specification does not forbid consecutive points with identical
coordinates,
shapefile readers must handle such cases. On the other hand, the degenerate,
zero length
parts that might result are not allowed."

And I think the last sentence is not respected by geotools writer.

I also think that it can be resolved by changing in the
org.geotools.data.shapefile.shp.ShapefileWriter.writeGeometry(Geometry)
method this code:

shapeBuffer.putInt(type.id);
handler.write(shapeBuffer,g);

by this one:

if (g.isEmpty()) {
       shapeBuffer.putInt(ShapeType.NULL.id);
}else{
     shapeBuffer.putInt(type.id);
     handler.write(shapeBuffer,g);
}

Can some one tell me if I'm right or wrong? Also, is this the right mailing
list?

Best regards,
Fernando Gonzalez

Here I copy paste some code to reproduce the use case:

       // create new shapefile data store
       ShapefileDataStore newShapefileDataStore = new ShapefileDataStore(
               new URL("file:///tmp/newSHP.shp"));

       // create the schema using from the original shapefile
       AttributeType[] atts = new AttributeType[2];
       atts[0] = AttributeTypeFactory.newAttributeType("the_geom",
               LineString.class);
       atts[1] = AttributeTypeFactory.newAttributeType("sometext",
               String.class);
       FeatureType ft = FeatureTypeBuilder.newFeatureType(atts, "type");
       newShapefileDataStore.createSchema(ft);

       WKTReader wktReader = new WKTReader();
       LineString geometry = (LineString) wktReader
               .read("LINESTRING (0 0, 10 10)");
       String value = "cool";

       FeatureWriter writer = newShapefileDataStore
               .getFeatureWriter(Transaction.AUTO_COMMIT);
       Feature newF = writer.next();
       newF.setAttribute("the_geom", geometry);
       newF.setAttribute("sometext", value);
       newF = writer.next();
       newF.setAttribute("the_geom", null);
       newF.setAttribute("sometext", value);

       writer.write();
       writer.close();

       newShapefileDataStore = new ShapefileDataStore(new URL(
               "file:///tmp/newSHP.shp"));
       FeatureCollection fc = newShapefileDataStore.getFeatureSource()
               .getFeatures();
       FeatureIterator fi = fc.features();
       while (fi.hasNext()) {
           Feature f = fi.next();
           Object[] values = f.getAttributes(null);
           for (Object object : values) {
               System.out.println(object);
           }

       }
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to