Folium outputs ValueError: missing geometries, however the geojson data looks normal. What am I missing?
Folium outputs ValueError: missing geometries, however the geojson data looks normal. What am I missing?
我目前正在尝试绘制推文数据集,我通过 Twitter Streaming API 收集并通过 folium 在交互式地图上转换为 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'>
您必须先加载 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 可以更清楚 data
中的 file
表示文件路径,而不是打开的文件描述符。
我目前正在尝试绘制推文数据集,我通过 Twitter Streaming API 收集并通过 folium 在交互式地图上转换为 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'>
您必须先加载 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 可以更清楚 data
中的 file
表示文件路径,而不是打开的文件描述符。