Reputation: 1
I have a polygon surrounding the country of Luxemburg (Europe). I have two points:
The first point should be in the polygon, the second one should not. Although I create the polygon counter-clockwise, Contains
returns true (1) for both points.
Code:
declare @points table (locationcode nvarchar(max), latitude real, longitude float, info nvarchar(max), ord int)
declare @locationcode nvarchar(max) = 'LUX'
insert into @points values
('LUX', 50.264980, 6.650216, 'right top', 1),
('LUX', 50.294766, 5.345107, 'left top', 2),
('LUX', 49.307025, 5.430560, 'left bottom', 3),
('LUX', 49.322218, 6.657984, 'right bottom', 4),
('LUX', 50.264980, 6.650216, 'right top', 5)
declare @buildstring nvarchar(max)
select @buildstring = coalesce(@buildstring + ', ','') + convert(nvarchar,latitude) + ' ' + convert(nvarchar,longitude) from @points where locationcode = @locationcode order by ord
declare @polygon geography = 'POLYGON((' + @buildstring + '))'
print 'polygon:'
print @polygon.ToString()
print @polygon.STIsValid()
-- point in polygon coordinates:
declare @pointInLatitude float
declare @pointInLongitude float
declare @pointIn geography
-- point out of polygon coordinates:
declare @pointOutLatitude float
declare @pointOutLongitude float
declare @pointOut geography
-- +/- center Luxenbourg city (Luxembourg):
set @pointInLatitude = 49.609133
set @pointInLongitude = 6.136210
set @pointIn = geography::Point(@pointInLatitude, @pointInLongitude, 4326)
-- +/- center Brussels city: (Belgium)
set @pointOutLatitude = 50.847726
set @pointOutLongitude = 4.349612
set @pointOut = geography::Point(@pointOutLatitude, @pointOutLongitude, 4326)
select @polygon.STContains(@pointIn) , @polygon.STContains(@pointOut)
Upvotes: 0
Views: 68