Example #1
0
class PostgisIntersectionGeocoder:
    """
    A replacement for ebpub.base.IntersectionGeocoder
    """
    def __init__(self, cxn):
        self.connection = cxn
        self.spelling = SpellingCorrector()

    def geocode(self, location_string):
        sides = intersection_re.split(location_string)
        if len(sides) != 2:
            raise ParsingError("Couldn't parse intersection: %r" % location_string)

        # Parse each side of the intersection to a list of possibilities.
        # Let the ParseError exception propagate, if it's raised.
        left_side = parse(sides[0])
        right_side = parse(sides[1])

        all_results = []
        seen_intersections = set()
        for street_a in left_side:
            street_a['street'] = self.spelling.correct(street_a['street'])
            for street_b in right_side:
                street_b['street'] = self.spelling.correct(street_b['street'])
                for result in self._db_lookup(street_a, street_b):
                    if result["intersection_id"] not in seen_intersections:
                        seen_intersections.add(result["intersection_id"])
                        all_results.append(result)

        if not all_results:
            raise DoesNotExist("Geocoder db couldn't find this intersection: %r" % location_string)
        elif len(all_results) == 1:
            return all_results.pop()
        else:
            raise AmbiguousResult(list(all_results), "Intersections DB returned %s results" % len(all_results))

    def _db_lookup(self, street_a, street_b):
        try:
            searcher = PostgisIntersectionSearcher(self.connection)
            intersections = searcher.search(
                predir_a=street_a['pre_dir'],
                street_a=street_a['street'],
                suffix_a=street_a['suffix'],
                postdir_a=street_a['post_dir'],
                predir_b=street_b['pre_dir'],
                street_b=street_b['street'],
                suffix_b=street_b['suffix'],
                postdir_b=street_b['post_dir'],
            )
            searcher.close()
        # except Exception, e:
        except DoesNotExist, e:
            raise DoesNotExist("Intersection db query failed: %r" % e)
        return [self._build_result(i) for i in intersections]
Example #2
0
 def __init__(self, cxn):
     self.connection = cxn
     self.spelling = SpellingCorrector()