Code snippet: Create new Field in a Shape File using GDAL/OGR in C#

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();  
     }  

Comments

Popular posts from this blog

Prevent WPF Global Keyboard Hook from stops working after hitting keys a while C# .NET

Generate ArcGIS Token By URL Request From ArcGIS Portal, Federated Environment , ESRI JS API and Angular snippets

Solution: IntelliJ not recognizing a particular file correctly, instead its stuck as a text file