Home Create geography polyline from points in T-SQL
Reply: 1

Create geography polyline from points in T-SQL

g2server
1#
g2server Published in 2014-06-17 09:15:25Z

I have a table schema that looks like this:

CREATE TABLE [dbo].[LongAndLats](
[Longitude] [decimal](9, 6) NULL,
[Latitude] [decimal](9, 6) NULL,
[SortOrder] [int] NULL
)

Sample data looks like this:

How can I convert these points into a geography polyline using TSQL?

g2server
2#
g2server Reply to 2014-06-17 09:15:25Z

try this: (note: the ordering of the points is important for the line to be generated correctly.)

DECLARE @BuildString NVARCHAR(MAX)
SELECT @BuildString = COALESCE(@BuildString + ',', '') + CAST([Longitude] AS NVARCHAR(50)) + ' ' + CAST([Latitude] AS NVARCHAR(50))
FROM dbo.LongAndLats
ORDER BY SortOrder             

SET @BuildString = 'LINESTRING(' + @BuildString + ')';   
DECLARE @LineFromPoints geography = geography::STLineFromText(@BuildString, 4326);
SELECT @LineFromPoints
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.38732 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO