Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
xbarin02 authored Nov 18, 2023
1 parent 93c8a0c commit 18d1877
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export { default as HereProvider } from './providers/hereProvider';
export { default as LocationIQProvider } from './providers/locationIQProvider';
export { default as OpenCageProvider } from './providers/openCageProvider';
export { default as OpenStreetMapProvider } from './providers/openStreetMapProvider';
export { default as CivilDefenseMapProvider } from './providers/civilDefenseMapProvider';
export { default as PeliasProvider } from './providers/peliasProvider';
export { default as MapBoxProvider } from './providers/mapBoxProvider';
export { default as GeoApiFrProvider } from './providers/geoApiFrProvider';
Expand Down
76 changes: 76 additions & 0 deletions src/providers/civilDefenseMapProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import AbstractProvider, {
EndpointArgument,
ParseArgument,
ProviderOptions,
RequestType,
SearchResult,
} from './provider';
import hasShape from '../lib/hasShape';

Check warning on line 8 in src/providers/civilDefenseMapProvider.ts

View workflow job for this annotation

GitHub Actions / Eslint

'hasShape' is defined but never used

export type RequestResult = RawResult[];

export interface RawResult {
place_id: string;
licence: string;
osm_type: string;
osm_id: number;
boundingbox: [string, string, string, string];
lat: string;
lon: string;
display_name: string;
class: string;
type: string;
importance: number;
icon?: string;
}

export type OpenStreetMapProviderOptions = {
searchUrl?: string;
reverseUrl?: string;
} & ProviderOptions;

export default class CivilDefenseMapProvider extends AbstractProvider<
RawResult[],
RawResult
> {
searchUrl: string;
reverseUrl: string;

constructor(options: OpenStreetMapProviderOptions = {}) {
super(options);

const host = 'https://civildefense.fit.vutbr.cz';
this.searchUrl = options.searchUrl || `${host}/search`;
this.reverseUrl = options.reverseUrl || `${host}/reverse`;
}

endpoint({ query, type }: EndpointArgument): string {
const params = typeof query === 'string' ? { q: query } : query;
params.format = 'json';

switch (type) {
case RequestType.REVERSE:
return this.getUrl(this.reverseUrl, params);

default:
return this.getUrl(this.searchUrl, params);
}
}

parse(response: ParseArgument<RequestResult>): SearchResult<RawResult>[] {
const records = Array.isArray(response.data)
? response.data
: [response.data];

return records.map((r) => ({
x: Number(r.lon),
y: Number(r.lat),
label: r.display_name,
bounds: [
[parseFloat(r.boundingbox[0]), parseFloat(r.boundingbox[2])], // s, w
[parseFloat(r.boundingbox[1]), parseFloat(r.boundingbox[3])], // n, e
],
raw: r,
}));
}
}
1 change: 1 addition & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as LegacyGoogleProvider } from './legacyGoogleProvider';
export { default as LocationIQProvider } from './locationIQProvider';
export { default as OpenCageProvider } from './openCageProvider';
export { default as OpenStreetMapProvider } from './openStreetMapProvider';
export { default as CivilDefenseMapProvider } from './civilDefenseMapProvider';
export { default as PeliasProvider } from './peliasProvider';
export { default as MapBoxProvider } from './mapBoxProvider';
export { default as GeoApiFrProvider } from './geoApiFrProvider';
Expand Down

0 comments on commit 18d1877

Please sign in to comment.