-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTomTomClient.cs
179 lines (147 loc) · 7.04 KB
/
TomTomClient.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Xml.XPath;
using System.Globalization;
using System.Reflection;
using System.Security;
namespace TomTomClient
{
public class TomTomClient
{
private string CredentialsClientUrl = "https://api.tomtom.com/lbs/geocoding/geocode?key={0}&language={1}&maxResults={2}&{3}";
private readonly string CredentialsKey;
private readonly string LanguageKey;
private static Random Random = new Random();
private static int SessionId = Random.Next();
private const string XPATH_RESPONSESTATUS = "/*[local-name()='geoResponse']";
private const string XPATH_RESPONSEADDRESS = "/*[local-name()='geoResponse']/*[local-name()='geoResult']";
public TomTomClient(string apiKey, string language = "en")
{
CredentialsKey = apiKey;
LanguageKey = language;
}
public List<Address> CodeAddress(string addressFreeform, string countryCodeIso3, int numberOfResult = 1)
{
string request = string.Format("&q={0}&CC={1}", Uri.EscapeUriString(addressFreeform), countryCodeIso3);
return CodeAddressInternal(request, numberOfResult);
}
public List<Address> CodeAddress(Address address, int numberOfResult = 1)
{
List<string> parts = new List<string>();
if (!string.IsNullOrEmpty(address.Number)) parts.Add(string.Format("ST={0}", (string)Uri.EscapeUriString(address.Number)));
if (!string.IsNullOrEmpty(address.Street)) parts.Add(string.Format("T={0}", (string)Uri.EscapeUriString(address.Street)));
if (!string.IsNullOrEmpty(address.City)) parts.Add(string.Format("L={0}", (string)Uri.EscapeUriString(address.City)));
if (!string.IsNullOrEmpty(address.ZIP)) parts.Add(string.Format("PC={0}", (string)Uri.EscapeUriString(address.ZIP)));
if (!string.IsNullOrEmpty(address.State)) parts.Add(string.Format("AA={0}", (string)Uri.EscapeUriString(address.State)));
if (!string.IsNullOrEmpty(address.Country)) parts.Add(string.Format("CC={0}", (string)Uri.EscapeUriString(address.Country)));
return CodeAddressInternal(string.Join("&", parts), numberOfResult);
}
private List<Address> CodeAddressInternal(string geocodingRequestUrl, int numberOfResult = 1)
{
List<Address> result = new List<Address>(); ;
Stream aStream = null;
WebResponse response = null;
try
{
string requestUrlFull = string.Format(CredentialsClientUrl, CredentialsKey, LanguageKey, numberOfResult, geocodingRequestUrl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrlFull);
response = request.GetResponse();
// parse the result
aStream = response.GetResponseStream();
XPathDocument docNav = new XPathDocument(aStream);
XPathNavigator nav = docNav.CreateNavigator();
int numberOfResults = -1;
// step 1: check response
XPathNodeIterator NodeIter = nav.Select(XPATH_RESPONSESTATUS);
if (NodeIter.MoveNext())
{
string numFound = NodeIter.Current.GetAttribute("count", "");
if (numFound != null && numFound.Length > 0)
{
numberOfResults = Convert.ToInt32(numFound);
}
}
// step 2: decode result addresses
if (numberOfResults > 0)
{
NodeIter = nav.Select(XPATH_RESPONSEADDRESS);
while (NodeIter.MoveNext())
{
result.Add(ExtractReponseAddress(NodeIter.Current));
}
}
}
finally
{
if (aStream != null) aStream.Close();
if (response != null) response.Close();
}
return result;
}
private static Address ExtractReponseAddress(XPathNavigator nav)
{
Address address = new Address();
// lat long
NumberFormatInfo provider = new NumberFormatInfo() { NumberDecimalSeparator = ".", NumberGroupSeparator = "," };
string lat = nav.SelectSingleNode(@"//*[local-name()='latitude']").Value;
if (lat != null && lat.Length > 0)
{
address.Position.Latitude = Convert.ToDouble(lat, provider);
}
string lng = nav.SelectSingleNode(@"//*[local-name()='longitude']").Value;
if (lng != null && lng.Length > 0)
{
address.Position.Longitude = Convert.ToDouble(lng, provider);
}
try {
address.Street = nav.SelectSingleNode(@"//*[local-name()='street']").Value.Trim();
} catch (Exception) { address.Street = string.Empty; }
try {
address.Number = nav.SelectSingleNode(@"//*[local-name()='houseNumber']").Value.Trim();
}
catch (Exception) { address.Number = string.Empty; }
try {
address.ZIP = nav.SelectSingleNode(@"//*[local-name()='postcode']").Value.Trim();
}
catch (Exception) { address.ZIP = string.Empty; }
try
{
address.City = nav.SelectSingleNode(@"//*[local-name()='city']").Value.Trim();
}
catch (Exception) { address.City = string.Empty; }
try {
address.Country = nav.SelectSingleNode(@"//*[local-name()='country']").Value.Trim();
}
catch (Exception) { address.Country = string.Empty; }
try
{
address.Geohash = nav.SelectSingleNode(@"//*[local-name()='geohash']").Value.Trim();
}
catch (Exception) { address.Geohash = string.Empty; }
try
{
address.Score = nav.SelectSingleNode(@"//*[local-name()='score']").Value.Trim();
}
catch (Exception) { address.Score = string.Empty; }
try
{
address.Confidence = nav.SelectSingleNode(@"//*[local-name()='confidence']").Value.Trim();
}
catch (Exception) { address.Confidence = string.Empty; }
// admin areas
try {
address.State = address.AdminArea1 = nav.SelectSingleNode(@"//*[local-name()='state']").Value.Trim();
}
catch (Exception) { address.State = address.AdminArea1 = string.Empty; }
try {
address.District = address.AdminArea2 = nav.SelectSingleNode(@"//*[local-name()='district']").Value.Trim();
}
catch (Exception) { address.District = address.AdminArea2 = string.Empty; }
return address;
}
}
}