Friday, January 13, 2017

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

C# , GDAL

0 comments :

Post a Comment

 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me