5

I have a point and a multipolygon in PostGIS and wish to calculate ST_Distance (and ST_ShortestLine respectively) as follows:

SELECT ST_Distance('SRID=25832;POINT(641099.6 5495250.5)'::geometry,
'SRID=25832;MULTIPOLYGON(((641101.557 5495236.647,641100.718 5495237.413,641099.327 5495238.694,641091.793 5495246.089,641091.044 5495246.812,641089.631 5495248.193,641083.116 5495255.763,641089.36 5495261.908,641091.447 5495259.774,641093.686 5495257.484,641094.3 5495256.835,641095.719 5495255.336,641097.142 5495253.833,641097.465 5495253.492,641099.644 5495251.132,641103.115 5495247.237,641105.224 5495244.807,641107.339 5495242.312,641108.953 5495240.371,641110.171 5495238.884,641111.912 5495236.721,641113.55 5495234.651,641115.383 5495232.319,641117.443 5495229.57,641118.218 5495228.571,641118.547 5495228.144,641123.997 5495221.03,641130.326 5495212.262,641125.432 5495208.729,641124.225 5495210.402,641123.101 5495211.935,641122.028 5495213.377,641120.616 5495215.249,641119.484 5495216.723,641117.48 5495219.463,641115.401 5495222.252,641113.602 5495224.495,641112.073 5495226.28,641110.782 5495227.752,641109.521 5495229.118,641108.305 5495230.389,641107.432 5495231.289,641107.366 5495231.358,641102.973 5495235.355,641101.557 5495236.647),(641092.903 5495249.874,641090.137 5495252.714,641089.639 5495253.241,641089.465 5495253.438,641089.335 5495253.672,641089.262 5495253.909,641089.236 5495254.167,641089.262 5495254.409,641089.315 5495254.593,641089.437 5495254.835,641089.572 5495255.011,641089.768 5495255.178,641089.961 5495255.289,641090.18 5495255.368,641090.45 5495255.407,641090.735 5495255.385,641090.983 5495255.311,641091.219 5495255.179,641091.457 5495254.961,641091.948 5495254.452,641094.696 5495251.622,641095.178 5495251.118,641095.361 5495250.897,641095.486 5495250.65,641095.551 5495250.398,641095.563 5495250.157,641095.519 5495249.881,641095.43 5495249.647,641095.305 5495249.45,641095.093 5495249.235,641094.933 5495249.126,641094.716 5495249.028,641094.488 5495248.974,641094.23 5495248.965,641093.979 5495249.008,641093.759 5495249.092,641093.573 5495249.205,641093.412 5495249.347,641092.903 5495249.874)))'::geometry
)

Both geometries visualized in QGIS (closeup):

enter image description here

As can be seen, distance should be something around 0.45... (units = meters), but the above query returns 0.0:

enter image description here

The ST_ShortestLine returns a Linestring with conincident start- and endpoint:

enter image description here

What am I missing?

2
  • 3
    You may be asking a wrong question. Your point, if I understand it right, is inside the polygon so the distance is zero. Are you rather interested in the distance from the point to the boundary of the polygon?
    – user30184
    Commented Apr 12, 2021 at 10:59
  • 1
    Of course the boundary... I fall for this every time, very embarrassing... Commented Apr 12, 2021 at 11:20

1 Answer 1

10

If the point is inside a polygon the distance from the point to polygon is zero.

Query the distance from the point to the boundary of the polygon instead.

SELECT ST_Distance('SRID=25832;POINT(641099.6 5495250.5)'::geometry,
ST_Boundary('SRID=25832;MULTIPOLYGON(((641101.557 5495236.647,641100.718 5495237.413,641099.327 5495238.694,641091.793 5495246.089,641091.044 5495246.812,641089.631 5495248.193,641083.116 5495255.763,641089.36 5495261.908,641091.447 5495259.774,641093.686 5495257.484,641094.3 5495256.835,641095.719 5495255.336,641097.142 5495253.833,641097.465 5495253.492,641099.644 5495251.132,641103.115 5495247.237,641105.224 5495244.807,641107.339 5495242.312,641108.953 5495240.371,641110.171 5495238.884,641111.912 5495236.721,641113.55 5495234.651,641115.383 5495232.319,641117.443 5495229.57,641118.218 5495228.571,641118.547 5495228.144,641123.997 5495221.03,641130.326 5495212.262,641125.432 5495208.729,641124.225 5495210.402,641123.101 5495211.935,641122.028 5495213.377,641120.616 5495215.249,641119.484 5495216.723,641117.48 5495219.463,641115.401 5495222.252,641113.602 5495224.495,641112.073 5495226.28,641110.782 5495227.752,641109.521 5495229.118,641108.305 5495230.389,641107.432 5495231.289,641107.366 5495231.358,641102.973 5495235.355,641101.557 5495236.647),(641092.903 5495249.874,641090.137 5495252.714,641089.639 5495253.241,641089.465 5495253.438,641089.335 5495253.672,641089.262 5495253.909,641089.236 5495254.167,641089.262 5495254.409,641089.315 5495254.593,641089.437 5495254.835,641089.572 5495255.011,641089.768 5495255.178,641089.961 5495255.289,641090.18 5495255.368,641090.45 5495255.407,641090.735 5495255.385,641090.983 5495255.311,641091.219 5495255.179,641091.457 5495254.961,641091.948 5495254.452,641094.696 5495251.622,641095.178 5495251.118,641095.361 5495250.897,641095.486 5495250.65,641095.551 5495250.398,641095.563 5495250.157,641095.519 5495249.881,641095.43 5495249.647,641095.305 5495249.45,641095.093 5495249.235,641094.933 5495249.126,641094.716 5495249.028,641094.488 5495248.974,641094.23 5495248.965,641093.979 5495249.008,641093.759 5495249.092,641093.573 5495249.205,641093.412 5495249.347,641092.903 5495249.874)))'::geometry
))

Result:

0.4533208354815918

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.