NOAA如何将City,St转换为长途?

时间:2013-04-10 19:46:13

标签: python noaa

NOAA REST服务提供了获取天气信息的方法。我注意到NOAA网站上有一个文本框,我可以输入City,St来获取该位置的天气信息。

使用REST服务,它仅列出有限的City,St及其对应的Lat,Lng:

http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?listCitiesLevel=1234

但是,如果我想查看MCRAE, GA的Lat,Lng,则它不会在REST服务中列为City,St。

我的目标是使用任何City,St作为python程序的输入,程序将检索该位置的天气数据。这最终涉及计算City,St坐标,然后使用坐标构建url。我希望REST服务能给我坐标,但似乎不可能。

我的问题是NOAA如何找出适用于任何特定城市的正确坐标,St?是否有可以轻松返回数据的REST服务?我宁愿不必模拟用户输入,然后屏幕抓取坐标。

1 个答案:

答案 0 :(得分:3)

使用openstreetmap.org

import urllib2
import urllib
import json

url = 'http://nominatim.openstreetmap.org/search?'
place = 'MCRAE, GA'
params = urllib.urlencode(dict(q=place, format='json'))
# print(url+params)
response = urllib.urlopen(url+params)
data = json.loads(response.read())[0]
print(data['lon'], data['lat'])

产量

(u'-82.9006993', u'32.0679541')
相关问题