Search code examples
sql-servert-sqlspatialsqlgeography

finding sql geography point within rectangular (polygon)


I have an interesting/annoying issue with finding lat and long of land marks inside the rectangular boundary. I believe my two points are inside my rectangular boundary. but as you can test yourself the result of the the first select is false instead of true!

DECLARE @boundingRect varchar(1000)
DECLARE @maxLat VARCHAR(20)
DECLARE @minLong VARCHAR(20)
DECLARE @minLat VARCHAR(20)
DECLARE @maxLong VARCHAR(20)


set @maxLat ='-36.06631759541187'
set @minLong ='125.23310677812492'
set @minLat ='-44.43329881450396'
set @maxLong='167.04707162187492'


SET @boundingRect = 'POLYGON((' +   @minLong + ' '  + @minLat + ', ' +
                                        @maxLong + ' ' + @minLat + ', ' + 
                                        @maxLong + ' ' + @maxLat + ', ' + 
                                        @minLong + ' ' + @maxLat + ', ' + 
                                        @minLong + ' ' + @minLat + '))'

DECLARE @Bounds AS Geography =GEOGRAPHY::STPolyFromText(@boundingRect,4326)

DECLARE @point1 AS GEOGRAPHY = GEOGRAPHY::Point(-37.81502, 144.94601, 4326)
DECLARE @point2 AS GEOGRAPHY = GEOGRAPHY::Point(-38.81502, 144.94601, 4326)


SELECT @Bounds.STIntersects(@point1)
SELECT @Bounds.STIntersects(@point2) 

enter image description here To give you background, I have list of land marks (lat,long) that I want to load on google maps. Since the number of landmarks are too many, I cannot return all of them at once. I need to return the landmarks that are in the areas that are visible to user , in their viewing boundary. I'm getting north west (max lat,min long) and south east (min lat, max long) of google maps boundary and sending it to my stored procedure to return back the list of the land marks within that boundary. However, as I explained above I have issues and some land marks are missing in list.


Solution

  • Actually, geography polygon isn't rectangle:

    enter image description here

    If you want rectangle, you can use geometry polygon:

    enter image description here