Creating a CSV for spatial data

One of my goals for data organization is to consolidate all of my spatial data into a single csv file. This will walk through the steps that I took to create the final table showign the spatial data that I have collected

library(raster)
library(maptools)
library(rgdal)

#readOGR command (see below) only works if the files are in a set work directory
setwd("~/Grad Year 3/Advanced Data Structures/spatial data")
report_info <- read.csv("~/Grad Year 3/Advanced Data Structures/LincolnshireArchaeobotany/reportinfo.csv")

Load reclass rasters created last week

The process of creating these rasters is shown here. I also imported the shapefiles representing temperature during the medieval period, terrain types, and degree of nucleation. My original plan had been to work with spatial data in QGIS, but I found it easier to perform the spatial joins directly in R.

#load last week's data
reclass_dem <- raster("~/Grad Year 3/Advanced Data Structures/spatial data/DEM_reclass.tif")
reclass_slope <- raster("~/Grad Year 3/Advanced Data Structures/spatial data/slope_reclass.tif")

#transform the sites table into a SpatialPointsDataFrame
coordinates(report_info) = ~lon + lat
projection(report_info) <- CRS("+proj=lonlat +ellps=WGS84")
report_info <- spTransform(report_info, CRS(projection(reclass_dem)))

#import the shapefile representing the medieval temperature. the rough plot below shows that some sites are in the slightly warmer grid square (the easternmost grid square) 
temperature <- readOGR(".", "medenglandtemp_poly_new", verbose=FALSE)
county <- readOGR(".", "LincolnshireBoundaries", verbose=FALSE)
plot(temperature)
points(report_info, col="blue")
lines(county, col="green")

#time to repeat the process to load the terrain types
terrain <- readOGR(".", "TerrainLincolns", verbose=FALSE)
#a rough map to show the diversity of terrain types within Lincolnshire
plot(terrain)
points(report_info, col="blue")
lines(county, col="green")

#now adding the polygons with data on the degree of nucleation
nucleation <- readOGR(".", "VornoiNucleationLinconls", verbose=FALSE)
#a rough map to show the spread of sites within the polygons
plot(nucleation)
points(report_info, col="blue")
lines(county, col="green")

Extracting the spatial data from all five layers directly to the points

I used the “extract” function from the raster package for the two rasters and the “over” function from the sp package for the spatial polygon files.

#extracting the data
sites_dem <- extract(reclass_dem, report_info)
sites_slope <- extract(reclass_slope, report_info)
sites_temperature <-over(report_info, temperature)
sites_terrain <-over(report_info, terrain)
sites_nucleation <-over(report_info, nucleation)

#change the format of the sites table back to a dataframe
report_info <- as.data.frame(report_info)
#select the her_no and her_cit columns and create a new dataframe to which the spatial data can be added
sites_spatial <- report_info[,3:4]
#add the extracted spatial data
sites_spatial$dem <- sites_dem
sites_spatial$slope <- sites_slope
sites_spatial$temppoly <- sites_temperature$id
sites_spatial$terrain <- sites_terrain$T_ZONE
sites_spatial$nucleation <- sites_nucleation$NUCLCAT_N

Ready to export

By writing a CSV file with all of the data in a single table, I will no longer need to import the spatial data each time that I want to work with spatial attributes. I can include it in my data model as it currently stands without losing third normal form.

#write.csv(sites_spatial, file= "~/Grad Year 3/Advanced Data Structures/LincolnshireArchaeobotany/sitesspatial.csv")