mapper - Geocoding, Local Search, Directions, Routes

Geocoding, local search, directions, route iterator, and related functions.

class Mapper(api_key='', referrer_url='')

Abstracts converting addresses to latitude/longitude and vice versa; finding points of interest; getting directions from one point to another; and iterating over routes between lists of points.

Initializes this Mapper with a given API key and referrer_url.

  • api_key (string) – Google Maps API key
  • referrer_url (string) – URL of the website that will be making the queries.

Convert an address to latitude and longitude.

Parameter:addr (string) – Address to convert
Returns:(latitude, longitude) tuple
Return type:(float, float)
Raises MapperError:
 If addr could not be geocoded.
directions(origin, destination)

Return a dictionary of information about the route from origin to destination.

  • origin (string) – Starting address
  • destination (string) – Ending address

Dictionary of information about the overall route from origin to destination with the following keys: meters, seconds, steps, start, end.

Return type:

dict(string: numeric or string)

Raises MapperError:

If the method was unable to find directions.

>>> mapper = Mapper()
>>> dirs = mapper.directions('San Diego, CA', 'Los Angeles, CA')
>>> for k, v in sorted(dirs.iteritems()):
...    print "%s: %s" % (k, v)
end: Los Angeles, CA
meters: 193463
seconds: 7024
start: San Diego, CA
steps: 6
get_listings(query, city, num=8, err_callback=None)

Perform a local search to find points of interest in a given city.

Listings are normalized by geocoding; if a point cannot be normalized, it is skipped. Thus, this method may return fewer listings than requested.

  • query (string) – Name or type of place you are looking for, e.g. ‘soul food’, ‘starbucks’
  • city (string) – City (or any locale, really) in which to search
  • numpoints (int) – The number of results to return
  • err_callback (func(string)) – Function that will be called with the text of any error messages.

list of Listing dictionaries.

Return type:

list of dict(string: string or float) with the following keys: name, address, street, city, region, postcode, country, lat, lng, url

Raises MapperError:

If the query or city was bad.

>>> mapper = Mapper()
>>> listings = mapper.get_listings('soul food', 'harlem, ny', num=1)
>>> for k, v in sorted(listings[0].iteritems()):
...    print "%s: %s" % (k, v)
address: 113 W 116th St, New York, NY 10026, USA
city: New York
country: US
lat: 40.8023467
lng: -73.9504037
name: Amy Ruth's Restaurant
postcode: 10026
region: NY
street: 113 W 116th St

Convert latitude and longitude to a street address.

Parameter:latlng ((float, float)) – (latitude, longitude) tuple
Returns:Closest street address to latlng
Return type:string
Raises MapperError:
 If there was no street address close to latlng

Return the canonical, normalized form of an address as a dict.

Parameter:addr (string) – Address to normalize.
Returns:Dictionary with the following keys: address, street, city, region, postcode, country, lat, lng.
Return type:dict(string: string or float)
Raises MapperError:
 If the address could not be normalized.
>>> mapper = Mapper()
>>> addr =  mapper.normalize_addr('1-4573 Chateau Boulevard RR 4 Whistler, BC V0N 1B4, Canada')
>>> for k, v in sorted(addr.iteritems()):
...    print "%s: %s" % (k, v)
address: 4573 Chateau Blvd, Whistler, BC, Canada
city: Whistler
country: CA
lat: 50.1174596
lng: -122.9466722
postcode: V0N
region: BC
street: 4573 Chateau Blvd
routes(from_addrs, to_addrs, callback=None, sleep_secs=0.5)

Yield route driving information for all pairs of addresses from from_addrs to to_addrs.

Try to compute a route from every address in from_addrs to every listing in to_addrs (len(from_points) * len(to_points) routes in total).

  • from_addrs (list(string)) – list of starting addresses for routes
  • to_addrs (list(string)) – list of ending addresses for routes
  • callback (func(string)) – Function that you supply, will be called periodically with a string giving the current status of the operation.
  • sleep_secs (float) – Seconds to sleep between each iteration so’s you don’t burn up your query limit.

list of dictionaries giving start, end, meters, seconds, and steps for each route.

Return type:

(generator of) list(dict(string: string or numeric))

Raises MapperError:

If there are too many consecutive errors in retrieving route directions.

>>> mapper = Mapper()
>>> from_points = mapper.get_listings('elementary school', 'boston, ma', num=2)
>>> from_addrs = [point['address'] for point in from_points]
>>> to_points = mapper.get_listings('starbucks', 'boston, ma', num=1)
>>> to_addrs = [point['address'] for point in to_points]
>>> for route in mapper.routes(from_addrs, to_addrs):
...    print "Route:"
...    for k, v in sorted(route.iteritems()):
...        print "  %s: %s" % (k, v)
  end: 222 Cambridge St, Boston, MA 02114, USA
  meters: 1702
  seconds: 302
  start: 16 Charter St, Boston, MA 02113, USA
  steps: 6
  end: 222 Cambridge St, Boston, MA 02114, USA
  meters: 1294
  seconds: 141
  start: 67 Brimmer St, Boston, MA 02108, USA
  steps: 7
exception MapperError(message, code=None)

Base class for errors in the mapper module.

Methods of class Mapper raise this when something goes wrong. Don’t depend on the exact string of the error message, because they’re implementation-dependent.

Create an exception with a message and optional status code.

  • message – Message explaining the exception.

Converts distance in km (at any latitude or longitude) to degrees latitude.

Assumes earth is a big sphere.

Parameter:dist (float) – distance in kilometers
Returns:dist in degrees latitude
Return type:float
km_to_degrees_longitude(dist, lat)

Converts distance in km at a given latitude into degrees longitude.

Degrees longitude represent less (East-West) distance as you move away from the equator. Assumes earth is a big sphere.

  • dist (float) – distance in kilometers at latitude lat
  • lat (float) – degrees latitude at which to measure distance dist

Distance in degrees longitude at the given latitude

Return type:


Indices and tables

Table Of Contents

Previous topic

cityspeed - Main Driver, Data Collection

Next topic

settings - CitySpeed Configuration File

This Page

Python Powered

Get CitySpeed at Fast, secure and Free Open Source software downloads