Thursday, 4 September 2014

Creating a Google Earth KML file from SQL

Quite a simple post this one, but I wanted to create a Google Earth KML file from SQL so that I could plot the location of a load of customers.

The SQL for this is below :

USE blah
GO
DECLARE @kml XML
DECLARE @kmlout NVARCHAR(MAX)

SELECT @kml = CAST('<?xml version="1.0" encoding="utf-16" ?>' +
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document>' +
' <name>BasicKML.kml</name>' +
' <open>1</open>' +
(
SELECT
  BusinessName AS 'Folder/name',
  0 AS 'Folder/open',
  BusinessName AS 'Folder/Placemark/name',
    '<br/>ID: ' + CAST(H.ID AS VARCHAR) +
    '<br/>Type: ' + 'BusinessName' +
    '<br/>City: ' + City +
    '<br/>Postcode: ' + PostCode +
    '<br/>' AS 'Folder/Placemark/description',
    CAST(Longitude AS VARCHAR)+ ',' + CAST(Latitude AS VARCHAR) AS 'Folder/Placemark/Point/coordinates'
    --'' AS 'Folder/Placemark/Icon/href'
FROM
  database.schemaname.Business
ORDER BY
  BusinessName
FOR XML PATH(''), ELEMENTS) + '</Document></kml>' AS XML)

SELECT @kml

No comments:

Post a Comment

CV Driven Development

Over recent years I've watched a worrying new trend emerge which I think of as CV Driven Development. What I mean by this is that people...