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
Exemplo n.º 3
0
    def _geocode(self, pq):
        """
        :arg PlaceQuery pq: PlaceQuery object to use for geocoding
        :returns: list of location Candidates
        """
        #: List of desired output fields
        #: See `ESRI docs <http://geocode.arcgis.com/arcgis/geocoding.html#output>_` for details
        outFields = ('Loc_name',
                     #'Shape',
                     'Score',
                     'Match_Addr', #based on address standards for the country
                     #'Address', # returned by default
                     #'Country' # 3-digit ISO 3166-1 code for a country. Example: Canada = "CAN"
                     #'Admin',
                     #'DepAdmin',
                     #'SubAdmin',
                     #'Locality',
                     #'Postal',
                     #'PostalExt',
                     'Addr_Type',
                     #'Type',
                     #'Rank',
                     'AddNum',
                     'StPreDir',
                     'StPreType',
                     'StName',
                     'StType',
                     'StDir',
                     #'Side',
                     #'AddNumFrom',
                     #'AddNumTo',
                     #'AddBldg',
                     'City',
                     'Subregion',
                     'Region',
                     'Postal',
                     'Country',
                     #'Ymax',
                     #'Ymin',
                     #'Xmin',
                     #'Xmax',
                     #'X',
                     #'Y',
                     'DisplayX',
                     'DisplayY',
                     #'LangCode',
                     #'Status',
                    )
        outFields = ','.join(outFields)
        query = dict(f='json', # default HTML. Other options are JSON and KMZ.
                     outFields=outFields,
                     #outSR=WKID, defaults to 4326
                     maxLocations=20, # default 1; max is 20
                     )

        # Postal-code only searches work in the single-line but not multipart geocoder
        # Remember that with the default postprocessors, postcode-level results will be eliminated
        if pq.query == pq.address == '' and pq.postal != '':
            pq.query = pq.postal

        if pq.query == '': # multipart
            method = 'findAddressCandidates'
            query = dict(query,
                         Address=pq.address,  # commonly represents the house number and street name of a complete address
                         Neighborhood=pq.neighborhood,
                         City=pq.city,
                         Subregion=pq.subregion,
                         Region=pq.state,
                         Postal=pq.postal,
                         #PostalExt=
                         CountryCode=pq.country, # full country name or ISO 3166-1 2- or 3-digit country code
                         )
            if pq.bounded and pq.viewbox is not None:
                query = dict(query, searchExtent=pq.viewbox.to_esri_wgs_json())
        else: # single-line
            method = 'find'
            magic_key = pq.key if hasattr(pq, 'key') else ''
            query = dict(query,
                         text=pq.query, # This can be a street address, place name, postal code, or POI.
                         sourceCountry=pq.country, # full country name or ISO 3166-1 2- or 3-digit country code
                         )
            if magic_key:
                query['magicKey'] = magic_key # This is a lookup key returned from the suggest endpoint.
            if pq.bounded and pq.viewbox is not None:
                query = dict(query, bbox=pq.viewbox.to_esri_wgs_json())

        endpoint = self._endpoint + '/' + method
        response_obj = self._get_json_obj(endpoint, query)
        returned_candidates = []  # this will be the list returned
        try:
            if method == 'find':
                locations = response_obj['locations']
            else:
                locations = response_obj['candidates']

            for location in locations:
                c = Candidate()
                if method == 'find':  # singlepart
                    attributes = location['feature']['attributes']
                else:  # findAddressCandidates / multipart
                    attributes = location['attributes']
                c.match_addr = attributes['Match_Addr']
                c.locator = attributes['Loc_name']
                c.locator_type = attributes['Addr_Type']
                c.score = attributes['Score']
                c.x = attributes['DisplayX']  # represents the actual location of the address.
                c.y = attributes['DisplayY']
                c.wkid = response_obj['spatialReference']['wkid']
                c.geoservice = self.__class__.__name__

                # Optional address component fields.
                for in_key, out_key in [('City', 'match_city'), ('Subregion', 'match_subregion'),
                                        ('Region', 'match_region'), ('Postal', 'match_postal'),
                                        ('Country', 'match_country')]:
                    setattr(c, out_key, attributes.get(in_key, ''))
                setattr(c, 'match_streetaddr', self._street_addr_from_response(attributes))
                returned_candidates.append(c)
        except KeyError:
            pass
        return returned_candidates
