UNPKG

@myrmidon/cadmus-refs-geonames-lookup

Version:
199 lines (193 loc) 6.78 kB
import * as i0 from '@angular/core'; import { Injectable, InjectionToken, Inject } from '@angular/core'; import { retry, catchError, of } from 'rxjs'; import * as i1 from '@angular/common/http'; import * as i2 from '@myrmidon/ngx-tools'; import { map } from 'rxjs/operators'; /** * GeoNames search types. */ var GeoNamesSearchType; (function (GeoNamesSearchType) { GeoNamesSearchType[GeoNamesSearchType["query"] = 0] = "query"; GeoNamesSearchType[GeoNamesSearchType["name"] = 1] = "name"; GeoNamesSearchType[GeoNamesSearchType["nameEquals"] = 2] = "nameEquals"; GeoNamesSearchType[GeoNamesSearchType["nameStartsWith"] = 3] = "nameStartsWith"; })(GeoNamesSearchType || (GeoNamesSearchType = {})); const GEONAMES_URL = 'https://secure.geonames.org/'; /** * A service to search GeoNames. */ class GeoNamesService { _http; _error; constructor(_http, _error) { this._http = _http; this._error = _error; } /** * Search GeoNames for toponyms. * @param request The search request. * @returns The search result. */ search(request) { // adapt request to query parameters const r = Object.assign({}, request); delete r.searchType; delete r.text; delete r.url; if (request.bbox) { r.north = request.bbox.north; r.south = request.bbox.south; r.east = request.bbox.east; r.west = request.bbox.west; } switch (request.searchType) { case GeoNamesSearchType.query: r.q = request.text; break; case GeoNamesSearchType.name: r.name = request.text; break; case GeoNamesSearchType.nameEquals: r.name_equals = request.text; break; case GeoNamesSearchType.nameStartsWith: r.name_startsWith = request.text; break; } // remove undefined values const filteredParams = Object.fromEntries(Object.entries(r) .filter(([key, value]) => value !== undefined) .map(([key, value]) => [key, String(value)])); console.log(`${request.url || GEONAMES_URL}search: `, filteredParams); return this._http .get((request.url || GEONAMES_URL) + 'search', { params: { ...filteredParams }, }) .pipe(retry(3), catchError(this._error.handleError)); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: GeoNamesService, deps: [{ token: i1.HttpClient }, { token: i2.ErrorService }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: GeoNamesService, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: GeoNamesService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [{ type: i1.HttpClient }, { type: i2.ErrorService }] }); /** * The token to provide the GeoNames user name. */ const GEONAMES_USERNAME_TOKEN = new InjectionToken('GEONAMES_USERNAME'); /** * Base options for a GeoNames search request. */ const BASE_OPTIONS = { searchType: 0, text: '', userName: '', type: 'json', }; /** * GeoNames reference lookup service. * You should provide the GEONAMES_USERNAME_TOKEN token with your * GeoNames user name. */ class GeoNamesRefLookupService { _userName; _geonames; constructor(_userName, _geonames) { this._userName = _userName; this._geonames = _geonames; } /** * Lookup toponyms. * @param filter The filter. * @param options The search options. * @returns Observable of toponyms. */ lookup(filter, options) { if (!filter.text) { return of([]); } if (!options) { options = BASE_OPTIONS; } // override userName if not set if (!options.userName) { options.userName = this._userName; } // override max rows and text if (options) { options.maxRows = filter.limit; } else { options = BASE_OPTIONS; } options.text = filter.text; console.info('GeoNames lookup: ', options); return this._geonames.search(options).pipe(map((r) => { return r.geonames; })); } /** * Get the name to display for the specified toponym. * @param item The toponym. * @returns The name to display for the specified toponym. */ getName(item) { if (!item) { return ''; } const sb = []; sb.push(item.name); // admin names if (item.adminName1) { sb.push(', '); sb.push(item.adminName1); } if (item.adminName2) { sb.push(', '); sb.push(item.adminName2); } if (item.adminName3) { sb.push(', '); sb.push(item.adminName3); } if (item.adminName4) { sb.push(', '); sb.push(item.adminName4); } if (item.adminName5) { sb.push(', '); sb.push(item.adminName5); } // country if (item.countryName) { sb.push(' ('); sb.push(item.countryName); sb.push(')'); } return sb.join(''); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: GeoNamesRefLookupService, deps: [{ token: GEONAMES_USERNAME_TOKEN }, { token: GeoNamesService }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: GeoNamesRefLookupService, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: GeoNamesRefLookupService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [{ type: undefined, decorators: [{ type: Inject, args: [GEONAMES_USERNAME_TOKEN] }] }, { type: GeoNamesService }] }); /* * Public API Surface of cadmus-refs-geonames-lookup */ /** * Generated bundle index. Do not edit. */ export { GEONAMES_USERNAME_TOKEN, GeoNamesRefLookupService, GeoNamesSearchType, GeoNamesService }; //# sourceMappingURL=myrmidon-cadmus-refs-geonames-lookup.mjs.map