Add new field in existing shape file using OGR in C#.
public void AddAttributeField(string oldShapeFile)
{
Ogr.RegisterAll();
DataSource dataSource = Ogr.Open(oldShapeFile, 1); //second argument in open specifies mode of data, 1 RW & 0 readonly mode
Layer layer = dataSource.GetLayerByIndex(0);
FieldDefn gdalFiedlDefn = new FieldDefn("NEW_FIELD",FieldType.OFTInteger);
layer.CreateField(gdalFiedlDefn, 1);
Feature feature = layer.GetNextFeature();
while (feature!= null)
{
feature.SetField("NEW_FIELD",feature.GetFID()); // Populate new field with feature FID
layer.SetFeature(feature);
feature = layer.GetNextFeature();
}
dataSource.FlushCache();
}

0 comments :
Post a Comment