我正在尝试使用Python的Folium包从URL读取JSON文件,但我不断收到错误-'Map' object has no attribute 'geo_json'
或'list' object has no attribute 'keys'
。
这是我使用Folium的代码的两个版本-
版本1:
from IPython.display import HTML
import folium
def embed_map(map, path="map.html"):
map.create_map(path=path)
return HTML('<iframe src="files/{path}" style="width: 100%; height: 510px;
border:
none"></iframe>'.format(path=path))
geojson_url_iw='http://kodapan.se/geodata/data/2015-06-26/kommuner.geo.json'
iw_map = folium.Map(location=[60.1282, 18.6435], zoom_start=5)
iw_map.geo_json(geo_path= geojson_url_iw)
embed_map(iw_map,'iw_map.html')
版本2:
import folium
def main():
sweden_location = [ 60.1282, 18.6435 ]
m = folium.Map(location=sweden_location, zoom_start=5)
geojson = r'kommuner.geo.json'
m.choropleth(geo_data=geojson)
m.save(outfile="map.html")
if __name__ == "__main__":
main()
我也尝试过使用json.load解析文件,但是没有用。
编辑:这是JSON文件的代码段。
[
{
"kod": "1440",
"postnummer": "44980",
"telefon": "0303-330000",
"epost": "kommun@ale.se",
"namn": "Ale kommun",
"geometry": {
"coordinates": [
[
[12.37511384238, 58.11508613311],
[12.39092963608, 58.10816346254],
[12.39635219392, 58.11532482191],
[12.42572438223, 58.10768598743],
[12.43476197863, 58.09383642732],
[12.42120558403, 58.05488558262],
[12.42301310331, 58.03216456268],
[12.41081234817, 58.02474720918],
[12.41849430511, 58.01541351467],
[12.41578302619, 58.00703504864],
[12.40177475177, 57.9893141188],
[12.37421008274, 57.9936254225],
[12.35251985138, 57.97925239172],
[12.31004314829, 57.9658323592],
[12.29016043621, 57.92314244749],
[12.30462059045, 57.90586018691],
[12.30010179225, 57.89745608439],
[12.13712088475, 57.86669593588],
[12.03883702389, 57.83724092156],
[12.04335582209, 57.82436933451],
[12.03318852614, 57.82797865449],
[12.01488739343, 57.81414096507],
[12.01149829477, 57.8455388528],
[12.01043839879, 57.85719462811],
[12.00946483558, 57.86789766962],
[12.04403364182, 57.91617414261],
[12.06753139246, 57.94280650831],
[12.08741410454, 57.95491649705],
[12.12107915114, 57.97109673256],
[12.13056862736, 58.01013890686],
[12.13452604959, 58.01383335415],
[12.16360303253, 58.00790882552],
[12.1706326328, 58.01654138344],
[12.22079841655, 58.02280291623],
[12.23166234424, 58.02534106327],
[12.24284579922, 58.0331235912],
[12.23613572623, 58.03853652482],
[12.24220674465, 58.04259568725],
[12.27895238243, 58.06136331836],
[12.33550871188, 58.08383768027],
[12.35020696699, 58.10697317331],
[12.37511384238, 58.11508613311]
]
],
"type": "Polygon"
},
"url": "http://www.ale.se/",
"postort": "ALAFORS"
},
这是URL- http://kodapan.se/geodata/data/2015-06-26/kommuner.geo.json
答案 0 :(得分:0)
除非您需要直接使用folium,否则我建议加载预安装的json包:
import json
data = your_file.json()
希望这会有所帮助!
编辑:
如果您的Json存储为字符串,则可以执行以下操作:
data = json.loads(your_file)
编辑2:
感谢您提供字符串。
问题似乎是该字符串在其周围有方括号。您可以评估将其作为列表的字符串,然后获取第一个(也是唯一一个)项目。该文件将是一个字典,大概可以满足您的目的。您应该能够解决如下问题:
new_dict = eval(your_file)[0]