In this topic
Setting geocoding properties
To get and set geocoding properties, perform the following steps:
- Get the locator.
- Get and set any of the geocoding properties using the IGeocodingProperties interface with either of the following options:
- Set the geocoding properties temporarily for use in a simple program or method, but do not call UpdateLocator. These changes are not saved in the locator the next time the locator is loaded. See the following code example:
public void GeocodingProperties()
{
// 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"));
ILocatorManager2 locatorManager = obj as ILocatorManager2;
ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspace
(workspace);
ILocator locator = locatorWorkspace.GetLocator("USLocator")as ILocator2;
// Set the geocoding properties.
IGeocodingProperties2 geocodingProperties = locator as IGeocodingProperties2;
geocodingProperties.EndOffsetDistance = 10;
geocodingProperties.EndOffsetDistanceUnits = esriUnits.esriFeet;
geocodingProperties.AddReferenceIDToMatchFields = true;
locatorWorkspace.UpdateLocator(locator);
}
[VB.NET]
Sub GeocodingProperties()
' Open a workspace from a file geodatabase.
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 a locator from the locator workspace.
obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
Dim locatorManager As ILocatorManager2 = obj
Dim locatorWorkspace As ILocatorWorkspace = locatorManager.GetLocatorWorkspace(workspace)
Dim locator As ILocator = locatorWorkspace.GetLocator("USLocator")
' Set the geocoding properties.
Dim geocodingProperties As IGeocodingProperties = locator
geocodingProperties.EndOffsetDistance = 10
geocodingProperties.EndOffsetDistanceUnits = esriUnits.esriFeet
geocodingProperties.AddReferenceIDToMatchFields = True
locatorWorkspace.UpdateLocator(locator)
End Sub
- Call UpdateLocator after the geocoding properties have been set. In this case, the geocoding properties are updated the next time the locator is loaded.
Setting reverse geocoding properties
To get and set reverse geocoding properties, perform the following steps:
- Get the locator.
- Get and set any of the reverse geocoding properties using the IReverseGeocodingProperties interface with either of the following options:
- Set the reverse geocoding properties temporarily for use in a simple program or method, but do not call UpdateLocator. These changes are not saved in the locator the next time the locator is loaded. See the following code example:
public void reverseGeocoding()
{
// Open a workspace from a file geodatabase.
System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID(
"esriDataSourcesGDB.FileGDBWorkspaceFactory"));
IWorkspaceFactory2 workspaceFactory = obj as IWorkspaceFactory2;
IWorkspace workspace = workspaceFactory.OpenFromFile(@"C:\UnitedStates.gdb", 0);
// Get a locator from the locator workspace.
obj = Activator.CreateInstance(Type.GetTypeFromProgID(
"esriLocation.LocatorManager"));
ILocatorManager2 locatorManager = obj as ILocatorManager2;
ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspace
(workspace);
IReverseGeocoding reverseGeocoding = (IReverseGeocoding)
locatorWorkspace.GetLocator("USLocator");
// Create a point at which to find the address.
IAddressGeocoding addressGeocoding = (IAddressGeocoding)reverseGeocoding;
IFields matchFields = addressGeocoding.MatchFields;
IField shapeField = matchFields.get_Field(matchFields.FindField("Shape"));
IPoint point = new PointClass();
point.SpatialReference = shapeField.GeometryDef.SpatialReference;
point.X = - 117.2;
point.Y = 34.06;
// Set the search tolerance for reverse geocoding.
IReverseGeocodingProperties reverseGeocodingProperties =
(IReverseGeocodingProperties)reverseGeocoding;
reverseGeocodingProperties.SearchDistance = 100;
reverseGeocodingProperties.SearchDistanceUnits = esriUnits.esriFeet;
// Find the address nearest the point.
IPropertySet addressProperties = reverseGeocoding.ReverseGeocode(point, false);
// Print the address properties.
IAddressInputs addressInputs = (IAddressInputs)reverseGeocoding;
IFields addressFields = addressInputs.AddressFields;
for (int i = 0; i < addressFields.FieldCount; i++)
{
IField addressField = addressFields.get_Field(i);
Console.WriteLine(addressField.AliasName + ": " +
addressProperties.GetProperty(addressField.Name));
}
}
[VB.NET]
Sub ReverseGeocoding()
' Open a workspace from a file geodatabase.
Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"))
Dim workspaceFactory As IWorkspaceFactory2 = obj
Dim workspace As IWorkspace = workspaceFactory.OpenFromFile("C:\Redlands\Redlands93.gdb", 0)
' Get a locator from the locator workspace.
obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
Dim locatorManager As ILocatorManager2 = obj
Dim locatorWorkspace As ILocatorWorkspace = locatorManager.GetLocatorWorkspace(workspace)
Dim locator As ILocator = locatorWorkspace.GetLocator("Redlands Locator")
Dim reverseGeocoding As IReverseGeocoding = locator
' Create a point at which to find the address.
Dim addressGeocoding As IAddressGeocoding = reverseGeocoding
Dim matchFields As IFields = addressGeocoding.MatchFields
Dim shapeField As IField = matchFields.Field(matchFields.FindField("Shape"))
Dim point As IPoint = New Point
point.SpatialReference = shapeField.GeometryDef.SpatialReference
point.X = -117.2
point.Y = 34.06
' Set the search tolerance for reverse geocoding.
Dim reverseGeocodingProperties As IReverseGeocodingProperties = reverseGeocoding
reverseGeocodingProperties.SearchDistance = 100
reverseGeocodingProperties.SearchDistanceUnits = esriUnits.esriMeters
' Find the address nearest the point.
Dim addressProperties As IPropertySet = reverseGeocoding.ReverseGeocode(point, False)
' Print the address properties.
Dim addressInputs As IAddressInputs = locator
Dim addressFields As IFields = addressInputs.AddressFields()
For i As Integer = 0 To addressFields.FieldCount - 1
Dim addressField As IField = addressFields.Field(i)
Console.WriteLine(addressField.AliasName + ": " + addressProperties.GetProperty(addressField.Name))
Next
End Sub
- Call UpdateLocator after the reverse geocoding properties have been set. In this case, the reverse geocoding properties are updated the next time the locator is loaded.
Setting server locator properties
To get and set server locator properties, perform the following steps:
- Get the server geocoding properties using the GetLocatorProperties method on the IGeocodeServer interface.
- Set the server geocoding properties by modifying the properties in this PropertySet, and pass the PropertySet to the method you are calling using the propMods parameter on the method. Reference the table in GetLocatorProperties for the properties that can be updated.
See the following code example:
[C#]
public void GeocodeAddressServerContext()
{
// Connect to ArcGIS Server.
IGISServerConnection GISServerConnection = new GISServerConnectionClass();
GISServerConnection.Connect("AGSServer");
IServerObjectManager serverObjectManager =
GISServerConnection.ServerObjectManager;
IServerContext serverContext = serverObjectManager.CreateServerContext(
"Geocode/California", "GeocodeServer");
IGeocodeServer geocodeServer = (IGeocodeServer)serverContext.ServerObject;
// Construct an address property set to geocode.
IPropertySet addressProperties = (IPropertySet)serverContext.CreateObject(
"esriSystem.PropertySet");
addressProperties.SetProperty("Street", "380 New York St.");
addressProperties.SetProperty("City", "Redlands");
addressProperties.SetProperty("State", "CA");
addressProperties.SetProperty("ZIP", "92373");
// Set the tolerance for the address to match.
IPropertySet locatorProperties = geocodeServer.GetLocatorProperties();
locatorProperties.SetProperty("MinimumMatchScore", "100");
locatorProperties.SetProperty("SpellingSensitivity", "100");
// Geocode the address.
IPropertySet matchProperties = geocodeServer.GeocodeAddress(addressProperties,
locatorProperties);
// Print the match properties.
IFields matchFields = geocodeServer.GetResultFields(null);
for (int i = 0; i < matchFields.FieldCount; i++)
{
IField matchField = matchFields.get_Field(i);
if (matchField.Type == esriFieldType.esriFieldTypeGeometry)
{
IPoint point = (IPoint)matchProperties.GetProperty(matchField.Name);
if (!point.IsEmpty)
{
Console.WriteLine("X: " + point.X);
Console.WriteLine("Y: " + point.Y);
}
}
else
{
Console.WriteLine(matchField.AliasName + ": " +
matchProperties.GetProperty(matchField.Name));
}
}
}
[VB.NET]
Sub GeocodeAddressServerContext()
' Open a GISServerConnection.
Dim gisServerConnection As IGISServerConnection = New GISServerConnection
gisServerConnection.Connect("AGSServer")
' Get a GeocodeServer from the GISServerConnection.
Dim serverObjectManager As IServerObjectManager = gisServerConnection.ServerObjectManager
Dim serverContext As IServerContext = serverObjectManager.CreateServerContext("Geocode/California", "GeocodeServer")
Dim geocodeServer As IGeocodeServer = serverContext.ServerObject
' Construct an address PropertySet to geocode.
Dim addressProperties As IPropertySet = serverContext.CreateObject("esriSystem.PropertySet")
With addressProperties
.SetProperty("Street", "380 New York St.")
.SetProperty("City", "Redlands")
.SetProperty("State", "CA")
.SetProperty("ZIP", "92373")
End With
' Set the geocoding properties to use to geocode the address.
Dim locatorProperties As IPropertySet = geocodeServer.GetLocatorProperties
locatorProperties.SetProperty("MinimumMatchScore", "100")
locatorProperties.SetProperty("SpellingSensitivity", "100")
' Geocode the address.
Dim matchProperties As IPropertySet = geocodeServer.GeocodeAddress(addressProperties, locatorProperties)
' Print the match properties.
Dim matchFields As IFields = geocodeServer.GetResultFields(Nothing)
Dim matchField As IField
Dim point As IPoint
For matchFieldIndex As Integer = 0 To matchFields.FieldCount - 1
matchField = matchFields.Field(matchFieldIndex)
If matchField.Type = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry Then
point = matchProperties.GetProperty(matchField.Name)
If Not point.IsEmpty Then
Console.WriteLine("X: " & point.X)
Console.WriteLine("Y: " & point.Y)
End If
Else
Console.WriteLine(matchField.AliasName & ": " & matchProperties.GetProperty(matchField.Name))
End If
Next
End Sub
See Also:
How to geocode a single addressHow to geocode a table of addresses
How to find the address closest to a point using reverse geocoding
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.Location ESRI.ArcGIS.Geodatabase ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)
Development licensing | Deployment licensing |
---|---|
Engine Developer Kit | Engine Runtime |
ArcView | ArcView |
ArcEditor | ArcEditor |
ArcInfo | ArcInfo |