Last active
February 22, 2019 15:52
-
-
Save fzenoni/bd06d3469f783dc740b836f4cc7e1d30 to your computer and use it in GitHub Desktop.
Muovi punto in direzione a piacere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sf) | |
get_utm_crs <- function(dat) { | |
# To determine UTM CRS, first compute average longitude points | |
if(st_crs(dat)$epsg != 4326) { | |
dat <- st_transform(dat, crs = 4326) | |
} | |
# Extra step if dat is not only made of points | |
if(!all(st_is(dat, 'POINT'))) { | |
oldw <- getOption("warn") | |
options(warn = -1) | |
dat <- st_cast(head(st_geometry(dat), 1000), 'POINT') | |
options(warn = oldw) | |
} | |
avg_lat <- st_coordinates(head(dat, 1000))[, 2] %>% mean | |
north_or_south = 6 | |
if(avg_lat < 0) { | |
north_or_south = 7 | |
} | |
avg_lon <- st_coordinates(head(dat, 1000))[, 1] %>% mean | |
crs <- paste0(32, north_or_south, as.integer(floor((avg_lon + 180) / 6.) + 1)) %>% as.integer | |
return(crs) | |
} | |
move_point <- function(origin=st_point(c(0,0)), | |
direction = c(north=0, south=0, east=0, west=0)) { | |
wgs84 = 4326 | |
origin <- st_sfc(origin, crs = wgs84) | |
projection = get_utm_crs(origin) | |
origin_m <- st_transform(origin, crs=projection) | |
meters_to_the_north = direction[1] | |
meters_to_the_south = direction[2] | |
meters_to_the_east = direction[3] | |
meters_to_the_west = direction[4] | |
new_point <- (st_coordinates(st_geometry(origin_m)) + | |
c(0, meters_to_the_north) + | |
c(0, -meters_to_the_south) + | |
c(meters_to_the_east, 0) + | |
c(-meters_to_the_west, 0)) %>% | |
st_point %>% st_sfc(crs=projection) %>% st_transform(crs=wgs84) | |
return(new_point) | |
} | |
rome <- st_point(c(12.4964, 41.9028)) | |
# Move by c(N,S,E,W) and return new lon/lat coordinates | |
move_point(rome,c(1,0,0,0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now works also for South emisphere