67 |
67 |
|
68 |
68 |
return result
|
69 |
69 |
|
|
70 |
@endpoint(
|
|
71 |
description=_('Return known similar addresses'),
|
|
72 |
parameters={
|
|
73 |
'q': {
|
|
74 |
'description': _('query string'),
|
|
75 |
'example_value': '169 rue du Chateau 75014 Paris'
|
|
76 |
},
|
|
77 |
'id': {
|
|
78 |
'description': _('address id, format id: BAN_id-lat-lon'),
|
|
79 |
},
|
|
80 |
'zipcode': {
|
|
81 |
'description': _('Restrict search to this zipcode'),
|
|
82 |
},
|
|
83 |
'limit': {
|
|
84 |
'description': _('Maximum number of responses'),
|
|
85 |
'example_value': '10'
|
|
86 |
},
|
|
87 |
})
|
|
88 |
def normalize(self, request, q=None, id=None, zipcode=None, limit=None):
|
|
89 |
if id:
|
|
90 |
# id format is BAN-id-lat-lon
|
|
91 |
lat, lon = id.split('-')[-2:]
|
|
92 |
ban_id = '-'.join(id.split('-')[:-2])
|
|
93 |
address = self.reverse(request, lat, lon)
|
|
94 |
address['text'] = address['display_name']
|
|
95 |
return {'data': [address]}
|
|
96 |
|
|
97 |
if not q:
|
|
98 |
return {'data': []}
|
|
99 |
|
|
100 |
scheme, netloc, path, params, query, fragment = urlparse.urlparse(self.service_url)
|
|
101 |
path = os.path.join(path, 'search/')
|
|
102 |
query_args = {'q': q, 'limit': limit or 30}
|
|
103 |
if zipcode:
|
|
104 |
query_args['postcode'] = zipcode
|
|
105 |
query = urlencode(query_args)
|
|
106 |
url = urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
|
|
107 |
|
|
108 |
addresses = []
|
|
109 |
features = self.requests.get(url).json().get('features', [])
|
|
110 |
|
|
111 |
for feature in features:
|
|
112 |
if not feature['geometry']['type'] == 'Point':
|
|
113 |
continue # skip unknown
|
|
114 |
address = feature.get('properties')
|
|
115 |
if not address or not address.get('label'):
|
|
116 |
continue
|
|
117 |
address['lon'] = str(feature['geometry']['coordinates'][0])
|
|
118 |
address['lat'] = str(feature['geometry']['coordinates'][1])
|
|
119 |
address['text'] = address['label']
|
|
120 |
# some compatibily with nominatim results
|
|
121 |
address['display_name'] = address['label']
|
|
122 |
if 'housenumber' in address:
|
|
123 |
address['house_number'] = address['housenumber']
|
|
124 |
if 'name' in address:
|
|
125 |
road = address['name']
|
|
126 |
house_number = address.get('housenumber')
|
|
127 |
if house_number and road.startswith(house_number):
|
|
128 |
road = road[len(house_number):].strip()
|
|
129 |
address['road'] = road
|
|
130 |
# create retrievable id
|
|
131 |
address['id'] = '%(id)s-%(lat)s-%(lon)s' % address
|
|
132 |
addresses.append(address)
|
|
133 |
return {'data': addresses}
|
|
134 |
|
70 |
135 |
@endpoint()
|
71 |
136 |
def reverse(self, request, lat, lon, **kwargs):
|
72 |
137 |
if kwargs.get('format', 'json') != 'json':
|
... | ... | |
86 |
151 |
result = {}
|
87 |
152 |
result['lon'] = str(feature['geometry']['coordinates'][0])
|
88 |
153 |
result['lat'] = str(feature['geometry']['coordinates'][1])
|
|
154 |
result['id'] = feature['properties']['id']
|
89 |
155 |
result['address'] = {'country': 'France'}
|
90 |
156 |
for prop in feature['properties']:
|
91 |
157 |
if prop in ('city', 'postcode', 'citycode'):
|
92 |
|
-
|