Exemplo n.º 1
0
    def _geocode(self, pq):
        if pq.query.strip() == '':
            # No single line query string; use address elements:
            query = {'addressLine':pq.address,
                     'locality':pq.city,
                     'adminDistrict':pq.state,
                     'postalCode':pq.postal,
                     'countryRegion':pq.country}
        else:
            query = {'query':pq.query}
        
        if pq.viewbox is not None:
            query = dict(query, **{'umv':pq.viewbox.to_bing_str()})
        if hasattr(pq, 'culture'): query = dict(query, c=pq.culture)
        if hasattr(pq, 'user_ip'): query = dict(query, uip=pq.user_ip)
        if hasattr(pq, 'user_lat') and hasattr(pq, 'user_lon'):
            query = dict(query, **{'ul':'%f,%f' % (pq.user_lat, pq.user_lon)})

        addl_settings = {'key':self._settings['api_key']}
        query = dict(query, **addl_settings)
        response_obj = self._get_json_obj(self._endpoint, query)
        returned_candidates = [] # this will be the list returned
        for r in response_obj['resourceSets'][0]['resources']:    
            c = Candidate()
            c.entity = r['entityType']
            c.locator = r['geocodePoints'][0]['calculationMethod'] # ex. "Parcel"
            c.confidence = r['confidence'] # High|Medium|Low
            c.match_addr = r['name'] # ex. "1 Microsoft Way, Redmond, WA 98052"
            c.x = r['geocodePoints'][0]['coordinates'][1] # long, ex. -122.13
            c.y = r['geocodePoints'][0]['coordinates'][0] # lat, ex. 47.64
            c.wkid = 4326
            c.geoservice = self.__class__.__name__
            returned_candidates.append(c)
        return returned_candidates
Exemplo n.º 2
0
    def _geocode(self, pq):
        def get_appended_location(location, **kwargs):
            """Add key/value pair to given dict only if value is not empty string."""
            for kw in kwargs:
                if kwargs[kw] != '':
                    location = dict(location, **{kw: kwargs[kw]})
            return location

        location = {}
        location = get_appended_location(location, street=pq.query)
        if location == {}:
            location = get_appended_location(location, street=pq.address)
        location = get_appended_location(location,
                                         city=pq.city,
                                         county=pq.subregion,
                                         state=pq.state,
                                         postalCode=pq.postal,
                                         country=pq.country)
        json_ = dict(location=location)
        json_ = json.dumps(json_)
        logger.debug('MQ json: %s', json_)
        query = dict(key=unquote(self._settings['api_key']), json=json_)
        if pq.viewbox is not None:
            query = dict(query, viewbox=pq.viewbox.to_mapquest_str())
        response_obj = self._get_json_obj(self._endpoint, query)
        logger.debug('MQ RESPONSE: %s', response_obj)
        returned_candidates = []  # this will be the list returned
        for r in response_obj['results'][0]['locations']:
            c = Candidate()
            c.locator = r['geocodeQuality']
            c.confidence = r[
                'geocodeQualityCode']  #http://www.mapquestapi.com/geocoding/geocodequality.html
            match_addr_elements = [
                'street', 'adminArea5', 'adminArea3', 'adminArea2',
                'postalCode'
            ]  # similar to ESRI
            c.match_addr = ', '.join(
                [r[k] for k in match_addr_elements if k in r])
            c.x = r['latLng']['lng']
            c.y = r['latLng']['lat']
            c.wkid = 4326
            c.geoservice = self.__class__.__name__
            returned_candidates.append(c)
        return returned_candidates
Exemplo n.º 3
0
    def _geocode(self, pq):
        if pq.query.strip() == '':
            # No single line query string; use address elements:
            query = {
                'addressLine': pq.address,
                'locality': pq.city,
                'adminDistrict': pq.state,
                'postalCode': pq.postal,
                'countryRegion': pq.country
            }
        else:
            query = {'query': pq.query}

        if pq.viewbox is not None:
            query = dict(query, **{'umv': pq.viewbox.to_bing_str()})
        if hasattr(pq, 'culture'):
            query = dict(query, c=pq.culture)
        if hasattr(pq, 'user_ip'):
            query = dict(query, uip=pq.user_ip)
        if hasattr(pq, 'user_lat') and hasattr(pq, 'user_lon'):
            query = dict(query, **{'ul': '%f,%f' % (pq.user_lat, pq.user_lon)})

        addl_settings = {'key': self._settings['api_key']}
        query = dict(query, **addl_settings)
        response_obj = self._get_json_obj(self._endpoint, query)
        returned_candidates = []  # this will be the list returned
        for r in response_obj['resourceSets'][0]['resources']:
            c = Candidate()
            c.entity = r['entityType']
            c.locator = r['geocodePoints'][0][
                'calculationMethod']  # ex. "Parcel"
            c.confidence = r['confidence']  # High|Medium|Low
            c.match_addr = r[
                'name']  # ex. "1 Microsoft Way, Redmond, WA 98052"
            c.x = r['geocodePoints'][0]['coordinates'][1]  # long, ex. -122.13
            c.y = r['geocodePoints'][0]['coordinates'][0]  # lat, ex. 47.64
            c.wkid = 4326
            c.geoservice = self.__class__.__name__
            returned_candidates.append(c)
        return returned_candidates
Exemplo n.º 4
0
 def _geocode(self, pq):
     def get_appended_location(location, **kwargs):
         """Add key/value pair to given dict only if value is not empty string."""
         for kw in kwargs:
             if kwargs[kw] != '':
                 location = dict(location, **{kw: kwargs[kw]})
         return location
     if pq.address.strip() != '':
         location = {}
         location = get_appended_location(location, street=pq.query)
         if location == {}:
             location = get_appended_location(location, street=pq.address)
         location = get_appended_location(location, city=pq.city, county=pq.subregion, state=pq.state,
                                          postalCode=pq.postal, country=pq.country)
         json_ = dict(location=location)
         json_ = json.dumps(json_)
         query = dict(key=unquote(self._settings['api_key']),
                      json=json_)
     else:
         query = dict(key=unquote(self._settings['api_key']),
                  location=pq.query)
     if pq.viewbox is not None:
         query = dict(query, viewbox=pq.viewbox.to_mapquest_str())
     response_obj = self._get_json_obj(self._endpoint, query)
     returned_candidates = [] # this will be the list returned
     for r in response_obj['results'][0]['locations']:
         c = Candidate()
         c.locator=r['geocodeQuality']
         c.confidence=r['geocodeQualityCode'] #http://www.mapquestapi.com/geocoding/geocodequality.html
         match_addr_elements = ['street', 'adminArea5', 'adminArea3',
                                'adminArea2', 'postalCode'] # similar to ESRI
         c.match_addr = ', '.join([r[k] for k in match_addr_elements if k in r])
         c.x = r['latLng']['lng']
         c.y = r['latLng']['lat']
         c.wkid = 4326
         c.geoservice = self.__class__.__name__
         returned_candidates.append(c)
     return returned_candidates