将GeoJSON叠加层加载到Folium中会引发ValueError:无法渲染缺少任何几何形状的对象

时间:2019-05-07 16:54:38

标签: python geojson folium

我正在尝试将县边界地图(.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')

1 个答案:

答案 0 :(得分:0)

由于geoJSON函数期望使用GeoJSON对象而不是文件路径,因此发生错误:

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

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

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")