Permalink
Browse files

Fixes #286, major breakage in full_geocode(convert_to_block=True), no…

…w with some tests ... still needs more coverage
  • Loading branch information...
1 parent 6f72897 commit de54a19747078e230f5a182f94187b70e8bb18e6 @slinkp slinkp committed Apr 3, 2012
Showing with 43 additions and 12 deletions.
  1. +7 −0 docs/changes/release_notes.rst
  2. +19 −12 ebpub/ebpub/geocoder/base.py
  3. +17 −0 ebpub/ebpub/geocoder/tests/geocoder.py
@@ -84,6 +84,13 @@ Backward Incompatibilities
New Features in 1.2
-------------------
+* ebpub.geocoder.base.full_geocode() now has a convert_to_block
+ argument, factored out from ebdata.retrieval. If True, this
+ tries to disambiguate bad blocks on good streets by rounding down
+ to the nearest block, eg. converting '299 Wabash St' to '200 block
+ of Wabash St.' May help geocoding when eg. census data doesn't
+ quite match reality.
+
* Schemas now have an ``edit_window`` field, representing how long (in
hours) users are allowed to edit their content after it's created. Used
by the ``neighbornews`` forms.
@@ -25,6 +25,7 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from ebpub.geocoder.parser.parsing import normalize, parse, ParsingError
+from ebpub.utils.text import address_to_block
import logging
import re
@@ -468,7 +469,6 @@ def full_geocode(query, search_places=True, zipcode=None, city=None, state=None,
places = Place.objects.filter(normalized_name=canonical_place)
if len(places) == 1:
logger.debug(u'geocoded %r to Place %s' % (query, places[0]))
-
return {'type': 'place', 'result': places[0], 'ambiguous': False}
elif len(places) > 1:
# TODO: Places don't know about city, state, zip...
@@ -496,21 +496,28 @@ def full_geocode(query, search_places=True, zipcode=None, city=None, state=None,
return {'type': 'address', 'result': results[0], 'ambiguous': False}
except InvalidBlockButValidStreet, e:
- # If the exact address couldn't be geocoded, try using the
- # normalized location name.
+ result = {
+ 'type': 'block',
+ 'ambiguous': True,
+ 'result': e.block_list,
+ 'street_name': e.street_name,
+ 'block_number': e.block_number,
+ }
if convert_to_block:
- block_name = address_to_block(kwargs['location_name'])
- if block_name != kwargs['location_name']:
+ # If the exact address couldn't be geocoded, try using the
+ # normalized block name.
+ block_name = address_to_block(query)
+ if block_name != query:
try:
- result = BlockGeocoder._do_geocode(block_name)
+ result['result'] = BlockGeocoder()._do_geocode(block_name)
result['result']['address'] = block_name
- except InvalidBlockButValidStreet, another_e:
- pass
- except AmbiguousResult, another_e:
+ result['ambiguous'] = False
+ logger.debug('Resolved %r to block %r' % (query, block_name))
+ except (InvalidBlockButValidStreet, AmbiguousResult):
pass
- # No joy, return all blocks.
- logger.debug('Invalid block for %r, returning all possible blocks' % query)
- return {'type': 'block', 'result': e.block_list, 'ambiguous': True, 'street_name': e.street_name, 'block_number': e.block_number}
+ if result['ambiguous']:
+ logger.debug('Invalid block for %r, returning all possible blocks' % query)
+ return result
except:
raise
@@ -70,6 +70,23 @@ def test_full_geocode__no_place(self):
from ebpub.geocoder.base import full_geocode, DoesNotExist
self.assertRaises(DoesNotExist, full_geocode, 'Bogus Place Name')
+ def test_full_geocode__bad_block_on_good_street(self):
+ from ebpub.geocoder.base import full_geocode
+ # This block goes up to 298.
+ result = full_geocode('299 S. Wabash Ave.')
+ self.assert_(result['ambiguous'])
+ self.assertEqual(result['type'], 'block')
+ self.assertEqual(result['street_name'], 'Wabash Ave.')
+ self.assertEqual(len(result['result']), 3)
+
+ def test_full_geocode__convert_to_block(self):
+ from ebpub.geocoder.base import full_geocode
+ # This block goes up to 298.
+ result = full_geocode('299 S. Wabash Ave.', convert_to_block=True)
+ self.failIf(result['ambiguous'])
+ self.assertEqual(result['type'], 'block')
+ self.assertEqual(result['result']['address'], '200 block of S. Wabash Ave.')
+
class TestDisambiguation(django.test.TestCase):

0 comments on commit de54a19

Please sign in to comment.