Exemplo n.º 4
0
    def _geocode(self, pq):
        """
        :arg PlaceQuery pq: PlaceQuery object to use for geocoding
        :returns: list of location Candidates
        """
        #: List of desired output fields
        #: See `ESRI docs <http://geocode.arcgis.com/arcgis/geocoding.html#output>_` for details
        outFields = (
            'Loc_name',
            #'Shape',
            'Score',
            'Match_Addr',  #based on address standards for the country
            #'Address', # returned by default
            #'Country' # 3-digit ISO 3166-1 code for a country. Example: Canada = "CAN"
            #'Admin',
            #'DepAdmin',
            #'SubAdmin',
            #'Locality',
            #'Postal',
            #'PostalExt',
            'Addr_Type',
            #'Type',
            #'Rank',
            'AddNum',
            'StPreDir',
            'StPreType',
            'StName',
            'StType',
            'StDir',
            #'Side',
            #'AddNumFrom',
            #'AddNumTo',
            #'AddBldg',
            'City',
            'Subregion',
            'Region',
            'Postal',
            'Country',
            #'Ymax',
            #'Ymin',
            #'Xmin',
            #'Xmax',
            #'X',
            #'Y',
            'DisplayX',
            'DisplayY',
            #'LangCode',
            #'Status',
        )
        outFields = ','.join(outFields)
        query = dict(
            f='json',  # default HTML. Other options are JSON and KMZ.
            outFields=outFields,
            #outSR=WKID, defaults to 4326
            maxLocations=20,  # default 1; max is 20
        )

        # Postal-code only searches work in the single-line but not multipart geocoder
        # Remember that with the default postprocessors, postcode-level results will be eliminated
        if pq.query == pq.address == '' and pq.postal != '':
            pq.query = pq.postal

        if pq.query == '':  # multipart
            method = 'findAddressCandidates'
            query = dict(
                query,
                Address=pq.
                address,  # commonly represents the house number and street name of a complete address
                Neighborhood=pq.neighborhood,
                City=pq.city,
                Subregion=pq.subregion,
                Region=pq.state,
                Postal=pq.postal,
                #PostalExt=
                CountryCode=pq.
                country,  # full country name or ISO 3166-1 2- or 3-digit country code
            )
            if pq.bounded and pq.viewbox is not None:
                query = dict(query, searchExtent=pq.viewbox.to_esri_wgs_json())
        else:  # single-line
            method = 'find'
            magic_key = pq.key if hasattr(pq, 'key') else ''
            query = dict(
                query,
                text=pq.
                query,  # This can be a street address, place name, postal code, or POI.
                sourceCountry=pq.
                country,  # full country name or ISO 3166-1 2- or 3-digit country code
            )
            if magic_key:
                query[
                    'magicKey'] = magic_key  # This is a lookup key returned from the suggest endpoint.
            if pq.bounded and pq.viewbox is not None:
                query = dict(query, bbox=pq.viewbox.to_esri_wgs_json())

        endpoint = self._endpoint + '/' + method
        response_obj = self._get_json_obj(endpoint, query)
        returned_candidates = []  # this will be the list returned
        try:
            if method == 'find':
                locations = response_obj['locations']
            else:
                locations = response_obj['candidates']

            for location in locations:
                c = Candidate()
                if method == 'find':  # singlepart
                    attributes = location['feature']['attributes']
                else:  # findAddressCandidates / multipart
                    attributes = location['attributes']
                c.match_addr = attributes['Match_Addr']
                c.locator = attributes['Loc_name']
                c.locator_type = attributes['Addr_Type']
                c.score = attributes['Score']
                c.x = attributes[
                    'DisplayX']  # represents the actual location of the address.
                c.y = attributes['DisplayY']
                c.wkid = response_obj['spatialReference']['wkid']
                c.geoservice = self.__class__.__name__

                # Optional address component fields.
                for in_key, out_key in [('City', 'match_city'),
                                        ('Subregion', 'match_subregion'),
                                        ('Region', 'match_region'),
                                        ('Postal', 'match_postal'),
                                        ('Country', 'match_country')]:
                    setattr(c, out_key, attributes.get(in_key, ''))
                setattr(c, 'match_streetaddr',
                        self._street_addr_from_response(attributes))
                returned_candidates.append(c)
        except KeyError:
            pass
        return returned_candidates