Beispiel #1
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));
                }
            }
        }
Beispiel #2
0
        // reverse geocode button
        private void btnReverseGeocode_Click(object sender, EventArgs e)
        {
            try
            {
                string strGeocoderName;
                string strGeocoderDateField;

                if (radioHWYNAME.Checked)
                {
                    strGeocoderName      = "E911ADMIN.RevGeocoderTOC_HWYNAME";
                    strGeocoderDateField = "RevGeoHWYNAME_Date";
                }
                else if (radioSTREETNAME.Checked)
                {
                    strGeocoderName      = "E911ADMIN.RevGeocoderTOC_STREETNAME";
                    strGeocoderDateField = "RevGeoSTREETNAME_Date";
                }
                else
                {
                    MessageBox.Show("Please select a Geocoder.", "Choose Geocoder", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // show the cursor as busy
                System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;


                // get access to the layer that is specified in the choose layer dropdown box
                //pGFlayer = null;
                arcFeatLayer = null;
                // loop through the map's layers and check for the layer with the targeted name (based on the choose layer combobox)
                for (int i = 0; i < clsE911Globals.pMap.LayerCount; i++)
                {
                    if (clsE911Globals.pMap.Layer[i].Name == cboChooseLayer.Text)
                    {
                        //pGFlayer = (IGeoFeatureLayer)clsE911Globals.pMap.Layer[i];
                        arcFeatLayer = (IFeatureLayer)clsE911Globals.pMap.Layer[i];
                    }
                }

                // check to assure the user chose a point layer
                if (arcFeatLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                {
                    MessageBox.Show("You must choose a Point Layer to Reverse Geocode.", "Must Be Point Layer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }


                // Get a locator from the locator Workspace
                //ILocatorWorkspace locatorWorkspace = clsE911StaticClass.GetFileGDBLocatorWorkspace(@"K:\AGRC Projects\E911_Editing\Locators\AddressLocatorData.gdb");
                //IReverseGeocoding reverseGeocoding = (IReverseGeocoding)locatorWorkspace.GetLocator("UtransHwys_AddrLocator_HWYNAME");

                ILocatorWorkspace locatorWorkspace = clsE911StaticClass.GetSDELocatorWorkspace("", "sde:sqlserver:e911.agrc.utah.gov", "E911", "OSA", "sde.DEFAULT");
                IReverseGeocoding reverseGeocoding = (IReverseGeocoding)locatorWorkspace.GetLocator(strGeocoderName);


                //ILocatorWorkspace locatorWorkspace = clsE911StaticClass.GetSDELocatorWorkspace("", "sde:sqlserver:sgid.agrc.utah.gov", "SGID10", "DBMS", "sde.DEFAULT", "agrc", "agrc");
                //IReverseGeocoding reverseGeocoding = (IReverseGeocoding)locatorWorkspace.GetLocator("TRANSPORTATION.Locator_RoadsAddrSys_COMPOSITE");

                // Set the search tolerance for reverse geocoding
                IReverseGeocodingProperties reverseGeocodingProperties = (IReverseGeocodingProperties)reverseGeocoding;
                //reverseGeocodingProperties.SearchDistance = 3048;
                reverseGeocodingProperties.SearchDistance      = Convert.ToDouble(txtSearchRadius.Text.Trim());
                reverseGeocodingProperties.SearchDistanceUnits = esriUnits.esriFeet;


                // now loop through the point feature layer
                IFeatureCursor arcFeatCur = arcFeatLayer.Search(null, false);
                IFeature       arcFeature;

                while ((arcFeature = arcFeatCur.NextFeature()) != null)
                {
                    IPoint arcPoint = (IPoint)arcFeature.Shape;

                    // 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 = arcPoint.X;
                    point.Y = arcPoint.Y;


                    // Find the address nearest the Point
                    IPropertySet addressProperties = reverseGeocoding.ReverseGeocode(point, false);

                    // Print the address properties
                    IAddressInputs addressInputs = (IAddressInputs)reverseGeocoding;
                    IFields        addressFields = addressInputs.AddressFields;

                    // get the "Street or Intersection" field that was returned from the reverse geocoder (three fields are returned, this is the first.. the others are place and zip)
                    IField addressField = addressFields.get_Field(0);

                    string strReverseGeocodeAddress = string.Empty;
                    strReverseGeocodeAddress = addressProperties.GetProperty(addressField.Name).ToString().Trim();



                    // check if the address is not null and then get the first word which should be the house number
                    if (strReverseGeocodeAddress != "")
                    {
                        // assign the address field the returned value
                        arcFeature.set_Value(arcFeature.Fields.FindField(cboAddressField.Text), strReverseGeocodeAddress);


                        // parse the address into an array
                        string[] arrReverseGeocodeAddress = strReverseGeocodeAddress.Split(' ');

                        // get the first item of of the array
                        string strHseNum = arrReverseGeocodeAddress[0];

                        // get the last item in the array
                        string strHwyName = arrReverseGeocodeAddress[arrReverseGeocodeAddress.Length - 1];

                        // get all items in the arrry except first
                        string strResultMinusNumber = "";
                        if (radioHWYNAME.Checked)
                        {
                            for (int i = 0; i < arrReverseGeocodeAddress.Length; i++)
                            {
                                if (i != 0)
                                {
                                    // check if value is N, S, E, or W - if so don't add them to the string
                                    if (arrReverseGeocodeAddress[i].ToString() == "N" | arrReverseGeocodeAddress[i].ToString() == "S" | arrReverseGeocodeAddress[i].ToString() == "E" | arrReverseGeocodeAddress[i].ToString() == "W")
                                    {
                                        // don't add the directionals
                                    }
                                    else
                                    {
                                        strResultMinusNumber = strResultMinusNumber + arrReverseGeocodeAddress[i].ToString();
                                    }
                                }
                            }
                        }
                        if (radioSTREETNAME.Checked)
                        {
                            for (int i = 0; i < arrReverseGeocodeAddress.Length; i++)
                            {
                                if (i != 0)
                                {
                                    strResultMinusNumber = strResultMinusNumber + " " + arrReverseGeocodeAddress[i].ToString();
                                }
                            }
                        }
                        strResultMinusNumber.TrimStart(' ');
                        strResultMinusNumber.TrimEnd(' ');
                        strResultMinusNumber.Trim();


                        // check if the strHseNum is an int before we write it to the feature class address number field
                        int  intHseNum;
                        bool isNumeric = int.TryParse(strHseNum, out intHseNum);

                        ////int intHwyName;
                        ////bool isHwyNameNumeric = int.TryParse(strHwyName, out intHwyName);

                        // if the result has an address
                        if (isNumeric)
                        {
                            arcFeature.set_Value(arcFeature.Fields.FindField(cboChooseAddressNumber.Text), intHseNum);
                        }
                        else
                        {
                            arcFeature.set_Value(arcFeature.Fields.FindField(cboChooseAddressNumber.Text), "Verify RevGeo Result");
                        }

                        // if the result has a highway name
                        ////if (isHwyNameNumeric)
                        ////{
                        ////    arcFeature.set_Value(arcFeature.Fields.FindField(cboChooseHwyName.Text), intHwyName);
                        ////}
                        ////else
                        ////{
                        ////    arcFeature.set_Value(arcFeature.Fields.FindField(cboChooseHwyName.Text), "Verify RevGeo Result");
                        ////}

                        // populate the street/hwy name field
                        if (strResultMinusNumber != "")
                        {
                            arcFeature.set_Value(arcFeature.Fields.FindField(cboChooseHwyName.Text), strResultMinusNumber);
                        }
                    }
                    else
                    {
                        arcFeature.set_Value(arcFeature.Fields.FindField(cboAddressField.Text), "NotFound");
                    }

                    //for (int i = 0; i < addressFields.FieldCount; i++)
                    //{
                    //    IField addressField = addressFields.get_Field(i);

                    //    //arcFeature.set_Value(arcFeature.Fields.FindField(cboAddressField.Text), addressProperties.GetProperty(addressField.Name));

                    //    MessageBox.Show(addressField.AliasName + ": " + addressProperties.GetProperty(addressField.Name));
                    //}

                    // update the run date field
                    arcFeature.set_Value(arcFeature.Fields.FindField(strGeocoderDateField), DateTime.Now);

                    arcFeature.Store();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Message: " + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine +
                                "Error Source: " + Environment.NewLine + ex.Source + Environment.NewLine + Environment.NewLine +
                                "Error Location:" + Environment.NewLine + ex.StackTrace,
                                "E911 Tool error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }