Finding towns within a distance radius from a database

Advert test
#1
Hi everyone,

I have been asked to write a script that will find towns within a radius of 10 miles (for instance) in a database.
I have a database of towns with their corresponding latitude and longitude and I am fine working with the database, SQl etc but I have no idea how to work the coordinates.
Does any one know how to calcuate distances using coordinates?
 
#3
Hi,

I wrote a method in a php class a few years back for calculating the distance between UK postcodes which worked by calculating the distance between grid reference points (northings and eastings), which I had stored in a database.

With a bit of tweaking you could use this function to do what you need. However, you'll have to find someway of converting lat/long co-ordinates to grid references, you're on your own here so I advise you search google code as it may be done already.

It's funny, this brings me back to my school days when we were doing trigonometry in Maths, and I said to my teacher: "Why would I want to know where a tree is going to fall when its cut, I've no interest in becoming a lumberjack!".

So low and behold I got my first lesson in Latin maths, enter Pythagoras with his 'theorem' and the famous 'hypotenuse'!

Anyway, sorry for going off in a tangent, it just instantly made me think of that day in school!

PHP:
function calcDistance($easting1,$northing1,$easting2,$northing2)
{

        // Calculate the northing and easting distance
        $northingDistance=$northing1-$northing2;
        $eastingDistance=$easting1-$easting2;
        
        // Calculate the distance between the two points (Pythagoras' Theorem)
        $hypotenuse=sqrt(($northingDistance*$northingDistance)+($eastingDistance*$eastingDistance));
        
        $kms = round($hypotenuse/1000,2);
        $miles = $kms * .62;
        
        return $miles;

}
 
#4
Hi oliflorence,

I'm working on some regional analysis and need to create a database of irish towns and their latitude and longitude.

Would you mind sharing the database you have?

many thanks,
 
#5
Hi There,
I am afraid the project did not materialise for me so at the end i did not build the database, thus I might need similar shortly.

Sorry i can't assist further.
 
Top