gpt4 book ai didi

python - 将 GeoJSON 叠加层加载到 Folium 会引发 ValueError : Cannot render objects with any missing geometries

转载 作者:行者123 更新时间:2023-12-04 17:37:43 38 4
gpt4 key购买 nike

我正在尝试将县边界 map (.geojson 文件)加载到 Folium 中并出现 ValueError。

GeoJSON 文件是从这里提取的: https://data.mo.gov/Demographics/Missouri-County-Boundaries-Map/n34b-fwqr

看起来像:

{
"type": "FeatureCollection",
"features": [
{"type":"Feature","properties":{"co_class":"3","acres":"327632.419997","pop_1990":"7547","cnty_seat":"Kahoka","countyname":"Clark","name_ucase":"CLARK","countygnis":"758477","countyfips":"045","pop_2000":"7416","sq_miles":"511.92565625"},"geometry":{"type":"MultiPolygon","coordinates":[[[[-91.72916190319887,40.61370307324286],[-91.7211904460685,40.60303954172671],[-91.7200583614958,40.60152712696914],

...

我已经使用 GeoPandas 验证了该文件,它似乎可以在那里正确加载和显示。我也尝试加载 .json 版本,但收到相同的结果。

import folium as f
import pathlib as pl

MO_LAT = 37.9643
MO_LONG = -91.8318
county_gjson = pl.Path.cwd() /"data" / "Missouri County Boundaries Map.geojson"
m = f.Map(
location=[MO_LAT, MO_LONG],
tiles='Mapbox Bright',
zoom_start=7
)

f.GeoJson(
county_gjson,
name='geojson'
).add_to(m)
f.LayerControl().add_to(m)
m.save("mymap.html")

我希望覆盖密苏里县的边界。当我运行代码时,我得到:

runfile('C:/Users/Jason/Documents/Missouri/mo_counties.py', wdir='C:/Users/Jason/Documents/Missouri')
Traceback (most recent call last):

File "<ipython-input-28-bdbf561151e1>", line 1, in <module>
runfile('C:/Users/Jason/Documents/Missouri/mo_counties.py', wdir='C:/Users/Jason/Documents/Missouri')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Jason/Documents/Missouri/mo_counties.py", line 23, in <module>
name='geojson'

File "C:\ProgramData\Anaconda3\lib\site-packages\folium\features.py", line 447, in __init__
raise ValueError('Cannot render objects with any missing geometries. {!r}'.format(data))

ValueError: Cannot render objects with any missing geometries. WindowsPath('C:/Users/Jason/Documents/Missouri/data/Missouri County Boundaries Map.geojson')

最佳答案

错误发生是因为 geoJSON 函数需要 GeoJSON 对象而不是文件路径:

f.GeoJson(
county_gjson,
^^^^^^^^^^^^
expects a valid GeoJSON object
).add_to(m)

从文件中读取 GeoJSON json.load() method可以利用:

county_path = os.path.join(pl.Path.cwd(),'data', 'Missouri County Boundaries Map.geojson') 
county_geojson = json.load(open(county_path))

修改示例

import folium as f
import json
import os

MO_LAT = 37.9643
MO_LONG = -91.8318
county_path = os.path.join(os.getcwd(),'data', 'Missouri County Boundaries Map.geojson')
county_geojson = json.load(open(county_path))

m= f.Map(
location=[MO_LAT, MO_LONG],
tiles='Mapbox Bright',
zoom_start=7
)

f.GeoJson(
county_geojson,
name='geojson'
).add_to(m)
f.LayerControl().add_to(m)
m.save("./mymap.html")

关于python - 将 GeoJSON 叠加层加载到 Folium 会引发 ValueError : Cannot render objects with any missing geometries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56027271/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com