public void ProcessRequest(HttpContext context)
        {
            // location
            // f
            // distance (in meters)
            // outSR

            PointN point;
            if (!TryParse(context.Request.Params["location"], out point))
            {
                throw new ArgumentException("The location parameter was not provided.");
            }

            // If an input spatial reference has been specified, add that information to the point.
            string inSRStr = context.Request.Params["inSR"];
            int inSRWKT;
            if (!string.IsNullOrEmpty(inSRStr) && int.TryParse(inSRStr, out inSRWKT))
            {
                point.SpatialReference = inSRWKT.ToSpatialReference();
            }

            string outSRStr = context.Request.Params["outSR"];
            int outSRWKT;
            SpatialReference outSR = null;
            if (!string.IsNullOrEmpty(outSRStr) && int.TryParse(outSRStr, out outSRWKT))
            {
                outSR = outSRWKT.ToSpatialReference();
            }

            // Get the distance.
            string distanceStr = context.Request.Params["distance"];
            double distance = 0;
            string units = _defaultUnits;
            if (!string.IsNullOrEmpty(distanceStr))
            {
                GetDistance(distanceStr, out distance, out units);
            }

            PropertySet result;

            using (var proxy = new GeocodeServerProxy { Url = ConfigurationManager.AppSettings["GeocodeService"] })
            {
                // Get the default properties for this geocode server.
                PropertySet locatorProperties = proxy.GetLocatorProperties();

                // Convert the property set into a dictionary for easier editing/adding of properties.
                var lPropDict = locatorProperties.PropertyArray.ToDictionary(k => k.Key, v => v.Value);
                lPropDict["OutputSpatialReference"] = outSR;

                lPropDict["ReverseDistance"] = distance;
                lPropDict["ReverseDistanceUnits"] = units;

                // Convert the dictionary back into a property array.
                locatorProperties.PropertyArray = (from kvp in lPropDict
                                                   select new PropertySetProperty
                                                   {
                                                       Key = kvp.Key,
                                                       Value = kvp.Value
                                                   }).ToArray();

                result = proxy.ReverseGeocode(point, true, locatorProperties);
            }

            // Convert the property array into a dictionary.
            var output = new Dictionary<string, object>();
            if (result.PropertyArray != null && result.PropertyArray.Length > 0)
            {
                var resultProperties = result.PropertyArray.ToDictionary(k => k.Key, v => v.Value);

                output["address"] = (from prop in result.PropertyArray
                                     where string.Compare(prop.Key, "Shape", true) != 0
                                     select prop).ToDictionary(k => k.Key, v => v.Value);

                var location = resultProperties["Shape"] as PointN;
                if (location != null)
                {
                    output["location"] = ToJsonPoint(location);
                }
            }

            var serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(output);

            context.Response.ContentType = "application/json";
            context.Response.Write(json);
        }
Exemplo n.º 2
0
 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);
 }