从经纬度坐标获取时区?

问题描述

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

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

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

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

我发现了一篇博文使用一段代码从地理坐标导出时区.

I found a blog post which uses a piece of code to derive timezones from geographical coordinates.

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

When I try to run the code, I get the error geonames is not defined. I have applied for an account with 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']


解决方案

这按预期工作:

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'

相关文章