Skip to content

Commit

Permalink
Replace geocoder with servicemap API
Browse files Browse the repository at this point in the history
  • Loading branch information
henrinie-nc committed Aug 2, 2024
1 parent 5a32620 commit a54fb98
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 44 deletions.
41 changes: 19 additions & 22 deletions src/map/HelsinkiProvider.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import AbstractProvider, {
import { JsonProvider } from 'leaflet-geosearch';
import type {
EndpointArgument,
ParseArgument,
SearchArgument,
SearchResult,
ParseArgument,
ProviderParams,
} from 'leaflet-geosearch/lib/providers/provider.js';

import {
HelsinkiGeocoderResponse,
HelsinkiGeocoderResponseItem,
} from './types';
import type { ServiceMapResponse, ServiceMapAddress } from './types';

export default class HelsinkiProvider extends AbstractProvider<
HelsinkiGeocoderResponse,
HelsinkiGeocoderResponseItem
const SERVICE_MAP_URL = 'https://api.hel.fi/servicemap/v2';
export default class HelsinkiProvider extends JsonProvider<
ServiceMapResponse,
ServiceMapAddress
> {
getParamString(params: Record<string, string | number | boolean>): string {
getParamString(params: ProviderParams): string {
return Object.keys(params)
.map(
(key) =>
Expand All @@ -27,32 +27,29 @@ export default class HelsinkiProvider extends AbstractProvider<
const url = this.endpoint({ query });

const request = await fetch(url);
const json = await request.json();
const json = (await request.json()) as ServiceMapResponse;

return this.parse({ data: json });
}

endpoint({ query }: EndpointArgument = { query: '' }): string {
const { params } = this.options;

const paramString = this.getParamString({
...params,
name: query as string,
q: query as string,
});

return `https://dev.hel.fi/geocoder/v1/address/?${paramString}&municipality=91`;
return `${SERVICE_MAP_URL}/search/?${paramString}&type=address&municipality=helsinki`;
}

parse({
data: { objects },
}: ParseArgument<HelsinkiGeocoderResponse>): Array<SearchResult> {
return objects.map((r) => {
parse({ data }: ParseArgument<ServiceMapResponse>): Array<SearchResult> {
return data.results?.map((address) => {
return {
x: r.location.coordinates[0],
y: r.location.coordinates[1],
label: r.name,
x: address.location?.coordinates[0] ?? 0,
y: address.location?.coordinates[1] ?? 0,
label: address.name?.fi ?? '',
bounds: null,
raw: r,
raw: address,
};
});
}
Expand Down
64 changes: 42 additions & 22 deletions src/map/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import i18n from '../i18n';
import { Point } from 'geojson';

export enum MapLayer {
generalMap = 'generalMap',
Expand Down Expand Up @@ -53,30 +52,51 @@ export const MapLayers: Record<MapLayer, MapLayerProperties> = {
format: 'image/png',
label: i18n.t(
'map.mapLayers.helsinkiOwnedAreas',
'Areas owned by the City of Helsinki'
'Areas owned by the City of Helsinki',
),
},
};

export type HelsinkiGeocoderResponse = {
meta: {
limit: number;
offset: number;
total_count: number;
previous: string | null;
next: string | null;
export interface ServiceMapAddress {
object_type: 'address';
name: {
fi: string;
sv: string;
en: string;
};
objects: Array<HelsinkiGeocoderResponseItem>;
};

export type HelsinkiGeocoderResponseItem = {
id: number;
location: Point;
name: string;
street: string;
number: number;
number: string;
number_end: string;
letter: string;
number_end: string | null;
municipality: string;
resource_uri: string;
};
modified_at: string;
municipality: {
id: string;
name: {
fi: string;
sv: string;
};
};
street: {
name: {
fi: string;
sv?: string;
en?: string;
};
};
location: {
type: 'Point';
coordinates: [number, number];
};
}

export interface ServiceMapResponse {
count: number;
next: string | null;
previous: string | null;
results: Array<ServiceMapAddress>;
}

export interface AddressResult {
x: number;
y: number;
label: string;
}

0 comments on commit a54fb98

Please sign in to comment.