GeoRegion.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Microsoft.IdentityModel.Tokens;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing.Drawing2D;
  5. using System.IdentityModel.Tokens.Jwt;
  6. using System.IO;
  7. using System.Text;
  8. using System.Text.Json;
  9. namespace TEAMModelOS.SDK.Extension
  10. {
  11. public static class GeoRegion
  12. {
  13. //取得國省市區地理資料架構
  14. public static regiondata GetRegionData()
  15. {
  16. regiondata region = new regiondata();
  17. //國際
  18. var regionTw = new List<regionrow>();
  19. using (StreamReader r = new StreamReader("JsonFile/Region/region_gl.json"))
  20. {
  21. string json = r.ReadToEnd();
  22. regionTw = JsonSerializer.Deserialize<List<regionrow>>(json);
  23. foreach (regionrow itemcy in regionTw)
  24. {
  25. //country
  26. string countryCode = itemcy.code;
  27. if (!region.country.ContainsKey(countryCode))
  28. {
  29. region.country.Add(countryCode, new regionbase() { code = countryCode, name = itemcy.name });
  30. }
  31. //province 無
  32. //city
  33. if (itemcy.children != null)
  34. {
  35. foreach (JsonElement itemcyChild in itemcy.children)
  36. {
  37. regionrow itemct = itemcyChild.ToObject<regionrow>();
  38. string provinceCode = "tw"; //台灣的省代碼用"tw"
  39. string cityCode = itemct.code;
  40. if (!region.city.ContainsKey(countryCode)) region.city.Add(countryCode, new Dictionary<string, Dictionary<string, regionbase>>());
  41. if (!region.city[countryCode].ContainsKey(provinceCode)) region.city[countryCode].Add(provinceCode, new Dictionary<string, regionbase>());
  42. if (!region.city[countryCode][provinceCode].ContainsKey(cityCode)) region.city[countryCode][provinceCode].Add(cityCode, new regionbase() { code = cityCode, name = itemct.name });
  43. //dist
  44. if (itemct.children != null)
  45. {
  46. foreach (JsonElement itemctChild in itemct.children)
  47. {
  48. regionrow itemds = itemctChild.ToObject<regionrow>();
  49. string distCode = itemds.code;
  50. if (!region.dist.ContainsKey(countryCode)) region.dist.Add(countryCode, new Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>());
  51. if (!region.dist[countryCode].ContainsKey(provinceCode)) region.dist[countryCode].Add(provinceCode, new Dictionary<string, Dictionary<string, regionbase>>());
  52. if (!region.dist[countryCode][provinceCode].ContainsKey(cityCode)) region.dist[countryCode][provinceCode].Add(cityCode, new Dictionary<string, regionbase>());
  53. if (!region.dist[countryCode][provinceCode][cityCode].ContainsKey(distCode)) region.dist[countryCode][provinceCode][cityCode].Add(distCode, new regionbase() { code = distCode, name = itemds.name });
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. //大陸
  61. var regionCn = new List<regionrow>();
  62. using (StreamReader r = new StreamReader("JsonFile/Region/region_cn.json"))
  63. {
  64. string json = r.ReadToEnd();
  65. regionCn = JsonSerializer.Deserialize<List<regionrow>>(json);
  66. //country
  67. string countryCode = "CN";
  68. string countryName = "中国";
  69. if (!region.country.ContainsKey(countryCode))
  70. {
  71. region.country.Add(countryCode, new regionbase() { code = countryCode, name = countryName });
  72. }
  73. //province
  74. foreach (regionrow itempv in regionCn)
  75. {
  76. string provinceCode = itempv.code.Replace("0000", "");
  77. if (!region.province.ContainsKey(countryCode)) region.province.Add(countryCode, new Dictionary<string, regionbase>());
  78. if (!region.province[countryCode].ContainsKey(provinceCode)) region.province[countryCode].Add(provinceCode, new regionbase() { code = provinceCode, name = itempv.name });
  79. //city
  80. if (itempv.children != null)
  81. {
  82. foreach (JsonElement itempvChild in itempv.children)
  83. {
  84. regionrow itemct = itempvChild.ToObject<regionrow>();
  85. string cityCode = itemct.code;
  86. if (!region.city.ContainsKey(countryCode)) region.city.Add(countryCode, new Dictionary<string, Dictionary<string, regionbase>>());
  87. if (!region.city[countryCode].ContainsKey(provinceCode)) region.city[countryCode].Add(provinceCode, new Dictionary<string, regionbase>());
  88. if (!region.city[countryCode][provinceCode].ContainsKey(cityCode)) region.city[countryCode][provinceCode].Add(cityCode, new regionbase() { code = cityCode, name = itemct.name });
  89. //dist
  90. if (itemct.children != null)
  91. {
  92. foreach (JsonElement itemctChild in itemct.children)
  93. {
  94. regionrow itemds = itemctChild.ToObject<regionrow>();
  95. string distCode = itemds.code;
  96. if (!region.dist.ContainsKey(countryCode)) region.dist.Add(countryCode, new Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>());
  97. if (!region.dist[countryCode].ContainsKey(provinceCode)) region.dist[countryCode].Add(provinceCode, new Dictionary<string, Dictionary<string, regionbase>>());
  98. if (!region.dist[countryCode][provinceCode].ContainsKey(cityCode)) region.dist[countryCode][provinceCode].Add(cityCode, new Dictionary<string, regionbase>());
  99. if (!region.dist[countryCode][provinceCode][cityCode].ContainsKey(distCode)) region.dist[countryCode][provinceCode][cityCode].Add(distCode, new regionbase() { code = distCode, name = itemds.name });
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. return region;
  107. }
  108. //取得EN版國省市區地理資料架構 ※EN版只取國
  109. public static regiondata GetRegionDataEn()
  110. {
  111. regiondata region = new regiondata();
  112. //國際
  113. var regionEn = new List<regionrow>();
  114. using (StreamReader r = new StreamReader("JsonFile/Region/region_en.json"))
  115. {
  116. string json = r.ReadToEnd();
  117. regionEn = JsonSerializer.Deserialize<List<regionrow>>(json);
  118. foreach (regionrow itemcy in regionEn)
  119. {
  120. //country
  121. string countryCode = itemcy.code;
  122. if (!region.country.ContainsKey(countryCode))
  123. {
  124. region.country.Add(countryCode, new regionbase() { code = countryCode, name = itemcy.name });
  125. }
  126. }
  127. }
  128. return region;
  129. }
  130. public class regiondata
  131. {
  132. public Dictionary<string, regionbase> country { get; set; } = new();
  133. public Dictionary<string, Dictionary<string, regionbase>> province { get; set; } = new();
  134. public Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>> city { get; set; } = new();
  135. public Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>> dist { get; set; } = new();
  136. }
  137. public class regionbase
  138. {
  139. public string code { get; set; } //代碼
  140. public string name { get; set; } //名稱
  141. }
  142. public class regionrow : regionbase
  143. {
  144. public List<object> children { get; set; }
  145. }
  146. //地區要去除的特殊字
  147. public static List<string> comeRemoveStr = new List<string>() { "省", "市", "区", "自治州", "县", "旗", "盟", "回族", "藏族", "羌族", "哈尼族", "彝族", "壮族", "苗族", "自治", "特别行政", "地區", "區", "縣" };
  148. //大陸直轄市的省ID
  149. public static List<string> municipalityId = new List<string>() { "11", "12", "31", "50", "81", "82" };
  150. }
  151. }