views:

131

answers:

5

I'm currently trying to reverse geocode a series of lat/long co-ordinates using the Virtual Earth/Bing Maps web services. Whilst I can successfully retrieve an address for the positions I also need to be able to retrieve the closest significant population centre for the position so I can display a heading and distance to the centre of the nearest town/city/metropolis etc. This is for cases where the location is travelling between locations e.g. on a highway/motorway.

Has anyone out there got any ideas how to do this as I've been banging my head against it for a few days now and I've gotten nowhere!

Cheers in advance...

A: 

If you can find a list of coordinates for major population centers, here is some code for computing great circle distances based on coordinates.

John D. Cook
@John D. Cook - thank you for posting that, I have something similar to calculate the bearing and distance between two locations - "all" I need now is the other location :)
Stephen Newman
A: 

It sounds like you're looking for a database of latitudes and longitudes of major cities, so you can calculate distances.

This is a link to a page giving a few dozen, world-wide.

There may be others, most likely US-centric (but that may be what you want).

pavium
@Pavium - I have thought about sourcing a list of lat/longs for population centres and performing a lookup against that but I'd prefer not to have to maintain that list if I can get away with it.
Stephen Newman
Yes, it would be a *big* list to maintain. There's plenty of the 'Google Maps' type of thing. You know, of course, about http://overstated.net/projects/lat_lon/ I hope you find something more 'programmatically' useful.
pavium
+2  A: 

try using the wikipedia location service, documented here http://www.geonames.org/export/wikipedia-webservice.html

Lorenzo Boccaccia
A: 

I think it is safe to assume that the nearest city is always quite close compared with the size of the Earth, so you can use a simple pythagoras triangle.

Suppose you are at (lat0, long0) and a trial city is at (lat1, long1).

Horizontal (EW) distance is roughly

d_ew = (long1 - long0) * cos(lat0)

This is multiplied by cos(lat0) to account for longitude lines getting closer together at high latitude.

Vertical (NS) distance is easier

d_ns = (lat1 - lat0)

So the distance between the two points is

d = sqrt(d_ew * d_ew + d_ns * d_ns)

You can refine this method for more exacting tasks, but this should be good enough for the nearest city.

For comparing distances, it will be fine to compare d squared, which means you can omit the sqrt operation.

Ewan Todd
A: 

I would do the following:

table 1:

T_CityPopulation

Fields:

CityTownInfo,Population,LonLat

Then compute distance between your current LonLat for each record in table 1, using a threshold value ignore towns/citys over x miles. Then sort the results by Population.

EDIT:

Even if you don't want to maintain a table, it has to be stored somewhere, I think if you maintained it yourself at least you have control over it, vs relying on another service.

Darknight
I agree introducing another service would also bring another point of failure, but I was hoping that there was a method to get this information directly from the virtual earth web services, which I'm calling anyway - ideally a call that would perform the reverse geocode and include with the result the closest significant population centre.
Stephen Newman