123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using Microsoft.IdentityModel.Tokens;
- using System;
- using System.Collections.Generic;
- using System.Drawing.Drawing2D;
- using System.IdentityModel.Tokens.Jwt;
- using System.IO;
- using System.Text;
- using System.Text.Json;
- namespace TEAMModelOS.SDK.Extension
- {
- public static class GeoRegion
- {
- //取得國省市區地理資料架構
- public static regiondata GetRegionData(List<regionrow> region_gl, List<regionrow> region_cn)
- {
- regiondata region = new regiondata();
- //國際
- if(region_gl.Count > 0)
- {
- foreach (regionrow itemcy in region_gl)
- {
- //country
- string countryCode = itemcy.code;
- if (!region.country.ContainsKey(countryCode))
- {
- region.country.Add(countryCode, new regionbase() { code = countryCode, name = itemcy.name });
- }
- //province 無
- //city
- if (itemcy.children != null)
- {
- foreach (JsonElement itemcyChild in itemcy.children)
- {
- regionrow itemct = itemcyChild.ToObject<regionrow>();
- string provinceCode = "tw"; //台灣的省代碼用"tw"
- string cityCode = itemct.code;
- if (!region.city.ContainsKey(countryCode)) region.city.Add(countryCode, new Dictionary<string, Dictionary<string, regionbase>>());
- if (!region.city[countryCode].ContainsKey(provinceCode)) region.city[countryCode].Add(provinceCode, new Dictionary<string, regionbase>());
- if (!region.city[countryCode][provinceCode].ContainsKey(cityCode)) region.city[countryCode][provinceCode].Add(cityCode, new regionbase() { code = cityCode, name = itemct.name });
- //dist
- if (itemct.children != null)
- {
- foreach (JsonElement itemctChild in itemct.children)
- {
- regionrow itemds = itemctChild.ToObject<regionrow>();
- string distCode = itemds.code;
- if (!region.dist.ContainsKey(countryCode)) region.dist.Add(countryCode, new Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>());
- if (!region.dist[countryCode].ContainsKey(provinceCode)) region.dist[countryCode].Add(provinceCode, new Dictionary<string, Dictionary<string, regionbase>>());
- if (!region.dist[countryCode][provinceCode].ContainsKey(cityCode)) region.dist[countryCode][provinceCode].Add(cityCode, new Dictionary<string, regionbase>());
- if (!region.dist[countryCode][provinceCode][cityCode].ContainsKey(distCode)) region.dist[countryCode][provinceCode][cityCode].Add(distCode, new regionbase() { code = distCode, name = itemds.name });
- }
- }
- }
- }
- }
- }
- //大陸
- if (region_cn.Count > 0)
- {
- //country
- string countryCode = "CN";
- string countryName = "中国";
- if (!region.country.ContainsKey(countryCode))
- {
- region.country.Add(countryCode, new regionbase() { code = countryCode, name = countryName });
- }
- //province
- foreach (regionrow itempv in region_cn)
- {
- string provinceCode = itempv.code.Replace("0000", "");
- if (!region.province.ContainsKey(countryCode)) region.province.Add(countryCode, new Dictionary<string, regionbase>());
- if (!region.province[countryCode].ContainsKey(provinceCode)) region.province[countryCode].Add(provinceCode, new regionbase() { code = provinceCode, name = itempv.name });
- //city
- if (itempv.children != null)
- {
- foreach (JsonElement itempvChild in itempv.children)
- {
- regionrow itemct = itempvChild.ToObject<regionrow>();
- string cityCode = itemct.code;
- if (!region.city.ContainsKey(countryCode)) region.city.Add(countryCode, new Dictionary<string, Dictionary<string, regionbase>>());
- if (!region.city[countryCode].ContainsKey(provinceCode)) region.city[countryCode].Add(provinceCode, new Dictionary<string, regionbase>());
- if (!region.city[countryCode][provinceCode].ContainsKey(cityCode)) region.city[countryCode][provinceCode].Add(cityCode, new regionbase() { code = cityCode, name = itemct.name });
- //dist
- if (itemct.children != null)
- {
- foreach (JsonElement itemctChild in itemct.children)
- {
- regionrow itemds = itemctChild.ToObject<regionrow>();
- string distCode = itemds.code;
- if (!region.dist.ContainsKey(countryCode)) region.dist.Add(countryCode, new Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>());
- if (!region.dist[countryCode].ContainsKey(provinceCode)) region.dist[countryCode].Add(provinceCode, new Dictionary<string, Dictionary<string, regionbase>>());
- if (!region.dist[countryCode][provinceCode].ContainsKey(cityCode)) region.dist[countryCode][provinceCode].Add(cityCode, new Dictionary<string, regionbase>());
- if (!region.dist[countryCode][provinceCode][cityCode].ContainsKey(distCode)) region.dist[countryCode][provinceCode][cityCode].Add(distCode, new regionbase() { code = distCode, name = itemds.name });
- }
- }
- }
- }
- }
- }
- return region;
- }
- //取得EN版國省市區地理資料架構 ※EN版只取國
- public static regiondata GetRegionDataEn()
- {
- regiondata region = new regiondata();
- //國際
- var regionEn = new List<regionrow>();
- using (StreamReader r = new StreamReader("JsonFile/Region/region_en.json"))
- {
- string json = r.ReadToEnd();
- regionEn = JsonSerializer.Deserialize<List<regionrow>>(json);
- foreach (regionrow itemcy in regionEn)
- {
- //country
- string countryCode = itemcy.code;
- if (!region.country.ContainsKey(countryCode))
- {
- region.country.Add(countryCode, new regionbase() { code = countryCode, name = itemcy.name });
- }
- }
- }
- return region;
- }
- public class regiondata
- {
- public Dictionary<string, regionbase> country { get; set; } = new();
- public Dictionary<string, Dictionary<string, regionbase>> province { get; set; } = new();
- public Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>> city { get; set; } = new();
- public Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>> dist { get; set; } = new();
- }
- public class regionbase
- {
- public string code { get; set; } //代碼
- public string name { get; set; } //名稱
- }
- public class regionrow : regionbase
- {
- public List<object> children { get; set; }
- }
- //地區要去除的特殊字
- public static List<string> comeRemoveStr = new List<string>() { "省", "市", "区", "自治州", "县", "旗", "盟", "回族", "藏族", "羌族", "哈尼族", "彝族", "壮族", "苗族", "自治", "特别行政", "地區", "區", "縣" };
- //大陸直轄市的省ID
- public static List<string> municipalityId = new List<string>() { "11", "12", "31", "50", "81", "82" };
- }
- }
|