簡體   English   中英

如何使用 GeoJSON 與英國地方當局一起繪制 Plotly Choropleth 地圖

[英]How to plot a Plotly Choropleth map with English local authorities using GeoJSON

我從此處的 Plotly 指南中復制了示例。 我能夠復制他們的美國縣地圖。

現在我正在嘗試制作一個類似的 Choropleth 地圖,除了使用英語地方當局。 我已經從這里下載了適用於英國較低層當局的 GeoJSON。 . 這是一個巨大的分辨率並且加載速度很慢,所以我將它壓縮為較低分辨率的 JSON。 我的 JSON 在這里: https ://github.com/thomasvalentine/Choropleth/blob/main/Local_Authority_Districts_(December_2021)_GB_BFC.json

這個 JSON 沒有 id 屬性,指導說這對於將 shapefile 與數據框鏈接起來很重要,所以我遍歷了 JSON 並插入了與我的數據框相對應的 id。 JSON 中一個條目的結構現在如下所示:

print(Local_authorities['features'][0])

{'type': 'Feature',
 'geometry': {'type': 'Polygon',
  'coordinates': [[[-1.24099449299996, 54.723193914000035],
    [-1.270640929999956, 54.72702718800008],
    [-1.319495009999969, 54.691288599000075],
    [-1.341375058999972, 54.65018898900007],
    [-1.380898315999957, 54.643917068000064],
    [-1.250706313999956, 54.62531901600005],
    [-1.22526241099996, 54.62567539600008],
    [-1.22504188299996, 54.62590810800003],
    [-1.173027954999952, 54.63341869200008],
    [-1.198195061999968, 54.69120971400008],
    [-1.24099449299996, 54.723193914000035]]]},
 'properties': {'OBJECTID': 1,
  'LAD21CD': 'E06000001',
  'LAD21NM': 'Hartlepool',
  'LAD21NMW': ' ',
  'BNG_E': 447160,
  'BNG_N': 531474,
  'LONG': -1.27018,
  'LAT': 54.67614,
  'GlobalID': '{CB7275CE-D16E-45F7-8E7D-33032FB9DF9D}',
  'SHAPE_Length': 0.8998598929545726,
  'SHAPE_Area': 0.013057380459647069},
 'id': 'Hartlepool'}

據我所知,這似乎與情節指導中的美國縣示例的結構相同。 我的虛擬數據如下所示:

    LA                   Val
0   Hartlepool           0
1   Middlesbrough        1
2   Redcar and Cleveland 2
3   Stockton-on-Tees     3
4   Darlington           4

我從情節指導中復制了代碼並修改了一些部分:

from urllib.request import urlopen
import json
# load GeoJSON file
with urlopen('file:///Users/thomasvalentine/Downloads/Local_Authority_Districts_(December_2021)_GB_BFC.json') as response:
    Local_authorities = json.load(response)


la_data = []
# Iterative over JSON
for i in range(len(Local_authorities["features"])):
    # Extract local authority name
    la = Local_authorities["features"][i]['properties']['LAD21NM']
    # Assign the local authority name to a new 'id' property for later linking to dataframe
    Local_authorities["features"][i]['id'] = la
    # While I'm at it, append local authority name to a list to make some dummy data to test, along with i for a value to test on map
    la_data.append([la,i])



import pandas as pd

# turn dummy data into a dataframe
df = pd.DataFrame(la_data)
# update column names
df.columns = ['LA','Val']



import plotly.express as px
# make choropleth
fig = px.choropleth(df, geojson=Local_authorities, locations='LA', color='Val',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           scope="europe",
                           labels={'val':'value'}
                          )

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

代碼運行沒有錯誤,但是當它在瀏覽器中打開時,它只是顯示一個隨機的大黃色形狀:在此處輸入圖像描述

我部署了choroplethmap_box而不是choropleth並且它起作用了。 請參考以下代碼:

from urllib.request import urlopen
import json


with urlopen('https://raw.githubusercontent.com/thomasvalentine/Choropleth/main/Local_Authority_Districts_(December_2021)_GB_BFC.json') as response:
    Local_authorities = json.load(response)


la_data = []
# Iterative over JSON
for i in range(len(Local_authorities["features"])):
    # Extract local authority name
    la = Local_authorities["features"][i]['properties']['LAD21NM']
    # Assign the local authority name to a new 'id' property for later linking to dataframe
    Local_authorities["features"][i]['id'] = la
    # While I'm at it, append local authority name to a list to make some dummy data to test, along with i for a value to test on map
    la_data.append([la,i])



import pandas as pd

# turn dummy data into a dataframe
df = pd.DataFrame(la_data)
# update column names
df.columns = ['LA','Val']

fig = px.choropleth_mapbox(df,
                           geojson=Local_authorities,
                           locations='LA',
                           color='Val',
                           featureidkey="properties.LAD21NM",
                           color_continuous_scale="Viridis",
                           mapbox_style="carto-positron",
                           center={"lat": 55.09621, "lon": -4.0286298},
                           zoom=4.2,
                           labels={'val':'value'})

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

這是輸出: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM