LiL*_*Tam 0 python json google-maps pandas
我正在做一个有关网络爬虫的项目。
我将获得一些点位置,包括(名称,经度,纬度),
然后将它们全部指向Google Map(使用Google Map API)
首先,
我从网站API获得了一些数据。-(300points locaiton)
然后使用Python pandas DataFrame导出Json文件
df = pd.DataFrame(total, columns=['name','lat', 'lng'])
df['lat'] = pd.to_numeric(df['lat'])
df['lng'] = pd.to_numeric(df['lng'])
jsonFile = df.to_json(orient='records')
with open('map.json','w') as f:
f.write(jsonFile)
Run Code Online (Sandbox Code Playgroud)
json结构看起来像这样
{
"name":"????",
"lat":24.13604,
"lng":120.6778
}
Run Code Online (Sandbox Code Playgroud)
所以这是问题所在:
如何使用Google Map API导入指向Google Map的指针?
我试图阅读Google Map API Document,它只是告诉我使用
map.data.loadGeoJson('map.json');
Run Code Online (Sandbox Code Playgroud)
终于我错了。和错误消息
uncaught exception: InvalidValueError: not a Feature or FeatureCollection
Run Code Online (Sandbox Code Playgroud)
Google Map API仅支持GeoJson吗?
如何将json文件更改为GeoJson?
或如何修改可在Google Map API中使用的json文件。
非常感谢你。
如何将json文件更改为GeoJson?
您可以使用geojson
这是一个例子:
进口:
import json
import pandas as pd
from geojson import Feature, FeatureCollection, Point
Run Code Online (Sandbox Code Playgroud)
数据框:
df = pd.DataFrame([{'name': '????', 'lat': 24.13604, 'lng': 120.6778}])
Run Code Online (Sandbox Code Playgroud)
转换为geojson:
# columns used for constructing geojson object
features = df.apply(
lambda row: Feature(geometry=Point((float(row['lng']), float(row['lat'])))),
axis=1).tolist()
# all the other columns used as properties
properties = df.drop(['lat', 'lng'], axis=1).to_dict('records')
# whole geojson object
feature_collection = FeatureCollection(features=features, properties=properties)
Run Code Online (Sandbox Code Playgroud)
然后,您可以保存geojson文件:
with open('/path/to/your/file.geojson', 'w', encoding='utf-8') as f:
json.dump(feature_collection, f, ensure_ascii=False)
Run Code Online (Sandbox Code Playgroud)