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) + ")"; } } }
/// <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)); } } }
public override void OnClick() { if (m_pAOIPanel.CurrentJob != null) { //show form AddressDialog pAddressDialog = new AddressDialog(); if (pAddressDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { ILocatorManager pLocatorMgr = new LocatorManagerClass(); ILocatorWorkspace pLocatorWS = pLocatorMgr.GetLocatorWorkspaceFromPath(m_strWorkspace); IAddressGeocoding pLocator = (IAddressGeocoding)pLocatorWS.GetLocator(m_strLocator); IPropertySet addressProperties = new PropertySetClass(); addressProperties.SetProperty("Street", pAddressDialog.street); IPropertySet matchProperties = pLocator.MatchAddress(addressProperties); if (pLocator.MatchFields.FieldCount == 0) { System.Windows.Forms.MessageBox.Show("No address found"); return; } IPoint pPoint = null; for (int i = 0; i < pLocator.MatchFields.FieldCount; i++) { if (pLocator.MatchFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { object pObject = matchProperties.GetProperty(pLocator.MatchFields.get_Field(i).Name); if (pObject is IPoint) { pPoint = (IPoint)pObject; } } } //calculate AOI ITopologicalOperator pTopo = (ITopologicalOperator)pPoint; IGeometry pGeom = pTopo.Buffer(100); IEnvelope pMyAOI = pGeom.Envelope; m_pAOIPanel.CurrentAOI = CreatePolyFromEnv(pMyAOI); IEnvelope pEnv = pGeom.Envelope; pEnv.Expand(2, 2, true); m_hookHelper.ActiveView.Extent = pEnv; m_hookHelper.ActiveView.Refresh(); } } }
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"; }
// 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); } }