Thursday, January 19, 2017

Code snippet : Create/update polygon attribute fields using Ogr and ESRI Arcobjects

Be The First To Comment
A code snippet example to check if a feature is standalone ESRI Shape or featureclass inside ESRI Gdb, then add new filed or update attribute value in attribute table .... standalone shape/features are created/updated using GDAL/OGR implementation and feature/featureclass stored in ESRI personal GDB are created/updated using ESRI ArcObjects.  Any solution to replace later with GDAL/Ogr is highly appreciated.  Please comment below if you find one.

 public void AddUpdateAttributeField(string oldFeatureFile)  
     {  
       DriverUtils.RegisterOgrDriver();  
       DataSource dataSource;  
       Layer layer;  
       var isShapeFile = IsShapeInGdb(oldFeatureFile);  
       if (isShapeFile)  
       {  
         dataSource = Ogr.Open(oldFeatureFile, 1); //second argument in open specifies mode of data, 1 RW & 0 readonly mode  
         layer = dataSource.GetLayerByIndex(0);  
         FieldDefn gdalFiedlDefn = new FieldDefn("FID_GDAL",FieldType.OFTInteger);  
         layer.CreateField(gdalFiedlDefn, 0);  
         Feature feature = layer.GetNextFeature();  
         while (feature!= null)  
         {  
           feature.SetField("FID_GDAL",feature.GetFID()); // Add FID shapefile  
           layer.SetFeature(feature);  
           feature = layer.GetNextFeature();  
         }  
         dataSource.FlushCache();  
       }  
       else  
       {  
         try  
         {  
           EnableEsriLiscences();  
           string gdbPath = Path.GetDirectoryName(oldFeatureFile);  
           string featureName = Path.GetFileNameWithoutExtension(oldFeatureFile);  
           IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();  
           IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(gdbPath, 1);  
           IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(featureName);  
           IFields fields = featureClass.Fields;  
           if (fields.FindField("FID_GDAL") == -1)  
           {  
             // Create a Int field called "FID_GDAL" for the fields collection

Friday, January 13, 2017

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

Be The First To Comment
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();  
     }  
 

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

About Me