Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2498 | - | 1 | |
2 | namespace GMap.NET.MapProviders |
||
3 | { |
||
4 | using System; |
||
5 | using System.Collections.Generic; |
||
6 | using System.Diagnostics; |
||
7 | using System.IO; |
||
8 | using System.Net; |
||
9 | using GMap.NET.Internals; |
||
10 | using GMap.NET.Projections; |
||
11 | using System.Text; |
||
12 | using System.Security.Cryptography; |
||
13 | |||
14 | /// <summary> |
||
15 | /// providers that are already build in |
||
16 | /// </summary> |
||
17 | public class GMapProviders |
||
18 | { |
||
19 | static GMapProviders() |
||
20 | { |
||
21 | list = new List<GMapProvider>(); |
||
22 | |||
23 | Type type = typeof(GMapProviders); |
||
24 | foreach (var p in type.GetFields()) |
||
25 | { |
||
26 | var v = p.GetValue(null) as GMapProvider; // static classes cannot be instanced, so use null... |
||
27 | if (v != null) |
||
28 | { |
||
29 | list.Add(v); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | Hash = new Dictionary<Guid, GMapProvider>(); |
||
34 | foreach (var p in list) |
||
35 | { |
||
36 | Hash.Add(p.Id, p); |
||
37 | } |
||
38 | |||
39 | DbHash = new Dictionary<int, GMapProvider>(); |
||
40 | foreach (var p in list) |
||
41 | { |
||
42 | DbHash.Add(p.DbId, p); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | GMapProviders() |
||
47 | { |
||
48 | } |
||
49 | |||
50 | public static readonly EmptyProvider EmptyProvider = EmptyProvider.Instance; |
||
51 | |||
52 | public static readonly OpenStreetMapProvider OpenStreetMap = OpenStreetMapProvider.Instance; |
||
53 | |||
54 | public static readonly OpenStreet4UMapProvider OpenStreet4UMap = OpenStreet4UMapProvider.Instance; |
||
55 | |||
56 | public static readonly OpenCycleMapProvider OpenCycleMap = OpenCycleMapProvider.Instance; |
||
57 | public static readonly OpenCycleLandscapeMapProvider OpenCycleLandscapeMap = OpenCycleLandscapeMapProvider.Instance; |
||
58 | public static readonly OpenCycleTransportMapProvider OpenCycleTransportMap = OpenCycleTransportMapProvider.Instance; |
||
59 | |||
60 | public static readonly OpenStreetMapQuestProvider OpenStreetMapQuest = OpenStreetMapQuestProvider.Instance; |
||
61 | public static readonly OpenStreetMapQuestSatteliteProvider OpenStreetMapQuestSattelite = OpenStreetMapQuestSatteliteProvider.Instance; |
||
62 | public static readonly OpenStreetMapQuestHybridProvider OpenStreetMapQuestHybrid = OpenStreetMapQuestHybridProvider.Instance; |
||
63 | |||
64 | public static readonly OpenSeaMapHybridProvider OpenSeaMapHybrid = OpenSeaMapHybridProvider.Instance; |
||
65 | |||
66 | #if OpenStreetOsm |
||
67 | public static readonly OpenStreetOsmProvider OpenStreetOsm = OpenStreetOsmProvider.Instance; |
||
68 | #endif |
||
69 | |||
70 | #if OpenStreetMapSurfer |
||
71 | public static readonly OpenStreetMapSurferProvider OpenStreetMapSurfer = OpenStreetMapSurferProvider.Instance; |
||
72 | public static readonly OpenStreetMapSurferTerrainProvider OpenStreetMapSurferTerrain = OpenStreetMapSurferTerrainProvider.Instance; |
||
73 | #endif |
||
74 | public static readonly WikiMapiaMapProvider WikiMapiaMap = WikiMapiaMapProvider.Instance; |
||
75 | |||
76 | public static readonly BingMapProvider BingMap = BingMapProvider.Instance; |
||
77 | public static readonly BingSatelliteMapProvider BingSatelliteMap = BingSatelliteMapProvider.Instance; |
||
78 | public static readonly BingHybridMapProvider BingHybridMap = BingHybridMapProvider.Instance; |
||
79 | |||
80 | public static readonly YahooMapProvider YahooMap = YahooMapProvider.Instance; |
||
81 | public static readonly YahooSatelliteMapProvider YahooSatelliteMap = YahooSatelliteMapProvider.Instance; |
||
82 | public static readonly YahooHybridMapProvider YahooHybridMap = YahooHybridMapProvider.Instance; |
||
83 | |||
84 | public static readonly GoogleMapProvider GoogleMap = GoogleMapProvider.Instance; |
||
85 | public static readonly GoogleSatelliteMapProvider GoogleSatelliteMap = GoogleSatelliteMapProvider.Instance; |
||
86 | public static readonly GoogleHybridMapProvider GoogleHybridMap = GoogleHybridMapProvider.Instance; |
||
87 | public static readonly GoogleTerrainMapProvider GoogleTerrainMap = GoogleTerrainMapProvider.Instance; |
||
88 | |||
89 | public static readonly GoogleChinaMapProvider GoogleChinaMap = GoogleChinaMapProvider.Instance; |
||
90 | public static readonly GoogleChinaSatelliteMapProvider GoogleChinaSatelliteMap = GoogleChinaSatelliteMapProvider.Instance; |
||
91 | public static readonly GoogleChinaHybridMapProvider GoogleChinaHybridMap = GoogleChinaHybridMapProvider.Instance; |
||
92 | public static readonly GoogleChinaTerrainMapProvider GoogleChinaTerrainMap = GoogleChinaTerrainMapProvider.Instance; |
||
93 | |||
94 | public static readonly GoogleKoreaMapProvider GoogleKoreaMap = GoogleKoreaMapProvider.Instance; |
||
95 | public static readonly GoogleKoreaSatelliteMapProvider GoogleKoreaSatelliteMap = GoogleKoreaSatelliteMapProvider.Instance; |
||
96 | public static readonly GoogleKoreaHybridMapProvider GoogleKoreaHybridMap = GoogleKoreaHybridMapProvider.Instance; |
||
97 | |||
98 | public static readonly NearMapProvider NearMap = NearMapProvider.Instance; |
||
99 | public static readonly NearSatelliteMapProvider NearSatelliteMap = NearSatelliteMapProvider.Instance; |
||
100 | public static readonly NearHybridMapProvider NearHybridMap = NearHybridMapProvider.Instance; |
||
101 | |||
102 | public static readonly OviMapProvider OviMap = OviMapProvider.Instance; |
||
103 | public static readonly OviSatelliteMapProvider OviSatelliteMap = OviSatelliteMapProvider.Instance; |
||
104 | public static readonly OviHybridMapProvider OviHybridMap = OviHybridMapProvider.Instance; |
||
105 | public static readonly OviTerrainMapProvider OviTerrainMap = OviTerrainMapProvider.Instance; |
||
106 | |||
107 | public static readonly YandexMapProvider YandexMap = YandexMapProvider.Instance; |
||
108 | public static readonly YandexSatelliteMapProvider YandexSatelliteMap = YandexSatelliteMapProvider.Instance; |
||
109 | public static readonly YandexHybridMapProvider YandexHybridMap = YandexHybridMapProvider.Instance; |
||
110 | |||
111 | public static readonly LithuaniaMapProvider LithuaniaMap = LithuaniaMapProvider.Instance; |
||
112 | public static readonly LithuaniaReliefMapProvider LithuaniaReliefMap = LithuaniaReliefMapProvider.Instance; |
||
113 | public static readonly Lithuania3dMapProvider Lithuania3dMap = Lithuania3dMapProvider.Instance; |
||
114 | public static readonly LithuaniaOrtoFotoMapProvider LithuaniaOrtoFotoMap = LithuaniaOrtoFotoMapProvider.Instance; |
||
115 | public static readonly LithuaniaOrtoFotoOldMapProvider LithuaniaOrtoFotoOldMap = LithuaniaOrtoFotoOldMapProvider.Instance; |
||
116 | public static readonly LithuaniaHybridMapProvider LithuaniaHybridMap = LithuaniaHybridMapProvider.Instance; |
||
117 | public static readonly LithuaniaHybridOldMapProvider LithuaniaHybridOldMap = LithuaniaHybridOldMapProvider.Instance; |
||
118 | public static readonly LithuaniaTOP50 LithuaniaTOP50Map = LithuaniaTOP50.Instance; |
||
119 | |||
120 | public static readonly LatviaMapProvider LatviaMap = LatviaMapProvider.Instance; |
||
121 | |||
122 | public static readonly MapBenderWMSProvider MapBenderWMSdemoMap = MapBenderWMSProvider.Instance; |
||
123 | |||
124 | public static readonly TurkeyMapProvider TurkeyMap = TurkeyMapProvider.Instance; |
||
125 | |||
126 | public static readonly CloudMadeMapProvider CloudMadeMap = CloudMadeMapProvider.Instance; |
||
127 | |||
128 | public static readonly SpainMapProvider SpainMap = SpainMapProvider.Instance; |
||
129 | |||
130 | public static readonly CzechMapProviderOld CzechOldMap = CzechMapProviderOld.Instance; |
||
131 | public static readonly CzechSatelliteMapProviderOld CzechSatelliteOldMap = CzechSatelliteMapProviderOld.Instance; |
||
132 | public static readonly CzechHybridMapProviderOld CzechHybridOldMap = CzechHybridMapProviderOld.Instance; |
||
133 | public static readonly CzechTuristMapProviderOld CzechTuristOldMap = CzechTuristMapProviderOld.Instance; |
||
134 | public static readonly CzechHistoryMapProviderOld CzechHistoryOldMap = CzechHistoryMapProviderOld.Instance; |
||
135 | |||
136 | public static readonly CzechMapProvider CzechMap = CzechMapProvider.Instance; |
||
137 | public static readonly CzechSatelliteMapProvider CzechSatelliteMap = CzechSatelliteMapProvider.Instance; |
||
138 | public static readonly CzechHybridMapProvider CzechHybridMap = CzechHybridMapProvider.Instance; |
||
139 | public static readonly CzechTuristMapProvider CzechTuristMap = CzechTuristMapProvider.Instance; |
||
140 | public static readonly CzechTuristWinterMapProvider CzechTuristWinterMap = CzechTuristWinterMapProvider.Instance; |
||
141 | public static readonly CzechHistoryMapProvider CzechHistoryMap = CzechHistoryMapProvider.Instance; |
||
142 | public static readonly CzechGeographicMapProvider CzechGeographicMap = CzechGeographicMapProvider.Instance; |
||
143 | |||
144 | public static readonly ArcGIS_Imagery_World_2D_MapProvider ArcGIS_Imagery_World_2D_Map = ArcGIS_Imagery_World_2D_MapProvider.Instance; |
||
145 | public static readonly ArcGIS_ShadedRelief_World_2D_MapProvider ArcGIS_ShadedRelief_World_2D_Map = ArcGIS_ShadedRelief_World_2D_MapProvider.Instance; |
||
146 | public static readonly ArcGIS_StreetMap_World_2D_MapProvider ArcGIS_StreetMap_World_2D_Map = ArcGIS_StreetMap_World_2D_MapProvider.Instance; |
||
147 | public static readonly ArcGIS_Topo_US_2D_MapProvider ArcGIS_Topo_US_2D_Map = ArcGIS_Topo_US_2D_MapProvider.Instance; |
||
148 | |||
149 | public static readonly ArcGIS_World_Physical_MapProvider ArcGIS_World_Physical_Map = ArcGIS_World_Physical_MapProvider.Instance; |
||
150 | public static readonly ArcGIS_World_Shaded_Relief_MapProvider ArcGIS_World_Shaded_Relief_Map = ArcGIS_World_Shaded_Relief_MapProvider.Instance; |
||
151 | public static readonly ArcGIS_World_Street_MapProvider ArcGIS_World_Street_Map = ArcGIS_World_Street_MapProvider.Instance; |
||
152 | public static readonly ArcGIS_World_Terrain_Base_MapProvider ArcGIS_World_Terrain_Base_Map = ArcGIS_World_Terrain_Base_MapProvider.Instance; |
||
153 | public static readonly ArcGIS_World_Topo_MapProvider ArcGIS_World_Topo_Map = ArcGIS_World_Topo_MapProvider.Instance; |
||
154 | |||
155 | public static readonly ArcGIS_DarbAE_Q2_2011_NAVTQ_Eng_V5_MapProvider ArcGIS_DarbAE_Q2_2011_NAVTQ_Eng_V5_Map = ArcGIS_DarbAE_Q2_2011_NAVTQ_Eng_V5_MapProvider.Instance; |
||
156 | |||
157 | public static readonly SwedenMapProvider SwedenMap = SwedenMapProvider.Instance; |
||
158 | |||
159 | static List<GMapProvider> list; |
||
160 | |||
161 | /// <summary> |
||
162 | /// get all instances of the supported providers |
||
163 | /// </summary> |
||
164 | public static List<GMapProvider> List |
||
165 | { |
||
166 | get |
||
167 | { |
||
168 | return list; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | static Dictionary<Guid, GMapProvider> Hash; |
||
173 | |||
174 | public static GMapProvider TryGetProvider(Guid id) |
||
175 | { |
||
176 | GMapProvider ret; |
||
177 | if (Hash.TryGetValue(id, out ret)) |
||
178 | { |
||
179 | return ret; |
||
180 | } |
||
181 | return null; |
||
182 | } |
||
183 | |||
184 | static Dictionary<int, GMapProvider> DbHash; |
||
185 | |||
186 | public static GMapProvider TryGetProvider(int DbId) |
||
187 | { |
||
188 | GMapProvider ret; |
||
189 | if (DbHash.TryGetValue(DbId, out ret)) |
||
190 | { |
||
191 | return ret; |
||
192 | } |
||
193 | return null; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /// <summary> |
||
198 | /// base class for each map provider |
||
199 | /// </summary> |
||
200 | public abstract class GMapProvider |
||
201 | { |
||
202 | /// <summary> |
||
203 | /// unique provider id |
||
204 | /// </summary> |
||
205 | public abstract Guid Id |
||
206 | { |
||
207 | get; |
||
208 | } |
||
209 | |||
210 | /// <summary> |
||
211 | /// provider name |
||
212 | /// </summary> |
||
213 | public abstract string Name |
||
214 | { |
||
215 | get; |
||
216 | } |
||
217 | |||
218 | /// <summary> |
||
219 | /// provider projection |
||
220 | /// </summary> |
||
221 | public abstract PureProjection Projection |
||
222 | { |
||
223 | get; |
||
224 | } |
||
225 | |||
226 | /// <summary> |
||
227 | /// provider overlays |
||
228 | /// </summary> |
||
229 | public abstract GMapProvider[] Overlays |
||
230 | { |
||
231 | get; |
||
232 | } |
||
233 | |||
234 | /// <summary> |
||
235 | /// gets tile image using implmented provider |
||
236 | /// </summary> |
||
237 | /// <param name="pos"></param> |
||
238 | /// <param name="zoom"></param> |
||
239 | /// <returns></returns> |
||
240 | public abstract PureImage GetTileImage(GPoint pos, int zoom); |
||
241 | |||
242 | static readonly List<GMapProvider> MapProviders = new List<GMapProvider>(); |
||
243 | |||
244 | protected GMapProvider() |
||
245 | { |
||
246 | using (var HashProvider = new SHA1CryptoServiceProvider()) |
||
247 | { |
||
248 | DbId = Math.Abs(BitConverter.ToInt32(HashProvider.ComputeHash(Id.ToByteArray()), 0)); |
||
249 | } |
||
250 | |||
251 | if (MapProviders.Exists(p => p.Id == Id || p.DbId == DbId)) |
||
252 | { |
||
253 | throw new Exception("such provider id already exsists, try regenerate your provider guid..."); |
||
254 | } |
||
255 | MapProviders.Add(this); |
||
256 | } |
||
257 | |||
258 | static GMapProvider() |
||
259 | { |
||
260 | WebProxy = EmptyWebProxy.Instance; |
||
261 | } |
||
262 | |||
263 | bool isInitialized = false; |
||
264 | |||
265 | /// <summary> |
||
266 | /// was provider initialized |
||
267 | /// </summary> |
||
268 | public bool IsInitialized |
||
269 | { |
||
270 | get |
||
271 | { |
||
272 | return isInitialized; |
||
273 | } |
||
274 | internal set |
||
275 | { |
||
276 | isInitialized = value; |
||
277 | } |
||
278 | } |
||
279 | |||
280 | /// <summary> |
||
281 | /// called before first use |
||
282 | /// </summary> |
||
283 | public virtual void OnInitialized() |
||
284 | { |
||
285 | // nice place to detect current provider version |
||
286 | } |
||
287 | |||
288 | /// <summary> |
||
289 | /// id for database, a hash of provider guid |
||
290 | /// </summary> |
||
291 | public readonly int DbId; |
||
292 | |||
293 | /// <summary> |
||
294 | /// area of map |
||
295 | /// </summary> |
||
296 | public RectLatLng? Area; |
||
297 | |||
298 | /// <summary> |
||
299 | /// minimum level of zoom |
||
300 | /// </summary> |
||
301 | public int MinZoom; |
||
302 | |||
303 | /// <summary> |
||
304 | /// maximum level of zoom |
||
305 | /// </summary> |
||
306 | public int? MaxZoom = 17; |
||
307 | |||
308 | /// <summary> |
||
309 | /// proxy for net access |
||
310 | /// </summary> |
||
311 | public static IWebProxy WebProxy; |
||
312 | |||
313 | /// <summary> |
||
314 | /// Connect trough a SOCKS 4/5 proxy server |
||
315 | /// </summary> |
||
316 | public static bool IsSocksProxy = false; |
||
317 | |||
318 | /// <summary> |
||
319 | /// NetworkCredential for tile http access |
||
320 | /// </summary> |
||
321 | public static ICredentials Credential; |
||
322 | |||
323 | /// <summary> |
||
324 | /// Gets or sets the value of the User-agent HTTP header. |
||
325 | /// It's pseudo-randomized to avoid blockages... |
||
326 | /// </summary> |
||
327 | public static string UserAgent = string.Format("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:{0}.0) Gecko/{2}{3:00}{4:00} Firefox/{0}.0.{1}", Stuff.random.Next(3, 14), Stuff.random.Next(1, 10), Stuff.random.Next(DateTime.Today.Year - 4, DateTime.Today.Year), Stuff.random.Next(12), Stuff.random.Next(30)); |
||
328 | |||
329 | /// <summary> |
||
330 | /// timeout for provider connections |
||
331 | /// </summary> |
||
332 | #if !PocketPC |
||
333 | public static int TimeoutMs = 5 * 1000; |
||
334 | #else |
||
335 | public static int TimeoutMs = 44 * 1000; |
||
336 | #endif |
||
337 | /// <summary> |
||
338 | /// Gets or sets the value of the Referer HTTP header. |
||
339 | /// </summary> |
||
340 | public string RefererUrl = string.Empty; |
||
341 | |||
342 | public string Copyright = string.Empty; |
||
343 | |||
344 | /// <summary> |
||
345 | /// true if tile origin at BottomLeft, WMS-C |
||
346 | /// </summary> |
||
347 | public bool InvertedAxisY = false; |
||
348 | |||
349 | static string languageStr = "en"; |
||
350 | public static string LanguageStr |
||
351 | { |
||
352 | get |
||
353 | { |
||
354 | return languageStr; |
||
355 | } |
||
356 | } |
||
357 | static LanguageType language = LanguageType.English; |
||
358 | |||
359 | /// <summary> |
||
360 | /// map language |
||
361 | /// </summary> |
||
362 | public static LanguageType Language |
||
363 | { |
||
364 | get |
||
365 | { |
||
366 | return language; |
||
367 | } |
||
368 | set |
||
369 | { |
||
370 | language = value; |
||
371 | languageStr = Stuff.EnumToString(Language); |
||
372 | } |
||
373 | } |
||
374 | |||
375 | /// <summary> |
||
376 | /// to bypass the cache, set to true |
||
377 | /// </summary> |
||
378 | public bool BypassCache = false; |
||
379 | |||
380 | /// <summary> |
||
381 | /// internal proxy for image managment |
||
382 | /// </summary> |
||
383 | internal static PureImageProxy TileImageProxy; |
||
384 | |||
385 | static readonly string requestAccept = "*/*"; |
||
386 | static readonly string responseContentType = "image"; |
||
387 | |||
388 | protected virtual bool CheckTileImageHttpResponse(WebResponse response) |
||
389 | { |
||
390 | //Debug.WriteLine(response.StatusCode + "/" + response.StatusDescription + "/" + response.ContentType + " -> " + response.ResponseUri); |
||
391 | return response.ContentType.Contains(responseContentType); |
||
392 | } |
||
393 | |||
394 | string Authorization = string.Empty; |
||
395 | |||
396 | /// <summary> |
||
397 | /// http://blog.kowalczyk.info/article/at3/Forcing-basic-http-authentication-for-HttpWebReq.html |
||
398 | /// </summary> |
||
399 | /// <param name="userName"></param> |
||
400 | /// <param name="userPassword"></param> |
||
401 | public void ForceBasicHttpAuthentication(string userName, string userPassword) |
||
402 | { |
||
403 | Authorization = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + userPassword)); |
||
404 | } |
||
405 | |||
406 | protected PureImage GetTileImageUsingHttp(string url) |
||
407 | { |
||
408 | PureImage ret = null; |
||
409 | |||
410 | #if !PocketPC |
||
411 | WebRequest request = IsSocksProxy ? SocksHttpWebRequest.Create(url) : WebRequest.Create(url); |
||
412 | #else |
||
413 | WebRequest request = WebRequest.Create(url); |
||
414 | #endif |
||
415 | if (WebProxy != null) |
||
416 | { |
||
417 | request.Proxy = WebProxy; |
||
418 | } |
||
419 | |||
420 | if (Credential != null) |
||
421 | { |
||
422 | request.PreAuthenticate = true; |
||
423 | request.Credentials = Credential; |
||
424 | } |
||
425 | |||
426 | if(!string.IsNullOrEmpty(Authorization)) |
||
427 | { |
||
428 | request.Headers.Set("Authorization", Authorization); |
||
429 | } |
||
430 | |||
431 | if (request is HttpWebRequest) |
||
432 | { |
||
433 | var r = request as HttpWebRequest; |
||
434 | r.UserAgent = UserAgent; |
||
435 | r.ReadWriteTimeout = TimeoutMs * 6; |
||
436 | r.Accept = requestAccept; |
||
437 | r.Referer = RefererUrl; |
||
438 | r.Timeout = TimeoutMs; |
||
439 | } |
||
440 | #if !PocketPC |
||
441 | else if (request is SocksHttpWebRequest) |
||
442 | { |
||
443 | var r = request as SocksHttpWebRequest; |
||
444 | |||
445 | if (!string.IsNullOrEmpty(UserAgent)) |
||
446 | { |
||
447 | r.Headers.Add("User-Agent", UserAgent); |
||
448 | } |
||
449 | |||
450 | if (!string.IsNullOrEmpty(requestAccept)) |
||
451 | { |
||
452 | r.Headers.Add("Accept", requestAccept); |
||
453 | } |
||
454 | |||
455 | if (!string.IsNullOrEmpty(RefererUrl)) |
||
456 | { |
||
457 | r.Headers.Add("Referer", RefererUrl); |
||
458 | } |
||
459 | } |
||
460 | #endif |
||
461 | using (var response = request.GetResponse()) |
||
462 | { |
||
463 | if (CheckTileImageHttpResponse(response)) |
||
464 | { |
||
465 | using (Stream responseStream = response.GetResponseStream()) |
||
466 | { |
||
467 | MemoryStream data = Stuff.CopyStream(responseStream, false); |
||
468 | |||
469 | Debug.WriteLine("Response[" + data.Length + " bytes]: " + url); |
||
470 | |||
471 | if (data.Length > 0) |
||
472 | { |
||
473 | ret = TileImageProxy.FromStream(data); |
||
474 | |||
475 | if (ret != null) |
||
476 | { |
||
477 | ret.Data = data; |
||
478 | ret.Data.Position = 0; |
||
479 | } |
||
480 | else |
||
481 | { |
||
482 | data.Dispose(); |
||
483 | } |
||
484 | } |
||
485 | data = null; |
||
486 | } |
||
487 | } |
||
488 | else |
||
489 | { |
||
490 | Debug.WriteLine("CheckTileImageHttpResponse[false]: " + url); |
||
491 | } |
||
492 | #if PocketPC |
||
493 | request.Abort(); |
||
494 | #endif |
||
495 | response.Close(); |
||
496 | } |
||
497 | return ret; |
||
498 | } |
||
499 | |||
500 | protected string GetContentUsingHttp(string url) |
||
501 | { |
||
502 | string ret = string.Empty; |
||
503 | |||
504 | #if !PocketPC |
||
505 | WebRequest request = IsSocksProxy ? SocksHttpWebRequest.Create(url) : WebRequest.Create(url); |
||
506 | #else |
||
507 | WebRequest request = WebRequest.Create(url); |
||
508 | #endif |
||
509 | |||
510 | if (WebProxy != null) |
||
511 | { |
||
512 | request.Proxy = WebProxy; |
||
513 | } |
||
514 | |||
515 | if (Credential != null) |
||
516 | { |
||
517 | request.PreAuthenticate = true; |
||
518 | request.Credentials = Credential; |
||
519 | } |
||
520 | |||
521 | if(!string.IsNullOrEmpty(Authorization)) |
||
522 | { |
||
523 | request.Headers.Set("Authorization", Authorization); |
||
524 | } |
||
525 | |||
526 | if (request is HttpWebRequest) |
||
527 | { |
||
528 | var r = request as HttpWebRequest; |
||
529 | r.UserAgent = UserAgent; |
||
530 | r.ReadWriteTimeout = TimeoutMs * 6; |
||
531 | r.Accept = requestAccept; |
||
532 | r.Referer = RefererUrl; |
||
533 | r.Timeout = TimeoutMs; |
||
534 | } |
||
535 | #if !PocketPC |
||
536 | else if (request is SocksHttpWebRequest) |
||
537 | { |
||
538 | var r = request as SocksHttpWebRequest; |
||
539 | |||
540 | if (!string.IsNullOrEmpty(UserAgent)) |
||
541 | { |
||
542 | r.Headers.Add("User-Agent", UserAgent); |
||
543 | } |
||
544 | |||
545 | if (!string.IsNullOrEmpty(requestAccept)) |
||
546 | { |
||
547 | r.Headers.Add("Accept", requestAccept); |
||
548 | } |
||
549 | |||
550 | if (!string.IsNullOrEmpty(RefererUrl)) |
||
551 | { |
||
552 | r.Headers.Add("Referer", RefererUrl); |
||
553 | } |
||
554 | } |
||
555 | #endif |
||
556 | using (var response = request.GetResponse()) |
||
557 | { |
||
558 | using (Stream responseStream = response.GetResponseStream()) |
||
559 | { |
||
560 | using (StreamReader read = new StreamReader(responseStream, Encoding.UTF8)) |
||
561 | { |
||
562 | ret = read.ReadToEnd(); |
||
563 | } |
||
564 | } |
||
565 | #if PocketPC |
||
566 | request.Abort(); |
||
567 | #endif |
||
568 | response.Close(); |
||
569 | } |
||
570 | |||
571 | return ret; |
||
572 | } |
||
573 | |||
574 | #if !PocketPC |
||
575 | /// <summary> |
||
576 | /// use at your own risk, storing tiles in files is slow and hard on the file system |
||
577 | /// </summary> |
||
578 | /// <param name="fileName"></param> |
||
579 | /// <returns></returns> |
||
580 | protected virtual PureImage GetTileImageFromFile(string fileName) |
||
581 | { |
||
582 | return GetTileImageFromArray(File.ReadAllBytes(fileName)); |
||
583 | } |
||
584 | #endif |
||
585 | protected virtual PureImage GetTileImageFromArray(byte [] data) |
||
586 | { |
||
587 | return TileImageProxy.FromArray(data); |
||
588 | } |
||
589 | |||
590 | protected static int GetServerNum(GPoint pos, int max) |
||
591 | { |
||
592 | return (int)(pos.X + 2 * pos.Y) % max; |
||
593 | } |
||
594 | |||
595 | public override int GetHashCode() |
||
596 | { |
||
597 | return (int)DbId; |
||
598 | } |
||
599 | |||
600 | public override bool Equals(object obj) |
||
601 | { |
||
602 | if (obj is GMapProvider) |
||
603 | { |
||
604 | return Id.Equals((obj as GMapProvider).Id); |
||
605 | } |
||
606 | return false; |
||
607 | } |
||
608 | |||
609 | public override string ToString() |
||
610 | { |
||
611 | return Name; |
||
612 | } |
||
613 | } |
||
614 | |||
615 | /// <summary> |
||
616 | /// represents empty provider |
||
617 | /// </summary> |
||
618 | public class EmptyProvider : GMapProvider |
||
619 | { |
||
620 | public static readonly EmptyProvider Instance; |
||
621 | |||
622 | EmptyProvider() |
||
623 | { |
||
624 | MaxZoom = null; |
||
625 | } |
||
626 | |||
627 | static EmptyProvider() |
||
628 | { |
||
629 | Instance = new EmptyProvider(); |
||
630 | } |
||
631 | |||
632 | #region GMapProvider Members |
||
633 | |||
634 | public override Guid Id |
||
635 | { |
||
636 | get |
||
637 | { |
||
638 | return Guid.Empty; |
||
639 | } |
||
640 | } |
||
641 | |||
642 | readonly string name = "None"; |
||
643 | public override string Name |
||
644 | { |
||
645 | get |
||
646 | { |
||
647 | return name; |
||
648 | } |
||
649 | } |
||
650 | |||
651 | readonly MercatorProjection projection = MercatorProjection.Instance; |
||
652 | public override PureProjection Projection |
||
653 | { |
||
654 | get |
||
655 | { |
||
656 | return projection; |
||
657 | } |
||
658 | } |
||
659 | |||
660 | public override GMapProvider[] Overlays |
||
661 | { |
||
662 | get |
||
663 | { |
||
664 | return null; |
||
665 | } |
||
666 | } |
||
667 | |||
668 | public override PureImage GetTileImage(GPoint pos, int zoom) |
||
669 | { |
||
670 | return null; |
||
671 | } |
||
672 | |||
673 | #endregion |
||
674 | } |
||
675 | |||
676 | public sealed class EmptyWebProxy : IWebProxy |
||
677 | { |
||
678 | public static readonly EmptyWebProxy Instance = new EmptyWebProxy(); |
||
679 | |||
680 | private ICredentials m_credentials; |
||
681 | public ICredentials Credentials |
||
682 | { |
||
683 | get |
||
684 | { |
||
685 | return this.m_credentials; |
||
686 | } |
||
687 | set |
||
688 | { |
||
689 | this.m_credentials = value; |
||
690 | } |
||
691 | } |
||
692 | |||
693 | public Uri GetProxy(Uri uri) |
||
694 | { |
||
695 | return uri; |
||
696 | } |
||
697 | |||
698 | public bool IsBypassed(Uri uri) |
||
699 | { |
||
700 | return true; |
||
701 | } |
||
702 | } |
||
703 | } |