private PropertySet GetModifiedLocatorProperties() { PropertySet propertySet = _locatorProperties.Copy(); PropertySetProperty prop = propertySet.PropertyArray.FirstOrDefault(o => o.Key == "MinimumMatchScore"); if (prop != null) { prop.Value = MinimumScore; } prop = propertySet.PropertyArray.FirstOrDefault(o => o.Key == "SpellingSensitivity"); if (prop != null) { prop.Value = SpellingSensitivity; } if (!String.IsNullOrEmpty(CoordinateSystem)) { List <PropertySetProperty> propList = new List <PropertySetProperty>(propertySet.PropertyArray); propList.Add(new PropertySetProperty("OutputSpatialReference", SpatialReference.Create(CoordinateSystem))); propertySet.PropertyArray = propList.ToArray(); } return(propertySet); }
/// <summary> /// CreatePropertySetProperty /// </summary> private PropertySetProperty CreatePropertySetProperty(string key, object value) { PropertySetProperty propSetProperty = new PropertySetProperty(); propSetProperty.Key = key; propSetProperty.Value = value; return(propSetProperty); }
/// <summary> /// Geocode an address based on the street name, city, state, and zip code /// Throws and exception and returns null if the address was unmatched. /// </summary> private PropertySet GeocodeAddress(string StreetAddress, string City, string State, string ZipCode) { PropertySet propSet = null; try { SanFranciscoLocator_GeocodeServer gc = new SanFranciscoLocator_GeocodeServer(); PropertySet pAddressProperties = new PropertySet(); Fields pAddressFields; Field pField; PropertySetProperty[] propSetProperty = new PropertySetProperty[4]; pAddressFields = gc.GetAddressFields(); for (int i = 0; i < pAddressFields.FieldArray.GetLength(0); i++) { pField = pAddressFields.FieldArray[i]; if (pField.Name.ToUpper() == "STREET") { propSetProperty[0] = CreatePropertySetProperty(pField.AliasName, StreetAddress) as PropertySetProperty; } if (pField.Name.ToUpper() == "CITY") { propSetProperty[1] = CreatePropertySetProperty(pField.AliasName, City) as PropertySetProperty; } if (pField.Name.ToUpper() == "STATE") { propSetProperty[2] = CreatePropertySetProperty(pField.AliasName, State) as PropertySetProperty; } if (pField.Name.ToUpper() == "ZIP") { propSetProperty[3] = CreatePropertySetProperty(pField.AliasName, ZipCode) as PropertySetProperty; } } pAddressProperties.PropertyArray = propSetProperty; // find the matching address propSet = gc.GeocodeAddress(pAddressProperties, null); } catch (Exception exception) { MessageBox.Show(exception.Message, "An error has occurred"); } // Throw and error if the geocoded address is "Unmatched" if ((propSet != null) && (propSet.PropertyArray[1].Value.ToString() == "U")) { throw (new System.Exception("Could not geocode [" + StreetAddress + "]")); } return(propSet); }
public override MatchedAddress GeocodeAddress(params AddressValue[] values) { ValidateAddressValues(values); PropertySet propertySet = GeocodeServer.GeocodeAddress(ConvertAddressValuesToProperties(values), GetModifiedLocatorProperties()); MatchedAddress matchedAddress = null; if (propertySet != null && propertySet.PropertyArray != null || propertySet.PropertyArray.Length > 0) { PropertySetProperty prop = propertySet.PropertyArray.FirstOrDefault(o => o.Key == "Status"); if (prop != null && prop.Value.ToString() == "M") { matchedAddress = new MatchedAddress(); prop = propertySet.PropertyArray.FirstOrDefault(o => o.Key == "Match_addr"); if (prop != null) { matchedAddress.Address = prop.Value.ToString(); } prop = propertySet.PropertyArray.FirstOrDefault(o => o.Key == "Score"); if (prop != null) { matchedAddress.Score = Convert.ToInt32(prop.Value); } prop = propertySet.PropertyArray.FirstOrDefault(o => o.Key == "Shape"); if (prop != null) { matchedAddress.Location = ((AppGeo.Clients.Ags.Proxy.Geometry)prop.Value).ToCommon().Centroid.Coordinate; } } } return(matchedAddress); }
public override void Reload() { Fields fields = GeocodeServer.GetAddressFields(); AddressFields = fields.FieldArray.Select(o => new AgsField(o)).Cast <CommonField>().ToList(); _locatorProperties = GeocodeServer.GetLocatorProperties(); PropertySetProperty prop = _locatorProperties.PropertyArray.FirstOrDefault(o => o.Key == "MinimumMatchScore"); if (prop != null) { MinimumScore = Convert.ToInt32(prop.Value); } prop = _locatorProperties.PropertyArray.FirstOrDefault(o => o.Key == "SpellingSensitivity"); if (prop != null) { SpellingSensitivity = Convert.ToInt32(prop.Value); } }
/// <summary> /// CreatePropertySetProperty /// </summary> private PropertySetProperty CreatePropertySetProperty(string key, object value) { PropertySetProperty propSetProperty = new PropertySetProperty(); propSetProperty.Key = key; propSetProperty.Value = value; return propSetProperty; }
/// <summary> /// Geocode an address based on the street name, city, state, and zip code /// Throws and exception and returns null if the address was unmatched. /// </summary> private PropertySet GeocodeAddress(string StreetAddress, string City, string State, string ZipCode) { PropertySet propSet = null; try { SanFranciscoLocator_GeocodeServer gc = new SanFranciscoLocator_GeocodeServer(); PropertySet pAddressProperties = new PropertySet(); Fields pAddressFields; Field pField; PropertySetProperty[] propSetProperty = new PropertySetProperty[4]; pAddressFields = gc.GetAddressFields(); for (int i = 0; i < pAddressFields.FieldArray.GetLength(0); i++) { pField = pAddressFields.FieldArray[i]; if (pField.Name.ToUpper() == "STREET") propSetProperty[0] = CreatePropertySetProperty(pField.AliasName, StreetAddress) as PropertySetProperty; if (pField.Name.ToUpper() == "CITY") propSetProperty[1] = CreatePropertySetProperty(pField.AliasName, City) as PropertySetProperty; if (pField.Name.ToUpper() == "STATE") propSetProperty[2] = CreatePropertySetProperty(pField.AliasName, State) as PropertySetProperty; if (pField.Name.ToUpper() == "ZIP") propSetProperty[3] = CreatePropertySetProperty(pField.AliasName, ZipCode) as PropertySetProperty; } pAddressProperties.PropertyArray = propSetProperty; // find the matching address propSet = gc.GeocodeAddress(pAddressProperties, null); } catch (Exception exception) { MessageBox.Show(exception.Message, "An error has occurred"); } // Throw and error if the geocoded address is "Unmatched" if ((propSet != null) && (propSet.PropertyArray[1].Value.ToString() == "U")) throw (new System.Exception("Could not geocode [" + StreetAddress + "]")); return propSet; }
/// <summary> /// Create a property location array /// </summary> private PropertySetProperty[] CreateLocationPropertyArray(string name, string X, string Y, SpatialReference sr) { PropertySetProperty[] propSetProperty; if (sr == null) propSetProperty = new PropertySetProperty[3]; else propSetProperty = new PropertySetProperty[4]; propSetProperty[0] = new PropertySetProperty(); propSetProperty[0].Key = "NAME"; propSetProperty[0].Value = name; propSetProperty[1] = new PropertySetProperty(); propSetProperty[1].Key = "X"; propSetProperty[1].Value = X; propSetProperty[2] = new PropertySetProperty(); propSetProperty[2].Key = "Y"; propSetProperty[2].Value = Y; if (sr != null) { propSetProperty[3] = new PropertySetProperty(); propSetProperty[3].Key = "SpatialReference"; propSetProperty[3].Value = sr; } return propSetProperty; }
/// <summary> /// Create property. /// </summary> /// <param name="key">Property key</param> /// <param name="value">Property value.</param> /// <returns>Property.</returns> private static PropertySetProperty _CreateProp(string key, object value) { PropertySetProperty prop = new PropertySetProperty(); prop.Key = key; prop.Value = value; return prop; }
/// <summary> /// Method gets field name property, which has Locator Field Name as a Key and /// Address Field Type as a Value. /// </summary> /// <param name="name">Field name.</param> /// <returns>Field name property for field mapping.</returns> private PropertySetProperty _GetFieldNamePropertyForSingleLine(string name) { PropertySetProperty property = new PropertySetProperty(); property.Key = name; property.Value = name; return property; }
/// <summary> /// Method gets field name property, which has Locator Field Name as a Key and /// Address Field Type as a Value. /// </summary> /// <param name="locatorFieldIndex">Index of a locator field.</param> /// <param name="addressFieldIndex">Index of a address field.</param> /// <returns>Field name property for field mapping.</returns> private PropertySetProperty _GetFieldNamePropertyForMultipleLines(int locatorFieldIndex, int addressFieldIndex) { PropertySetProperty property = new PropertySetProperty(); property.Key = _locatorFieldNames[locatorFieldIndex]; property.Value = _addressFields[addressFieldIndex].Type.ToString(); return property; }
/// <summary> /// Method gets address property, which has Locator Field Name as a Key and /// Address as a Value. /// </summary> /// <param name="index">Index of address field.</param> /// <param name="address">Source address.</param> /// <returns>Property Set Property.</returns> private PropertySetProperty _GetAddressProperty(int index, Address address) { PropertySetProperty property = new PropertySetProperty(); property.Key = _locatorFieldNames[index]; AddressPart addressPart = _addressFields[index].Type; property.Value = address[addressPart]; return property; }
public static GeocodeResult Geocode(string serviceUri, string address) { GeocodeServerProxy proxy = new GeocodeServerProxy(serviceUri); PropertySet geocodePropSet = new PropertySet(); PropertySetProperty geocodeProp = new PropertySetProperty(); geocodeProp.Key = "KeyField"; geocodeProp.Value = address; geocodePropSet.PropertyArray = new PropertySetProperty[] { geocodeProp }; PropertySet results = proxy.GeocodeAddress(geocodePropSet, null); return new GeocodeResult(results); }
public List <AddressModel> ProcessAddresses(List <AddressModel> addresses) { #region Property Set PropertySet geocodePropSet = new PropertySet(); PropertySetProperty[] propArray = new PropertySetProperty[2]; PropertySetProperty geocodePropAddr = new PropertySetProperty(); geocodePropAddr.Key = "Address"; geocodePropAddr.Value = "Address"; propArray[0] = geocodePropAddr; PropertySetProperty geocodePropPostal = new PropertySetProperty(); geocodePropPostal.Key = "Postal"; geocodePropPostal.Value = "Postal"; propArray[1] = geocodePropPostal; geocodePropSet.PropertyArray = propArray; #endregion #region Input Record Set // Create a new recordset to store input addresses to be batch geocoded RecordSet addressTable = new RecordSet(); Field[] fieldarray = new Field[5]; // Following field properties are required for batch geocode to work: // Length, Name, Type. There also needs to be a field of type OID. Field field0 = new Field(); field0.Name = "OID"; field0.Type = esriFieldType.esriFieldTypeOID; field0.Length = 50; fieldarray[0] = field0; Field field1 = new Field(); field1.Name = "Address"; field1.Type = esriFieldType.esriFieldTypeString; field1.Length = 50; fieldarray[1] = field1; Field field2 = new Field(); field2.Name = "City"; field2.Type = esriFieldType.esriFieldTypeString; field2.Length = 50; fieldarray[2] = field2; Field field3 = new Field(); field3.Name = "State"; field3.Type = esriFieldType.esriFieldTypeString; field3.Length = 50; fieldarray[3] = field3; Field field4 = new Field(); field4.Name = "Postal"; field4.Type = esriFieldType.esriFieldTypeString; field4.Length = 50; fieldarray[4] = field4; Fields fields = new Fields(); fields.FieldArray = fieldarray; addressTable.Fields = fields; #endregion List <Record> records = new List <Record>(); foreach (AddressModel address in addresses) { Record record = new Record(); record.Values = new object[5] { address.ID, address.Address, address.City, address.State, address.Zip }; records.Add(record); } // convert to record set for input to geocoder Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); addressTable.Records = records.ToArray(); RecordSet results = proxy.GeocodeAddresses(addressTable, geocodePropSet, null); List <AddressModel> geoCodedAddresses = new List <AddressModel>(); foreach (Record record in results.Records) { string[] address = record.Values[6].ToString().Split(','); geoCodedAddresses.Add(new AddressModel() { ID = (int)record.Values[1], Address = address[0].Trim(), Zip = record.Values[18].ToString(), Longitude = record.Values[24].ToString(), Latitude = record.Values[25].ToString(), City = record.Values[15].ToString(), State = record.Values[17].ToString() }); } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); proxy.Dispose(); return(geoCodedAddresses); }