I am trying to get all the points inside the polygon and after a few tests I finally caught.
STIntersects
always return 1 for all my entities even in cases when a point "visually" not in a polygon.
I have found posts about polygon creation direction and try it but it didnt't help me :)
So I've decided to create simple case:
- polygon with 4 vertexes somewhere in Russia (corners: 55 37, 56 38)
- one point inside this polygon
- and another one outside
here is a link to google map with points
and results of this test knocked me out of a rut
declare @cw geography, @ccw geography, @pointIn geography, @pointOut geography
-- counterclockwise direction from bottom left corner
set @ccw = geography::STPolyFromText (
'POLYGON((
55.0 37.0
,55.0 38.0
,56.0 38.0
,56.0 37.0
,55.0 37.0
))', 4326
)
-- clockwise direction from bottom left corner
set @cw = geography::STPolyFromText (
'POLYGON((
55.0 37.0
,56.0 37.0
,56.0 38.0
,55.0 38.0
,55.0 37.0
))', 4326
)
set @pointIn = geography::Point(55.5, 37.5, 4326)
set @pointOut = geography::Point(54, 36, 4326)
select @pointIn.STIntersects(@ccw) ccw, @pointIn.STIntersects(@cw) cw
-- result: 1 0
--here i should get inversed values, but it didnt happens
select @pointOut.STIntersects(@ccw) ccw, @pointOut.STIntersects(@cw) cw
-- result: 1 0
Why does this happen? I just can't understand what I am missing
I expect that the pointIn
should return 1 when my polygon is small and return 0 when my polygon is whole world minus selected area, and pointOut
should return 0 in first case and return 1 in the second one
But both points return 1 in counterclockwise polygon.
UPDATE
finaly
My error was in the order of the input parameters of geography::STPolyFromText
first should be lng and the second lat.
and in extension the order is diferent
msdn says: Point ( Lat, Long, SRID )