Created
September 15, 2022 15:12
-
-
Save simonrolph/029a5649542a07b81d54ad206ed8aa37 to your computer and use it in GitHub Desktop.
Reproject longitude and latitude to OSGB grid using {sf} and produce a column referring to unique 1km grid squares
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
#load pacakges | |
library(sf) | |
library(magrittr) #for the pipe | |
#create a test dataframe | |
test <- expand.grid(latitude = 50:52,longitude = -1:1) | |
#load as sf, transform, round to 1km | |
test2 <- st_as_sf(test,coords =c("longitude","latitude"),crs = 4326) %>% | |
st_transform(27700) %>% | |
st_coordinates() %>% #extract the coordinages | |
round(digits = -3) %>% #to 1km, use digits = -2 for 100m | |
data.frame() | |
#create a pseudo grid square reference (it doesn't ahve the two letters but it is unique for each 1km grid square) | |
test2$km_grid <- paste0(test2$X,test2$Y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment