Friday, August 12, 2011

Convert Lat/Lon into a Geolocation SQL Server 2008(Example)

To convert the latitude and longitude coordinates to a geography point, we will use the StPointFromText function. 
The STPointFromText returns a geography instance from an Open Geospatial Consortium (OGC) Well-Known Text (WKT) representation.

CREATE TABLE  Locations(lat float ,lon float,[GeoLocation] GEOGRAPHY)

INSERT INTO Locations(lat,lon)values
((34.1433454039943,-118.408653442307),34.2228933701786,-119.152547489247)

UPDATE Locations set [GeoLocation] = geography::STPointFromText('POINT(' + CAST([Lon] AS VARCHAR(20)) + ' ' + CAST([Lat] AS VARCHAR(20)) + ')', 4326)

No comments:

Post a Comment