def _geocode(self, location): query = { 'Address':location.address, 'City':location.city, 'Postcode':location.postal, 'Country':location.country, 'outfields':'Loc_name', 'f':'json'} query = self.append_token_if_needed(query) response_obj = self._get_json_obj(self._endpoint, query) returned_candidates = [] # this will be the list returned try: for rc in response_obj['candidates']: c = Candidate() c.locator = rc['attributes']['Loc_name'] c.score = rc['score'] c.match_addr = rc['address'] c.x = rc['location']['x'] c.y = rc['location']['y'] c.wkid = self._wkid c.geoservice = self.__class__.__name__ returned_candidates.append(c) except KeyError as ex: logger.warning('Received unusual JSON result from geocode: %s, %s', response_obj, ex) return [] return returned_candidates
def _geocode(self, location): query = { 'SingleLine':location.query, 'Address':location.address, 'City':location.city, 'State':location.state, 'Zip':location.postal, 'Country':location.country, 'outfields':'Loc_name,Addr_Type,Zip4_Type', 'f':'json'} query = self.append_token_if_needed(query) response_obj = self._get_json_obj(self._endpoint, query) try: wkid = response_obj['spatialReference']['wkid'] except KeyError: pass returned_candidates = [] # this will be the list returned try: for rc in response_obj['candidates']: c = Candidate() c.locator = rc['attributes']['Loc_name'] c.score = rc['score'] c.match_addr = rc['address'] c.x = rc['location']['x'] c.y = rc['location']['y'] c.wkid = wkid c.geoservice = self.__class__.__name__ returned_candidates.append(c) except KeyError: pass return returned_candidates
def _geocode(self, location): query = { 'SingleLine': location.query, 'Address': location.address, 'City': location.city, 'State': location.state, 'Zip': location.postal, 'Country': location.country, 'outfields': 'Loc_name,Addr_Type,Zip4_Type', 'f': 'json' } query = self.append_token_if_needed(query) response_obj = self._get_json_obj(self._endpoint, query) try: wkid = response_obj['spatialReference']['wkid'] except KeyError: pass returned_candidates = [] # this will be the list returned try: for rc in response_obj['candidates']: c = Candidate() c.locator = rc['attributes']['Loc_name'] c.score = rc['score'] c.match_addr = rc['address'] c.x = rc['location']['x'] c.y = rc['location']['y'] c.wkid = wkid c.geoservice = self.__class__.__name__ returned_candidates.append(c) except KeyError: pass return returned_candidates
def _geocode(self, location): query = { 'Address': location.address, 'City': location.city, 'Postcode': location.postal, 'Country': location.country, 'outfields': 'Loc_name', 'f': 'json' } query = self.append_token_if_needed(query) response_obj = self._get_json_obj(self._endpoint, query) returned_candidates = [] # this will be the list returned try: for rc in response_obj['candidates']: c = Candidate() c.locator = rc['attributes']['Loc_name'] c.score = rc['score'] c.match_addr = rc['address'] c.x = rc['location']['x'] c.y = rc['location']['y'] c.wkid = self._wkid c.geoservice = self.__class__.__name__ returned_candidates.append(c) except KeyError as ex: logger.warning('Received unusual JSON result from geocode: %s, %s', response_obj, ex) return [] return returned_candidates
def _geocode(self, location): query = { 'Address':location.address, 'City':location.city, 'Postcode':location.postal, 'Country':location.country, 'outfields':'Loc_name', 'f':'json'} query = self.append_token_if_needed(query) response_obj = self._get_json_obj(self._endpoint, query) if response_obj is False: return [] returned_candidates = [] # this will be the list returned try: for rc in response_obj['candidates']: c = Candidate() c.locator = rc['attributes']['Loc_name'] c.score = rc['score'] c.match_addr = rc['address'] c.x = rc['location']['x'] c.y = rc['location']['y'] c.wkid = self._wkid c.geoservice = self.__class__.__name__ returned_candidates.append(c) except KeyError as ex: print "I'm not what you expected, but hey, I'm still JSON! %s" % ex #TODO: put on error stack return [] return returned_candidates
def _create_candidate_from_intersection_element(intersection_element, source_operation): c = Candidate() c.locator = source_operation c.match_addr = _get_text_from_nodelist( intersection_element.getElementsByTagName("FULLINTERSECTION")[0].childNodes) + ", WASHINGTON, DC" c.y = float(_get_text_from_nodelist(intersection_element.getElementsByTagName("LATITUDE")[0].childNodes)) c.x = float(_get_text_from_nodelist(intersection_element.getElementsByTagName("LONGITUDE")[0].childNodes)) confidence_level_elements = intersection_element.getElementsByTagName("ConfidenceLevel") c.score = float(_get_text_from_nodelist(confidence_level_elements[0].childNodes)) c.geoservice = self.__class__.__name__ return c
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
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
def _create_candidate_from_address_element(match, source_operation): if match.getElementsByTagName("FULLADDRESS").length > 0: full_address = _get_text_from_nodelist(match.getElementsByTagName("FULLADDRESS")[0].childNodes) else: full_address = _get_text_from_nodelist( match.getElementsByTagName("STNAME")[0].childNodes) + " " + _get_text_from_nodelist(match.getElementsByTagName("STREET_TYPE")[0].childNodes) city = _get_text_from_nodelist(match.getElementsByTagName("CITY")[0].childNodes) state = _get_text_from_nodelist(match.getElementsByTagName("STATE")[0].childNodes) zipcode = _get_text_from_nodelist(match.getElementsByTagName("ZIPCODE")[0].childNodes) c = Candidate() c.match_addr = full_address + ", " + city + ", " + state + ", " + zipcode confidence_level_elements = match.getElementsByTagName("ConfidenceLevel") c.score = float(_get_text_from_nodelist(confidence_level_elements[0].childNodes)) c.y = float(_get_text_from_nodelist(match.getElementsByTagName("LATITUDE")[0].childNodes)) c.x = float(_get_text_from_nodelist(match.getElementsByTagName("LONGITUDE")[0].childNodes)) c.locator = source_operation c.geoservice = self.__class__.__name__ return c
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
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