private void locatorButton_Click(object sender, EventArgs e)
        {
            addressLabel.Text = m_orgAddrLabel;

            DialogResult result = openFileDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                String locatorPath = openFileDialog.FileName;
                locatorPath = locatorPath.Substring(0, locatorPath.LastIndexOf('.'));
                if (locatorPath != null && locatorPath != "")
                {
                    locatorTextBox.Text    = locatorPath;
                    addressTextBox.Enabled = true;

                    // Open the workspace
                    String workspaceName = locatorPath.Substring(0, locatorPath.LastIndexOf("\\"));
                    String locatorName   = locatorPath.Substring(locatorPath.LastIndexOf("\\") + 1);


                    // Get the locator
                    System.Object     obj              = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
                    ILocatorManager2  locatorManager   = obj as ILocatorManager2;
                    ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath(workspaceName);
                    m_locator = locatorWorkspace.GetLocator(locatorName);

                    m_addressFields    = get_AddressFields();
                    addressLabel.Text += " (" + String.Join(",", m_addressFields) + ")";
                }
            }
        }
        public static ILocatorWorkspace GetSDELocatorWorkspace(String server, String instance, String database, String authenication, String version)
        {
            // Set up the SDE connection properties
            IPropertySet connectionProperties = new PropertySetClass();

            connectionProperties.SetProperty("SERVER", server);
            //propertySet.SetProperty("DBCLIENT", dbclient);
            connectionProperties.SetProperty("INSTANCE", instance);
            connectionProperties.SetProperty("DATABASE", database);
            connectionProperties.SetProperty("AUTHENTICATION_MODE", authenication);
            connectionProperties.SetProperty("VERSION", version);
            //connectionProperties.SetProperty("USER", username);
            //connectionProperties.SetProperty("PASSWORD", pass);

            // Get the Workspace
            System.Object      obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"));
            IWorkspaceFactory2 workspaceFactory = obj as IWorkspaceFactory2;
            IWorkspace         workspace        = workspaceFactory.Open(connectionProperties, 0);

            obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
            ILocatorManager2          locatorManager           = obj as ILocatorManager2;
            ILocatorWorkspace         locatorWorkspace         = locatorManager.GetLocatorWorkspace(workspace);
            IDatabaseLocatorWorkspace databaseLocatorWorkspace = (IDatabaseLocatorWorkspace)locatorWorkspace;

            return(locatorWorkspace);
        }
Example #3
0
        /// <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));
                }
            }
        }
        private void GeocodeAddress()
        {
            // Get the locator
            System.Object     obj              = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
            ILocatorManager2  locatorManager   = obj as ILocatorManager2;
            ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspaceFromPath(@"C:\California_fgdb.gdb");
            ILocator          locator          = locatorWorkspace.GetLocator("California_city_state_zip_94_new");

            // Set up the address properties
            IAddressInputs addressInputs     = locator as IAddressInputs;
            IFields        addressFields     = addressInputs.AddressFields;
            IPropertySet   addressProperties = new PropertySetClass();

            addressProperties.SetProperty(addressFields.get_Field(0).Name, this.AddressTextBox.Text);
            addressProperties.SetProperty(addressFields.get_Field(1).Name, this.CityTextBox.Text);
            addressProperties.SetProperty(addressFields.get_Field(2).Name, this.StateTextBox.Text);
            addressProperties.SetProperty(addressFields.get_Field(3).Name, this.ZipTextBox.Text);

            // Match the Address
            IAddressGeocoding addressGeocoding = locator as IAddressGeocoding;
            IPropertySet      resultSet        = addressGeocoding.MatchAddress(addressProperties);

            // Print out the results
            object names, values;

            resultSet.GetAllProperties(out names, out values);
            string[] namesArray  = names as string[];
            object[] valuesArray = values as object[];
            int      length      = namesArray.Length;
            IPoint   point       = null;

            for (int i = 0; i < length; i++)
            {
                if (namesArray[i] != "Shape")
                {
                    this.ResultsTextBox.Text += namesArray[i] + ": " + valuesArray[i].ToString() + "\n";
                }
                else
                {
                    if (point != null && !point.IsEmpty)
                    {
                        point = valuesArray[i] as IPoint;
                        this.ResultsTextBox.Text += "X: " + point.X + "\n";
                        this.ResultsTextBox.Text += "Y: " + point.Y + "\n";
                    }
                }
            }

            this.ResultsTextBox.Text += "\n";
        }
        public static ILocatorWorkspace GetFileGDBLocatorWorkspace(string path)
        {
            // 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(path, 0); // example of path string is... @"C:\UnitedStates.gdb"

            // Get a locator from the locator Workspace
            obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
            ILocatorManager2  locatorManager   = obj as ILocatorManager2;
            ILocatorWorkspace locatorWorkspace = locatorManager.GetLocatorWorkspace(workspace);

            return(locatorWorkspace);
        }