This week I picked up this simple tip for spatial enabled applications. Just say you have an existing database that has the latitude and longitude stored as separate columns such as a Latitude and Longitude as you would like to make these columns available so that spatial operations can be performed on them.
Create a view on the table with the Lat, Long as parameters to the Geography::Point function which creates a spatial point.
Your table might look like:
The following SQL will do the trick to convert the points to spatial column:
CREATE VIEW SpatialView AS -- Create a view over a table without a spatial column SELECT Name, geography::Point(Latitude, Longitude, 4326) as Location FROM UnSpatialTable
So now you have a spatial column that you can include in your spatial queries such as, finding the distance between points:
DECLARE @location GEOGRAPHY SELECT @Location = Location from SpatialView where name = 'test' SELECT @Location.STDistance(Location) as DistanceToOther from SpatialView
Creating a buffer around a point:
SELECT Location.STBuffer(20) as BufferedLocation from SpatialView where Name = 'test'
This approach might be useful if you collecting data on a mobile device running the compact edition of SQL which doesn't have any spatial features. You can collect your data as lat,long values and then do the spatial manipulation via the view. Don't forget to think about spatial indexes!