lvalnegri

poly_measures.R

May 16th, 2018
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.78 KB | None | 0 0
  1. library(rgdal)
  2. library(sp)
  3. library(rgeos)
  4.  
  5. # Georeferenced UK statistical data typically comes in one of two spatial coordinate reference systems:
  6. #  - latitude/longitude points based on the World Geodetic System 1984 (‘WGS 84′),
  7. #  - eastings/northings system of the British National Grid (based on the 1936 Ordnance Survey Great Britain datum ‘OSGB 36′).
  8. # The latter system has the benefit of being measured in meters, which enables more accurate spatial analysis to be undertaken.
  9. # 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),
  10. # rgdal library, R’s implementation of the cross-platform Geospatial Data Abstraction Library.
  11. # The transformations themselves are described by the following *proj.4* strings
  12.  
  13. wgs = '+proj=longlat +datum=WGS84'
  14. 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'
  15. 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'
  16.  
  17. # read shapefile
  18. shp.path <- 'C:/cloud/OneDrive - University College London/data/boundaries/shp'
  19. shp.fn <- 'OA'
  20. shp <- readOGR(shp.path, shp.fn)
  21.  
  22. # Centroids
  23. centroids <- gCentroid(shp, byid = TRUE)
  24. y1 <- cbind(shp@data, centroids@coords)
  25.  
  26. # Perimeter, Area, ... Need reprojection for units and to km and km2
  27. shp <- spTransform(shp, CRS(bng))
  28. len <- gLength(shp, byid = TRUE)
  29. area <- gArea(shp, byid = TRUE)
  30. y2 <- cbind(shp@data, len, area)
  31.  
  32. # write final result
  33. y <- merge(y1, y2)
  34. data.path <- 'C:/cloud/OneDrive - University College London/data/geography/'
  35. write.csv(y, paste0(shp.path, 'OA_measures.csv'), row.names = FALSE)
Advertisement
Add Comment
Please, Sign In to add comment