Last active
July 8, 2020 18:29
-
-
Save serialc/9cb20918c92a22b3921e0200d9331746 to your computer and use it in GitHub Desktop.
R script to transform longitude to UTM zone and EPSG code for projections
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
# Functions to convert a longitude to the UTM zone and EPSG code | |
# Get the UTM zone for a given longitude | |
long2UTM <- function(long) {(floor((long + 180)/6) %% 60) + 1 } | |
# get the EPSG code (int) for a given longitude and hemisphere | |
UTMzone2EPSG <- function(utmzone, hemisphere) { | |
if( tolower(hemisphere) %in% c("north", "n") ) { | |
if( utmzone < 10 ) { epsg = '3260' } else { epsg = '326' } | |
} else if( tolower(hemisphere) %in% c("south", "s") ) { | |
if( utmzone < 10 ) { epsg = '3270' } else { epsg = '327' } | |
} | |
else { stop(paste0("hemisphere provided '", hemisphere, "' is not valid. Valid values are: north or south")) } | |
return(as.integer(paste0(epsg, utmzone))) | |
} | |
# Tests | |
# vancouver, CA | |
long2UTM(-123) | |
UTMzone2EPSG(long2UTM(-123), 'north') | |
# melbourne, AU | |
long2UTM(145) | |
UTMzone2EPSG(long2UTM(145), 's') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment