Created
June 24, 2020 14:17
-
-
Save lbusett/5323304f28380c200e4944d7dc6027d5 to your computer and use it in GitHub Desktop.
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
# funzioni necessarie | |
find_UTM_zone <- function(longitude, latitude) { | |
# Special zones for Svalbard and Norway | |
if (latitude >= 72.0 && latitude < 84.0 ) | |
if (longitude >= 0.0 && longitude < 9.0) | |
return(31); | |
if (longitude >= 9.0 && longitude < 21.0) | |
return(33) | |
if (longitude >= 21.0 && longitude < 33.0) | |
return(35) | |
if (longitude >= 33.0 && longitude < 42.0) | |
return(37) | |
(floor((longitude + 180) / 6) %% 60) + 1 | |
} | |
find_UTM_hemisphere <- function(latitude) { | |
ifelse(latitude > 0, "north", "south") | |
} | |
# Dato un poligono, tova il centroide in latlon | |
poly_centr <- poly %>% | |
sf::st_bbox() %>% | |
sf::st_as_sfc() %>% | |
sf::st_centroid() %>% | |
sf::st_transform(4326) | |
# Determina UTM zone e crea EPSG corrispondente (crs_utm) | |
utm_zone <- find_UTM_zone(sf::st_coordinates(poly_centr)[1], | |
sf::st_coordinates(poly_centr)[2]) | |
utm_hem <- find_UTM_hemisphere(sf::st_coordinates(poly_centr)[2]) | |
if (utm_hem == "north") { | |
crs_utm <- st_crs(as.numeric(paste0("326", utm_zone))) | |
} else { | |
crs_utm <- st_crs(as.numeric(paste0("327", utm_zone))) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment