Mazhar
Mazhar

Reputation: 3837

TSQL Geography Spatial Data Polygon Lat/Long Search

TSQL SQL Server 2008 r2

Is it possible to store a Polygon consisting of Latitudes and Longitudes and then perform a search within that Polygon to see whether given a given Latitude/Longitude exists within that Polygon?

If so, how does one go about this?

If not, can someone provide a suggestion as to the best way to go about this?

Example Polygon

LatLng(55.297622963050465, -4.627166781574488),
LatLng(55.25851203752759, -3.5724792815744877),
LatLng(55.034056344339206, -3.5450134612619877),
LatLng(54.955277168321636, -3.7647400237619877),
LatLng(54.92372217917785, -4.113555941730738),
LatLng(54.96946883538248, -4.561248812824488),
LatLng(55.151935308382306, -4.712310824543238);

Lat/Long to search

LatLng(55.10029008969451, -4.100017953449488),

Thanks in advance

Upvotes: 0

Views: 2418

Answers (2)

Mazhar
Mazhar

Reputation: 3837

Thanks to @Jon Bellamy I was able to come to this solution

Along with help from Ed's Spatial Blog to "fix" the Polygon Ring Orientation problem

IF OBJECT_ID ( 'dbo.SpatialTable', 'U' ) IS NOT NULL 
    DROP TABLE dbo.SpatialTable;
GO

CREATE TABLE SpatialTable 
    ( id INT IDENTITY (1,1),
    GeogCol1 GEOGRAPHY, 
    GeogCol2 AS GeogCol1.STAsText() );
GO

DECLARE @geom GEOMETRY = 'POLYGON(( 55.297622963050465 -4.627166781574488,
                                    55.25851203752759 -3.5724792815744877,
                                    55.034056344339206 -3.5450134612619877,
                                    54.955277168321636 -3.7647400237619877,
                                    54.92372217917785 -4.113555941730738,
                                    54.96946883538248 -4.561248812824488,
                                    55.151935308382306 -4.712310824543238,
                                    55.297622963050465 -4.627166781574488))';

DECLARE @geog GEOGRAPHY = @geom.MakeValid().STUnion(@geom.STStartPoint()).STAsText()

INSERT INTO SpatialTable (GeogCol1) VALUES (@geog)

DECLARE @g GEOGRAPHY 
SET @g = (SELECT GEOGRAPHY::STPolyFromText(GeogCol2, 4326) FROM dbo.SpatialTable); -- 4326 is a common SRID

DECLARE @p GEOGRAPHY
SET @p = GEOGRAPHY::STPointFromText('POINT(55.10029008969451 -4.100017953449488)', 4326); -- SRID MUST match polygon

SELECT @p.STIntersects(@g); -- 1 is true, 0 is false

IF OBJECT_ID ( 'dbo.SpatialTable', 'U' ) IS NOT NULL 
    DROP TABLE dbo.SpatialTable;
GO

Upvotes: 0

Jon Bellamy
Jon Bellamy

Reputation: 3373

In short, yes it is possible. I would suggest you start with the following MSDN link and do a little research from there:

Working with Spatial Data

Specifically, note the following methods available for use in getting your answer:

  • STWithin()
  • STIntersects()

Please note that with SQL 2008 R2, you are limited to polygons that are no bigger than a single hemisphere. If you need support for larger objects, I would consider SQL 2012 or higher.

Basic Example:

declare @g geography = Geography::STPolyFromText("<WKT Polygon Text>, 4326); -- 4326 is a common SRID
declare @p point = Geography::STPointFromText("<WKT Point Text>, 4326); -- SRID MUST match polygon

select @p.STWithin(@g); -- 1 is true, 0 is false

Upvotes: 1

Related Questions