2

Let's say I have a data set with the date, latitude, and longitude.

dt = data.table(date = c("2017-10-24 05:01:05",
                         "2017-10-24 05:01:57", 
                         "2017-10-24 05:02:54"),
                lat = c(-6.2704925537109375,
                        -6.2704925537109375,
                        -6.2704925537109375),
                long = c(106.5803680419922, 
                         106.5803680419922,
                         106.5803680419922))

The time is UTC. Is it possible to transfer that UTC to the local time using the lat and long?

Florian
  • 24,425
  • 4
  • 49
  • 80
ATMA
  • 1,450
  • 4
  • 23
  • 33

1 Answers1

5

I found a good answer on converting longitude and latitude to timezones here, so here is how we can apply that method to your use case.

library(data.table)
library(googleway)
library(lubridate)
dt = data.table(date = c("2017-10-24 05:01:05",
                         "2017-10-24 05:01:57", 
                         "2017-10-24 05:02:54"),
                lat = c(-6.2704925537109375,
                        -6.2704925537109375,
                        -6.2704925537109375),
                long = c(106.5803680419922, 
                         106.5803680419922,
                         106.5803680419922))

dt$date <- with_tz(as.POSIXct(dt$date), "UTC")

convert_time <- function(lat,lon,time_x)
{
  my_time <- google_timezone(c(lat, lon), key = NULL)
  as.POSIXct(format(time_x, tz=my_time$timeZoneId)) 
}

dt[,time_conv:=convert_time(lat,long,date),by=1:nrow(dt)]

Output:

                  date       lat     long           time_conv
1: 2017-10-24 03:01:05 -6.270493 106.5804 2017-10-24 10:01:05
2: 2017-10-24 03:01:57 -6.270493 106.5804 2017-10-24 10:01:57
3: 2017-10-24 03:02:54 -6.270493 106.5804 2017-10-24 10:02:54

Hope this helps!


Important notes:

  • See SymbolixAU's comment below. Although technically you do not need one, as per Google's requirements you should request an API key and use that in the google_timezone() function above.
  • The Google API has certain usage limits. If you plan to do this on a large dataframe, consider subsetting only unique longitude and latitude pairs and possibly even rounding them to x digits. You can then apply google_timezone() on these unique combinations, and later merge them back to the original dataframe.

Florian
  • 24,425
  • 4
  • 49
  • 80
  • 1
    Nice solution. This [SO question](https://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location-using-latitude-and-longitude-coordinates) might also be usefull – Gilles San Martin Jan 23 '18 at 21:14
  • Wow, that is an extensive answer. Thanks for sharing. – Florian Jan 24 '18 at 06:36
  • 1
    While it's great to see `googleway` being used, bear in mind that as per [Google's requirements](https://developers.google.com/maps/documentation/timezone/intro) you **should** use an API key (even though technically you don't need one). – SymbolixAU Jan 28 '18 at 20:46
  • 1
    @SymbolixAU thanks for the comment (and an awesome package), I have added that to the answer. – Florian Jan 28 '18 at 21:09