ArcObjects Library Reference  (Location)    

GeocodedFeatureClass_Example

[Visual Basic 6.0]
Public Sub AutomateGeocodedFeatureClass(pWorkspace As esriGeoDatabase.IWorkspace, strFeatureClassName As String, strTableName As String)
 
  '+++ This example demonstrates how to automate a geocoded feature class so that edits to the address table are
  '+++ automatically reflected in the geocoded feature class. This code presented here makes the following assumptions:
  '+++  (1) A reference to the workspace containing the address table and geocoded feature class has
  '+++      already been obtained.
  '+++  (2) The feature class was created by geocoding the address table, and that the locator is attached
  '+++      to the geocoded feature class. Use the LocatorManager object to determine if the feature class
  '+++      has an attached locator.
  '+++  (3) The fields containing the address information in the address table are named "Address" and "ZIP".
  '+++      This example assumes that the locator used to create the geocoded feature class was a US Streets
  '+++      with Zone (GDB) locator. Use the IAddressInputs interface on the locator to determine the address
  '+++      field names in the address table.
  '+++  (4) The geocoded feature class contains a field named "Join_OID" which is a foreign key to the ObjectID
  '+++      field on the address table.
 
  Dim pESRILicenseInfo As esriSystem.IESRILicenseInfo
  Dim pFeatureWorkspace As esriGeoDatabase.IFeatureWorkspace
  Dim pFeatureClass As esriGeoDatabase.IFeatureClass
  Dim pTable As esriGeoDatabase.ITable
  Dim strAddressFieldNames() As String
  Dim pPropertySet As esriSystem.IPropertySet
  Dim pSchemaLock As esriGeoDatabase.ISchemaLock
  Dim pClassSchemaEdit As esriGeoDatabase.IClassSchemaEdit3
  Dim pUID As New esriSystem.UID
  Dim pRelationshipClass As esriGeoDatabase.IRelationshipClass2
 
  '+++ the user must have either an ArcEditor or ArcInfo license to automate the geocoded feature class
  Set pESRILicenseInfo = New esriSystem.ESRILicenseInfo
  If pESRILicenseInfo.DefaultProduct = esriProductCodeViewer Then Exit Sub
 
  '+++ only geocoded feature classes in geodatabases can be automated
  If pWorkspace.Type = esriFileSystemWorkspace Then Exit Sub
 
  '+++ get references to the geocoded feature class and address table
  Set pFeatureWorkspace = pWorkspace
  Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strFeatureClassName)
  Set pTable = pFeatureWorkspace.OpenTable(strTableName)
 
  '+++ register the feature class as a GeocodedFeature custom feature class
  Set pSchemaLock = pFeatureClass
  pSchemaLock.ChangeSchemaLock esriExclusiveSchemaLock
  Set pClassSchemaEdit = pFeatureClass
  pUID.Value = "esriLocation.GeocodedFeature"
  pClassSchemaEdit.AlterInstanceCLSID pUID
 
  '+++ create the property set for the Geocoded Feature Class extension
  ReDim strAddressFieldNames(0 To 1)
  strAddressFieldNames(0) = "ADDRESS"
  strAddressFieldNames(1) = "ZIP"
  Set pPropertySet = New esriSystem.PropertySet
  With pPropertySet
    .SetProperty "OriginalAddressFieldNames", strAddressFieldNames
    .SetProperty "UpdateOnEdit", True
    .SetProperty "UnmatchOnly", False
  End With
   
  '+++ register the GeocodedFeatureClassExtension on the feature class
  pUID.Value = "esriLocation.GeocodedFeatureClassExtension"
  pClassSchemaEdit.AlterClassExtensionCLSID pUID, pPropertySet
 
  '+++ create a relationship class between the address table and the geocoded feature class
  Set pRelationshipClass = pFeatureWorkspace.CreateRelationshipClass("Geocoding", pTable, pFeatureClass, _
    "is geocoded to", "is geocoded from", esriRelCardinalityOneToOne, esriRelNotificationForward, True, _
    False, Nothing, pFeatureClass.OIDFieldName, "", "Join_OID", "")
 
  '+++ release the schema lock on the feature class
  pSchemaLock.ChangeSchemaLock esriSharedSchemaLock
   
End Sub

[Visual Basic .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)

        ' Gets 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 propertySet for the GeocodedFeatureClassExtention
        Dim addressFieldNames() As String = {"ADDRESS", "ZIP"}
        Dim propertySet = 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
        Dim feature As IFeature = featureClass.GetFeature(1)
        Dim row As IObject = table.GetRow(1)
        geocodedFeatureClass.GeocodeAddress(row, feature)
    End Sub

[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);

            // Gets 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 = { "ADDRESS", "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;

            IFeature feature = featureClass.GetFeature(1);
            IObject row = table.GetRow(1) as IObject;

            geocodedFeatureClass.GeocodeAddress(row, feature);

            IDataset ds = relationshipClass as IDataset;
            ds.Delete();
        }