How to create a dynamic geocoded feature class


Creating a dynamic geocoded feature class

You can create a relationship between the geocoded feature class and the table of addresses. Once this relationship is created, any rows of addresses added, deleted, or updated from the address table automatically trigger the features in the output feature class to be re-geocoded and updated.
To create a dynamic geocoded feature class, perform the following steps:
  1. Obtain a reference to the geocoded feature class and the address table.
  2. Register the feature class as a geocoded feature custom feature class.
  3. Create a property set for the geocoded feature class extension.
  4. In this property set, set the OriginalAddressFieldNames with a String array of the address field names, and set the UpdateOnEdit and UnmatchOnly properties as well.
  5. Register the geocoded feature class extension with the geocoded feature class.
  6. Create the geocoded feature class.
See the following code example:
[C#]
public void GeocodedFeatureClass()
{
    // Get the workspace.
    System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID(
        "esriDataSourcesGDB.FileGDBWorkspaceFactory"));
    IWorkspaceFactory2 workspaceFactory = obj as IWorkspaceFactory2;
    IWorkspace workspace = workspaceFactory.OpenFromFile(@"C:\UnitedStates.gdb", 0);

    // Get references to the geocoded feature class and address table.
    IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
    IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("US_Locations");
    ITable table = featureWorkspace.OpenTable("Addresses");

    // Register the feature class as a GeocodedFeature custom feature class.
    ISchemaLock schemaLock = featureClass as ISchemaLock;
    schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
    IClassSchemaEdit classSchemaEdit = featureClass as IClassSchemaEdit;
    UID uid = new UIDClass();
    uid.Value = "esriLocation.GeocodedFeature";
    IPropertySet props = new PropertySetClass();
    classSchemaEdit.AlterInstanceCLSID(uid);

    // Create the property set for the Geocoded Feature Class extension.
    String[] addressFieldNames = 
    {
        "Street", "City", "State", "ZIP"
    };
    IPropertySet propertySet = new PropertySetClass();
    propertySet.SetProperty("OriginalAddressFieldNames", addressFieldNames);
    propertySet.SetProperty("UpdateOnEdit", true);
    propertySet.SetProperty("UnmatchOnly", false);

    // Register the GeocodedFeatureClassExtension on the feature class.
    uid.Value = "esriLocation.GeocodedFeatureClassExtension";
    classSchemaEdit.AlterClassExtensionCLSID(uid, propertySet);

    IRelationshipClass relationshipClass = featureWorkspace.CreateRelationshipClass(
        "Geocoding", table as IObjectClass, featureClass as IObjectClass, 
        "is geocoded to", "is geocoded from",
        esriRelCardinality.esriRelCardinalityOneToOne,
        esriRelNotification.esriRelNotificationForward, true, false, null,
        featureClass.OIDFieldName, "", table.OIDFieldName, "");

    schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);

    // Create the geocoded feature class.
    IGeocodedFeatureClass geocodedFeatureClass = (featureClass as IObjectClass)
        .Extension as IGeocodedFeatureClass;
}
[VB.NET]
Sub GeocodedFeatureClass()
    ' Get the workspace.
    Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"))
    Dim workspaceFactory As IWorkspaceFactory2 = obj
    Dim workspace As IWorkspace = workspaceFactory.OpenFromFile("C:\UnitedStates.gdb", 0)
    
    ' Get references to the geocoded feature class and address table.
    Dim featureWorkspace As IFeatureWorkspace = workspace
    Dim featureClass As IFeatureClass = featureWorkspace.OpenFeatureClass("US_Locations")
    Dim table As ITable = featureWorkspace.OpenTable("addresses")
    
    ' Register the feature class as a GeocodedFeature custom feature class.
    Dim schemaLock As ISchemaLock = featureClass
    schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock)
    Dim classSchemaEdit As IClassSchemaEdit = featureClass
    Dim uid As UID = New UID
    uid.Value = "esriLocation.GeocodedFeature"
    Dim props As IPropertySet = New PropertySet
    classSchemaEdit.AlterInstanceCLSID(uid)
    
    ' Create the property set for the GeocodedFeatureClassExtention.
    Dim addressFieldNames() As String = {"ADDRESS", "CITY", "STATE", "ZIP"}
    Dim propertySet As IPropertySet = New PropertySet

    propertySet.SetProperty("OriginalAddressFieldNames", addressFieldNames)

        propertySet.SetProperty("UpdateOnEdit", True)

            propertySet.SetProperty("UnmatchOnly", False)
                
                ' Register the GeocodedFeatureClassExtension on the feature class.
                uid.Value = "esriLocation.GeocodedFeatureClassExtension"
                classSchemaEdit.AlterClassExtensionCLSID(uid, propertySet)
                Dim relationshipClass As IRelationshipClass
                
                relationshipClass = featureWorkspace.CreateRelationshipClass("Geocoding", table, featureClass, _
                                    "is geocoded to ", "is geocoded from ", _
                                    esriRelCardinality.esriRelCardinalityOneToOne, _
                                    esriRelNotification.esriRelNotificationForward, _
                                    True, False, Nothing, featureClass.OIDFieldName, _
                                    "", table.OIDFieldName, "")
                
                schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock)
                
                ' Create the geocoded feature class.
                Dim objectClass As IObjectClass = featureClass
                Dim geocodedFeatureClass As IGeocodedFeatureClass = objectClass.Extension
            End Sub






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced