diff --git a/spec/__snapshots__/pelias.spec.ts.snap b/spec/__snapshots__/pelias.spec.ts.snap index 91a635bd..129b7c6e 100644 --- a/spec/__snapshots__/pelias.spec.ts.snap +++ b/spec/__snapshots__/pelias.spec.ts.snap @@ -19,24 +19,47 @@ exports[`L.Control.Geocoder.Openrouteservice > geocodes Innsbruck 1`] = ` "lat": 47.272308, "lng": 11.407851, }, - "name": "Innsbruck, Austria", + "name": "Innsbruck, TR, Austria", "properties": { "accuracy": "centroid", - "confidence": 1, + "addendum": { + "concordances": { + "dbp:id": "Innsbruck", + "fb:id": "en.innsbruck", + "gn:id": 2775220, + "gp:id": 550763, + "nyt:id": "N21126484242539583751", + "qs_pg:id": 274233, + "wd:id": "Q1735", + "wk:page": "Innsbruck", + }, + }, + "category": [ + "admin:admin2", + "admin", + ], "continent": "Europe", + "continent_gid": "whosonfirst:continent:102191581", "country": "Austria", "country_a": "AUT", + "country_code": "AT", + "country_gid": "whosonfirst:country:85632785", "county": "Innsbruck", - "county_a": "IN", + "county_gid": "whosonfirst:county:102049723", + "distance": 739.011, + "gid": "whosonfirst:locality:101748061", "id": "101748061", - "label": "Innsbruck, Austria", + "label": "Innsbruck, TR, Austria", "layer": "locality", "localadmin": "Innsbruck", + "localadmin_gid": "whosonfirst:localadmin:1108837687", "locality": "Innsbruck", - "match_type": "exact", + "locality_gid": "whosonfirst:locality:101748061", "name": "Innsbruck", - "region": "Tirol", + "region": "Tyrol", "region_a": "TR", + "region_gid": "whosonfirst:region:85681661", + "source": "whosonfirst", "source_id": "101748061", }, }, diff --git a/spec/pelias.spec.ts b/spec/pelias.spec.ts index 7d119b34..9df0b0be 100644 --- a/spec/pelias.spec.ts +++ b/spec/pelias.spec.ts @@ -13,8 +13,17 @@ describe('L.Control.Geocoder.Openrouteservice', () => { geocoding: { version: '0.2', attribution: 'openrouteservice.org | OpenStreetMap contributors | Geocoding by Pelias', - query: {}, - warnings: ["performance optimization: excluding 'address' layer"], + query: { + size: 10, + lang: { + name: 'English', + iso6391: 'en', + iso6393: 'eng', + via: 'header', + defaulted: false + } + }, + warnings: [`performance optimization: excluding 'address' layer`], engine: { name: 'Pelias', author: 'Mapzen', version: '1.0' } }, type: 'FeatureCollection', @@ -24,22 +33,45 @@ describe('L.Control.Geocoder.Openrouteservice', () => { geometry: { type: 'Point', coordinates: [11.407851, 47.272308] }, properties: { id: '101748061', + gid: 'whosonfirst:locality:101748061', layer: 'locality', + source: 'whosonfirst', source_id: '101748061', + country_code: 'AT', name: 'Innsbruck', - confidence: 1, - match_type: 'exact', + distance: 739.011, accuracy: 'centroid', country: 'Austria', + country_gid: 'whosonfirst:country:85632785', country_a: 'AUT', - region: 'Tirol', + region: 'Tyrol', + region_gid: 'whosonfirst:region:85681661', region_a: 'TR', county: 'Innsbruck', - county_a: 'IN', + county_gid: 'whosonfirst:county:102049723', localadmin: 'Innsbruck', + localadmin_gid: 'whosonfirst:localadmin:1108837687', locality: 'Innsbruck', + locality_gid: 'whosonfirst:locality:101748061', continent: 'Europe', - label: 'Innsbruck, Austria' + continent_gid: 'whosonfirst:continent:102191581', + label: 'Innsbruck, TR, Austria', + category: [ + 'admin:admin2', + 'admin' + ], + addendum: { + concordances: { + 'dbp:id': 'Innsbruck', + 'fb:id': 'en.innsbruck', + 'gn:id': 2775220, + 'gp:id': 550763, + 'nyt:id': 'N21126484242539583751', + 'qs_pg:id': 274233, + 'wd:id': 'Q1735', + 'wk:page': 'Innsbruck' + } + } }, bbox: [11.3218091258, 47.2470573997, 11.452584553, 47.29398] } @@ -50,7 +82,7 @@ describe('L.Control.Geocoder.Openrouteservice', () => { ); const feature = result[0]; - expect(feature.name).toBe('Innsbruck, Austria'); + expect(feature.name).toBe('Innsbruck, TR, Austria'); expect(feature.center).toStrictEqual({ lat: 47.272308, lng: 11.407851 }); expect(feature.bbox).toStrictEqual({ _southWest: { lat: 47.2470573997, lng: 11.3218091258 }, diff --git a/src/geocoders/pelias.ts b/src/geocoders/pelias.ts index de6b968e..25e65fab 100644 --- a/src/geocoders/pelias.ts +++ b/src/geocoders/pelias.ts @@ -22,7 +22,7 @@ export class Pelias implements IGeocoder { text: query }); const data = await getJSON(this.options.serviceUrl + '/search', params); - return this._parseResults(data, 'bbox'); + return this._parseResults(data); } async suggest(query: string): Promise { @@ -31,7 +31,7 @@ export class Pelias implements IGeocoder { text: query }); const data = await getJSON(this.options.serviceUrl + '/autocomplete', params); - return this._parseResults(data, 'bbox'); + return this._parseResults(data); } async reverse(location: L.LatLngLiteral, scale: number): Promise { @@ -41,42 +41,26 @@ export class Pelias implements IGeocoder { 'point.lon': location.lng }); const data = await getJSON(this.options.serviceUrl + '/reverse', params); - return this._parseResults(data, 'bounds'); + return this._parseResults(data); } - _parseResults(data, bboxname): GeocodingResult[] { - const results: GeocodingResult[] = []; - new L.GeoJSON(data, { - pointToLayer(feature, latlng) { - return new L.CircleMarker(latlng, {radius: 10}); - }, - onEachFeature(feature, layer: any) { - const result = {} as GeocodingResult; - let bbox; - let center; - - if (layer.getBounds) { - bbox = layer.getBounds(); - center = bbox.getCenter(); - } else if (layer.feature.bbox) { - center = layer.getLatLng(); - bbox = new L.LatLngBounds( - L.GeoJSON.coordsToLatLng(layer.feature.bbox.slice(0, 2)), - L.GeoJSON.coordsToLatLng(layer.feature.bbox.slice(2, 4)) - ); - } else { - center = layer.getLatLng(); - bbox = new L.LatLngBounds(center, center); - } - - result.name = layer.feature.properties.label; - result.center = center; - result[bboxname] = bbox; - result.properties = layer.feature.properties; - results.push(result); - } + _parseResults(data: GeoJSON.FeatureCollection): GeocodingResult[] { + return (data.features || []).map((f): GeocodingResult => { + const c = f.geometry.coordinates; + const center = new L.LatLng(c[1], c[0]); + + const bbox = + Array.isArray(f.bbox) && f.bbox.length === 4 + ? new L.LatLngBounds([f.bbox[1], f.bbox[0]], [f.bbox[3], f.bbox[2]]) + : new L.LatLngBounds(center, center); + + return { + name: f.properties!.label, + center, + bbox, + properties: f.properties + }; }); - return results; } } @@ -133,39 +117,130 @@ export type PeliasResponse = GeoJSON.FeatureCollection; +} + +interface Properties extends Identity, Labels, Scoring, Address, Hierarchy, Addendum {} + interface Geocoding { - version: string; + version: string; attribution: string; - query: Query; - warnings: string[]; - engine: Engine; + query: Query; + warnings: string[]; + engine: Engine; } interface Engine { - name: string; - author: string; + name: string; + author: string; version: string; } interface Query { + size: number; + lang: { + name: string; + iso6391: string; + iso6393: string; + via: string; + defaulted: boolean + } + text?: string; + parser?: string; + parsed_text?: Record; + sources?: string[]; + layers?: string[]; }