1

I'm trying to determin whether a point is within a polygon. I created a polygon a two points with this tool: https://clydedacruz.github.io/openstreetmap-wkt-playground/

Polygon:

POLYGON((18.25103759765625 42.682771889291274,18.267517089843754 42.715741189305874,18.229064941406254 42.737262677320984,18.184204157441854 42.755415642264865,18.193359375 42.778267389968846,18.2647705078125 42.79170562395723,18.327941894531254 42.7876744539042,18.3746337890625 42.7735632912314,18.410339355468754 42.74062471803208,18.304138295352463 42.758104538811835,18.36090087890625 42.724485161605344,18.44879150390625 42.712377738077464,18.436889704316854 42.68613688294755,18.372802846133713 42.704977614599755,18.39202892035246 42.66392442101616,18.34533702582121 42.67200259850259,18.318786676973104 42.69757660890005,18.32244873046875 42.645744683373806,18.27850341796875 42.63025406904714,18.29040538519621 42.6672904969717,18.25103759765625 42.682771889291274))

First point:

POINT(18.241882380098104 42.964127551339516)

Second point:

POINT(17.793273953720927 43.26887292145395)

You can clearly see that non of the points lay within the polygon.

But if I try to run this query:

DECLARE @polygon geography;
SET @polygon = geography::STGeomFromText(
'POLYGON((18.25103759765625 42.682771889291274,18.267517089843754 42.715741189305874,18.229064941406254 42.737262677320984,18.184204157441854 42.755415642264865,18.193359375 42.778267389968846,18.2647705078125 42.79170562395723,18.327941894531254 42.7876744539042,18.3746337890625 42.7735632912314,18.410339355468754 42.74062471803208,18.304138295352463 42.758104538811835,18.36090087890625 42.724485161605344,18.44879150390625 42.712377738077464,18.436889704316854 42.68613688294755,18.372802846133713 42.704977614599755,18.39202892035246 42.66392442101616,18.34533702582121 42.67200259850259,18.318786676973104 42.69757660890005,18.32244873046875 42.645744683373806,18.27850341796875 42.63025406904714,18.29040538519621 42.6672904969717,18.25103759765625 42.682771889291274))'
, 4326);
DECLARE @pointIn geography;
SET @pointIn = geography::STGeomFromText ('POINT(18.241882380098104 42.964127551339516)' , 4326 );

DECLARE @pointOut geography;
SET @pointOut = geography::STGeomFromText ( 'POINT(17.793273953720927 43.26887292145395)', 4326 );

SELECT @polygon.STContains( @pointOut) AS 'STContains', 
    @polygon.STIntersects( @pointOut) AS 'STIntersects', 
       @pointOut.STWithin( @polygon ) AS 'STWithin';

SELECT @polygon.STContains( @pointIn) AS 'STContains', 
    @polygon.STIntersects( @pointIn) AS 'STIntersects', 
       @pointIn.STWithin( @polygon ) AS 'STWithin';

I get results for both point to be true (1) for STContains, STIntersects and STWithin.

STContains  STIntersects    STWithin
1   1   1

What am I doing wrong here?

1 Answer 1

2

Your polygon seems to wind clockwise. For geography type in MSSQL Server that means that the polygon is filling the whole world except the area what you think it fills.

enter image description here

Turn the polygon around with ReorientObject https://docs.microsoft.com/en-us/sql/t-sql/spatial-geography/reorientobject-geography-data-type?view=sql-server-ver15

0

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.