GeoRegion.cs 8.4 KB

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