In this topic
- Using the geoprocessing tool
- Using the underlying ArcObjects
- Creating an ArcSDE locator
- Adding a place name alias table to a locator
Using the geoprocessing tool
You usually create an address locator by using the CreateAddressLocator geoprocessing tool located in the Geocoding toolbox. See the following code example:
[C#]
public void AddressLocatorViaGeoprocessing()
{
Geoprocessor GP = new Geoprocessor();
GP.OverwriteOutput = true;
String workspacePath = @"C:\UnitedStates.gdb";
CreateAddressLocator createAddressLocator = new CreateAddressLocator();
createAddressLocator.in_address_locator_style = "US Address - Dual Ranges";
// Put quotes around the path name if it has spaces.
String referenceData = workspacePath + @"\US_Streets 'Primary Table'";
createAddressLocator.in_reference_data = referenceData;
createAddressLocator.in_field_map = "'Feature ID' ID;" +
"'*From Left' L_F_F_ADD;" + "'*To Left' L_T_F_ADD;" +
"'*From Right' R_F_F_ADD;" + "'*To Right' R_T_F_ADD;" +
"'Prefix Direction' PREDIR;" + "'Prefix Type' NAMEPREFIX;" +
"'*Street Name' NAME;" + "'Suffix Type' NAMESUFFIX;" +
"'Suffix Direction' SUFDIR;" + "'Left City or Place' pcLo_NAMEL;" +
"'Right City or Place' pcRo_NAMER;" + "'Left Zipcode' L_PC;" +
"'Right Zipcode' R_PC;" + "'Left State' L_ORDER01;" +
"'Right State' R_ORDER01";
createAddressLocator.out_address_locator = workspacePath + @"\US_Locator";
createAddressLocator.config_keyword = "#";
try
{
IGeoProcessorResult result = GP.Execute(createAddressLocator, null)as
IGeoProcessorResult;
if (result != null)
{
if (result.Status != esriJobStatus.esriJobSucceeded)
Console.WriteLine("Failed to create the address Locator: ");
else
Console.WriteLine("Created the address Locator. ");
}
else
{
if (GP.MessageCount != 0)
{
for (int i = 0; i < GP.MessageCount; i++)
{
Console.WriteLine("GP Message " + i + " " + GP.GetMessage(i));
}
}
else
Console.WriteLine("Execution failed with no status. ");
}
}
catch (Exception e)
{
Console.WriteLine(
"An Exception occured while executing the CreateAddressLocator Tool: "
+ e);
}
}
[VB.NET]
Public Sub AddressLocatorViaGeoprocessing()
Dim GP As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor
Dim workspacePath As String = "C:\UnitedStates.gdb"
Dim createAddressLocator As CreateAddressLocator = New CreateAddressLocator()
' Put quotes around the path name if it has spaces.
createAddressLocator.in_address_locator_style = "US Address - Dual Ranges"
Dim referenceData As String = workspacePath & "\US_Streets 'Primary Table'"
createAddressLocator.in_reference_data = referenceData
createAddressLocator.in_field_map = "'Feature ID' ID;" + _
"'*From Left' L_F_F_ADD;" + _
"'*To Left' L_T_F_ADD;" + _
"'*From Right' R_F_F_ADD;" + _
"'*To Right' R_T_F_ADD;" + _
"'Prefix Direction' PREDIR;" + _
"'Prefix Type' NAMEPREFIX;" + _
"'*Street Name' NAME;" + _
"'Suffix Type' NAMESUFFIX;" + _
"'Suffix Direction' SUFDIR;" + _
"'Left City or Place' pcLo_NAMEL;" + _
"'Right City or Place' pcRo_NAMER;" + _
"'Left Zipcode' L_PC;" + _
"'Right Zipcode' R_PC;" + _
"'Left State' L_ORDER01;" + _
"'Right State' R_ORDER01"
createAddressLocator.out_address_locator = workspacePath & "\US_Streets_Locator"
Try
Dim result As IGeoProcessorResult = GP.Execute(createAddressLocator, Nothing)
If (Not result Is Nothing) Then
If (Not result.Status = esriJobStatus.esriJobSucceeded) Then
Console.WriteLine("Failed to create the address Locator: ")
Else
Console.WriteLine("Created the address Locator. ")
End If
Else
If (Not GP.MessageCount = 0) Then
For i As Integer = 0 To GP.MessageCount - 1
Console.WriteLine("GP Message " & i & " " + GP.GetMessage(i))
Next
Else
Console.WriteLine("Execution failed with no status. ")
End If
End If
Catch ex As Exception
Console.WriteLine("An Exception occured while executing the CreateAddressLocator Tool: " & ex.ToString())
End Try
End Sub
Using the underlying ArcObjects
Do the following steps to use the underlying ArcObjects:
- Open the default local locator workspace to get the locator style.
- Get the locator style to base the new locator.
- Open the feature class to use as reference data.
- Set the feature class as the primary reference data table for the locator.
- Store the new locator in the same workspace as the reference data.
The following code example shows how to use the locator workspace, address locator style, and address locator reference data objects to create ESRIGen2AddressLocator:
[C#]
public void AddressLocatorPersonalGDBViaArcObjects()
{
// Open the default local locator workspace to get the locator style.
System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID(
"esriLocation.LocatorManager"));
ILocatorManager2 locatorManager2 = obj as ILocatorManager2;
ILocatorWorkspace locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath
("");
// Get the locator style to base the new locator.
ILocatorStyle locatorStyle = locatorWorkspace.GetLocatorStyle(
"US Address - Dual Ranges");
// Open the feature class to use as reference data.
IWorkspaceFactory2 workspaceFactory2 = new AccessWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory2.OpenFromFile(@
"D:\workspace\arcobjects\location\redlands.mdb", 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Streets");
// Set the feature class as the primary reference data table for the locator.
IDataset dataset = (IDataset)featureClass;
IReferenceDataTables referenceDataTables = (IReferenceDataTables)locatorStyle;
IEnumReferenceDataTable enumReferenceDataTable = referenceDataTables.Tables;
enumReferenceDataTable.Reset();
IReferenceDataTable referenceDataTable = enumReferenceDataTable.Next();
IReferenceDataTableEdit referenceDataTableEdit = (IReferenceDataTableEdit)
referenceDataTable;
IName name = dataset.FullName;
referenceDataTableEdit.Name_2 = (ITableName)name;
// Store the new locator in the same workspace as the reference data.
if (referenceDataTables.HasEnoughInfo)
{
locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath(@
"D:\workspace\arcobjects\location\redlands.mdb");
ILocator locator = locatorWorkspace.AddLocator("New Redlands Locator",
(ILocator)locatorStyle, "", null);
}
}
[VB.NET]
Public Sub AddressLocatorPersonalGDBViaArcObjects()
' Open the default local locator workspace to get the locator style.
Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
Dim locatorManager2 As ILocatorManager2 = CType(obj, ILocatorManager2)
Dim locatorWorkspace As ILocatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath("")
' Get the locator style to base the new locator.
Dim locatorStyle As ILocatorStyle = locatorWorkspace.GetLocatorStyle("US Address - Dual Ranges")
' Open the feature class to use as reference data.
obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"))
Dim workspaceFactory2 As IWorkspaceFactory2 = CType(obj, IWorkspaceFactory2)
Dim workspace As IWorkspace = workspaceFactory2.OpenFromFile("D:\workspace\arcobjects\location\redlands.gdb", 0)
Dim featureWorkspace As IFeatureWorkspace = CType(workspace, IFeatureWorkspace)
Dim featureClass As IFeatureClass = featureWorkspace.OpenFeatureClass("Streets")
' Set the feature class as the primary reference data table for the locator.
Dim dataset As IDataset = CType(featureClass, IDataset)
Dim referenceDataTables As IReferenceDataTables = CType(locatorStyle, IReferenceDataTables)
Dim enumReferenceDataTable As IEnumReferenceDataTable = referenceDataTables.Tables
enumReferenceDataTable.Reset()
Dim referenceDataTable As IReferenceDataTable = enumReferenceDataTable.Next
Dim referenceDataTableEdit As IReferenceDataTableEdit = CType(referenceDataTable, IReferenceDataTableEdit)
Dim Name As IName = dataset.FullName
referenceDataTableEdit.Name_2 = CType(Name, ITableName)
' Store the new locator in the same workspace as the reference data.
If referenceDataTables.HasEnoughInfo Then
locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath("D:\workspace\arcobjects\location\redlands.mdb")
Dim locator As ILocator = locatorWorkspace.AddLocator("New Redlands Locator", CType(locatorStyle, ILocator), "", Nothing)
End If
End Sub
Creating an ArcSDE locator
Do the following steps to create an ArcSDE locator:
- Open an ArcSDE workspace.
- Open the database locator workspace for the ArcSDE workspace.
- Get the US Address - Dual Ranges locator style.
- Open the feature workspace containing the reference data.
- Set the reference data on the new locator.
- Store the locator if the reference data is properly specified.
The following code example shows how to use the locator workspace and address locator style objects to create an ArcSDE locator:
[C#]
public void AddressLocatorArcSDEViaArcObjects()
{
// Open an ArcSDE workspace.
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("server", "mendota");
propertySet.SetProperty("instance", "esri_sde");
propertySet.SetProperty("database", "arcobjects");
propertySet.SetProperty("user", "sde");
propertySet.SetProperty("password", "sde");
propertySet.SetProperty("version", "SDE.Default");
System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID(
"esriDataSourcesGDB.SdeWorkspaceFactory"));
IWorkspaceFactory2 workspaceFactory2 = obj as IWorkspaceFactory2;
IWorkspace workspace = workspaceFactory2.Open(propertySet, 0);
// Open the database locator workspace for the ArcSDE workspace.
obj = Activator.CreateInstance(Type.GetTypeFromProgID(
"esriLocation.LocatorManager"));
ILocatorManager2 locatorManager2 = obj as ILocatorManager2;
ILocatorWorkspace locatorWorkspace = locatorManager2.GetLocatorWorkspace
(workspace);
// Get the Locator Style from the client workspace
ILocatorWorkspace clientLocatorWorkspace =
locatorManager.GetLocatorWorkspaceFromPath("");
ILocatorStyle locatorStyle = clientLocatorWorkspace.GetLocatorStyle(
"US Address - Dual Ranges");
// Open the feature workspace containing the reference data.
// Set the reference data on the new locator.
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(
"sde.SDE.altanta_st");
IDataset dataset = (IDataset)featureClass;
IReferenceDataTables referenceDataTables = (IReferenceDataTables)locatorStyle;
IEnumReferenceDataTable enumReferenceDataTable = referenceDataTables.Tables;
enumReferenceDataTable.Reset();
IReferenceDataTable referenceDataTable = enumReferenceDataTable.Next();
IReferenceDataTableEdit referenceDataTableEdit = (IReferenceDataTableEdit)
referenceDataTable;
referenceDataTableEdit.Name_2 = (ITableName)dataset.FullName;
// Store the locator if the reference data is properly specified.
if (referenceDataTables.HasEnoughInfo)
{
ILocator locator = locatorWorkspace.AddLocator("My New SDE Locator",
(ILocator)locatorStyle, "", null);
}
}
[VB.NET]
Public Sub AddressLocatorArcSDEViaArcObjects()
' Open an ArcSDE workspace.
Dim propertySet As IPropertySet = New PropertySetClass
With propertySet
.SetProperty("server", "mendota")
.SetProperty("instance", "esri_sde")
.SetProperty("database", "arcobjects")
.SetProperty("user", "sde")
.SetProperty("password", "sde")
.SetProperty("version", "SDE.Default")
End With
Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"))
Dim workspaceFactory2 As IWorkspaceFactory2 = CType(obj, IWorkspaceFactory2)
Dim workspace As IWorkspace = workspaceFactory2.Open(propertySet, 0)
' Open the database locator workspace for the ArcSDE workspace.
Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
Dim locatorManager2 As ILocatorManager2 = CType(obj, ILocatorManager2)
Dim locatorWorkspace As ILocatorWorkspace = locatorManager2.GetLocatorWorkspace(workspace)
' get the Locator Style from the client workspace
Dim clientLocatorWorkspace As ILocatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath("")
Dim locatorStyle As ILocatorStyle = clientLocatorWorkspace.GetLocatorStyle("US Address - Dual Ranges")
' Open the feature workspace containing the reference data.
' Set the reference data on the new locator.
Dim featureWorkspace As IFeatureWorkspace = CType(workspace, IFeatureWorkspace)
Dim featureClass As IFeatureClass = featureWorkspace.OpenFeatureClass("sde.SDE.altanta_st")
Dim dataset As IDataset = CType(featureClass, IDataset)
Dim referenceDataTables As IReferenceDataTables = CType(locatorStyle, IReferenceDataTables)
Dim enumReferenceDataTable As IEnumReferenceDataTable = referenceDataTables.Tables
enumReferenceDataTable.Reset()
Dim referenceDataTable As IReferenceDataTable = enumReferenceDataTable.Next
Dim referenceDataTableEdit As IReferenceDataTableEdit = CType(referenceDataTable, IReferenceDataTableEdit)
referenceDataTableEdit.Name_2 = CType(dataset.FullName, ITableName)
' Store the locator if the reference data is properly specified.
If referenceDataTables.HasEnoughInfo Then
Dim locator As ILocator = locatorWorkspace.AddLocator("My New SDE Locator", CType(locatorStyle, ILocator), "", Nothing)
End If
End Sub
Adding a place name alias table to a locator
Do the following steps to add a place name alias table to a locator:
- Open a workspace.
- Get the locator from the locator workspace.
- Get the table from the feature workspace.
- Find the address fields in the place name alias table.
- Setup the place name alias table.
- Add the new locator to the locator workspace.
See the following code example:
[C#]
public void PlaceNameAlias()
{
// 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 the Locator
obj = Activator.CreateInstance(Type.GetTypeFromProgID(
"esriLocation.LocatorManager"));
ILocatorManager locatorManager = obj as ILocatorManager2;
ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspace
(workspace);
ILocator locator = locatorWorkspace.GetLocator("USLocator");
// Get the Alias Table
IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
ITable aliasTable = featureWorkspace.OpenTable("aliasTable");
IDataset dataset = aliasTable as IDataset;
ITableName aliasTableName = dataset.FullName as ITableName;
// Find the address fields in the place name alias table
IAddressInputs addressInputs = locator as IAddressInputs;
IFields addressFields = addressInputs.AddressFields;
IField addressField = null;
String addressFieldNames = "";
String[] defaultInputFieldNames;
int fieldCount = addressFields.FieldCount;
for (int i = 0; i < fieldCount; i++)
{
addressField = addressFields.get_Field(i);
defaultInputFieldNames = addressInputs.get_DefaultInputFieldNames
(addressField.Name)as String[];
for (int j = 0; j < defaultInputFieldNames.Length; j++)
{
if (aliasTable.FindField(defaultInputFieldNames[j]) != - 1)
{
if (addressFieldNames != "")
addressFieldNames += ",";
addressFieldNames += defaultInputFieldNames[j];
}
}
}
// Setup IPlaceNameAlias
IPlaceNameAlias placeNameAlias = locator as IPlaceNameAlias;
placeNameAlias.Table = aliasTableName;
placeNameAlias.AddressFields = addressFieldNames;
placeNameAlias.AliasField = "AliasName";
// Update the locator
locatorWorkspace.UpdateLocator(locator);
}
[VB.NET]
Sub PlaceNameAlias()
' 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 the locator
obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
Dim locatorManager As ILocatorManager2 = obj
Dim locatorWorkspace As ILocatorWorkspace = locatorManager.GetLocatorWorkspace(workspace)
Dim locator As ILocator2 = locatorWorkspace.GetLocator("USLocator")
Dim featureWorkspace As IFeatureWorkspace = workspace
Dim aliasTable As ITable = featureWorkspace.OpenTable("aliasTable")
Dim dataset As IDataset = aliasTable
Dim aliasTableName As ITableName = dataset.FullName
' Find the address fields in the place name alias table
Dim addressInputs As IAddressInputs = locator
Dim addressFields As IFields = addressInputs.AddressFields
Dim addressField As IField
Dim addressFieldNames As String = ""
Dim defaultInputFieldNames() As String
Dim fieldCount As Integer = addressFields.FieldCount
For i As Integer = 0 To fieldCount - 1
addressField = addressFields.Field(i)
defaultInputFieldNames = addressInputs.DefaultInputFieldNames(addressField.Name)
For j As Integer = 0 To defaultInputFieldNames.Length - 1
If aliasTable.FindField(defaultInputFieldNames(j)) <> -1 Then
If addressFieldNames <> "" Then
addressFieldNames + = ","
End If
addressFieldNames + = defaultInputFieldNames(j)
End If
Next
Next
' Setup IPlaceNameAlias
Dim placeNameAlias As IPlaceNameAlias = locator
placeNameAlias.Table = aliasTableName
placeNameAlias.AddressFields = addressFieldNames
placeNameAlias.AliasField = "CONAME"
' Update the locator
locatorWorkspace.UpdateLocator(locator)
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):
ESRI.ArcGIS.DataSourcesGDB ESRI.ArcGIS.GeocodingTools ESRI.ArcGIS.Geodatabase ESRI.ArcGIS.Geoprocessor ESRI.ArcGIS.Location ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)System.IO
Development licensing | Deployment licensing |
---|---|
Engine Developer Kit | Engine Runtime |
ArcView | ArcView |
ArcEditor | ArcEditor |
ArcInfo | ArcInfo |