Skip to content

Commit e11217b

Browse files
authored
pelias: update Pelias provider (#365)
* pelias: standardize bbox param, simplify parser * pelias: update type defs
1 parent deabd80 commit e11217b

File tree

3 files changed

+203
-73
lines changed

3 files changed

+203
-73
lines changed

spec/__snapshots__/pelias.spec.ts.snap

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,47 @@ exports[`L.Control.Geocoder.Openrouteservice > geocodes Innsbruck 1`] = `
1919
"lat": 47.272308,
2020
"lng": 11.407851,
2121
},
22-
"name": "Innsbruck, Austria",
22+
"name": "Innsbruck, TR, Austria",
2323
"properties": {
2424
"accuracy": "centroid",
25-
"confidence": 1,
25+
"addendum": {
26+
"concordances": {
27+
"dbp:id": "Innsbruck",
28+
"fb:id": "en.innsbruck",
29+
"gn:id": 2775220,
30+
"gp:id": 550763,
31+
"nyt:id": "N21126484242539583751",
32+
"qs_pg:id": 274233,
33+
"wd:id": "Q1735",
34+
"wk:page": "Innsbruck",
35+
},
36+
},
37+
"category": [
38+
"admin:admin2",
39+
"admin",
40+
],
2641
"continent": "Europe",
42+
"continent_gid": "whosonfirst:continent:102191581",
2743
"country": "Austria",
2844
"country_a": "AUT",
45+
"country_code": "AT",
46+
"country_gid": "whosonfirst:country:85632785",
2947
"county": "Innsbruck",
30-
"county_a": "IN",
48+
"county_gid": "whosonfirst:county:102049723",
49+
"distance": 739.011,
50+
"gid": "whosonfirst:locality:101748061",
3151
"id": "101748061",
32-
"label": "Innsbruck, Austria",
52+
"label": "Innsbruck, TR, Austria",
3353
"layer": "locality",
3454
"localadmin": "Innsbruck",
55+
"localadmin_gid": "whosonfirst:localadmin:1108837687",
3556
"locality": "Innsbruck",
36-
"match_type": "exact",
57+
"locality_gid": "whosonfirst:locality:101748061",
3758
"name": "Innsbruck",
38-
"region": "Tirol",
59+
"region": "Tyrol",
3960
"region_a": "TR",
61+
"region_gid": "whosonfirst:region:85681661",
62+
"source": "whosonfirst",
4063
"source_id": "101748061",
4164
},
4265
},

spec/pelias.spec.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ describe('L.Control.Geocoder.Openrouteservice', () => {
1313
geocoding: {
1414
version: '0.2',
1515
attribution: 'openrouteservice.org | OpenStreetMap contributors | Geocoding by Pelias',
16-
query: {},
17-
warnings: ["performance optimization: excluding 'address' layer"],
16+
query: {
17+
size: 10,
18+
lang: {
19+
name: 'English',
20+
iso6391: 'en',
21+
iso6393: 'eng',
22+
via: 'header',
23+
defaulted: false
24+
}
25+
},
26+
warnings: [`performance optimization: excluding 'address' layer`],
1827
engine: { name: 'Pelias', author: 'Mapzen', version: '1.0' }
1928
},
2029
type: 'FeatureCollection',
@@ -24,22 +33,45 @@ describe('L.Control.Geocoder.Openrouteservice', () => {
2433
geometry: { type: 'Point', coordinates: [11.407851, 47.272308] },
2534
properties: {
2635
id: '101748061',
36+
gid: 'whosonfirst:locality:101748061',
2737
layer: 'locality',
38+
source: 'whosonfirst',
2839
source_id: '101748061',
40+
country_code: 'AT',
2941
name: 'Innsbruck',
30-
confidence: 1,
31-
match_type: 'exact',
42+
distance: 739.011,
3243
accuracy: 'centroid',
3344
country: 'Austria',
45+
country_gid: 'whosonfirst:country:85632785',
3446
country_a: 'AUT',
35-
region: 'Tirol',
47+
region: 'Tyrol',
48+
region_gid: 'whosonfirst:region:85681661',
3649
region_a: 'TR',
3750
county: 'Innsbruck',
38-
county_a: 'IN',
51+
county_gid: 'whosonfirst:county:102049723',
3952
localadmin: 'Innsbruck',
53+
localadmin_gid: 'whosonfirst:localadmin:1108837687',
4054
locality: 'Innsbruck',
55+
locality_gid: 'whosonfirst:locality:101748061',
4156
continent: 'Europe',
42-
label: 'Innsbruck, Austria'
57+
continent_gid: 'whosonfirst:continent:102191581',
58+
label: 'Innsbruck, TR, Austria',
59+
category: [
60+
'admin:admin2',
61+
'admin'
62+
],
63+
addendum: {
64+
concordances: {
65+
'dbp:id': 'Innsbruck',
66+
'fb:id': 'en.innsbruck',
67+
'gn:id': 2775220,
68+
'gp:id': 550763,
69+
'nyt:id': 'N21126484242539583751',
70+
'qs_pg:id': 274233,
71+
'wd:id': 'Q1735',
72+
'wk:page': 'Innsbruck'
73+
}
74+
}
4375
},
4476
bbox: [11.3218091258, 47.2470573997, 11.452584553, 47.29398]
4577
}
@@ -50,7 +82,7 @@ describe('L.Control.Geocoder.Openrouteservice', () => {
5082
);
5183

5284
const feature = result[0];
53-
expect(feature.name).toBe('Innsbruck, Austria');
85+
expect(feature.name).toBe('Innsbruck, TR, Austria');
5486
expect(feature.center).toStrictEqual({ lat: 47.272308, lng: 11.407851 });
5587
expect(feature.bbox).toStrictEqual({
5688
_southWest: { lat: 47.2470573997, lng: 11.3218091258 },

src/geocoders/pelias.ts

Lines changed: 134 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class Pelias implements IGeocoder {
2222
text: query
2323
});
2424
const data = await getJSON<any>(this.options.serviceUrl + '/search', params);
25-
return this._parseResults(data, 'bbox');
25+
return this._parseResults(data);
2626
}
2727

2828
async suggest(query: string): Promise<GeocodingResult[]> {
@@ -31,7 +31,7 @@ export class Pelias implements IGeocoder {
3131
text: query
3232
});
3333
const data = await getJSON<any>(this.options.serviceUrl + '/autocomplete', params);
34-
return this._parseResults(data, 'bbox');
34+
return this._parseResults(data);
3535
}
3636

3737
async reverse(location: L.LatLngLiteral, scale: number): Promise<GeocodingResult[]> {
@@ -41,42 +41,26 @@ export class Pelias implements IGeocoder {
4141
'point.lon': location.lng
4242
});
4343
const data = await getJSON<any>(this.options.serviceUrl + '/reverse', params);
44-
return this._parseResults(data, 'bounds');
44+
return this._parseResults(data);
4545
}
4646

47-
_parseResults(data, bboxname): GeocodingResult[] {
48-
const results: GeocodingResult[] = [];
49-
new L.GeoJSON(data, {
50-
pointToLayer(feature, latlng) {
51-
return new L.CircleMarker(latlng, {radius: 10});
52-
},
53-
onEachFeature(feature, layer: any) {
54-
const result = {} as GeocodingResult;
55-
let bbox;
56-
let center;
57-
58-
if (layer.getBounds) {
59-
bbox = layer.getBounds();
60-
center = bbox.getCenter();
61-
} else if (layer.feature.bbox) {
62-
center = layer.getLatLng();
63-
bbox = new L.LatLngBounds(
64-
L.GeoJSON.coordsToLatLng(layer.feature.bbox.slice(0, 2)),
65-
L.GeoJSON.coordsToLatLng(layer.feature.bbox.slice(2, 4))
66-
);
67-
} else {
68-
center = layer.getLatLng();
69-
bbox = new L.LatLngBounds(center, center);
70-
}
71-
72-
result.name = layer.feature.properties.label;
73-
result.center = center;
74-
result[bboxname] = bbox;
75-
result.properties = layer.feature.properties;
76-
results.push(result);
77-
}
47+
_parseResults(data: GeoJSON.FeatureCollection<GeoJSON.Point>): GeocodingResult[] {
48+
return (data.features || []).map((f): GeocodingResult => {
49+
const c = f.geometry.coordinates;
50+
const center = new L.LatLng(c[1], c[0]);
51+
52+
const bbox =
53+
Array.isArray(f.bbox) && f.bbox.length === 4
54+
? new L.LatLngBounds([f.bbox[1], f.bbox[0]], [f.bbox[3], f.bbox[2]])
55+
: new L.LatLngBounds(center, center);
56+
57+
return {
58+
name: f.properties!.label,
59+
center,
60+
bbox,
61+
properties: f.properties
62+
};
7863
});
79-
return results;
8064
}
8165
}
8266

@@ -133,39 +117,130 @@ export type PeliasResponse = GeoJSON.FeatureCollection<GeoJSON.Geometry, Propert
133117
geocoding: Geocoding;
134118
};
135119

136-
interface Properties {
137-
id: string;
138-
layer: string;
139-
source_id: string;
140-
name: string;
141-
confidence: number;
142-
match_type: string;
143-
accuracy: string;
144-
country: string;
145-
country_a: string;
146-
region: string;
147-
region_a: string;
148-
county: string;
149-
county_a: string;
150-
localadmin: string;
151-
locality: string;
152-
continent: string;
153-
label: string;
120+
interface Identity {
121+
id: string;
122+
gid: string;
123+
layer: string;
124+
source: string;
125+
source_id: string;
154126
}
155127

128+
interface Labels {
129+
name: string;
130+
label: string;
131+
category?: string[];
132+
}
133+
134+
interface Hierarchy {
135+
country_code?: string;
136+
137+
ocean?: string;
138+
ocean_gid?: string;
139+
ocean_a?: string;
140+
141+
marinearea?: string;
142+
marinearea_gid?: string;
143+
marinearea_a?: string;
144+
145+
continent?: string;
146+
continent_gid?: string;
147+
continent_a?: string;
148+
149+
empire?: string;
150+
empire_gid?: string;
151+
empire_a?: string;
152+
153+
country?: string;
154+
country_gid?: string;
155+
country_a?: string;
156+
157+
dependency?: string;
158+
dependency_gid?: string;
159+
dependency_a?: string;
160+
161+
macroregion?: string;
162+
macroregion_gid?: string;
163+
macroregion_a?: string;
164+
165+
region?: string;
166+
region_gid?: string;
167+
region_a?: string;
168+
169+
macrocounty?: string;
170+
macrocounty_gid?: string;
171+
macrocounty_a?: string;
172+
173+
county?: string;
174+
county_gid?: string;
175+
county_a?: string;
176+
177+
localadmin?: string;
178+
localadmin_gid?: string;
179+
localadmin_a?: string;
180+
181+
locality?: string;
182+
locality_gid?: string;
183+
locality_a?: string;
184+
185+
borough?: string;
186+
borough_gid?: string;
187+
borough_a?: string;
188+
189+
neighbourhood?: string;
190+
neighbourhood_gid?: string;
191+
neighbourhood_a?: string;
192+
193+
postalcode?: string;
194+
postalcode_gid?: string;
195+
postalcode_a?: string;
196+
}
197+
198+
interface Address {
199+
unit?: string;
200+
housenumber?: string;
201+
street?: string;
202+
postalcode?: string;
203+
}
204+
205+
interface Scoring {
206+
accuracy: string;
207+
confidence?: number;
208+
distance?: number;
209+
match_type?: string;
210+
}
211+
212+
interface Addendum {
213+
addendum?: Record<string, Object>;
214+
}
215+
216+
interface Properties extends Identity, Labels, Scoring, Address, Hierarchy, Addendum {}
217+
156218
interface Geocoding {
157-
version: string;
219+
version: string;
158220
attribution: string;
159-
query: Query;
160-
warnings: string[];
161-
engine: Engine;
221+
query: Query;
222+
warnings: string[];
223+
engine: Engine;
162224
}
163225

164226
interface Engine {
165-
name: string;
166-
author: string;
227+
name: string;
228+
author: string;
167229
version: string;
168230
}
169231

170232
interface Query {
233+
size: number;
234+
lang: {
235+
name: string;
236+
iso6391: string;
237+
iso6393: string;
238+
via: string;
239+
defaulted: boolean
240+
}
241+
text?: string;
242+
parser?: string;
243+
parsed_text?: Record<string, string>;
244+
sources?: string[];
245+
layers?: string[];
171246
}

0 commit comments

Comments
 (0)