library(rgdal) library(sp) library(rgeos) # Georeferenced UK statistical data typically comes in one of two spatial coordinate reference systems: # - latitude/longitude points based on the World Geodetic System 1984 (‘WGS 84′), # - eastings/northings system of the British National Grid (based on the 1936 Ordnance Survey Great Britain datum ‘OSGB 36′). # The latter system has the benefit of being measured in meters, which enables more accurate spatial analysis to be undertaken. # While converting data between these systems is a [tricky mathematically process]http://www.hannahfry.co.uk/blog/2012/02/01/converting-british-national-grid-to-latitude-and-longitude-ii), # rgdal library, R’s implementation of the cross-platform Geospatial Data Abstraction Library. # The transformations themselves are described by the following *proj.4* strings wgs = '+proj=longlat +datum=WGS84' bng = '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs' mrc = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs' # read shapefile shp.path <- 'C:/cloud/OneDrive - University College London/data/boundaries/shp' shp.fn <- 'OA' shp <- readOGR(shp.path, shp.fn) # Centroids centroids <- gCentroid(shp, byid = TRUE) y1 <- cbind(shp@data, centroids@coords) # Perimeter, Area, ... Need reprojection for units and to km and km2 shp <- spTransform(shp, CRS(bng)) len <- gLength(shp, byid = TRUE) area <- gArea(shp, byid = TRUE) y2 <- cbind(shp@data, len, area) # write final result y <- merge(y1, y2) data.path <- 'C:/cloud/OneDrive - University College London/data/geography/' write.csv(y, paste0(shp.path, 'OA_measures.csv'), row.names = FALSE)