I'm trying to use R to rasterize a number of shapefiles and I'm having trouble dealing with cells on the edge of shapes, in other words the if a cell is partially covered by an NA value and partially covered by a value associated with the shape. If any part of the cell is covered by a shape I would like the final raster product to have that value. For example in the image below I show the original shapefile (in green colors) and the result of a rasterize operation using R (in grey-scale). All of the red circles show areas where I would like the raster to have a value because some portion of the cell is covered by a shape.
I've tried using various functions with rasterize (max, mean, na.rm = T) and haven't had any luck producing the result I'm looking for. Below are links to the datafiles and my basic procedure.
R is my tool of choice but I'm happy with solutions in Arc or QGis as well. With the caveat that any solution needs to be relatively fast (I have 900 of these and I would ideally like to get this done in less than a day) and batchable (I'm familiar with model builder in Arc)
##Load libraries library("rgdal") #Handling Shapefiles library("raster") #Handling Raster files library("ncdf4") #For reading raster from .nc file ##Load in shapefile as spatialpolygon myShape <- readOGR(dsn = "USDM_20110802.shp", layer = "USDM_20110802") ##Load in Raster #This will throw a waring about the variable name it can be ignored #The netCDF file is multilayer but we only need on for a mask myRaster <- raster("precip.V1.0.mon.ltm.nc") ##Rotate Raster-orignial file use longitude of 0 to 360 we want -180 to 180 to work with the shapefile #This will also throw an error which can be ignored myRaster <- rotate(myRaster) ##Rasterize the shape file #I've tried all the following shapeRaster <- rasterize(myShape, myRaster, field = "DM") shapeRaster <- rasterize(myShape, myRaster, field = "DM", na.rm = T) shapeRaster <- rasterize(myShape, myRaster, field = "DM", fun = max) shapeRaster <- rasterize(myShape, myRaster, field = "DM", fun = max, na.rm = T) shapeRaster <- rasterize(myShape, myRaster, field = "DM", fun = mean) ##My goal is for this cell to have a value of 0 instead of NA #in the image it's the top circled cell on the lefthand side shapeRaster[24,215]