2

I have 1) a point layer and 2) a binary raster file (1 and 0). I need to estimate the Euclidean distance from each point to the nearest "1" pixel. I know there is a tool in ArcGIS but I only have access to QGIS/GRASS; in GRASS there is a clear way to estimate this distance but it's between two vectorial layers (using v.distance).

So far, I tried:

1) in QGIS, the Proximity tool, but it doesn't give me the result I want, since it considers only one raster layer

2) in GRASS, r.distance but I get an error ("Raster map is not CELL"), and I am still trying to figure it out, but I'm still not sure which values will it give me since I don't see any option to select a set of pixels ("1", in this case), instead of all the layer

Does anyone know how to do that in QGIS?

3

With QGIS:

One way is to polygonize your raster and then follow Calculating minimum distance between points and polygons in QGIS?

Another way, may be.

  1. Create a point layer with the processing "Create grid" with the same resolution of your raster
  2. Join the attribut (1 or o) with the plugin "Point sampling tool"
  3. Create a distance matrix Vector -> Analysis Tools -> Distance Matrix (choose "Use only the nearest (k) target points" as 1
  • Thanks!! When creating the grid, is it possible that each point would fall in the center of the pixel? How can I do that? – mtao Jun 28 '18 at 16:59
  • Another problem is that I think my raster file has all pixels with "1" values... I converted from vector to raster, and I don't know how to change the white cells (supposedly the ones without a value in the shapefile) from "1" to another value. Do you know how to change that? – mtao Jun 28 '18 at 22:08
3

If you're willing to do some scripting, here's another approach:

You could loop thru the points vector, extract each point one by one, and convert to a raster. This temporary raster will have a single pixel with a value, and all the rest will be NULL. Then use r.distance with two input rasters: this new single pixel and the original binary raster to get the distance from the pixel to the nearest "1" value. For this to work, you'll probably need to convert all the "0" values in the binary raster to NULL (see r.null setnull=0).

It could go something like:

g.copy rast="binary_raster","binary_nulls"
r.null "binary_nulls" setnull=0
v.db.select -c "point_vector" column=cat >> cat_list.txt
while read c; do
    v.to.rast --o "point_vector" output="point_vector" cats=${c} use=val value=${c}
    r.distance input=binary_nulls,point_vector >> distance_results.txt
done < cat_list.txt
2

In GRASS, r.to.vect to vectorize your raster, and then v.distance to compute the distances between the two vectors.

  • I know of the existence of that tool but the problem is that I need the vector to be in a raster. Any idea of how to do it with a raster? – mtao Jun 28 '18 at 22:05
  • 1
    You're not deleting the raster, just making a copy of it to get the results. You can then take the results (in vector form) and convert back to raster, with v.to.rast. – mankoff Jun 29 '18 at 7: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.