我目前正在尝试在地图上通过草叶绘制推文的数据集,这些数据是我通过Twitter Streaming API收集的,并转换为geojson文件。但是,当尝试使用下面的代码时,我收到以下错误消息。
当然,我检查了我的数据集,但是看起来不错(请参见下文),并且在geojson.io上加载时没有任何问题。我在这里跟叶有问题吗?因为据我所知,数据(及其中的几何形状)很好。任何帮助将不胜感激!
以下是我的数据示例:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.4, 52.53]
},
"properties": {
"text": "Busy 2 week ahead! This Saturday spinning all night in the main room of humboldthain club -#berlin - then traveling\u2026",
"created_at": "Mon Apr 15 12:57:26 +0000 2019"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.37, 52.50]
},
"properties": {
"text": "OFFICE today.\n.\n.\n#berlin #berlinerphilharmonie #sanofi #eenewolke @ Sanofi Berlin",
"created_at": "Mon Apr 15 12:59:23 +0000 2019"
}
}
]
}
这是代码:
from argparse import ArgumentParser
import folium
def get_parser():
parser = ArgumentParser()
parser.add_argument('--geojson')
parser.add_argument('--map')
return parser
def make_map(geojson_file, map_file):
tweet_map = folium.Map(location=[50, 5],
zoom_start=5)
geojson_layer = folium.GeoJson(open(geojson_file),
name='geojson')
geojson_layer.add_to(tweet_map)
tweet_map.save(map_file)
if __name__ == '__main__':
parser = get_parser()
args = parser.parse_args()
make_map(args.geojson, args.map)
这是错误:
"Traceback (most recent call last):
File "twitter_map_basic.py", line 21, in <module>
make_map(args.geojson, args.map)
File "twitter_map_basic.py", line 13, in make_map
name = 'geojson'>
File "C:\Users\user\AppData\Local\Programs\Python\Python37\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. <_io.TextIOWrapper name='staedteKlein.geojson' mode='r' encoding='cp1252'>
答案 0 :(得分:0)
您必须先加载geojson_file
,然后再将其传递到folium.GeoJson
:
import json
geojson_layer = folium.GeoJson(json.load(open(geojson_file)),
name='geojson')
或仅传递文件名:
geojson_layer = folium.GeoJson(geojson_file,
name='geojson')
documentation可能更清楚,file
中的data
表示文件路径,而不是打开的文件描述符。