forked from AppGeo/AppGeo.Clients
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonGeocodeService.cs
271 lines (225 loc) · 6.44 KB
/
CommonGeocodeService.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2012 Applied Geographics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using GeoAPI.Geometries;
namespace AppGeo.Clients
{
public abstract class CommonGeocodeService
{
private CommonHost _host = null;
private string _name = null;
private List<CommonField> _addressFields = null;
private int _spellingSensitivity = 80;
private int _minimumScore = 60;
private string _coordinateSystem = null;
public List<CommonField> AddressFields
{
get
{
return _addressFields;
}
protected set
{
_addressFields = value;
}
}
public string CoordinateSystem
{
get
{
return _coordinateSystem;
}
set
{
_coordinateSystem = value;
}
}
public abstract double EndOffset { get; }
public CommonHost Host
{
get
{
return _host;
}
protected set
{
_host = value;
}
}
public abstract bool IsAvailable { get; }
public int MinimumScore
{
get
{
return _minimumScore;
}
set
{
if (!(0 <= value && value <= 100))
{
throw new ArgumentException("MinimumScore must be in the range of 0 to 100");
}
_minimumScore = value;
}
}
public string Name
{
get
{
return _name;
}
protected set
{
_name = value;
}
}
public abstract double SideOffset { get; }
public abstract string SideOffsetUnits { get; }
public int SpellingSensitivity
{
get
{
return _spellingSensitivity;
}
set
{
if (!(0 <= value && value <= 100))
{
throw new ArgumentException("SpellingSensitivity must be in the range of 0 to 100");
}
_spellingSensitivity = value;
}
}
public List<MatchedAddress> FindAddressCandidates(params string[] values)
{
return FindAddressCandidates(GetAddressValues(values));
}
public List<MatchedAddress> FindAddressCandidates(List<AddressValue> values)
{
return FindAddressCandidates(values.ToArray());
}
public abstract List<MatchedAddress> FindAddressCandidates(params AddressValue[] values);
private AddressValue[] GetAddressValues(string[] values)
{
List<AddressValue> addressValues = new List<AddressValue>();
for (int i = 0; i < Math.Min(values.Length, AddressFields.Count); ++i)
{
if (!String.IsNullOrEmpty(values[i]))
{
addressValues.Add(new AddressValue(AddressFields[i].Name, values[i]));
}
}
return addressValues.ToArray();
}
public MatchedAddress GeocodeAddress(params string[] values)
{
return GeocodeAddress(GetAddressValues(values));
}
public MatchedAddress GeocodeAddress(List<AddressValue> values)
{
return GeocodeAddress(values.ToArray());
}
public abstract MatchedAddress GeocodeAddress(params AddressValue[] values);
public DataTable GetAddressCandidatesTable(params string[] values)
{
return GetAddressCandidatesTable(GetAddressValues(values));
}
public DataTable GetAddressCandidatesTable(List<AddressValue> values)
{
return GetAddressCandidatesTable(values.ToArray());
}
public DataTable GetAddressCandidatesTable(params AddressValue[] values)
{
List<MatchedAddress> matchedAddresses = FindAddressCandidates(values);
DataTable table = new DataTable();
table.Columns.Add("Address", typeof(string));
table.Columns.Add("Score", typeof(int));
table.Columns.Add("X", typeof(double));
table.Columns.Add("Y", typeof(double));
foreach (MatchedAddress matchedAddress in matchedAddresses)
{
DataRow row = table.NewRow();
row["Address"] = matchedAddress.Address;
row["Score"] = matchedAddress.Score;
row["X"] = matchedAddress.Location == null ? null : (object)matchedAddress.Location.X;
row["Y"] = matchedAddress.Location == null ? null : (object)matchedAddress.Location.Y;
table.Rows.Add(row);
}
return table;
}
protected void ValidateAddressValues(AddressValue[] values)
{
if (values.Length == 0)
{
throw new ArgumentException("At least one address value must be provided.");
}
foreach (AddressValue addressValue in values)
{
if (!AddressFields.Any(o => String.Compare(o.Name, addressValue.Name, true) == 0))
{
throw new ArgumentException(String.Format("This geocoder does not contain an address field named \"{0}\".", addressValue.Name));
}
}
foreach (CommonField field in AddressFields.Where(o => o.Required))
{
if (!values.Any(o => String.Compare(o.Name, field.Name, true) == 0))
{
throw new ArgumentException(String.Format("Required address value \"{0}\" is missing.", field.Name));
}
}
}
public abstract void Reload();
}
public struct AddressValue
{
public string Name;
public string Value;
public AddressValue(string name, string value)
{
Name = name;
Value = value;
}
}
[DataContract]
public class MatchedAddress : IComparable<MatchedAddress>
{
[DataMember]
public string Address = null;
[DataMember]
public int Score = 0;
[DataMember]
public Coordinate Location = null;
public int CompareTo(MatchedAddress other)
{
int c = -Score.CompareTo(other.Score);
if (c == 0)
{
c = Address.CompareTo(other.Address);
if (c == 0)
{
c = Location.CompareTo(other.Location);
}
}
return c;
}
public override string ToString()
{
return String.Format("{0} | {1} | {2}", Score, Address, Location);
}
}
}