Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
JamesTT
Starting Member
2 Posts |
Posted - 2007-03-13 : 18:41:27
|
I have a database that holds a postcode/zipcode the town and then the longitude and latitude, what I wanted to know is it possible to write a query to say If I wanted to get a list of postcodes that were in a radius of 4 miles or kilometers from say Postcode 4612 or the town of Abbeywood. Some thing tells me I will need to get my old trigonometry maths books out but if any one could give me some pointers that would be great.4612 Abbeywood 151.61991 -25.99742176 Abbotsbury 150.86645 -33.872452046 Abbotsford 151.12888 -33.84981 |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-03-13 : 19:44:40
|
if a bounding box was good enough (min/max lat/long), that would be trivial to implement in sql.a true radius is more complicated. www.elsasoft.org |
|
|
JamesTT
Starting Member
2 Posts |
Posted - 2007-03-13 : 19:50:43
|
Hi jezemine thank you for the reply I will go back and find out if a bounding box would be good enough but if not would I have to create a another table holding holding a list of all the post codes and there distrances between one another as I can see that would create a massive table. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 01:54:41
|
If not correcting for Earth spherical (which only makes a difference of 0.5-1-5 % anyway) , use pythagoras theoremSQRT(POWER(Lat1 - Lat2, 2) + Power(Long1 - Long2, 2)) -- This the distance in coordinates. Just multiply with some constant to get real distance in either mile or km.gives the "real" distance to the other coordinates. But since you almost certainly want the nearest, get rid of the SQRT part (saves a lot a calculations) and use onlyPOWER(Lat1 - Lat2, 2) + Power(Long1 - Long2, 2)to get the shortest "pseudo-distance".Peter LarssonHelsingborg, Sweden |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-03-14 : 04:58:59
|
here's what we do:1) Find the Min/Max Lat/Long of a bounding box. Select rows "within" that first. Use an index for Lat/Long. That will be quick!2) For rows that are within the bounding box then use a Great Circle algorithm, or similar, to filter the bounding "Box" to a "Circle".Assuming you are looking for proximity of Miles, rather than fractions of an inch!, sacrifice trigonometric accuracy for speed.See also:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=40295 - Great Circle Formulahttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=57242 - Accuracy v. SpeedKristen |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-03-14 : 07:26:16
|
This function computes the great circle distance in Kilometers using the Haversine formula distance calculation.If you want it in miles, change the average radius of Earth to miles in the function.create function dbo.F_GREAT_CIRCLE_DISTANCE ( @Latitude1 float, @Logitude1 float, @Latitude2 float, @Logitude2 float )returns floatas/*fUNCTION: F_GREAT_CIRCLE_DISTANCE Computes the Great Circle distance in kilometers between two points on the Earth using the Haversine formula distance calculation.Input Parameters: @Logitude1 - Logitude in degrees of point 1 @Latitude1 - Latitude in degrees of point 1 @Logitude2 - Logitude in degrees of point 2 @Latitude2 - Latitude in degrees of point 2*/begindeclare @radius floatdeclare @lon1 floatdeclare @lon2 floatdeclare @lat1 floatdeclare @lat2 floatdeclare @a floatdeclare @distance float-- Sets average radius of Earth in Kilometersset @radius = 6371.0E-- Convert degrees to radiansset @lon1 = radians( @Logitude1 )set @lon2 = radians( @Logitude2 )set @lat1 = radians( @Latitude1 )set @lat2 = radians( @Latitude2 )set @a = sqrt(square(sin((@lat2-@lat1)/2.0E)) + (cos(@lat1) * cos(@lat2) * square(sin((@lon2-@lon1)/2.0E))) )set @distance = @radius * ( 2.0E *asin(case when 1.0E < @a then 1.0E else @a end ))return @distanceend CODO ERGO SUM |
|
|
codezilla94
Starting Member
6 Posts |
Posted - 2007-11-12 : 20:09:25
|
(spam removed -graz) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|