首页 > 解决方案 > 将geojson 图层添加到folium 地图会产生空白输出(仅我使用的geojson 文件会导致此结果)

问题描述

我正在使用 Jupyter 笔记本创建一个folium 地图并尝试添加一个芝加哥社区边界的 geojson 文件。当我尝试其他 geojson 文件时,它们工作正常,但这个只产生空白输出。当我在不使用 geojson 文件的情况下尝试地图时,地图和标记显示正确。

我正在使用直接从芝加哥数据门户下载的 geojson 文件。https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Community-Areas-current-/cauq-8yn6

# Install and import libraries
import numpy as np # library to handle data in a vectorized manner

import pandas as pd # library for data analsysis
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

!pip install xlrd
import xlrd # library to read excel files

!pip install geopy
from geopy.geocoders import Nominatim # convert an address into latitude and longitude values

# Matplotlib and associated plotting modules
import matplotlib.cm as cm
import matplotlib.colors as colors
import matplotlib.pyplot as plt

!pip install folium
import folium # map rendering library

# Read the excel file of Chciago's life expectancy at birth
url = "https://citytech-health-atlas-data-prod.s3.amazonaws.com/uploads/uploader/path/601/Life_Expectancy.xlsx"
chicago_le_raw = pd.read_excel(url)
chicago_le_raw.head()

# Only keep 2016 data and only keep relevant columns
chicago_le_comm = chicago_le_raw[chicago_le_raw['Year']==2016]
chicago_le_comm = chicago_le_comm[['Geo_Group', 'Geo_ID', 'Number']]

# Store Chicago city life expectancy in a separate variable and drop the city rows
chicago_le = round(chicago_le_comm.iloc[0, 2], 1)
chicago_le_comm.drop(chicago_le_comm[chicago_le_comm['Geo_ID']==606].index, axis=0, inplace=True)

# Further clean the data
chicago_le_comm.set_index('Geo_ID', inplace=True) # set the index to community number
for i in chicago_le_comm['Geo_Group'].index:      # remove community number from names of communities
    chicago_le_comm['Geo_Group'][i] = chicago_le_comm['Geo_Group'][i].split('-')[1]
chicago_le_comm.rename(columns={'Geo_Group': 'Community',
                                'Number': 'Life Expectancy'}, inplace=True)
chicago_le_comm['Life Expectancy'] = round(chicago_le_comm['Life Expectancy'], 1)

print("The life expectancy at birth in 2016 for Chicago is", chicago_le)
chicago_le_comm

# Define empty variables
lat = []
lng = []

# Retrieve the coordinates
for comm in chicago_le_comm['Community']:
    geolocator = Nominatim(user_agent="ch_explorer")
    address = '{}, Chicago, IL'.format(comm)
    location = geolocator.geocode(address)
    lat.append(location.latitude)
    lng.append(location.longitude)

temp = pd.DataFrame({'Community': chicago_le_comm['Community'], 'lat': lat, 'lng': lng})

# Get GPS coordinates of Chicago
geolocator = Nominatim(user_agent="ch_explorer")
address = 'Chicago, IL'
location = geolocator.geocode(address)
lat_ch = location.latitude
lng_ch = location.longitude

# Download and store a geojson file of Chicago
!wget --quiet https://raw.githubusercontent.com/frozenkorean/Coursera_Capstone/master/chicago-communities.geojson -O CH.geojson
ch_geo = r'CH.geojson'

# Create a map of Chicago
map_chicago = folium.Map(location=[lat_ch, lng_ch], zoom_start=12)

for lat, lng, label in zip(temp['lat'], temp['lng'], chicago_le_comm['Community']):
    label = folium.Popup(label, parse_html=True)
    folium.CircleMarker([lat, lng],
                        radius=5,
                        popup=label,
                        color='blue',
                        fill=True,
                        fill_color='#3186cc',
                        fill_opacity=0.7,
                        parse_html=False).add_to(map_chicago)

folium.GeoJson(ch_geo, name='geojson').add_to(map_chicago)

map_chicago

我还想使用这些数据生成一个等值线图,但它也导致了一个空白输出。

# Choropleth map of life expectancy of Chicago communities
map_chicago = folium.Map(location=[lat_ch, lng_ch], zoom_start=12)

folium.Choropleth(geo_data = ch_geo,
                  data = chicago_le_comm,
                  columns = ['Community', 'Life Expectancy'],
                  key_on = 'feature.properties.community',
                  fill_color = 'YlOrRd',
                  fill_opacity = 0.7,
                  line_opacity = 0.2,
                  legend_name = 'Life Expectancy at Birth (2016)'
                  ).add_to(map_chicago)

map_chicago

我希望 geojson 文件在芝加哥地图上设置社区边界,但它只产生了一个空白。任何帮助将不胜感激。

标签: pythongeojsonfolium

解决方案


推荐阅读