I have a raster area which i could also convert into a vector area. I need all cells which lie in direct vicinity of the edge(s).

My first try was to convert the vector area to lines and then the resulting border line back to raster. The problem with this approach is that i won't get all border cells- but instead just some and spaces in between (see picture).

Any better idea how to solve my issue?

enter image description here


Here's one method that works. Suppose your raster polygon is named poly.

First, you need to know the cell size in advance, so check with:

g.region -p rast=poly

Take note of the resolution. Now use the r.grow.distance module to create distances from NULL cells with the -n flag:

r.grow.distance -n poly dist=poly_dist val=poly_val --o

This creates two new raster maps - the distance from null cells and the value of the poly raster at each cell.

Now find all those cells where the distance is just one cell size (these are the border cells)

r.mapcalc "poly_border = if(poly_dist==<enter resolution here>, poly_val, null())" --o

That should do it.

  • Thx that seems like a working approach! Another way i found is to compute with r.neigbors the null() cells around each cell and then select with r.mapcalc all cells which do not have the maximum number (9 in the case of r.neigbors size=9). – Samuel Müller Feb 3 at 10:08

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.