目标
- 将多个国家的省份地区合并到一张地图上展示
- 将没有在 ISO 3166-2 codes 区域标准中的区域块加入到地图中
实现方案
- 对应第一个目标,我们可以将多个国家的省份地区合并到一个geojson文件中去,主要调整其
features
key的值,该key的值类型字典数组.
核心代码实现:
from pathlib import Path
import geojson
from shapely.geometry import shape, GeometryCollection
def read_geojson(file_path):
if isinstance(file_path, Path):
file_path = str(file_path.absolute())
with open(file_path) as f:
return geojson.load(f)
def write_geojson(geojson_obj, file_path):
with open(file_path, 'w') as f:
geojson.dump(geojson_obj, f)
# 定义文件基础路径
base_path = Path('superset/superset-frontend/plugins/legacy-plugin-chart-country-map/src/countries')
# 获取基础内容
base_data = read_geojson(base_path.joinpath('russia.geojson'))
# 循环合并内容
file_names = ['azerbaijan', 'armenia']
for file_name in file_names:
geojson_obj = read_geojson(Path(base_path.joinpath(f'{file_name}.geojson')))
data_list = geojson_obj['features']
base_data['features'].extend(data_list)
# 更新最新文件
write_geojson(base_data, base_path.joinpath('russia_new.geojson'))
- 对应第二个目标因为我们没有地区的原始geojson配置,通过在img2geojson上手动绘制大体的区域图形,然后下载数据到 data.geojson,然后在手动修改原始的 geojson数据,使其能够识别,
然后还要手动修改 russia_new.geojson
,feature
key值中增加类似一下内容,以下是示例,注意要自己分配一个ISO
及NMAE_1
{ "type": "Feature", "properties": { "ISO": "TEST_A", "NAME_1": "测试的地方-South Ossetia" },"geometry":{"coordinates":[[43.692872059681775,42.64792851554839],[43.57774378841228,42.582526870254355],[43.62823864423214,42.16173176396933],[43.721149178941374,42.14376306902662],[43.79386177132275,42.12878525990388],[43.9211088079897,42.18568208833088],[44.09279131777842,42.22458202068904],[44.26851341603154,42.052343337980005],[44.47857201624382,42.052343337980005],[44.61793781830835,42.23355552660672],[44.59572008174737,42.37994218509803],[44.35536456804326,42.49769990089118],[44.23013732560855,42.612263405457895],[43.96958386957641,42.55872742544025],[43.74942629819853,42.62118159884136],[43.71508979624079,42.653870713681755],[43.690852265447035,42.652385217370295],[45.615716169314084,42.62415404614447]],"type":"LineString"}}
备注:注意容器启动的superset要更新其内部的 geojson文件,由于其内部的geojson文件名称被编译,建议通过关键省份搜索处文件名,示例命令
find . -name "*.geojson" | xargs grep -H "Beijing"
实现效果如下
- 合并地区及增加自定义地区:
评论3
hokwsokgar
想想你的文章写的特别好https://www.jiwenlaw.com/
hwxybbbatt
怎么收藏这篇文章?
ighhuipvij
不错不错,我喜欢看 https://www.237fa.com/