从经纬度和时间计算时区
Calculating timezone from lat long and time
在这个 df 中,我有纬度、经度和日期时间,我想计算该特定位置在该特定时间的时区。
> dput(df)
structure(list(lat = structure(c(1L, 7L, 4L, 2L, 3L, 10L, 9L,
6L, 5L, 8L), .Label = c("18<f8> 26' 01.91\" N", "18<f8> 29' 09.80\" N",
"19<f8> 24' 36.35\" N", "19<f8> 40' 19.15\" N", "27<f8> 57' 02.07\" N",
"32<f8> 22' 45.20\" N", "32<f8> 42' 56.65\" N", "33<f8> 29' 39.01\" N",
"38<f8> 58' 56.02\" N", "41<f8> 39' 10.09\" N"), class = "factor"),
long = structure(c(1L, 10L, 3L, 2L, 4L, 6L, 8L, 7L, 5L, 9L
), .Label = c("068<f8> 57' 57.17\" W", "069<f8> 55' 52.36\" W",
"071<f8> 23' 28.83\" W", "071<f8> 26' 35.85\" W", "082<f8> 27' 25.83\" W",
"083<f8> 32' 16.32\" W", "086<f8> 18' 27.85\" W", "094<f8> 40' 14.85\" W",
"111<f8> 55' 33.78\" W", "117<f8> 09' 39.90\" W"), class = "factor"),
datetimeval = structure(c(503649060, 811578660, 812097060,
861690660, 762935460, 588666660, 681201060, 524817060, 673425060,
776500260), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("lat",
"long", "datetimeval"), row.names = c(NA, -10L), class = "data.frame")
>
我正在使用 googleway 包中的 google_timezone 函数。但我不知道如何将经纬度转换为十进制格式。
df$timezone <- googleway::google_timezone( c(-37.81659, 144.9841),timestamp = df$datetimeval, key = key)
这里有一个选项可以转换为 numeric
并从 google
中提取基于 TimeZone API
的时区
library(tidyverse)
library(googleway)
# get the signs of the columns 'lat/long' by checking if there is 'N' or 'E',
# change it to 1 or else -1
signs <- df %>%
transmute_at(vars(lat, long),
list(~ c(-1, 1)[(str_detect(., "[NE]") + 1)] ))
# mutate the 'lat/long' columns by extracting the numeric part
# get a dot product sum (degree + (min/60) + (sec/3600)
df1 <- df %>%
mutate_at(vars(lat, long),
list(~ str_extract_all(str_remove(., "<f.*>"), "[0-9.]+") %>%
map_dbl(~ as.numeric(.x) %*% c(1, 1/60, 1/3600))))
# change the sign of the columns by multiplying with signs
df1[1:2] <- df1[1:2] * signs
# create the timezone column by looping through each row with `pmap`,
# connect to TimeZone API and extract the details
out <- df1 %>%
mutate(timezone = pmap(., ~ google_timezone(c(..1, ..2),
timestamp = ..3, key = "<key>")))
-输出
out
# lat long datetimeval timezone
#1 18.43386 -68.96588 1985-12-17 01:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#2 32.71574 -117.16108 1995-09-20 02:31:00 3600, -28800, OK, America/Los_Angeles, Pacific Daylight Time
#3 19.67199 -71.39134 1995-09-26 02:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#4 18.48606 -69.93121 1997-04-22 02:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#5 19.41010 -71.44329 1994-03-06 01:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#6 41.65280 -83.53787 1988-08-27 02:31:00 3600, -18000, OK, America/New_York, Eastern Daylight Time
#7 38.98223 -94.67079 1991-08-03 02:31:00 3600, -21600, OK, America/Chicago, Central Daylight Time
#8 32.37922 -86.30774 1986-08-19 02:31:00 3600, -21600, OK, America/Chicago, Central Daylight Time
#9 27.95058 -82.45718 1991-05-05 02:31:00 3600, -18000, OK, America/New_York, Eastern Daylight Time
#10 33.49417 -111.92605 1994-08-10 02:31:00 0, -25200, OK, America/Phoenix, Mountain Standard Time
在这个 df 中,我有纬度、经度和日期时间,我想计算该特定位置在该特定时间的时区。
> dput(df)
structure(list(lat = structure(c(1L, 7L, 4L, 2L, 3L, 10L, 9L,
6L, 5L, 8L), .Label = c("18<f8> 26' 01.91\" N", "18<f8> 29' 09.80\" N",
"19<f8> 24' 36.35\" N", "19<f8> 40' 19.15\" N", "27<f8> 57' 02.07\" N",
"32<f8> 22' 45.20\" N", "32<f8> 42' 56.65\" N", "33<f8> 29' 39.01\" N",
"38<f8> 58' 56.02\" N", "41<f8> 39' 10.09\" N"), class = "factor"),
long = structure(c(1L, 10L, 3L, 2L, 4L, 6L, 8L, 7L, 5L, 9L
), .Label = c("068<f8> 57' 57.17\" W", "069<f8> 55' 52.36\" W",
"071<f8> 23' 28.83\" W", "071<f8> 26' 35.85\" W", "082<f8> 27' 25.83\" W",
"083<f8> 32' 16.32\" W", "086<f8> 18' 27.85\" W", "094<f8> 40' 14.85\" W",
"111<f8> 55' 33.78\" W", "117<f8> 09' 39.90\" W"), class = "factor"),
datetimeval = structure(c(503649060, 811578660, 812097060,
861690660, 762935460, 588666660, 681201060, 524817060, 673425060,
776500260), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("lat",
"long", "datetimeval"), row.names = c(NA, -10L), class = "data.frame")
>
我正在使用 googleway 包中的 google_timezone 函数。但我不知道如何将经纬度转换为十进制格式。
df$timezone <- googleway::google_timezone( c(-37.81659, 144.9841),timestamp = df$datetimeval, key = key)
这里有一个选项可以转换为 numeric
并从 google
TimeZone API
的时区
library(tidyverse)
library(googleway)
# get the signs of the columns 'lat/long' by checking if there is 'N' or 'E',
# change it to 1 or else -1
signs <- df %>%
transmute_at(vars(lat, long),
list(~ c(-1, 1)[(str_detect(., "[NE]") + 1)] ))
# mutate the 'lat/long' columns by extracting the numeric part
# get a dot product sum (degree + (min/60) + (sec/3600)
df1 <- df %>%
mutate_at(vars(lat, long),
list(~ str_extract_all(str_remove(., "<f.*>"), "[0-9.]+") %>%
map_dbl(~ as.numeric(.x) %*% c(1, 1/60, 1/3600))))
# change the sign of the columns by multiplying with signs
df1[1:2] <- df1[1:2] * signs
# create the timezone column by looping through each row with `pmap`,
# connect to TimeZone API and extract the details
out <- df1 %>%
mutate(timezone = pmap(., ~ google_timezone(c(..1, ..2),
timestamp = ..3, key = "<key>")))
-输出
out
# lat long datetimeval timezone
#1 18.43386 -68.96588 1985-12-17 01:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#2 32.71574 -117.16108 1995-09-20 02:31:00 3600, -28800, OK, America/Los_Angeles, Pacific Daylight Time
#3 19.67199 -71.39134 1995-09-26 02:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#4 18.48606 -69.93121 1997-04-22 02:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#5 19.41010 -71.44329 1994-03-06 01:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#6 41.65280 -83.53787 1988-08-27 02:31:00 3600, -18000, OK, America/New_York, Eastern Daylight Time
#7 38.98223 -94.67079 1991-08-03 02:31:00 3600, -21600, OK, America/Chicago, Central Daylight Time
#8 32.37922 -86.30774 1986-08-19 02:31:00 3600, -21600, OK, America/Chicago, Central Daylight Time
#9 27.95058 -82.45718 1991-05-05 02:31:00 3600, -18000, OK, America/New_York, Eastern Daylight Time
#10 33.49417 -111.92605 1994-08-10 02:31:00 0, -25200, OK, America/Phoenix, Mountain Standard Time