Exemplo n.º 1
0
    def _geocode(self, pq):
        query = {'text':pq.query}

        if pq.country:
            query = dict(query, **{'boundary.country':pq.country})

        if pq.viewbox is not None:
            box = pq.viewbox.to_mapzen_dict()
            query = dict(query, **box)

        if hasattr(pq, 'key'):
            # Swap to the place endpoint and return a single result.
            self._endpoint = self._key_endpoint
            query = { 'ids':pq.key }

        if self._settings.has_key('api_key'):
            query['api_key'] = self._settings['api_key']

        response_obj = self._get_json_obj(self._endpoint, query)
        returned_candidates = [] # this will be the list returned
        features_in_response = response_obj['features']
        for r in features_in_response:
            properties = r['properties']
            geometry = r['geometry']

            score = 100 * float(properties['confidence']) \
                    if properties.has_key('confidence') else 0
            locality = properties['locality'] \
                       if properties.has_key('locality') else ''
            region = properties['region'] \
                     if properties.has_key('region') else ''
            label = properties['label'] \
                    if properties.has_key('label') else ''
            layer = properties['layer'] \
                    if properties.has_key('layer') else ''

            c = Candidate()
            c.locator = layer
            c.match_addr = label
            c.match_region = region
            c.match_city = locality
            c.locator_type = layer
            c.x = float(geometry['coordinates'][0])
            c.y = float(geometry['coordinates'][1])
            c.score = score
            c.wkid = self._wkid
            c.geoservice = self.__class__.__name__
            returned_candidates.append(c)
        return returned_candidates
Exemplo n.º 2
0
    def _geocode(self, pq):
        query = {'text': pq.query}

        if pq.country:
            query = dict(query, **{'boundary.country': pq.country})

        if pq.viewbox is not None:
            box = pq.viewbox.to_mapzen_dict()
            query = dict(query, **box)

        if hasattr(pq, 'key'):
            # Swap to the place endpoint and return a single result.
            self._endpoint = self._key_endpoint
            query = {'ids': pq.key}

        if self._settings.has_key('api_key'):
            query['api_key'] = self._settings['api_key']

        response_obj = self._get_json_obj(self._endpoint, query)
        returned_candidates = []  # this will be the list returned
        features_in_response = response_obj['features']
        for r in features_in_response:
            properties = r['properties']
            geometry = r['geometry']

            score = 100 * float(properties['confidence']) \
                    if properties.has_key('confidence') else 0
            locality = properties['locality'] \
                       if properties.has_key('locality') else ''
            region = properties['region'] \
                     if properties.has_key('region') else ''
            label = properties['label'] \
                    if properties.has_key('label') else ''
            layer = properties['layer'] \
                    if properties.has_key('layer') else ''

            c = Candidate()
            c.locator = layer
            c.match_addr = label
            c.match_region = region
            c.match_city = locality
            c.locator_type = layer
            c.x = float(geometry['coordinates'][0])
            c.y = float(geometry['coordinates'][1])
            c.score = score
            c.wkid = self._wkid
            c.geoservice = self.__class__.__name__
            returned_candidates.append(c)
        return returned_candidates