我有一个包含2个字段http.createServer(onRequest).listen(config.port);
和store_name
的csv文件。一个城市中可以有多家商店。
我想要一个包含5个字段的输出csv,city
,store_name
,city
,address
,latitude
。
例如,如果csv的一项是longitude
,我希望输出csv包含5个字段(如上所述)中的所有信息,如下所示:
Starbucks, Chicago
,
Starbucks, Chicago, "200 S Michigan Ave, Chicago, IL 60604, USA", 41.8164613, -87.8127855
其余的结果等等。
在将其与Google Maps API配合使用之前,我试图通过GeoPy使用Nomanitim进行处理。虽然我不知道什么是解决此问题的最佳方法。请注意,源csv中有一百万个这样的条目,但是一旦可行,购买API密钥就不是问题。
我确实尝试仅使用熊猫对Nominatim进行地理编码,但这只会在每个条目的输出csv中创建一个结果。我想获取每个结果,如上面的示例中所述。不确定如何实现。
Starbucks, Chicago, "8 N Michigan Ave, Chicago, IL 60602, USA", 41.8164613, -87.8127855
答案 0 :(得分:1)
您可以将reverse geocoding
用于此目的。根据官方文档here,这是一种将地理坐标转换为人类可读地址的方法。
我在我的一个项目中使用了以下功能,但仍在工作。您可以根据需要进行修改。
import requests
GCODE_URL = 'https://maps.googleapis.com/maps/api/geocode/json?'
GCODE_KEY = 'YOUR API KEY'
def reverse_gcode(location):
location = str(location).replace(' ','+')
nav_req = 'address={}&key={}'.format(location,GCODE_KEY)
request = GCODE_URL + nav_req
result = requests.get(request)
data = result.json()
status = data['status']
geo_location = {}
if str(status) == "OK":
sizeofjson = len(data['results'][0]['address_components'])
for i in range(sizeofjson):
sizeoftype = len(data['results'][0]['address_components'][i]['types'])
if sizeoftype == 3:
geo_location[data['results'][0]['address_components'][i]['types'][2]] = data['results'][0]['address_components'][i]['long_name']
else:
if data['results'][0]['address_components'][i]['types'][0] == 'administrative_area_level_1':
geo_location['state'] = data['results'][0]['address_components'][i]['long_name']
elif data['results'][0]['address_components'][i]['types'][0] == 'administrative_area_level_2':
geo_location['city'] = data['results'][0]['address_components'][i]['long_name']
geo_location['town'] = geo_location['city']
else:
geo_location[data['results'][0]['address_components'][i]['types'][0] ]= data['results'][0]['address_components'][i]['long_name']
formatted_address = data['results'][0]['formatted_address']
geo_location['lat'] = data['results'][0]['geometry']['location']['lat']
geo_location['lang']= data['results'][0]['geometry']['location']['lng']
geo_location['formatted_address']=formatted_address
return geo_location
print(reverse_gcode("Starbucks, Chicago"))
输出将为json
格式,如下所示:
{'street_number': '8', 'town': 'Cook County', 'locality': 'Chicago', 'city': 'Cook County', 'lat': 41.882413, 'neighborhood': 'Chicago Loop', 'route': 'North Michigan Avenue', 'lang': -87.62468799999999, 'postal_code': '60602', 'country': 'United States', 'formatted_address': '8 N Michigan Ave, Chicago, IL 60602, USA', 'state': 'Illinois'}