-
-
Save hsavran/585c585bc5e96e8bc29cd8d0f5fba1e5 to your computer and use it in GitHub Desktop.
Turns SQL Server Geography to GeoJSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION GetGeoJSON (@geo geography) | |
RETURNS varchar(max) | |
WITH SCHEMABINDING | |
/* | |
* Reference: http://stackoverflow.com/questions/6506720/reformat-sqlgeography-polygons-to-json | |
*/ | |
AS | |
BEGIN | |
DECLARE @Result varchar(max) | |
SELECT @Result = '{' + | |
CASE @geo.STGeometryType() | |
WHEN 'POINT' THEN | |
'"type": "Point","coordinates":' + | |
REPLACE(REPLACE(REPLACE(REPLACE(@geo.ToString(),'POINT ',''),'(','['),')',']'),' ',',') | |
WHEN 'POLYGON' THEN | |
'"type": "Polygon","coordinates":' + | |
'[' + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@geo.ToString(),'POLYGON ',''),'(','['),')',']'),'], ',']],['),', ','],['),' ',',') + ']' | |
WHEN 'MULTIPOLYGON' THEN | |
'"type": "MultiPolygon","coordinates":' + | |
'[' + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@geo.ToString(),'MULTIPOLYGON ',''),'(','['),')',']'),'], ',']],['),', ','],['),' ',',') + ']' | |
ELSE NULL | |
END | |
+'}' | |
RETURN @Result | |
END | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment