I want to create a presence-absence raster out of a shapefile (vector) with the rasterize command of the R-package in R. Until now I only get wrong results.

The raster should just distinguish between the two options ('shapefile on this cell present', 'shapefile on this cell abscent').

I tried it out with several options of the rasterize-command:

forest_polygons_raster_mask <- rasterize(forest_polygons, ras, mask = TRUE)

forest_polygons_raster_update <- rasterize(forest_polygons, ras, update =  

forest_polygons_raster_cov <- rasterize(forest_polygons, ras, getCover = 

the command with the getCover option does not stop, also after several hours. With both of the other commands I get a wrong result and my raster has presence-cells in areas where the shapefile is abscent.

The original shapefile with two holes: :

That is the raster I get after the rasterize command with the update-option. One hole is missing, and the other is split into two smaller holes. No abscence was recognized wrong, just presence was recognized wrong:

Shapefile layer over raster layer, the holes in the shapefile are not left out as abscence in the raster:

Did anyone have a similar problem? What might the problem be? My shapefile is quite ordinary with different fields of the categories: ID, Shape (Polygon), Classification (text), Shape_Area, Shape_Length. The cellsize of the input raster for the rasterize process is quite small (so I do not expect that the errors are due to two big cells).

  • 1
    Not sure we can help without getting your data - is that possible? Or something that shows the same problem?
    – Spacedman
    Jul 6, 2018 at 20:11
  • Yes, I completely see your point. Unfortunately it is sensitive data, so I cannot share it and I have never found a similar problem in any other data...
    – Li12
    Aug 2, 2018 at 8:50
  • Without your original data it is tough to figure out what is going on - long run times may be an indication of geometry problems though, have you checked the validity of your vector data?
    – Simbamangu
    Jan 5, 2019 at 12:41

1 Answer 1


The option that uses getCover = TRUE is correct, but:

  1. This option returns the proportion of cell covered by polygon. So the raster values have a range between 1 and 0 so you can check these values that are greater than 0, but given these magnitude and the R color scales, they seem to be 0 (black).
  2. This option returns 0 as background.
  3. You should change values greater than 0 to 1:

    forest_polygons_raster_cov[forest_polygons_raster_cov[] > 0] <- 1

enter image description here

  • Thanks for the tip! I will try it out and update this thread how it worked out!
    – Li12
    Aug 2, 2018 at 8:51
  • Unfortunately the process with getCover = True seems to not run through. With mask or update = True the runtime is about 2-3 hours. With getCover it did not finish after approximately 17 hours now, even though I parallelized it... Do you have any ideas what the problem is with getCover = True?
    – Li12
    Aug 3, 2018 at 9:29
  • 1
    Can you provide example files? getCover = TRUE split each pixel into 100 parts and calculate the percentage. This makes the processing process even slower. Aug 13, 2018 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.