Is there a way to add missing CRS? I have been given some old Esri shapefiles and a large proportion of them have CRS: NA
, I'd like to be able to batch set the missing CRS. I can set it manually when I import into QGIS but can't seem to set it using R. I tried st_transform()
but it doesn't seem to work if there is no CRS set in the first place.
Thanks for the comments. I tried st_crs <-
and st_set_crs()
but still couldn't seem to get it set correctly or plot the shapefiles. I noticed the ones with missing .prj
files were the ones that had no CRS which makes sense. so I wrote a chunk to create a proj file for each shapefile in the directory if one does not allready exist. Bit of a hack but it works :)
EDIT: Added a crude logging system to track the changes
p = "/path/to/shape/files"
proj_str_bng = 'PROJCS["British_National_Grid",GEOGCS["GCS_OSGB_1936",DATUM["D_OSGB_1936",SPHEROID["Airy_1830",6377563.396,299.3249646]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",400000.0],PARAMETER["False_Northing",-100000.0],PARAMETER["Central_Meridian",-2.0],PARAMETER["Scale_Factor",0.999601272],PARAMETER["Latitude_Of_Origin",49.0],UNIT["Meter",1.0]]'
proj_str_wgs84 = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
shp_to_prj <- list.files(paste0(wd, p), pattern = "\\.shp$")
if (!file.exists(paste0(wd,p,"prj_log"))){
write("file, dt", file = paste0(wd,p,"prj_log"))
}
for (i in 1:length(shp_to_prj)) {
shp_to_prj[[i]] <- paste0(wd, p, str_remove_all(shp_to_prj[[i]], ".shp"), ".prj")
if(file.exists(shp_to_prj[[i]])) {
next
}
write(proj_str_bng, file = shp_to_prj[[i]])
write(paste0( shp_to_prj[[i]], ",",Sys.time()), file = paste0(wd,p,"prj_log"), append=TRUE)
}
write(paste0( "^===============================================================^", ",",Sys.time()), file = paste0(wd,p,"prj_log"), append=TRUE)