Skip to content

Instantly share code, notes, and snippets.

@fxcosta
Created February 26, 2020 00:02
Show Gist options
  • Save fxcosta/666d391c2ccb85165d478928a0c6aef8 to your computer and use it in GitHub Desktop.
Save fxcosta/666d391c2ccb85165d478928a0c6aef8 to your computer and use it in GitHub Desktop.
SQL Server Spatial data Intersects function: check if a specific lat and long is within a coordinate bounds (like from google maps)
DECLARE @boundingRect varchar(1000)
DECLARE @maxLat VARCHAR(20)
DECLARE @minLong VARCHAR(20)
DECLARE @minLat VARCHAR(20)
DECLARE @maxLong VARCHAR(20)
set @maxLat ='-14.862246900491535'
set @minLong ='-40.843608842004876'
set @minLat ='-14.861471748622773'
set @maxLong='-40.84465222131167'
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(-14.8618379050855, -40.8441956036087, 4326)
DECLARE @point2 AS GEOGRAPHY = GEOGRAPHY::Point(-38.81502, 144.94601, 4326)
SELECT @Bounds.STIntersects(@point1)
SELECT @Bounds.STIntersects(@point2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment