Tom,<br><br>Can you check the destination shapefile with 'ogrinfo' and see if it works?<br>If ogrinfo gives a valid output of the new file, check with 'ogrinfo -al' and compare it with the old file's output.<br>
( <a href="http://www.gdal.org/ogrinfo.html">http://www.gdal.org/ogrinfo.html</a> )<br><br><div class="gmail_quote">On Wed, Aug 4, 2010 at 6:57 PM, Tom Jeffery <span dir="ltr"><<a href="mailto:tjeffery@gmail.com">tjeffery@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
I'm working with GDAL's Java bindings, and trying to simply add an index<br>
field to a shapefile. This is working, but the resulting shapefile gets<br>
corrupted somehow, where viewing it in ArcMap causes the message "Warning,<br>
inconsistent extent." and viewing it in ArcCatalog shows all the points in<br>
the shapefile in the very upper left corner of the preview. (Viewing the<br>
metadata tab in ArcCatalog will crash the program.)<br>
<br>
Below is the method where I'm adding the index field... is there anything<br>
I'm missing / obviously wrong here? Any help would be greatly appreciated!<br>
<br>
Thanks,<br>
-Tom Jeffery<br>
<br>
public static void addIndex(File input, String fieldName) throws Exception<br>
{<br>
ogr.RegisterAll();<br>
DataSource dataSource = ogr.Open(input.getAbsolutePath(), true);<br>
<br>
if (dataSource == null) {<br>
throw new Exception("Error opening input file while trying to add<br>
index.");<br>
}<br>
<br>
// Shapefiles have a single layer<br>
Layer layer = dataSource.GetLayer(0);<br>
<br>
// Creating an OFTInteger alone didn't seem to do the trick, but setting<br>
the width to 9 created a "Long Integer"<br>
// field (as viewed in ESRI)<br>
FieldDefn newField = new FieldDefn(fieldName, ogr.OFTInteger);<br>
newField.SetWidth(9);<br>
int result = layer.CreateField(newField);<br>
if (result != ogr.OGRERR_NONE)<br>
throw new Exception("Got OGR error code: " + result + " when trying to<br>
add new field to shapefile.");<br>
<br>
for (int i = 0; i < layer.GetFeatureCount(); i++) {<br>
org.gdal.ogr.Feature feature = layer.GetFeature(i);<br>
feature.SetField(fieldName, i);<br>
layer.SetFeature(feature);<br>
feature.delete();<br>
}<br>
// you must call "delete" (or Destroy in c/c++) to properly close / write<br>
the datasource..<br>
layer.delete();<br>
dataSource.delete();<br>
}<br>
<font color="#888888">--<br>
View this message in context: <a href="http://osgeo-org.1803224.n2.nabble.com/gdal-dev-Editing-attributes-with-ogr-causing-corrupt-shapefile-tp5372483p5372483.html" target="_blank">http://osgeo-org.1803224.n2.nabble.com/gdal-dev-Editing-attributes-with-ogr-causing-corrupt-shapefile-tp5372483p5372483.html</a><br>
Sent from the GDAL - Dev mailing list archive at Nabble.com.<br>
_______________________________________________<br>
gdal-dev mailing list<br>
<a href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/gdal-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Best regards,<br>Chaitanya kumar CH.<br>/tʃaɪθənjə/ /kʊmɑr/ <br>+91-9494447584<br>17.2416N 80.1426E<br>