Folium错误“地图”对象没有属性“ geo_json”

时间:2019-07-18 05:15:56

标签: python geojson folium

使用GeoJson时出现A​​ttributeError错误。我已经阅读了文档,但找不到任何提示。

import folium
from folium import GeoJson 


LA_COORDINATES = (34.0522, -118.2437)
zip_geo = r'zip-code-tabulation-areas-2012.geojson'

df_zip.to_json('ticket_agg.json')
df_zip.columns = ['Zipcode','Ticket_Count']

# creation of the choropleth
map1 = folium.Map(location=LA_COORDINATES, zoom_start=12)
map1.GeoJson(geo_path = zip_geo, 
              data_out = 'ticket_agg.json', 
              data = df_zip,
              columns = ['Zipcode','Ticket_Count'],
              key_on = 'feature.properties.external_id',
              fill_color = 'YlOrRd', 
              fill_opacity = 0.7, 
              line_opacity = 0.2,
              legend_name = 'Tickets per zip')

display(map1)

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-61-f15e83f4678b> in <module>
     19 # creation of the choropleth
     20 map1 = folium.Map(location=LA_COORDINATES, zoom_start=12)
---> 21 map1.GeoJson(geo_path = zip_geo, 
     22               data_out = 'ticket_agg.json',
     23               data = df_zip,

AttributeError: 'Map' object has no attribute 'GeoJson'

这是我正在使用的geo_json文件的链接:http://s3-us-west-2.amazonaws.com/boundaries.latimes.com/archive/1.0/boundary-set/zip-code-tabulation-areas-2012.geojson

任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

显然是错误

  

AttributeError:“地图”对象没有属性“ GeoJson”

发生,因为GeoJson不是Map类的方法。

我建议升级到最新版的,其中可以这样创建choropleth maps

folium.Choropleth(
    geo_path = zip_geo, 
    data = df_zip,
    columns = ['Zipcode','Ticket_Count'],
    key_on = 'feature.properties.external_id',
    fill_color = 'YlOrRd', 
    fill_opacity = 0.7, 
    line_opacity = 0.2,
    legend_name = 'Tickets per zip'
).add_to(map1)

列出更改项