2013-04-01 48 views
8

我試圖獲得緯度和經度座標的時區,但我有幾個問題 的失誤可能是非常基本的從Lat長座標獲取時區?

我有一個表中有大約600行的數據庫。每行包含世界某個地方的lat長座標 我想將這些座標饋入一個函數,然後檢索時區。目標是將具有600個地方中的每個地方的本地時間戳的事件轉換爲UTC時間

我找到了一個blog post,它使用a piece of code從地理座標導出時區。

當我嘗試運行代碼時,出現錯誤geonames is not defined。我已經申請了geonames賬戶。

我想我已經將函數文件保存在錯誤的目錄或簡單的東西中。誰能幫

#------------------------------------------------------------------------------- 
# Converts latitude longitude into a time zone 
# REF: https://gist.github.com/pamelafox/2288222 
# REF: http://blog.pamelafox.org/2012/04/converting-addresses-to-timezones-in.html 
#------------------------------------------------------------------------------- 

geonames_client = geonames.GeonamesClient('Username_alpha') 
geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928}) 
user.timezone = geonames_result['timezoneId'] 
+4

你似乎沒有*進口*'geonames'。添加一個'import geonames'行? –

+2

另請參閱[本社區wiki條目](http://stackoverflow.com/q/16086962/634824) –

+1

相關:[從Python/Django獲取城市時區](http://stackoverflow.com/q/16505501/ 4279),請參閱[我的答案中的'pytzwhere'示例](http://stackoverflow.com/a/16519004/4279)。 – jfs

回答

1

這按預期工作:

import geonames 
geonames_client = geonames.GeonamesClient('demo') 
geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928}) 
print geonames_result['timezoneId'] 

輸出:

'Europe/Paris' 
+0

@manuel_riel這是什麼庫...導入geonames? – sirvon

+1

只是一個包裝geonames.org的Python包。它在Pypi https://pypi.python.org/pypi/geonames/ –

+0

你鏈接的軟件包似乎沒有GeonamesClient類。你可能使用不同的包裝? – waterproof

2
import requests 
lat = 48.871236 ## your latitude 
lon = 2.77928 ## your longitude 

url = "http://api.geonames.org/timezoneJSON?formatted=true&lat={}&lng={}&username=demo".format(lat,lon) 

r = requests.get(url) ## Make a request 
return r.json()['timezoneId'] ## return the timezone 
+0

它很好用。然而,使用這個web服務有沒有什麼限制? – Sean

8

隨着tzwhere和pytz:

import datetime 
import pytz 
from tzwhere import tzwhere 

tzwhere = tzwhere.tzwhere() 
timezone_str = tzwhere.tzNameAt(37.3880961, -5.9823299) # Seville coordinates 
timezone_str 
#> Europe/Madrid 

timezone = pytz.timezone(timezone_str) 
dt = datetime.datetime.now() 
timezone.utcoffset(dt) 
#> datetime.timedelta(0, 7200) 
+1

它可以作爲我們需要的離線解決方案。 雖然有些可能會遇到錯誤: OSError:導入tzwhere時無法找到lib geos_c或加載其任何變體。 這有助於:http://stackoverflow.com/a/23057508/3699126 – sunsetjunks

+0

不適用於mac os x sierra和python 2.7 –

+0

任何其他細節?消息錯誤?你可以嘗試使用Python 3嗎? – tuxayo

0

我能夠做適合使用timezonefinder我的目的查找:

import datetime 
import timezonefinder, pytz 

tf = timezonefinder.TimezoneFinder() 

# Get the tz-database-style time zone name (e.g. 'America/Vancouver') or None 
timezone_str = tf.certain_timezone_at(lat=49.2827, lng=-123.1207) 

if timezone_str is None: 
    print "Could not determine the time zone" 
else: 
    # Display the current time in that time zone 
    timezone = pytz.timezone(timezone_str) 
    dt = datetime.datetime.utcnow() 
    print "The time in %s is %s" % (timezone_str, dt + timezone.utcoffset(dt)) 

還有的timezonefinder的方法及其對its pypi page限制的討論。

timezonefinderpytz可以在同名的pip包中找到。