A Trask
A Trask

Reputation: 13

Using latitude and longitude to generate timezone

I have about 9 million records with latitude and longitude, in addition to the timestamp in EST. I want to use the latitude and longitude to generate the appropriate regional timezone, from which I then want to adjust all these times for the relevant timezone.

I have tried using geonames

data_raw$tz <- mapply(GNtimezone, data$lat,data$lon)

However, this returns the following error:

Error in getJson("timezoneJSON", list(lat = lat, lng = lng, radius = 0)) : 
error code 13 from server: ERROR: canceling statement due to statement timeout

I have tried to use a method described in this post.

data$tz_url <- sprintf("https://maps.googleapis.com/maps/api/timezone/%s?location=%s,%s&timestamp=%d&sensor=%s", 
              "xml", 
              data$lat, 
              data$lon, 
              as.numeric(data$time), 
              "false")

for(i in 1:100){
  data$tz[i] <- xmlParse(readLines(data$tz_url[i]), isURL=TRUE)[["string(//time_zone_name)"]]
}  

With this method, I am able to get the urls for the XML data. But when I try to pull the XML data in a for loop, and append the timezone to the dataframe, it doesn't do it for all the records... (in fact, only 10 records at a time intermittently).

Does anyone know of any alternate methods or packages to get the three character timezone (i.e. EST) for about 9 million records relatively quickly? Your help is much appreciated. Or better yet, if you have ideas on why the code I used above isn't working, I'd appreciate that too.

Upvotes: 0

Views: 1500

Answers (2)

SymbolixAU
SymbolixAU

Reputation: 26248

I've written the package googleway to access google maps API. You'll need a valid API key (and, for Google to handle 9 million calls you'll have to pay for it as their free one only covers 2500)

library(googleway)

key <- "your_api_key"

google_timezone(location = c(-37, 144),
                key = key)


$dstOffset
[1] 0

$rawOffset
[1] 36000

$status
[1] "OK"

$timeZoneId
[1] "Australia/Hobart"

$timeZoneName
[1] "Australian Eastern Standard Time"

Upvotes: 1

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241450

For a list of methods of converting latitude and longitude to time zone, see this post. These mechanisms will return the IANA/Olson time zone identifier, such as America/Los_Angeles.

However, you certainly don't want to make 9 million individual HTTP calls. You should attempt to group the records to distinct locations to minimize the number of lookups. If they are truly random, then you will still have a large number of locations, so you should consider the offline mechanisms described in the previous post (i.e. using the tz_world shapefile with some sort of geospatial lookup mechanism).

Once you have the IANA/Olson time zone identifier for the location, you can then use R's time zone functionality (as.POSIXct, format, etc.) with each of corresponding timestamp to obtain the abbreviation.

However, do recognize that time zone abbreviations themselves can be somewhat ambiguous. They are useful for human readability, but not much else.

Upvotes: 2

Related Questions