Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 Spatial: find a point in polygon

I am using SQL Server 2008 spatial data types. I have a table with all States (as polygons) as data type GEOMETRY. Now I want to check if a point's coordinates (latitudes, longitudes) as data type GEOGRAPHY, is inside that State or not.

I could not find any example using the new spatial data types. Currently, I have a workaround which was implemented many years ago, but it has some drawbacks.

I've both SQL Server 2008 and 2012. If the new version has some enhancements, I can start working in it too.

Thanks.

UPDATE 1:

I am adding a code sample for a bit more clarity.

declare @s geometry  --GeomCol is of this type too. declare @z geography --GeogCol is of this type too.  select @s = GeomCol from AllStates where STATE_ABBR = 'NY'  select @z = GeogCol from AllZipCodes where ZipCode = 10101 
like image 320
Farhan Avatar asked Jun 15 '12 15:06

Farhan


People also ask

How do you check if a point is in a polygon SQL?

To test whether a point and a polygon have any intersection at all, use STIntersects(). To test whether a point is wholly contained inside a polygon, use STContains(), and to test whether it lies on the boundary use STBoundary().

What is geography :: Point in SQL?

GEOGRAPHY::Point(Latitude, Longitude, SRID) SRID stands for Spatial Reference Identifier. The most common SRID is 4326, which has the information in meters. For other type of SRID's run this query in SQLServer: select * from sys.spatial_reference_systems.

What is spatial results in SQL Server?

The Spatial results window provides visual mapping tools for viewing spatial data. To view spatial results, your query results must include a spatial column with either geometry or geography data. The Spatial results window is only available if your results are returned to a grid in the Results window.

What is difference between geography and geometry in SQL Server?

Geometry : Stores data based on a flat (Euclidean) coordinate system. The data type is often used to store the X and Y coordinates that represent lines, points, and polygons in two-dimensional spaces. Geography : Stores data based on a round-earth coordinate system.


2 Answers

I think the geography method STIntersects() will do what you want:

DECLARE @g geography; DECLARE @h geography; SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326); SET @h = geography::Point(47.653, -122.358, 4326)  SELECT @g.STIntersects(@h) 
like image 91
Ben Thul Avatar answered Sep 24 '22 08:09

Ben Thul


If you cannot change the data-type for the stored polygons to GEOGRAPHY then you can convert the input latitude and longitude to GEOMETRY and use the STContains or STIntersects against the converted value.

DECLARE @PointGeography GEOGRAPHY = geography::Point(43.365267, -80.971974, 4326) DECLARE @PointGeometry GEOMETRY = geometry::STGeomFromWKB(@PointGeography.STAsBinary(), 4326);  SELECT @PolygonGeometry.STContains(@PointGeometry); 

Going the opposite direction -- trying to convert the GEOMETRY polygons to GEOGRPAHY -- is error-prone and likely to fail from my experience.

And note that if you try to create the GEOMETRY point directly from the latitude and longitude values then the STContains (or STIntersects) won't work (i.e. won't give a match when they should).

like image 40
Christopher King Avatar answered Sep 23 '22 08:09

Christopher King