+ def parse_geocodejson(self):
+ self.parse_geojson()
+ if self.result is not None:
+ self.result = [r['geocoding'] for r in self.result]
+
+ def parse_xml(self):
+ et = ET.fromstring(self.page)
+
+ self.header = dict(et.attrib)
+ self.result = []
+
+ for child in et:
+ if child.tag == 'result':
+ eq_(0, len(self.result), "More than one result in reverse result")
+ self.result.append(dict(child.attrib))
+ elif child.tag == 'addressparts':
+ address = {}
+ for sub in child:
+ address[sub.tag] = sub.text
+ self.result[0]['address'] = address
+ elif child.tag == 'extratags':
+ self.result[0]['extratags'] = {}
+ for tag in child:
+ self.result[0]['extratags'][tag.attrib['key']] = tag.attrib['value']
+ elif child.tag == 'namedetails':
+ self.result[0]['namedetails'] = {}
+ for tag in child:
+ self.result[0]['namedetails'][tag.attrib['desc']] = tag.text
+ elif child.tag in ('geokml'):
+ self.result[0][child.tag] = True
+ else:
+ assert child.tag == 'error', \
+ "Unknown XML tag %s on page: %s" % (child.tag, self.page)
+
+
+class DetailsResponse(GenericResponse):
+
+ def __init__(self, page, fmt='json', errorcode=200):
+ self.page = page
+ self.format = fmt
+ self.errorcode = errorcode
+ self.result = []
+ self.header = dict()
+
+ if errorcode == 200:
+ getattr(self, 'parse_' + fmt)()
+
+ def parse_html(self):
+ content, errors = tidy_document(self.page,
+ options={'char-encoding' : 'utf8'})
+ self.result = {}
+
+ def parse_json(self):
+ self.result = [json.JSONDecoder(object_pairs_hook=OrderedDict).decode(self.page)]
+
+
+class StatusResponse(GenericResponse):
+
+ def __init__(self, page, fmt='text', errorcode=200):
+ self.page = page
+ self.format = fmt
+ self.errorcode = errorcode
+
+ if errorcode == 200 and fmt != 'text':
+ getattr(self, 'parse_' + fmt)()
+
+ def parse_json(self):
+ self.result = [json.JSONDecoder(object_pairs_hook=OrderedDict).decode(self.page)]
+
+
+def geojson_result_to_json_result(geojson_result):
+ result = geojson_result['properties']
+ result['geojson'] = geojson_result['geometry']
+ if 'bbox' in geojson_result:
+ # bbox is minlon, minlat, maxlon, maxlat
+ # boundingbox is minlat, maxlat, minlon, maxlon
+ result['boundingbox'] = [
+ geojson_result['bbox'][1],
+ geojson_result['bbox'][3],
+ geojson_result['bbox'][0],
+ geojson_result['bbox'][2]
+ ]
+ return result
+
+
+def geojson_results_to_json_results(geojson_results):
+ if 'error' in geojson_results:
+ return
+ return list(map(geojson_result_to_json_result, geojson_results['features']))