Exemplo n.º 1
0
    def _make_candidate_from_result(self, result):
        """ Make a Candidate from a Google geocoder results dictionary. """
        candidate = Candidate()
        candidate.match_addr = result['formatted_address']
        candidate.x = result['geometry']['location']['lng']
        candidate.y = result['geometry']['location']['lat']
        candidate.locator = self.LOCATOR_MAPPING.get(result['geometry']['location_type'], '')
        candidate.entity_types = result['types']
        candidate.partial_match = result.get('partial_match', False)

        component_lookups = {
            'city': {'type': 'locality', 'key': 'long_name'},
            'subregion': {'type': 'administrative_area_level_2', 'key': 'long_name'},
            'region': {'type': 'administrative_area_level_1', 'key': 'short_name'},
            'postal': {'type': 'postal_code', 'key': 'long_name'},
            'country': {'type': 'country', 'key': 'short_name'},
        }
        for (field, lookup) in component_lookups.items():
            setattr(candidate, 'match_' + field, self._get_component_from_result(result, lookup))
        candidate.geoservice = self.__class__.__name__
        return candidate