mark
mark

Reputation: 62736

How to check whether a geopoint belongs to a certain city using some web services?

Given:

Question: determine whether the given geopoint belongs to the city.

My current solution is trivial:

  1. Do reverse geocoding (I am using the python googlemaps package). This yields the results dictionary.
  2. Examine results['Placemark'][0]['AddressDetails'] looking for the LocalityName keyword.
  3. The given geopoint is deemed inside the city, if the found LocalityName equals the name of the city.

So far so good. Even if it is not the best of algorithms, it is surely the simplest. However, there are problems. Take for instance:

The GoogleMaps.reverse_geocode(57.14369, -2.22314) yields the following result:

{ u'Placemark': [ { u'AddressDetails': { u'Accuracy': 6,
                                         u'Country': { u'CountryName': u'UK',
                                                       u'CountryNameCode': u'GB',
                                                       u'Thoroughfare': { u'ThoroughfareName': u'A944'}}},
                    u'ExtendedData': { u'LatLonBox': { u'east': -2.2200414,
                                                       u'north': 57.1480762,
                                                       u'south': 57.1453783,
                                                       u'west': -2.2239393}},
                    u'Point': { u'coordinates': [-2.221984, 57.1467683, 0]},
                    u'address': u'A944, Aberdeen, Aberdeen City AB15, UK',
                    u'id': u'p1'}],
  u'_id': 7635459717214061L,
  u'loc': [-2.2231, 57.1437]}

The problem is that the formatted address (A944, Aberdeen, Aberdeen City AB15, UK) is not broken into details - the returned AddressDetails is essentially useless.

So, it looks like one has to parse the returned address manually, which seems to be a real PITA in a general case.

I would like to know how other folks solve this problem. Using the google maps web services API is not a requirement.

Thanks.

Upvotes: 0

Views: 870

Answers (1)

Andrew Leach
Andrew Leach

Reputation: 12973

The reverse geocoding web service for http://maps.googleapis.com/maps/api/geocode/json?latlng=57.14369,-2.22314&sensor=false

yields far more data than you have posted from the Python package, including

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "A944",
               "short_name" : "A944",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Aberdeen City",
               "short_name" : "Aberdeen City",
               "types" : [ "administrative_area_level_2", "political" ]
            },
...

which seems to fit the bill. You get "Aberdeen City".

Upvotes: 1

Related Questions