-
Notifications
You must be signed in to change notification settings - Fork 1
/
OSMData.cs
689 lines (616 loc) · 26.9 KB
/
OSMData.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Drawing;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
namespace OSM2SHP
{
public class OSMDictionary
{
public string language = "";
public Dictionary<string, DictCatalog> catalog = new Dictionary<string, DictCatalog>();
public Dictionary<string, DictCatalog> moretags = new Dictionary<string, DictCatalog>();
public Dictionary<string, Dictionary<string, string>> clas = new Dictionary<string, Dictionary<string, string>>();
public static OSMDictionary ReadFromFile(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
string jsontext = sr.ReadToEnd();
sr.Close();
fs.Close();
OSMDictionary result = new OSMDictionary();
Newtonsoft.Json.Linq.JToken osmd = (Newtonsoft.Json.Linq.JContainer)Newtonsoft.Json.JsonConvert.DeserializeObject(jsontext);
foreach (Newtonsoft.Json.Linq.JProperty suntoken in osmd)
{
if (suntoken.Name == "language")
result.language = suntoken.Value.ToString();
if (suntoken.Name == "catalog")
{
foreach (Newtonsoft.Json.Linq.JProperty cat in suntoken.Value)
{
DictCatalog c = new DictCatalog();
foreach (Newtonsoft.Json.Linq.JProperty catv in cat.Value)
{
if (catv.Name == "name") c.name = catv.Value.ToString();
if (catv.Name == "description") c.description = catv.Value.ToString();
if (catv.Name == "link") c.link = catv.Value.ToString();
if (catv.Name == "keywords") c.keywords = catv.Value.ToString();
};
result.catalog.Add(cat.Name, c);
};
};
if (suntoken.Name == "moretags")
{
foreach (Newtonsoft.Json.Linq.JProperty cat in suntoken.Value)
{
DictCatalog c = new DictCatalog();
foreach (Newtonsoft.Json.Linq.JProperty catv in cat.Value)
{
if (catv.Name == "name") c.name = catv.Value.ToString();
if (catv.Name == "description") c.description = catv.Value.ToString();
if (catv.Name == "link") c.link = catv.Value.ToString();
if (catv.Name == "keywords") c.keywords = catv.Value.ToString();
};
result.moretags.Add(cat.Name, c);
};
};
if (suntoken.Name == "class")
{
foreach (Newtonsoft.Json.Linq.JProperty cat in suntoken.Value)
{
Dictionary<string, string> kv = new Dictionary<string, string>();
foreach (Newtonsoft.Json.Linq.JProperty catv in cat.Value)
kv.Add(catv.Name, catv.Value.ToString());
result.clas.Add(cat.Name, kv);
};
};
};
return result;
}
public class DictCatalog
{
public string name;
public string description;
public string link;
public string keywords;
}
public string Translate(string text)
{
string result = text;
foreach (KeyValuePair<string, DictCatalog> kvp in catalog)
if (kvp.Key == text) return kvp.Value.name;
foreach (KeyValuePair<string, DictCatalog> kvp in moretags)
if (kvp.Key == text) return kvp.Value.name;
return result;
}
public string TranslateClass(string clas, string text)
{
string result = text;
foreach (KeyValuePair<string, Dictionary<string, string>> kvp in this.clas)
if (kvp.Key == clas)
foreach (KeyValuePair<string, string> tr in kvp.Value)
if (tr.Key == text)
return tr.Value;
return result;
}
public string Translate(string key, string value)
{
string result = value;
foreach (KeyValuePair<string, Dictionary<string, string>> kvp in clas)
if (kvp.Key == key)
foreach (KeyValuePair<string, string> vvp in kvp.Value)
if (vvp.Key == value)
return vvp.Value;
return result;
}
}
public class OSMCatalog
{
public List<OSMCatalogRecord> records = new List<OSMCatalogRecord>();
public int Count
{
get
{
return records.Count;
}
}
public string GetTopParentCategory(string category)
{
string result = category;
if (Count == 0) return result;
bool ex = true;
while (ex)
{
ex = false;
for (int i = 0; i < Count; i++)
if (this[i].name == result)
{
ex = true;
if (this[i].parent == null) return result;
if (this[i].parent.Length == 0) return result;
result = this[i].parent[0];
break;
};
};
return result;
}
private string[] GetHeirarchyString(int index)
{
string[] result = new string[] { records[index].name, dict.Translate(records[index].name) };
while ((records[index].parent != null) && (records[index].parent.Length > 0))
{
foreach (string parent in records[index].parent)
for (int i = 0; i < records.Count; i++)
if (records[i].name == parent)
{
index = i;
break;
};
result[0] = records[index].name + @"\" + result[0];
result[1] = dict.Translate(records[index].name) + @"\" + result[1];
};
return result;
}
public OSMCatalogHierarchy[] GetHierarchyElements(string category, int hierarchyIndex)
{
if (hierarchyIndex == 0) return new OSMCatalogHierarchy[0];
if (records == null) return new OSMCatalogHierarchy[0];
if (records.Count == 0) return new OSMCatalogHierarchy[0];
for (int i = 0; i < records.Count; i++)
if (records[i].name == category)
return GetHierarchyElements(records[i].id, hierarchyIndex);
return new OSMCatalogHierarchy[0];
}
public OSMCatalogHierarchy GetHierarchyElement(string category, int hierarchyIndex)
{
if (hierarchyIndex == 0) return null;
if (records == null) return null;
if (records.Count == 0) return null;
for (int i = 0; i < records.Count; i++)
if (records[i].name == category)
return GetHierarchyElement(records[i].id, hierarchyIndex);
return null;
}
public OSMCatalogHierarchy[] GetHierarchyElements(int category, int hierarchyIndex)
{
if (hierarchyIndex == 0) return new OSMCatalogHierarchy[0];
if (records == null) return new OSMCatalogHierarchy[0];
if (records.Count == 0) return new OSMCatalogHierarchy[0];
List<OSMCatalogHierarchy> result = new List<OSMCatalogHierarchy>();
List<int> rids = new List<int>();
hierarchyIndex = Math.Abs(hierarchyIndex) - 1;
for(int i=0;i<records.Count;i++)
if (records[i].id == category)
{
OSMCatalogHierarchy hel = new OSMCatalogHierarchy();
hel.id = records[i].id;
hel.dict_name = dict.Translate(hel.name = records[i].name);
string[] hs = GetHeirarchyString(i);
hel.fullname = hs[0];
hel.dict_fullname = hs[1];
hel.default_icon = records[i].name;
result.Add(hel);
rids.Add(i);
};
if (hierarchyIndex > 0)
for (int h = 0; h < hierarchyIndex; h++)
{
bool has_parent = false;
for (int el = result.Count - 1; el >= 0; el--)
if ((records[rids[el]].parent != null) && (records[rids[el]].parent.Length > 0))
{
bool remove = false;
foreach (string parent in records[rids[el]].parent)
{
for (int i = 0; i < records.Count; i++)
if (records[i].name == parent)
{
has_parent = remove = true;
OSMCatalogHierarchy hel = new OSMCatalogHierarchy();
hel.id = records[i].id;
hel.dict_name = dict.Translate(hel.name = records[i].name);
string[] hs = GetHeirarchyString(i);
hel.fullname = hs[0];
hel.dict_fullname = hs[1];
hel.default_icon = records[i].name;
result.Add(hel);
rids.Add(i);
};
};
if (remove)
{
result.RemoveAt(el);
rids.RemoveAt(el);
};
};
if (!has_parent) result.ToArray();
};
return result.ToArray();
}
public OSMCatalogHierarchy GetHierarchyElement(int category, int hierarchyIndex)
{
if (hierarchyIndex == 0) return null;
if (records == null) return null;
if (records.Count == 0) return null;
hierarchyIndex = Math.Abs(hierarchyIndex) - 1;
OSMCatalogHierarchy hel_item = null;
int hel_index = -1;
for (int i = 0; i < records.Count; i++)
if (records[i].id == category)
{
hel_item = new OSMCatalogHierarchy();
hel_item.id = records[i].id;
hel_item.dict_name = dict.Translate(hel_item.name = records[i].name);
string[] hs = GetHeirarchyString(i);
hel_item.fullname = hs[0];
hel_item.dict_fullname = hs[1];
hel_item.default_icon = records[i].name;
hel_index = i;
break;
};
if (hel_item == null) return null;
while ((hierarchyIndex > 0) && (records[hel_index].parent != null) && (records[hel_index].parent.Length > 0))
{
for (int i = 0; i < records.Count; i++)
if (records[i].name == records[hel_index].parent[0])
{
hel_item = new OSMCatalogHierarchy();
hel_item.id = records[i].id;
hel_item.dict_name = dict.Translate(hel_item.name = records[i].name);
string[] hs = GetHeirarchyString(i);
hel_item.fullname = hs[0];
hel_item.dict_fullname = hs[1];
hel_item.default_icon = records[i].name;
hel_index = i;
i = records.Count;
hierarchyIndex--;
break;
};
};
return hel_item;
}
public OSMCatalogRecord this[int index]
{
get
{
return records[index];
}
}
public OSMCatalogRecord ByName(string name)
{
if (Count == 0) return null;
foreach (OSMCatalogRecord rec in records)
if (rec.name == name)
return rec;
return null;
}
public OSMCatalogRecord ByID(int id)
{
if (Count == 0) return null;
foreach (OSMCatalogRecord rec in records)
if (rec.id == id)
return rec;
return null;
}
public OSMDictionary dict = new OSMDictionary();
public class OSMCatalogRecord
{
public string name;
public string[] parent;
[JsonIgnore]
public Dictionary<string, string> tags = new Dictionary<string, string>();
[JsonIgnore]
public Dictionary<string, Dictionary<string, string>> moretags = new Dictionary<string, Dictionary<string, string>>();
public bool poi;
public string[] type;
public int id;
}
public static OSMCatalog ReadFromFile(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
string jsontext = sr.ReadToEnd();
sr.Close();
fs.Close();
Newtonsoft.Json.Linq.JArray src = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(jsontext);
List<OSMCatalogRecord> res = new List<OSMCatalogRecord>();
for (int i = 0; i < src.Count; i++)
{
Newtonsoft.Json.Linq.JToken t = (Newtonsoft.Json.Linq.JToken)src[i];
OSMCatalogRecord sc = new OSMCatalogRecord();
sc.id = (int)t["id"];
sc.name = t["name"].ToString();
sc.poi = (bool)t["poi"];
Newtonsoft.Json.Linq.JArray sub_arr = (Newtonsoft.Json.Linq.JArray)t["parent"];
if (sub_arr.Count > 0)
{
sc.parent = new string[sub_arr.Count];
for (int a = 0; a < sub_arr.Count; a++)
sc.parent[a] = sub_arr[a].ToString();
};
sub_arr = (Newtonsoft.Json.Linq.JArray)t["type"];
if (sub_arr.Count > 0)
{
sc.type = new string[sub_arr.Count];
for (int a = 0; a < sub_arr.Count; a++)
sc.type[a] = sub_arr[a].ToString();
};
Newtonsoft.Json.Linq.JToken tags = (Newtonsoft.Json.Linq.JContainer)t["tags"];
foreach (Newtonsoft.Json.Linq.JProperty suntoken in tags)
sc.tags.Add(suntoken.Name, suntoken.Value.ToString());
Newtonsoft.Json.Linq.JToken moretags = (Newtonsoft.Json.Linq.JContainer)t["moretags"];
foreach (Newtonsoft.Json.Linq.JProperty suntoken in moretags)
{
Dictionary<string, string> subs = new Dictionary<string, string>();
Newtonsoft.Json.Linq.JToken elmt = (Newtonsoft.Json.Linq.JContainer)suntoken.Value;
foreach (Newtonsoft.Json.Linq.JProperty et in elmt)
subs.Add(et.Name, et.Value.ToString());
sc.moretags.Add(suntoken.Name, subs);
};
res.Add(sc);
};
OSMCatalog catalogue = new OSMCatalog();
catalogue.records = res;
return catalogue;
}
public OSMCatalogHierarchyList GetHierarchy(int hierarchyIndex)
{
return GetHierarchy(hierarchyIndex, false, -1);
}
public OSMCatalogHierarchyList GetHierarchy(int hierarchyIndex, bool onlyBasic, int saveNoCategory)
{
OSMCatalogHierarchyList result = new OSMCatalogHierarchyList();
{
if ((saveNoCategory > 0) || (!onlyBasic))
{
OSMCatalogHierarchy hel = new OSMCatalogHierarchy();
hel.id = -1; // "NoCategory"
hel.name = hel.fullname = hel.dict_name = hel.dict_fullname = "NoCategory";
hel.iconList.Add(hel.default_icon = "nocategory");
result.Add(hel);
};
if ((saveNoCategory == 2) || (!onlyBasic))
{
OSMCatalogHierarchy hel = new OSMCatalogHierarchy();
hel.id = -2; // "NoValuesButKeys"
hel.name = hel.fullname = hel.dict_name = hel.dict_fullname = "NoValuesButKeys";
hel.iconList.Add(hel.default_icon = "novalues");
result.Add(hel);
};
};
if (onlyBasic) return result;
if((records != null) && (records.Count > 0))
for (int i = 0; i < records.Count; i++)
{
if (hierarchyIndex == 0)
{
string all_tags = "";
if (records[i].tags.Count == 0)
all_tags = records[i].name;
else
foreach (KeyValuePair<string, string> tag in records[i].tags)
{
if (all_tags.Length > 0) all_tags += ",";
all_tags += String.Format("{0}:{1}", tag.Key, tag.Value);
};
if (result.IndexOf(all_tags) < 0)
result.Add(new OSMCatalogHierarchy(records[i].id, all_tags, records[i].name));
}
else
{
OSMCatalogHierarchy[] hels = this.GetHierarchyElements(records[i].id, hierarchyIndex);
foreach (OSMCatalogHierarchy hel in hels)
if (result.IndexOf(hel.id) < 0)
result.Add(hel);
};
};
return result;
}
}
public class OSMCatalogHierarchy
{
public int id;
public string name;
public string fullname;
public string dict_name;
public string dict_fullname;
public string default_icon = "noicon";
public List<string> iconList = new List<string>(new string[] { "noicon" });
public long nodes = 0;
public long file_size = 0;
public string FileSize
{
get
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
double len = file_size;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len = len / 1024;
};
string res = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0.##} {1}", len, sizes[order]);
return res;
}
}
public OSMCatalogHierarchy() { }
public OSMCatalogHierarchy(int id, string name)
{
this.id = id;
this.name = this.fullname = this.dict_name = this.dict_fullname = name;
}
public OSMCatalogHierarchy(int id, string name, string def_icon)
{
this.id = id;
this.name = this.fullname = this.dict_name = this.dict_fullname = name;
this.default_icon = def_icon;
if (this.iconList.IndexOf(def_icon) < 0)
this.iconList.Add(def_icon);
}
public byte[] FileHeader
{
get
{
string text = dict_name;
if (name != dict_name) text = dict_name + " (" + name + ")";
return System.Text.Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
"<kml>\r\n<Document>\r\n<name>" + text + "</name>\r\n<createdby>KMZRebuilder XP</createdby>\r\n");
}
}
public byte[] FileHeaderFull
{
get
{
string text = dict_fullname;
if (fullname != dict_fullname) text = dict_fullname + " (" + fullname + ")";
return System.Text.Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
"<kml>\r\n<Document>\r\n<name>" + text + "</name>\r\n<createdby>KMZRebuilder XP</createdby>\r\n");
}
}
public byte[] FileFooter
{
get
{
return System.Text.Encoding.UTF8.GetBytes("</Document></kml>");
}
}
public byte[] FolderHeader
{
get
{
string text = dict_name;
if (name != dict_name) text = dict_name + " (" + name + ")";
return System.Text.Encoding.UTF8.GetBytes("<Folder>\r\n<name>" + text + "</name>\r\n");
}
}
public byte[] FolderHeaderFull
{
get
{
string text = dict_fullname;
if (fullname != dict_fullname) text = dict_fullname + " (" + fullname + ")";
return System.Text.Encoding.UTF8.GetBytes("<Folder>\r\n<name>" + text + "</name>\r\n");
}
}
public byte[] FolderFooter
{
get
{
return System.Text.Encoding.UTF8.GetBytes("</Folder>\r\n");
}
}
public byte[] FileStyles
{
get
{
string styles = "";
for (int i = 0; i < iconList.Count; i++)
styles += "\t<Style id=\"" + iconList[i] + "\"><IconStyle><Icon><href>images/" + iconList[i] + ".png</href></Icon></IconStyle></Style>";
return System.Text.Encoding.UTF8.GetBytes(styles);
}
}
}
public class OSMCatalogHierarchyList: List<OSMCatalogHierarchy>
{
public int IndexOf(int elementID)
{
if (this.Count == 0) return -1;
for (int i = 0; i < this.Count; i++)
if (this[i].id == elementID)
return i;
return -1;
}
public int IndexOf(string name)
{
if (this.Count == 0) return -1;
for (int i = 0; i < this.Count; i++)
if (this[i].name == name)
return i;
return -1;
}
public static Image GetImageFromZip(string zipfile, string imagefile)
{
try
{
FileStream fs = File.OpenRead(zipfile);
ZipFile zf = new ZipFile(fs);
foreach (ZipEntry zipEntry in zf)
{
if (!zipEntry.IsFile) continue; // Ignore directories
if (zipEntry.Name.ToLower() != imagefile.ToLower()) continue;
byte[] buffer = new byte[4096]; // 4K is optimum
Stream zipStream = zf.GetInputStream(zipEntry);
try
{
Stream ms = new MemoryStream();
StreamUtils.Copy(zipStream, ms, buffer);
ms.Position = 0;
Image im = new Bitmap(ms);
ms.Dispose();
zf.Close();
fs.Close();
return im;
}
catch
{
};
};
zf.Close();
fs.Close();
}
catch { };
return null;
}
public static Stream GetImageFileFromZip(string zipfile, string imagefile)
{
try
{
FileStream fs = File.OpenRead(zipfile);
ZipFile zf = new ZipFile(fs);
foreach (ZipEntry zipEntry in zf)
{
if (!zipEntry.IsFile) continue; // Ignore directories
if (zipEntry.Name.ToLower() != imagefile.ToLower()) continue;
byte[] buffer = new byte[4096]; // 4K is optimum
Stream zipStream = zf.GetInputStream(zipEntry);
try
{
Stream ms = new MemoryStream();
StreamUtils.Copy(zipStream, ms, buffer);
ms.Position = 0;
zf.Close();
fs.Close();
return ms;
}
catch
{
};
};
zf.Close();
fs.Close();
}
catch { };
return null;
}
public byte[] GetFileHeader(string name)
{
return System.Text.Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
"<kml>\r\n<Document>\r\n<name>" + name + "</name>\r\n<createdby>KMZRebuilder XP</createdby>\r\n");
}
public byte[] GetFileFooter()
{
return System.Text.Encoding.UTF8.GetBytes("</Document></kml>");
}
public byte[] GetFileStyles(string[] iconList)
{
string styles = "";
for (int i = 0; i < iconList.Length; i++)
styles += "\t<Style id=\"" + iconList[i] + "\"><IconStyle><Icon><href>images/" + iconList[i] + ".png</href></Icon></IconStyle></Style>";
return System.Text.Encoding.UTF8.GetBytes(styles);
}
}
}