Thursday 13 June 2019

Overlaying spatial polygon with grid and checking in which grid element specific coordinates are located using R



How can one use R to



  1. split a shapefile in 200 meter squares/sub-polygons,

  2. plot this grid (incl. ID numbers for each square) over the original map below, and

  3. evaluate in which square specific geographic coordinates are located.



I am a beginner in GIS and this is perhaps a basic question, but I haven't found a tutorial on how to do this in R.


What I have done so far is loading a shapefile of NYC and plotting some exemplary geographic coordinates.


I am looking for an example (R code) how to this with the data below.


# Load packages 
library(maptools)

# Download shapefile for NYC
# OLD URL (no longer working)
# shpurl <- "http://www.nyc.gov/html/dcp/download/bytes/nybb_13a.zip"
shpurl <- "https://www1.nyc.gov/assets/planning/download/zip/data-maps/open-data/nybb_13a.zip"


tmp <- tempfile(fileext=".zip")
download.file(shpurl, destfile=tmp)
files <- unzip(tmp, exdir=getwd())

# Load & plot shapefile
shp <- readShapePoly(files[grep(".shp$", files)])
plot(shp)

# Define coordinates

points_of_interest <- data.frame(y=c(919500, 959500, 1019500, 1049500, 1029500, 989500),
x =c(130600, 150600, 180600, 198000, 248000, 218000),
id =c("A"), stringsAsFactors=F)

# Plot coordinates
points(points_of_interest$y, points_of_interest$x, pch=19, col="red")

enter image description here



Answer



Here is an example using a SpatialGrid object:



### read shapefile
library("rgdal")
shp <- readOGR("nybb_13a", "nybb")

proj4string(shp) # units us-ft
# [1] "+proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333
# +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83
# +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0"

### define coordinates and convert to SpatialPointsDataFrame

poi <- data.frame(x=c(919500, 959500, 1019500, 1049500, 1029500, 989500),
y=c(130600, 150600, 180600, 198000, 248000, 218000),
id="A", stringsAsFactors=F)
coordinates(poi) <- ~ x + y
proj4string(poi) <- proj4string(shp)

### define SpatialGrid object
bb <- bbox(shp)
cs <- c(3.28084, 3.28084)*6000 # cell size 6km x 6km (for illustration)
# 1 ft = 3.28084 m

cc <- bb[, 1] + (cs/2) # cell offset
cd <- ceiling(diff(t(bb))/cs) # number of cells per direction
grd <- GridTopology(cellcentre.offset=cc, cellsize=cs, cells.dim=cd)
grd
# cellcentre.offset 923018 129964
# cellsize 19685 19685
# cells.dim 8 8

sp_grd <- SpatialGridDataFrame(grd,
data=data.frame(id=1:prod(cd)),

proj4string=CRS(proj4string(shp)))
summary(sp_grd)
# Object of class SpatialGridDataFrame
# Coordinates:
# min max
# x 913175 1070655
# y 120122 277602
# Is projected: TRUE
# ...


Now you can use the implemented over-method to obtain the cell IDs:


over(poi, sp_grd)
# id
# 1 57
# 2 51
# 3 38
# 4 39
# 5 14
# 6 28


To plot the shapefile and the grid with the cell IDs:


library("lattice")
spplot(sp_grd, "id",
panel = function(...) {
panel.gridplot(..., border="black")
sp.polygons(shp)
sp.points(poi, cex=1.5)
panel.text(...)
})


spplot1


or without colour/colour key:


library("lattice")
spplot(sp_grd, "id", colorkey=FALSE,
panel = function(...) {
panel.gridplot(..., border="black", col.regions="white")
sp.polygons(shp)
sp.points(poi, cex=1.5)
panel.text(..., col="red")
})


spplot2


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...