Python 从经纬度坐标获取时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15742045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:51:46  来源:igfitidea点击:

Getting Time Zone from Lat Long Coordinates?

pythontimezonelatitude-longitudegeonames

提问by John Smith

I am trying to get the time zones for latitude and longitude coordinates but am having a few problems The mistakes are probably very basic

我正在尝试获取经纬度坐标的时区,但遇到了一些问题错误可能非常基本

I have a table in a database with around 600 rows. Each row contains a lat long coordinate for somewhere in the world I want to feed these co-ordinates into a function and then retrieve the time zone. The aim being to convert events which have a local time stamp within each of these 600 places into UTC time

我在数据库中有一个表,大约有 600 行。每一行都包含世界上某个地方的经纬度坐标,我想将这些坐标输入一个函数,然后检索时区。目的是将在这 600 个地方中的每个地方都具有本地时间戳的事件转换为 UTC 时间

I found a blog postwhich uses a piece of codeto derive timezones from geographical coordinates.

我找到了一篇博客文章,它使用一段代码从地理坐标中导出时区。

When I try to run the code, I get the error geonames is not defined. I have applied for an account with geonames.

当我尝试运行代码时,出现错误geonames is not defined。我已经申请了 geonames 的帐户。

I think I have just saved the function file in the wrong directory or something simple. Can anyone help

我想我刚刚将函数文件保存在错误的目录或简单的目录中。谁能帮忙

#-------------------------------------------------------------------------------
# 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']

采纳答案by Manuel Riel

This works as expected:

这按预期工作:

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

Output:

输出:

'Europe/Paris'

回答by Bilal

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

回答by tuxayo

With tzwhere and pytz:

使用 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)

回答by rakslice

I was able to do a lookup suitable for my purposes using timezonefinder:

我能够使用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))

There's a discussion of the methods of timezonefinder and its limitations on its pypi page.

其 pypi 页面上讨论了 timezonefinder 的方法及其限制。

timezonefinderand pytzcan be found in the pippackages of the same name.

timezonefinder并且pytz可以pip在同名的包中找到。