Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions spec/__snapshots__/pelias.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
Expand Down
48 changes: 40 additions & 8 deletions spec/pelias.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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]
}
Expand All @@ -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 },
Expand Down
193 changes: 134 additions & 59 deletions src/geocoders/pelias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Pelias implements IGeocoder {
text: query
});
const data = await getJSON<any>(this.options.serviceUrl + '/search', params);
return this._parseResults(data, 'bbox');
return this._parseResults(data);
}

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

async reverse(location: L.LatLngLiteral, scale: number): Promise<GeocodingResult[]> {
Expand All @@ -41,42 +41,26 @@ export class Pelias implements IGeocoder {
'point.lon': location.lng
});
const data = await getJSON<any>(this.options.serviceUrl + '/reverse', params);
return this._parseResults(data, 'bounds');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't recall the history of this but the bbox param isn't named bounds, someone has put effort into supporting alternative key names and I'm not sure why 🤷

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<GeoJSON.Point>): 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;
}
}

Expand Down Expand Up @@ -133,39 +117,130 @@ export type PeliasResponse = GeoJSON.FeatureCollection<GeoJSON.Geometry, Propert
geocoding: Geocoding;
};

interface Properties {
id: string;
layer: string;
source_id: string;
name: string;
confidence: number;
match_type: string;
accuracy: string;
country: string;
country_a: string;
region: string;
region_a: string;
county: string;
county_a: string;
localadmin: string;
locality: string;
continent: string;
label: string;
interface Identity {
id: string;
gid: string;
layer: string;
source: string;
source_id: string;
}

interface Labels {
name: string;
label: string;
category?: string[];
}

interface Hierarchy {
country_code?: string;

ocean?: string;
ocean_gid?: string;
ocean_a?: string;

marinearea?: string;
marinearea_gid?: string;
marinearea_a?: string;

continent?: string;
continent_gid?: string;
continent_a?: string;

empire?: string;
empire_gid?: string;
empire_a?: string;

country?: string;
country_gid?: string;
country_a?: string;

dependency?: string;
dependency_gid?: string;
dependency_a?: string;

macroregion?: string;
macroregion_gid?: string;
macroregion_a?: string;

region?: string;
region_gid?: string;
region_a?: string;

macrocounty?: string;
macrocounty_gid?: string;
macrocounty_a?: string;

county?: string;
county_gid?: string;
county_a?: string;

localadmin?: string;
localadmin_gid?: string;
localadmin_a?: string;

locality?: string;
locality_gid?: string;
locality_a?: string;

borough?: string;
borough_gid?: string;
borough_a?: string;

neighbourhood?: string;
neighbourhood_gid?: string;
neighbourhood_a?: string;

postalcode?: string;
postalcode_gid?: string;
postalcode_a?: string;
}

interface Address {
unit?: string;
housenumber?: string;
street?: string;
postalcode?: string;
}

interface Scoring {
accuracy: string;
confidence?: number;
distance?: number;
match_type?: string;
}

interface Addendum {
addendum?: Record<string, Object>;
}

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<string, string>;
sources?: string[];
layers?: string[];
}