【问题标题】:Getting Time Zone from Lat Long Coordinates? [duplicate]从经纬度坐标获取时区? [复制]
【发布时间】:2020-06-08 20:41:39
【问题描述】:

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

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

我找到了一个blog post,它使用a piece of code 从地理坐标中获取时区。

当我尝试运行代码时,我收到错误 geonames is not defined。我已经申请了一个带有地名的帐户。

我想我只是将函数文件保存在错误的目录中或一些简单的东西。谁能帮忙

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

【问题讨论】:

标签: python timezone latitude-longitude geonames


【解决方案1】:

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

【讨论】:

  • 它是我们需要的离线解决方案。虽然有些可能会遇到错误:OSError: Could not find lib geos_c or load any its variant when importing tzwhere。这有帮助:stackoverflow.com/a/23057508/3699126
  • 在 mac os x sierra 和 python 2.7 中不起作用
  • 还有其他详细信息吗?消息错误?你可以试试 Python 3 吗?
  • 没有错误,调用tzNameAt方法返回None
  • 这在 High Sierra 和 Python 2.7 中对我有用
【解决方案2】:

我能够使用timezonefinder 进行适合我目的的查找:

import datetime
import timezonefinder, pytz

tf = timezonefinder.TimezoneFinder()

# From the lat/long, 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))

its pypi page 链接的文档中讨论了 timezonefinder 的方法及其限制。

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

【讨论】:

  • 我还没有对多个输入坐标进行全面测试,所以我不能说准确度,但是速度方面的 timezonefinder 比 tzwhere 快很多。至少对我来说。
【解决方案3】:

这按预期工作:

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'

【讨论】:

  • @manuel_riel 这是什么库...导入地名?
  • 只是一个包装 geonames.org 的 Python 包。它在 Pypi pypi.python.org/pypi/geonames
  • 您链接的包似乎没有 GeonamesClient 类。您是否使用了不同的包?
  • 这个答案不再有效。
  • 提到的库在 Linux 下不工作。该代码仅支持 python2,并且有硬编码的 windows 路径。
【解决方案4】:
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

【讨论】:

  • 效果很好。但是,使用此 Web 服务是否有任何命中限制?
  • 这是我收到的{'status': {'message': 'the daily limit of 20000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.', 'value': 18}} :)
  • 对于我来说使用 python 3.8 时,错误是 KeyError: 'timezoneId'
猜你喜欢
  • 1970-01-01
  • 2020-05-04
  • 2021-12-09
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
相关资源
最近更新 更多