You will want to import your shape file. You can try package rgdal
, specifically function readOGR
.
After reading into R your csv file, you may want to coerce it to a SpatialPoints
class. Make sure your projections are identical.
After you have the polygon and SpatialPoints
, it should be just a matter of using function over
from package sp
.
I recently asked a similar question. I hope the data in the self contained example should be sufficient to work through the second part of your question.
Define a grid at the desired resolution and rasterize by mean point value:
library(raster)
g <- raster(pts) ## gives an empty 10*10 grid on your extent/crs
## set the resolution (pixel size x/y)
res(g) <- c(5, 7) ## whatever you want
## rasterize by "value" column with na.rm optionally
r <- rasterize(pts, g, field = pts$value, fun = mean, na.rm = TRUE)
This won't interpolate at all, it's just n-points to 1-cell by point-in-cell membership, so your res()
values should mean you get points in the cells you need to populate.
This is probably going to run faster than you might expect as long as you have sufficient memory, but try a test first with 1000 (or whatever) points:
r <- rasterize(pts[sample(nrow(pts), 1000), ], g, field = pts$value, fun = mean, na.rm = TRUE)
See ?raster::interpolate
for more options.
(You can set the tolerance for points2Grid
if your points really are meant to be regular, or see ?rasterFromXYZ
for a digits option. )
Best Answer
When you read a CSV with no options the driver doesn't know where the coordinate columns are so returns a data frame:
So you need to pass a couple of options that end up telling the OGR driver where the coordinates are:
and there's your
sf
spatial object.OGR CSV driver documentation is here: https://gdal.org/drv_csv.html