Skip to content

Instantly share code, notes, and snippets.

@nohe427
Last active February 25, 2016 14:17
Show Gist options
  • Save nohe427/fab91781a083e1e54ccb to your computer and use it in GitHub Desktop.
Save nohe427/fab91781a083e1e54ccb to your computer and use it in GitHub Desktop.
Update features after append
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
namespace NewAppend
{
class Program
{
private static LicenseInitializer m_AOLicenseInitializer = new NewAppend.LicenseInitializer();
//Parameters that are needed to be updated by user running
private static String fromDatabase = @"E:\Downloads\AppendZoningData\CityZoningData.gdb";
private static String toDatabase = @"E:\Downloads\AppendZoningData\CountyZoningData.gdb";
private static int numberOfSubTypes = 15;
private static string fieldToUpdate = "zone_name";
private static string featureClassName = "ZoningDistrict"; //These must exist in both GDBs
[STAThread()]
static void Main(string[] args)
{
//ESRI License Initializer generated code.
m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced },
new esriLicenseExtensionCode[] { });
//ESRI License Initializer generated code.
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = Activator.CreateInstance(factoryType) as IWorkspaceFactory;
IFeatureWorkspace fromDb = workspaceFactory.OpenFromFile(fromDatabase, 0) as IFeatureWorkspace;
IFeatureWorkspace toDb = workspaceFactory.OpenFromFile(toDatabase, 0) as IFeatureWorkspace;
IFeatureClass toFeatureClass = toDb.OpenFeatureClass(featureClassName);
IFeatureClass fromFeatureClass = fromDb.OpenFeatureClass(featureClassName);
int subCode;
IEnumSubtype EnumSubType = ((ISubtypes) fromFeatureClass).Subtypes;
EnumSubType.Next(out subCode);
List<String> subTypesList = new List<string>();
while (subCode < numberOfSubTypes)
{
String subName = ((ISubtypes) fromFeatureClass).SubtypeName[subCode];
subTypesList.Add(subName);
if (subCode == numberOfSubTypes - 1)
{
break;
}
EnumSubType.Next(out subCode);
}
#region buildWhereClause
StringBuilder stringBuilder = new StringBuilder(String.Format("{0} IN (", fieldToUpdate));
for (int i = 0; i < subTypesList.Count; i++)
{
if (i < subTypesList.Count - 1)
{
stringBuilder.Append(String.Format("'{0}',", i));
}
else
{
stringBuilder.Append(String.Format("'{0}')", i));
}
}
string whereClause = stringBuilder.ToString();
#endregion
IQueryFilter queryFilter = new QueryFilter { SubFields = fieldToUpdate, WhereClause = whereClause};
IFeatureCursor featureCursor = toFeatureClass.Update(queryFilter, false);
IFeature feature = featureCursor.NextFeature();
int fieldLength = featureCursor.Fields.Field[featureCursor.Fields.FindField(fieldToUpdate)].Length;
while (feature != null)
{
string futureValue =
subTypesList[int.Parse(feature.Value[featureCursor.FindField(fieldToUpdate)].ToString())];
if (futureValue.Length > 30)
{
futureValue = futureValue.Substring(0, fieldLength-1);
}
feature.Value[feature.Fields.FindField(fieldToUpdate)] = futureValue;
featureCursor.UpdateFeature(feature);
feature = featureCursor.NextFeature();
}
//featureCursor.Fields.Field[featureCursor.FindField("zone_name")]
//Do not make any call to ArcObjects after ShutDownApplication()
m_AOLicenseInitializer.ShutdownApplication();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment