ArcObjects Library Reference  

FindClosestIntersection

About the Find the closest intersection from a point Sample

[C#]

FindClosestIntersection.cs

using System;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Location;
using ESRI.ArcGIS.esriSystem;

namespace ClosestIntersectionFromPoint
{
    class FindClosestIntersection
    {
        private static IAoInitialize m_License = null;

        // X and Y values can either be passed in at the command line or from the command prompt after starting
        static void Main(string[] args)
        {
            // Initialize the license
            getLicense();

            // See if the address point was passed in at the command line and if not, enter the values now
            double X, Y;
            if (args == null || args.Length == 0)
            {
                Console.WriteLine("Enter a X value: ");
                X = double.Parse(Console.ReadLine());
                Console.WriteLine("Enter a Y value: ");
                Y = double.Parse(Console.ReadLine());
            }
            else
            {
                X = double.Parse(args[0]);
                Y = double.Parse(args[1]);
            }

            // Call the reverseGeocode method
            ReverseGeocodeIntersection(X, Y);

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();

            // Return the license
            returnLicense();
        }

        /// <summary>
        /// The main functionality to use Intersection Reverse Geocoding 
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        private static void ReverseGeocodeIntersection(double X, double Y)
        {
            // Get a locator from the locator Workspace
            System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
            ILocatorManager2 locatorManager = obj as ILocatorManager2;
            ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath(@"C:\California_fdb.gdb");
            ILocator locator = locatorWorkspace.GetLocator("California_streets_10");
            IReverseGeocoding reverseGeocoding = locator as IReverseGeocoding;

            // Get the spatial reference from the locator
            IAddressGeocoding addressGeocoding = locator as IAddressGeocoding;
            IFields matchFields = addressGeocoding.MatchFields;
            IField shapeField = matchFields.get_Field(matchFields.FindField("Shape"));
            ISpatialReference spatialReference = shapeField.GeometryDef.SpatialReference;

            // Set up the point from the X and Y values
            IPoint point = new PointClass();
            point.SpatialReference = spatialReference;
            point.X = X;
            point.Y = Y;

            // Set the search tolerance for reverse geocoding
            IReverseGeocodingProperties reverseGeocodingProperties = reverseGeocoding as IReverseGeocodingProperties;
            reverseGeocodingProperties.SearchDistance = 2;
            reverseGeocodingProperties.SearchDistanceUnits = esriUnits.esriKilometers;

            // Determine if the locator supports intersection geocoding.
            // intersectionGeocoding will be null if it is not supported.
            IIntersectionGeocoding intersectionGeocoding = locator as IIntersectionGeocoding;
            if (intersectionGeocoding == null)
            {
                Console.WriteLine("You must use a locator that supports intersections.  Use a locator that was built off of one"
                    + "of the US Streets Locator styles.");
            }
            else
            {
                // Find the intersection that is nearest to the Point
                IPropertySet addressProperties = reverseGeocoding.ReverseGeocode(point, true);

                // Print the intersection properties
                IAddressInputs addressInputs = reverseGeocoding as IAddressInputs;
                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));
                }
            }
        }

        #region Helper Methods

        private static void getLicense()
        {
            if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop))
                throw new Exception("Could not set version. ");
            m_License = new AoInitializeClass();
            m_License.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
        }

        private static void returnLicense()
        {
            m_License.Shutdown();
        }

        #endregion
    }
}

[Visual Basic .NET]

FindClosestIntersection.vb

Imports Microsoft.VisualBasic
Imports System
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Location
Imports ESRI.ArcGIS.esriSystem

Namespace ClosestIntersectionFromPoint
	Friend Class FindClosestIntersection
		Private Shared m_License As IAoInitialize = Nothing

		' X and Y values can either be passed in at the command line or from the command prompt after starting
		Shared Sub Main(ByVal args() As String)
			' Initialize the license
			getLicense()

			' See if the address point was passed in at the command line and if not, enter the values now
			Dim X, Y As Double
			If args Is Nothing OrElse args.Length = 0 Then
				Console.WriteLine("Enter a X value: ")
				X = Double.Parse(Console.ReadLine())
				Console.WriteLine("Enter a Y value: ")
				Y = Double.Parse(Console.ReadLine())
			Else
				X = Double.Parse(args(0))
				Y = Double.Parse(args(1))
			End If

			' Call the reverseGeocode method
			ReverseGeocodeIntersection(X, Y)

			Console.WriteLine("Press any key to exit...")
			Console.ReadLine()

			' Return the license
			returnLicense()
		End Sub

		''' <summary>
		''' The main functionality to use Intersection Reverse Geocoding 
		''' </summary>
		''' <param name="X"></param>
		''' <param name="Y"></param>
		Private Shared Sub ReverseGeocodeIntersection(ByVal X As Double, ByVal Y As Double)
      ' Get a locator from the locator Workspace
      Dim obj As System.Object = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"))
			Dim locatorManager As ILocatorManager2 = TryCast(obj, ILocatorManager2)
      Dim locatorWorkspace As ILocatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath("C:\California_fdb.gdb")
			Dim locator As ILocator = locatorWorkspace.GetLocator("CaliforniaTest")
			Dim reverseGeocoding As IReverseGeocoding = TryCast(locator, IReverseGeocoding)

			' Get the spatial reference from the locator
			Dim addressGeocoding As IAddressGeocoding = TryCast(locator, IAddressGeocoding)
			Dim matchFields As IFields = addressGeocoding.MatchFields
			Dim shapeField As IField = matchFields.Field(matchFields.FindField("Shape"))
			Dim spatialReference As ISpatialReference = shapeField.GeometryDef.SpatialReference

			' Set up the point from the X and Y values
			Dim point As IPoint = New PointClass()
			point.SpatialReference = spatialReference
			point.X = X
			point.Y = Y

			' Set the search tolerance for reverse geocoding
			Dim reverseGeocodingProperties As IReverseGeocodingProperties = TryCast(reverseGeocoding, IReverseGeocodingProperties)
			reverseGeocodingProperties.SearchDistance = 2
			reverseGeocodingProperties.SearchDistanceUnits = esriUnits.esriKilometers

			' Determine if the locator supports intersection geocoding.
			' intersectionGeocoding will be null if it is not supported.
			Dim intersectionGeocoding As IIntersectionGeocoding = TryCast(locator, IIntersectionGeocoding)
			If intersectionGeocoding Is Nothing Then
				Console.WriteLine("You must use a locator that supports intersections.  Use a locator that was built off of one" & "of the US Streets Locator styles.")
			Else
				' Find the intersection that is nearest to the Point
				Dim addressProperties As IPropertySet = reverseGeocoding.ReverseGeocode(point, True)

				' Print the intersection properties
				Dim addressInputs As IAddressInputs = TryCast(reverseGeocoding, IAddressInputs)
				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).ToString())
				Next i
			End If
		End Sub

		#Region "Helper Methods"

		Private Shared Sub getLicense()
			If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)) Then
				Throw New Exception("Could not set version. ")
			End If
			m_License = New AoInitializeClass()
			m_License.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced)
		End Sub

		Private Shared Sub returnLicense()
			m_License.Shutdown()
		End Sub

		#End Region
	End Class
End Namespace