views:

318

answers:

1

I am looking to calculate the shortest distance between two points inside SQL Server 2008 taking into account land mass only.

I have used the geography data type along with STDistance() to work out point x distance to point y as the crow flies, however this sometimes crosses the sea which i am trying to avoid.

I have also created a polygon around the land mass boundary I am interested in.

I believe that I need to combine these two methods to ensure that STDistance always remains within polygon - unless there is a simpler solution.

Thanks for any advice

+1  A: 

Use STIntersects - http://msdn.microsoft.com/en-us/library/bb933899%28v=SQL.105%29.aspx to find out what part of the line is over land.

After reading your comment your requirement makes sense. However I'm pretty sure there are no inbuilt techniques to do this in SQL Server. I'm assuming you are ignoring roads, and taking an as-the-crow-flies approach but over land only.

The only way I can think to do this would be to convert your area into a raster (grid cells) and perform a cost path analysis. You would set the area of sea to have a prohibitively high cost so the algorithm would route around the sea. See this link for description of technique:

http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=cost_path

Otherwise try implementing the algorithm below!

http://bit.ly/ckvciz

There may be other libraries that do this. Alteratively how about using the new Google Directions API between the two cities - you'd get actual road distances then.

http://code.google.com/apis/maps/documentation/directions/

geographika
Thanks for the reply. Unless I misunderstand that will not give me the shortest distance within the land mass, it would merely allow me to differentiate between the distance across land with the distance across sea.Maybe a worked example will help me to explain. As the crow flies, Malaga (Spain) is closer to Tanger (Morocco) than Madrid. If we can only travel by land, Tanger is still accessible (after a long journey Europe, Middle East etc) however it is now a lot further away than Madrid, Paris etc. Therefore, I’m trying to calculate the distances across land mass only.
dpwb
SQL Server doesn't differentiate between land or sea (or hills, for that matter).
Brent Ozar
I'd assume the polygons would be attributed correctly?
geographika