OpenBlock 1.2 documentation

geocoder Package

geocoder Package

base Module

Flexible geocoding against our models, including db.Location, streets.Place, streets.Block, streets.Intersection ...

class ebpub.geocoder.base.Address(*args, **kwargs)

Bases: dict

A simple container class for representing a single street address.

classmethod from_cache(cached)

Builds an Address object from a GeocoderCache result object.

location

Everything else full_geocode() can return has a .location, so this is here for consistency of API.

class ebpub.geocoder.base.AddressGeocoder(use_cache=True)

Bases: ebpub.geocoder.base.Geocoder

Treats the location_string as an address and looks for a matching Block.

class ebpub.geocoder.base.BlockGeocoder(use_cache=True)

Bases: ebpub.geocoder.base.AddressGeocoder

Geocodes the location_string as a streets.Block.

class ebpub.geocoder.base.Geocoder(use_cache=True)

Bases: object

Generic Geocoder class.

Subclasses must override the following attribute:

_do_geocode(self, location_string)
Actually performs the geocoding. The base class implementation of geocode() calls this behind the scenes.
geocode(location)

Geocodes the given location, handling caching behind the scenes.

class ebpub.geocoder.base.IntersectionGeocoder(use_cache=True)

Bases: ebpub.geocoder.base.Geocoder

Geocodes the location_string as a streets.Intersection.

class ebpub.geocoder.base.SmartGeocoder(use_cache=True)

Bases: ebpub.geocoder.base.Geocoder

Checks whether the location_string looks like an Intersection, Block, or Address, and delegates to the appropriate Geocoder subclass.

ebpub.geocoder.base.disambiguate(geocoder_results, guess=False, **kwargs)

Disambiguate a list of geocoder results based on city, state, zip. Result will be a list, which may be the original list or a subset of it.

If guess==True, returns the first remaining result, which could be wildly off from what you expect (eg. in the case of 'invalid block but valid street')... so use with caution.

ebpub.geocoder.base.full_geocode(query, search_places=True, convert_to_block=True, guess=False, **disambiguation_kwargs)

Tries the full geocoding stack on the given query (a string):

  • Normalizes whitespace/capitalization
  • Searches the Misspelling table to corrects location misspellings
  • Searches the Location table
  • Failing that, searches the Place table (if search_places is True)
  • Failing that, uses the SmartGeocoder to parse this as an address, block, or intersection
  • Failing that, raises whichever error is raised by the geocoder -- except AmbiguousResult, in which case all possible results are returned

Returns a dictionary of {type, result, ambiguous}, where ambiguous is True or False, and type can be on of these strings:

  • 'location' -- in which case result is a Location object.
  • 'place' -- in which case result is a Place object. (This is only possible if search_places is True.)
  • 'address' -- in which case result is an Address object as returned by geocoder.geocode().
  • 'block' -- in which case result is an Address object based on the block.

When ambiguous is True in the output dict, result will be a list of objects, and vice versa.

You can control behavior with ambiguous results in several ways:

  • By passing guess=True, only the first result will be returned.
  • By passing additional kwargs such as zipcode, city, or state, they will be used to attempt to disambiguate address or block results as needed. Keys should be keys in each Address result; invalid keys have no effect.
  • By default, if the exact address is not matched, it will be rounded down to the nearest 100, eg. '123 Main St' will be converted to '100 block of Main St', and tried again with BlockGeocoder. This is enabled by default; you can pass convert_to_block=False to turn it off.

models Module

class ebpub.geocoder.models.GeocoderCache(*args, **kwargs)

Bases: django.db.models.base.Model

Persistent cache for Geocoder results. Not sure why this merits a custom model; see http://developer.openblockproject.org/ticket/163

Parameters:
  • id (AutoField) -- Id
  • normalized_location (CharField, required) -- Normalized location
  • address (CharField, required) -- Address
  • city (CharField, required) -- City
  • state (CharField, required) -- State
  • zip (CharField, required) -- Zip
  • location (PointField, required) -- Location
  • block (ForeignKey) -- Block
  • intersection (ForeignKey) -- Intersection
  • generated_at (DateTimeField) -- Generated at
classmethod populate(normalized_location, address)

Populates the cache from an Address object.

class ebpub.geocoder.models.GeocoderCacheAdmin(model, admin_site)

Bases: django.contrib.admin.options.ModelAdmin

Adding to admin so you at least can delete them from somewhere

reverse Module

ebpub.geocoder.reverse.reverse_geocode(point)

Looks up the nearest block to the point.

Argument can be either a Point instance, or an (x, y) tuple, or a WKT string.

Returns (block, distance (in degrees I think??))