I'm interested in undertaking spatial matching in R with use of the sp package on some of the publicly available data consisting of:
- Polygons: Nomenclature of territorial units for statistics
- X/Y coordinates: sourced from Doogal's website, with Easting values being assigned to
x
and Northing toy
.
Problem
The challenge seems to be with setting up the correct CRS for both of those data sets.
Illustration
I've initially attempted to set up projections to those discussed here, via:
shpNUTS <- spTransform(
shpNUTS,
CRSobj = CRS(
"+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 +no_defs"
)
)
proj4string(pointsDta) <- proj4string(shpNUTS)
res <- over(pointsDta, shpNUTS)
results in an unusable map:
Similarly, the attempt to remove projection via:
proj4string(shpNUTS) <- CRS(as.character(NA))
Does not improve the map.
Matching
The matching is undertaken in a simple manner,
sp::over(pointsDta, sp::geometry(shpNUTS))
but I've also tried:
res2 <- spatialEco::point.in.poly(as(pointsDta, "SpatialPointsDataFrame"), shpNUTS)
The matching does not return overlapping points; this is obviously wrong as x/y
coordinates sourced from Doogal'swebsite reflect UK postcodes so all of the postcodes should be matched against a set of UK NUTS geographies.
Remaining code
The shapefile is read via the readOGR
function available through rgdal package, Doogal's data is read via readCSV
in R. Remaining code reflects basic transformations:
pointsDta <- doogalDta[, c("x", "y")]
pointsDta <- na.omit(pointsDta)
coordinates(pointsDta) <- ~ x + y
# reproject nuts
shpNUTS <- spTransform(
shpNUTS,
CRSobj = CRS(
"+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 +no_defs"
)
)
proj4string(pointsDta) <- proj4string(shpNUTS)
res <- over(pointsDta, shpNUTS)
Update
Updates in reply to provided comments.
Shapefiles
Concering the shapefiles, I've sourced this file, that is made available through the Eurostat website.