Skip to content

Instantly share code, notes, and snippets.

@simonrolph
Created September 15, 2022 15:12
Show Gist options
  • Save simonrolph/029a5649542a07b81d54ad206ed8aa37 to your computer and use it in GitHub Desktop.
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
#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