This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
/
ReverseGeocodeIntersection.ashx.cs
216 lines (182 loc) · 5.45 KB
/
ReverseGeocodeIntersection.ashx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;
using ESRI.ArcGIS.SOAP;
using Wsdot.Gis;
namespace TrafficImpactPlanning
{
/// <summary>
/// <para>REST Endpoint for the ESRI SOAP SDK ReverseGeocode operation.</para>
/// <para>location: A point defined by two <see cref="double"/> values separated by a string.</para>
/// </summary>
/// <remarks>
/// The reason you would use this handler instead of just using the ArcGIS REST API reverse geocode method is that the REST version does not
/// provide the ability to return intersections.
/// </remarks>
public class ReverseGeocodeIntersection : IHttpHandler
{
static readonly Regex _distanceRegex = new Regex(@"(?in)(?<number>\d+(\.\d+)?)(\s*(?<unit>\w+))?");
const string _defaultUnits = "Meters";
static Dictionary<string, object> ToJsonPoint(PointN point)
{
if (point == null) return null;
var output = new Dictionary<string, object>();
output["x"] = point.X;
output["y"] = point.Y;
var srDict = ToJsonSR(point.SpatialReference);
if (srDict != null) {
output["spatialReference"] = srDict;
}
return output;
}
static Dictionary<string, object> ToJsonSR(SpatialReference sr)
{
if (sr.WKIDSpecified)
{
var output = new Dictionary<string, object>();
output.Add("wkid", sr.WKID);
return output;
}
return null;
}
static bool TryParse(string locationString, out PointN point)
{
point = null;
if (string.IsNullOrEmpty(locationString))
{
return false;
}
// Try to split coordinates
var coordStrings = locationString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (coordStrings.Length < 2)
{
return false;
}
point = new PointN();
bool fail = false;
for (int i = 0; i < 2; i++)
{
double coord;
if (double.TryParse(coordStrings[i], out coord))
{
if (i == 0)
{
point.X = coord;
}
else if (i == 1)
{
point.Y = coord;
}
}
else
{
fail = true;
break;
}
}
return !fail;
}
static void GetDistance(string distanceString, out double distance, out string units)
{
var match = _distanceRegex.Match(distanceString);
if (!match.Success)
{
units = _defaultUnits;
distance = 0;
}
else
{
double number;
if (!double.TryParse(match.Groups["number"].Value, out number))
{
number = 0;
}
distance = number;
var unitGroup = match.Groups["unit"];
units = unitGroup.Success ? unitGroup.Value : _defaultUnits;
}
}
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);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}