I have several million coordinates (which are either British National Grid or Long/Lat) and need to convert them. I know using R you can do this in a spatial operation (such as in my example below using sf
). I was however wondering if it could be done without creating a spatial object first (and just be a mathematical function?). Is there a package that can do this, or has someone already written a function that does this that they are willing to share?
Pages 49 to 51 in this PDF detail a fairly long equation for the conversions - but before jumping into coding that I wondered if anyone had any alternative solutions.
library(data.table)
library(sf)
# Sample data
DT1 <- data.table(DT1_id = sprintf("%s%0*d", "UID_", 7, 1:3000000),DT1_x =sample(45000:55000, size = 3000000,replace = TRUE),DT1_y =sample(25000:35000, size = 3000000,replace = TRUE))
# BNG to WGS84
DT1_sf <- st_as_sf(DT1, coords = c("DT1_x","DT1_y"),crs=27700)
DT1_sf_4326 <- DT1_sf %>% st_transform(crs = 4326)
DT1[, LAT := data.frame(st_coordinates(DT1_sf_4326))[,2]]
DT1[, LONG := data.frame(st_coordinates(DT1_sf_4326))[,1]]
# WGS84 to BNG
DT1_sf <- st_as_sf(DT1, coords = c("LONG","LAT"),crs=4326)
DT1_sf_27700 <- DT1_sf %>% st_transform(crs = 27700)
DT1[, EASTING := data.frame(st_coordinates(DT1_sf_27700))[,1]]
DT1[, NORTHING := data.frame(st_coordinates(DT1_sf_27700))[,2]]