Getting Time Zone from Lat Long Coordinates? [duplicate]

梦想的初衷 提交于 2019-12-20 11:55:16

问题


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

I found a blog post which uses a piece of code to 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.

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

回答1:


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'



回答2:


With tzwhere and 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)



回答3:


I was able to do a lookup suitable for my purposes using 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.

timezonefinder and pytz can be found in the pip packages of the same name.




回答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


来源:https://stackoverflow.com/questions/15742045/getting-time-zone-from-lat-long-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!