6

I'm trying to convert one raster into a SpatialPolygons. I've got a big raster (12 millions pixels) with a lot of zeros, and some negative values (representing some depression in a DEM). I would like to cluster all these negatives values into polygons, to get a map of all the depressions on my map.

I'm a R beginner and I can't find a suitable function to do the job.

Does anyone have a clue ?

3

The tool (part of the raster & rgeos libraries) that you need to consider is:

output_shp <- rasterToPolygons(x, fun=function(x){x<0}, n=4, na.rm=TRUE, digits=12, dissolve=TRUE)

Where 'x' is your DEM-raster.
This results in a 'SpatialPolygonsDataFrame', which is what you were after.

However, before you run the rasterToPolygons-tool, it would be advisable to reclassify your DEM into -1s and 0s:

reclassified_raster <- reclassify(x,c(-Inf,0,-1, 0,Inf,1))

This will give you a raster with all the negative values as '-1' and all positive values as '0', for use in the rasterToPolygons-tool. If this step is not taken, then every height in the elevation model will become its own polygon.

  • Yes I saw this tool but it's for 4,8 o 16 nodes polygons only. I got some very complex and big polygons to create. – A.Rogeau Sep 25 '15 at 13:52
  • Moreover, this method would take several days to create my polygons, if it can do it.. – A.Rogeau Sep 25 '15 at 13:53
  • Have you tried with dissolve=TRUE ? – Mikkel Lydholm Rasmussen Sep 25 '15 at 13:56
  • Otherwise, you could also consider the GDAL implementation - see gdal.org/gdal_polygonize.html - although this turns everything into polygons, not just the bits you want, unless you also use a mask layer. – Mikkel Lydholm Rasmussen Sep 25 '15 at 13:59
  • Thanks! Is it possible to set all the negatives pixels to -1, in order to generate these polygons ? – A.Rogeau Sep 25 '15 at 14:03

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.