# Calculating shortest distance between multiple Polygon borders to a common Polygon border?

I have 2 sets of Polygons and I need to calculate the shortest distance between the polygons on one layer to the single polygon (one feature) on the other layer! How can I do that? Any help on how to accomplish this in QGIS?

The real world case is to calculate the shortest distance from the edge of a land parcel to the water/ocean. I will need to do this once or twice, but the multiple polygon layer has 10k's of features).

Being not of many words I have included an image of what I need to do!

• Do you need to perform this task only a few times or in a programmatically way? – mgri Sep 30 '17 at 14:43
• I just need to do it once or twice! The multiple polygon layer has some 25k poligons. But i would think that this must be a fairly common problem in GIS!?! – Mats Wedin Sep 30 '17 at 14:59
• I have a very simple idea for general purposes, but please confirm me that the water polygon (colored with blue) is composed of only one feature. – mgri Sep 30 '17 at 15:04
• Please Edit the question in response to requests for clarification. It is unfair to those who would help you to need to mine comments for critical information. – Vince Sep 30 '17 at 15:07
• Yes the Water polygon is only one feature – Mats Wedin Sep 30 '17 at 15:17

Since you don't need to perform this task in a programmatically way, you may follow these simple steps:

1. Select the water polygon layer from the Layers Panel;
2. Open the Python Console:

1. Open the editor and run the following code (see the image below):
``````layer=iface.activeLayer()
for feat in layer.getFeatures():
print feat.geometry().exportToWkt()
``````

1. Copy the result (just double click on the line which starts with Polygon ((...)) and then ctrl+c);

2. Select the land parcel layer from the Layers Panel and then open the Field Calculator. Create a new field with the following expression (see the image below):

``````distance(\$geometry,geom_from_wkt( 'Polygon ((-1.06346153846153846 -0.68461538461538463, 1.27115384615384608 -0.68461538461538463, 1.27115384615384608 0.80000000000000004, -1.06346153846153846 0.80000000000000004, -1.06346153846153846 -0.68461538461538463),(-0.92959048475307471 0.66890818799452745, -0.94814336044880765 0.27311350648555988, -0.89248473336160905 0.08758474952823136, -0.92340619285449721 -0.07939113173336443, -0.86774756576729861 -0.27110418058927044, -0.80590464678152229 -0.40715860235797807, -0.6884031007085476 -0.53702873222810821, -0.55234867893983997 -0.54321302412668571, -0.42866284096828777 -0.54321302412668571, -0.31734558679389036 -0.54321302412668571, -0.23076550021380404 -0.57413448361957387, -0.10707966224225141 -0.57413448361957387, 0.07226480281649961 -0.53084444032953049, 0.20831922458520724 -0.49373868893806483, 0.36911081394822531 -0.51229156463379755, 0.65358824128279558 -0.56176589982241865, 0.97517142000883172 -0.48755439703948711, 1.00609287950171966 -0.38860572666224535, 1.04938292279176304 -0.21544555350207206, 1.07412009038607348 0.01337324674529994, 1.04319863089318554 0.09376904142680897, 1.0246457551974526 0.24219204699267183, 1.0308300470960301 0.39061505255853457, 1.07412009038607348 0.48337943103719894, 1.10504154987896186 0.61324956090732896, 0.93806566861736584 0.63180243660306168, 0.7216154521671494 0.62561814470448418, 0.59174532229701926 0.58851239331301841, 0.45569090052831163 0.61324956090732896, 0.38766368964395781 0.67509247989310506, 0.17121347319374136 0.67509247989310506, 0.02897475952645623 0.63180243660306168, -0.10089537034367391 0.60088097711017363, -0.45340000856259821 0.67509247989310506, -0.55234867893983997 0.693645355588838, -0.70077168450570282 0.70601393938599322, -0.92959048475307471 0.66890818799452745))' ))
``````

Obviously, you need to replace the above WKT Polygon with your case. Furthermore, I assumed you wanted a field with decimals, so adapt it to your case.

1. This will create a new field storing a value which is the minimum distance from that feature to the water polygon:

• I sort of get an error on the first part! indentationError! – Mats Wedin Sep 30 '17 at 15:59
• @MatsWedin try to copy each single row manually (no problem for the first two rows, while add 4 blank spaces before pasting the third row). For copy, I mean copying the row by selecting it starting from the beginning of the first word in the line. – mgri Sep 30 '17 at 16:03
• I got it to work i just ignored the error and followed your instructions! Awesome! – Mats Wedin Sep 30 '17 at 16:17