[GIS] How to compute distances between points and polygon borders

distanceqgis

I have point data and polygon in vector format. For each point, I want the distance from the point to the border of the polygon. However, if the point is inside the polygon, I want negative distance and if the point is outside the polygon, I want positive distance. Is there any way to do this in QGIS?

Best Answer

One possible solution:

  1. Convert the polygons to lines using Vector > Geometry Tools > Polygons to Lines. (I am unclear if this creates closed polylines or individual line segments, which may be an issue later. If individual, you can dissolve/merge the resulting lines and not allow multipart features. If closed polylines you can use Explode Lines to get individual lines.)

  2. Use the MMQGIS plugin's Hub Distance tool or GRASS's v.distance command to get the distances from each of your points to the nearest line. Note that the MMQGIS plugin won't give perpendicular (shortest) distance to line, but rather to a midpoint or node. In this case you'd want individual line segments and to be aware it's not actually the shortest distance. As such that plugin may not work for your needs. Getting the distance from a point to a line is discussed at some other questions here (another solution uses PostGIS's ST_Distance query):

  3. Once you have the distance attribute on your points, do a select by location/spatial query to select all records that intersect/are within your original polygons. Use the Field Calculator to multiply the distance attribute by -1 for only the selected records. This assumes the datatype will allow the proper signed range of values to hold your distances. If not, you may first need to create a new field of the correct datatype and copy the distance values into it.

As a side note for users of ArcGIS, the process is simpler because of the ways in which ArcGIS handles calculating proximity. There's less concern in finding an easy/proper method to get the actual distance from point to line because the tools handle multiple cases. Otherwise the steps are pretty much the same.

Related Question