I am using r to do some spatial analysis.
Here is my project, I am using raster files (that contain information on land use) and a polyline (which is a border between two regions). I am analyzing land use change for each pixel according to the position of that pixel regarding the border. In a nutshell I want to do a spatial regression discontinuity analysis.
Here a visual figure:
I have been able to collect the data of each of my (17 rasters) using that code :
r1 <- raster("~path/r1.tif") r2 <- raster("~path/r2.tif") ... r17 <- raster("~path/r17.tif") rasStack = stack(r1,r2,...,r17) points <- rasterToPoints(rasStack,spatial=TRUE) write.csv(points, file = "~path/points.csv")
This provides me with a csv file that contain 20 variables (point_id, lat, long, r1_value, r2_value, ..., r17_value).
I do not want to aggregate the pixels into bigger ones. Hence I have about 22 million pixels.
Here is my question: I am trying to figure out how to add to my csv file one variable with the distance between each pixel and the line. I want to find a way to get a negative distance if the pixel is on the left, and a positive distance if the pixel is on the right.
I found one alternative way to do it on QGIS, by (1) converting the border shapefile into a chain using chainage, (2) create a distance matrix for each pixel to the nearest chain, (3) create a one side buffer around the line border, (4) clip the pixels to this one side buffer, (5) merge clipped pixels to the previous one and set them as either being on the right or on the left of the border.
However I am trying to do it in r to avoid doing things manually, and because I am sure there must be one easier solution. I found some functions like get.knn but did not get the expected results.
Here an image of the line and the whole raster: