# PostGIS point distance from polygon not 0 when on edge

I have a pretty simple problem:

SELECT * FROM ST_Distance(ST_GeographyFromText('POLYGON((70 -40,70 -39,71 -39,71 -40,70 -40))'), ST_GeographyFromText('POINT(70.48 -39)'), False)

returns 118.45656881 Shouldn't it be 0? Isn't the point on the edge of the polygon? Why does

SELECT * FROM ST_Distance(ST_GeographyFromText('POLYGON((70 -40,70 -39,71 -39,71 -40,70 -40))'), ST_GeographyFromText('POINT(70.48 -40)'), False)

return 0?

SELECT * FROM ST_Distance(ST_GeographyFromText('POINT(70 -39.5)'), ST_GeographyFromText('POINT(70 -39)'));
SELECT * FROM ST_Distance(ST_GeographyFromText('POINT(70 -39.5)'), ST_GeographyFromText('POINT(70 -40)'));

Why does this return two different (although only slightly) numbers?

• Is this the same distance from the point to the polygon's centroid? – Paul H Dec 15 '15 at 22:05
• Is what the distance from the point to the centroid? the distance returned? I don't really understand your question. SELECT * FROM ST_Distance(ST_GeographyFromText('POINT(70.5 -39.5)'), ST_GeographyFromText('POINT(70.48 -39)'), False); SELECT * FROM ST_Distance(ST_GeographyFromText('POINT(70.5 -39.5)'), ST_GeographyFromText('POINT(70.48 -40)'), False); ; Return different values – prawdziwy-sok Dec 15 '15 at 22:18
• My question is: what is the distance from the point to the centroid of the polygon and is that the same value as what you're seeing? If the answer to the second question is "yes" then we know a little more about how the ST_Distance function works. – Paul H Dec 15 '15 at 22:21
• Make sure to use the edit button beneath your question to revise it with any requested clarifications. Comments are temporary. – PolyGeo Dec 15 '15 at 22:30

The video explains it very well. If you take the edge in question and segmentize it

SELECT st_asewkt( st_segmentize(geog,10000)) FROM ST_GeographyFromText('LINESTRING (70 -39,71 -39)') geog

you going to have the following :

the point is the the point you are asking about. The line represents the edge. North is up.

(distance measurement done by hand,no snapping)

As for the 2nd query that returns 0? It does because hte point is actually inside your polygon.

To be more clear the intersection between the two geometries is empty because they simply don't overlap.

select st_asewkt(st_intersection(ST_GeographyFromText('POLYGON((70 -40,70 -39,71 -39,71 -40,70 -40))'), ST_GeographyFromText('POINT(70.48 -39)')))

st_asewkt
--------------
SRID=4326;GEOMETRYCOLLECTION EMPTY
• Thank you - my mistake was thinking that polygon edges are arcs on the surface of the spheroid, not straight lines going actually through the sphere. – prawdziwy-sok Dec 16 '15 at 9:03
• If you have polygon as geography, the shortest path from one corner to another is an arc on the surface. Your polygon edges do not go underground. Your real mistake is to think that the shortest arc from POINT (70 -39) to POINT (71 -39) will follow the -39 latitude. – user30184 Dec 16 '15 at 16:30
• This is the WKT from the ST_Segmentize query: LINESTRING ( 70 -39, 70.1111072111057 -39.00042152460096, 70.22221734714948 -39.00073767723941, 70.33332957252897 -39.00094845004032, 70.44444305154765 -39.0010538377534, 70.55555694845235 -39.0010538377534, 70.66667042747102 -39.00094845004032, 70.77778265285052 -39.0007376772394, 70.88889278889428 -39.000421524600945, 71 -39 ) As you see, only the start and end points have latitude -39. – user30184 Dec 16 '15 at 16:41