# Nearest distance from point to pixel in QGIS?

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?

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, 2018 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, 2018 at 22:08

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
In GRASS, `r.to.vect` to vectorize your raster, and then `v.distance` to compute the distances between the two vectors.
• 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`. Jun 29, 2018 at 7:08