This post covers the following methods: STArea, STLength, STDistance, STIsEmpty, STIsClosed, STIsSimple, STIsRing.
The following methods are self evident. I’ve included here for the sake of completeness. You do need to be aware of them, They are great for calculating & displaying attributes associated with your maps.
Example use: Show Population Density. ie: People per square mile. -- =====< Sample: Area example >================= DECLARE @g GEOMETRY; SET @g = GEOMETRY::STGeomFromText('MULTIPOLYGON (((5 5, 8 5, 8 8, 5 8, 5 5)), ((0 0, 3 0, 3 3, 0 3, 0 0), (0.25 0.25, 0.25 0.75, 0.75 0.75 , 0.75 0.25, 0.25 0.25), (1 1, 1 2, 2 2, 2 1, 1 1)))', 0); SELECT @g.STArea() AS [Area], @g.STAsText() AS WKT, @g AS Geo go The display of the MultiPolygon, Area = 16.75
Example use: Show Population Density. ie: People per square mile.
-- =====< Sample: Area example >================= DECLARE @g GEOMETRY; SET @g = GEOMETRY::STGeomFromText('MULTIPOLYGON (((5 5, 8 5, 8 8, 5 8, 5 5)), ((0 0, 3 0, 3 3, 0 3, 0 0), (0.25 0.25, 0.25 0.75, 0.75 0.75 , 0.75 0.25, 0.25 0.25), (1 1, 1 2, 2 2, 2 1, 1 1)))', 0); SELECT @g.STArea() AS [Area], @g.STAsText() AS WKT, @g AS Geo go
The display of the MultiPolygon, Area = 16.75
Example use: List all Points (Students) within walking distance 3km of this point (the School) As the August 08 release of Books Online states that Geography object can only calculate the distance if at least one of the objects is a point, I've included copious examples to show this is incorrect. Both Geometry & Geography can calculate the distance between any combination of spatial objects. (To be fair to the documentation team this was a limitation at one point while the product was in development. BOL will be corrected in the Dec 08 release)
Example use: List all Points (Students) within walking distance 3km of this point (the School)
As the August 08 release of Books Online states that Geography object can only calculate the distance if at least one of the objects is a point, I've included copious examples to show this is incorrect. Both Geometry & Geography can calculate the distance between any combination of spatial objects. (To be fair to the documentation team this was a limitation at one point while the product was in development. BOL will be corrected in the Dec 08 release)
-- ====< Geometry Distance function example >=============== DECLARE @g GEOMETRY; SET @g = GEOMETRY::STGeomFromText('MULTIPOLYGON( (( 5 5, 8 5, 8 8, 5 8,5 5)) ,((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2) ,(0.25 0.25, 0.75 0.25, 0.75 0.75, 0.25 0.75, 0.25 0.25)) )', 0); DECLARE @L1 AS GEOMETRY = 'LINESTRING(0 4, 8 3 )' DECLARE @P1 AS GEOMETRY = 'POINT(2 6)' -- Distance Polygon to Line SELECT 'MultiPolygon to Line' AS Comment, @g.STDistance(@L1) AS 'Shortest Distance' ,@g.STAsText()AS WKT, @g AS GEOMETRY UNION ALL -- Distance Line to Point SELECT 'Line to Point', @L1.STDistance(@P1), @L1.STAsText(), @L1 UNION ALL -- Distance Point to Polygon SELECT 'Point to Polygon', @P1.STDistance(@g), @P1.STAsText(), @P1.STBuffer(0.1) go -- ====< Geography Distance function example >=========================== -- Note: Books Online states that Geography STDistance is limited & -- can only show distance if at least one of the objects is a Point. This is false. -- As can be seen below it works with any combination of objects. -- This Error will be corrected in the next release of Books Online. DECLARE @g GEOGRAPHY; DECLARE @S1 GEOGRAPHY; DECLARE @L1 GEOGRAPHY; DECLARE @L2 GEOGRAPHY; DECLARE @P1 GEOGRAPHY; DECLARE @P2 GEOGRAPHY; --SET @g = GEOGRAPHY::STGeomFromText('MULTIPOLYGON( (( 5 5, 8 5, 8 8, 5 8,5 5)) -- ,((0 0, 3 0, 3 3, 0 3, 0 0), (2 2, 2 1, 1 1, 1 2, 2 2) -- ,(0.25 0.25, 0.25 0.75, 0.75 0.75, 0.75 0.25, 0.25 0.25)) )', 4326) -- Exterior --SET @g = GEOGRAPHY::STGeomFromText('POLYGON( (2 2, 2 1, 1 1, 1 2, 2 2) )', 4326) -- Interior (Clockwise points) --SET @g = GEOGRAPHY::STGeomFromText('POLYGON( (2 2, 1 2, 1 1, 2 1, 2 2) )', 4326) -- Exterior (AntiClockwise: "Look to the Left" of the direction you are drawing) SET @g = GEOGRAPHY::STGeomFromText('POLYGON( (0 0, 3 0, 3 3, 0 3, 0 0) ,(2 2, 2 1, 1 1, 1 2, 2 2), (0.25 0.25, 0.25 0.75 ,0.75 0.75, 0.75 0.25, 0.25 0.25))', 4326) -- Exterior SET @S1 = GEOGRAPHY::STGeomFromText('POLYGON(( 5 5, 8 5, 8 8, 5 8,5 5))', 4326) -- Exterior SET @L1 = GEOGRAPHY::STGeomFromText('LINESTRING(0 4, 8 3 )', 4326); SET @L2 = GEOGRAPHY::STGeomFromText('LINESTRING(0 6, 2 6 )', 4326); SET @P1 = GEOGRAPHY::STGeomFromText('POINT(2 6)', 4326); SET @P2 = GEOGRAPHY::STGeomFromText('POINT(1 7)', 4326); -- Distance Polygon to Line SELECT 'MultiPolygon to Line' as Comment, @g.STDistance(@L1) as 'Shortest Distance', @g.STAsText()as WKT, @g as Geo UNION ALL -- Distance Line to Point SELECT 'Line to Point', @L1.STDistance(@P1), @L1.STAsText(), @L1 UNION ALL --Distance Point to Polygon SELECT 'Point to Polygon', @P1.STDistance(@g), @P1.STAsText(), @P1.STBuffer(20000) UNION ALL --Distance Polygon to Polygon SELECT 'Polygon to Polygon', @S1.STDistance(@g), @S1.STAsText(), @S1 UNION ALL --Distance Point to Point SELECT 'Point to Point', @P2.STDistance(@P1), @P2.STAsText(), @P2.STBuffer(20000) UNION ALL --Distance Line to Line SELECT 'Line to Line', @L2.STDistance(@L1), @L2.STAsText(), @L2 go
-- ====< Geometry Distance function example >=============== DECLARE @g GEOMETRY; SET @g = GEOMETRY::STGeomFromText('MULTIPOLYGON( (( 5 5, 8 5, 8 8, 5 8,5 5)) ,((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2) ,(0.25 0.25, 0.75 0.25, 0.75 0.75, 0.25 0.75, 0.25 0.25)) )', 0); DECLARE @L1 AS GEOMETRY = 'LINESTRING(0 4, 8 3 )' DECLARE @P1 AS GEOMETRY = 'POINT(2 6)' -- Distance Polygon to Line SELECT 'MultiPolygon to Line' AS Comment, @g.STDistance(@L1) AS 'Shortest Distance' ,@g.STAsText()AS WKT, @g AS GEOMETRY UNION ALL -- Distance Line to Point SELECT 'Line to Point', @L1.STDistance(@P1), @L1.STAsText(), @L1 UNION ALL -- Distance Point to Polygon SELECT 'Point to Polygon', @P1.STDistance(@g), @P1.STAsText(), @P1.STBuffer(0.1) go
-- ====< Geography Distance function example >=========================== -- Note: Books Online states that Geography STDistance is limited & -- can only show distance if at least one of the objects is a Point. This is false. -- As can be seen below it works with any combination of objects. -- This Error will be corrected in the next release of Books Online. DECLARE @g GEOGRAPHY; DECLARE @S1 GEOGRAPHY; DECLARE @L1 GEOGRAPHY; DECLARE @L2 GEOGRAPHY; DECLARE @P1 GEOGRAPHY; DECLARE @P2 GEOGRAPHY; --SET @g = GEOGRAPHY::STGeomFromText('MULTIPOLYGON( (( 5 5, 8 5, 8 8, 5 8,5 5)) -- ,((0 0, 3 0, 3 3, 0 3, 0 0), (2 2, 2 1, 1 1, 1 2, 2 2) -- ,(0.25 0.25, 0.25 0.75, 0.75 0.75, 0.75 0.25, 0.25 0.25)) )', 4326) -- Exterior --SET @g = GEOGRAPHY::STGeomFromText('POLYGON( (2 2, 2 1, 1 1, 1 2, 2 2) )', 4326) -- Interior (Clockwise points) --SET @g = GEOGRAPHY::STGeomFromText('POLYGON( (2 2, 1 2, 1 1, 2 1, 2 2) )', 4326) -- Exterior (AntiClockwise: "Look to the Left" of the direction you are drawing) SET @g = GEOGRAPHY::STGeomFromText('POLYGON( (0 0, 3 0, 3 3, 0 3, 0 0) ,(2 2, 2 1, 1 1, 1 2, 2 2), (0.25 0.25, 0.25 0.75 ,0.75 0.75, 0.75 0.25, 0.25 0.25))', 4326) -- Exterior SET @S1 = GEOGRAPHY::STGeomFromText('POLYGON(( 5 5, 8 5, 8 8, 5 8,5 5))', 4326) -- Exterior SET @L1 = GEOGRAPHY::STGeomFromText('LINESTRING(0 4, 8 3 )', 4326); SET @L2 = GEOGRAPHY::STGeomFromText('LINESTRING(0 6, 2 6 )', 4326); SET @P1 = GEOGRAPHY::STGeomFromText('POINT(2 6)', 4326); SET @P2 = GEOGRAPHY::STGeomFromText('POINT(1 7)', 4326); -- Distance Polygon to Line SELECT 'MultiPolygon to Line' as Comment, @g.STDistance(@L1) as 'Shortest Distance', @g.STAsText()as WKT, @g as Geo UNION ALL -- Distance Line to Point SELECT 'Line to Point', @L1.STDistance(@P1), @L1.STAsText(), @L1 UNION ALL --Distance Point to Polygon SELECT 'Point to Polygon', @P1.STDistance(@g), @P1.STAsText(), @P1.STBuffer(20000) UNION ALL --Distance Polygon to Polygon SELECT 'Polygon to Polygon', @S1.STDistance(@g), @S1.STAsText(), @S1 UNION ALL --Distance Point to Point SELECT 'Point to Point', @P2.STDistance(@P1), @P2.STAsText(), @P2.STBuffer(20000) UNION ALL --Distance Line to Line SELECT 'Line to Line', @L2.STDistance(@L1), @L2.STAsText(), @L2 go
-- < Empty means no points, different to NULL >== DECLARE @temp TABLE (Shape VARCHAR(10), [geom] GEOMETRY); INSERT INTO @temp VALUES ('Nothing', NULL) ,('Im Empty', 'POLYGON EMPTY') ,('Point', 'POINT(3 3)') ,('LineString', 'LINESTRING(0 0, 3 3)') ,('Polygon', 'POLYGON((0 0, 3 0, 0 3, 0 0))'); SELECT Shape, [geom].STDimension() as [STDimension] FROM @temp WHERE geom.STIsEmpty() = 1;
For more info see SQL Server 2008 Books Online Geometry Methods Supported by Spatial Indexes
Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.