Skip to content

Commit 8cb3493

Browse files
committed
update overall code structure & generify search
1 parent 29384e6 commit 8cb3493

File tree

9 files changed

+199
-160
lines changed

9 files changed

+199
-160
lines changed

packages/clients/textLocator/src/addPlugins.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Zoom from '@polar/plugin-zoom'
1212

1313
import Header from './plugins/Header'
1414
import GeometrySearch from './plugins/GeometrySearch'
15-
import { searchCoastalGazetteer } from './utils/coastalGazetteer/toponymSearch'
15+
import { searchCoastalGazetteerByToponym } from './utils/coastalGazetteer/searchToponym'
1616
import { idRegister } from './services'
1717

1818
// this is fine for list-like setup functions
@@ -56,7 +56,10 @@ export const addPlugins = (core) => {
5656
searchMethods: [{ type: 'coastalGazetteer' }],
5757
addLoading: 'plugin/loadingIndicator/addLoadingKey',
5858
removeLoading: 'plugin/loadingIndicator/removeLoadingKey',
59-
customSearchMethods: { coastalGazetteer: searchCoastalGazetteer },
59+
customSearchMethods: {
60+
// @ts-expect-error | Local parameter requirements diverge from type
61+
coastalGazetteer: searchCoastalGazetteerByToponym,
62+
},
6063
customSelectResult: {
6164
/* categoryDenkmalsucheAutocomplete: selectResult */
6265
},

packages/clients/textLocator/src/locales.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const locales: LanguageOption[] = [
2626
[wmtsTopplusOpenLight]: 'TopPlusOpen (Light)',
2727
[wmtsTopplusOpenLightGrey]: 'TopPlusOpen (Light, Grau)',
2828
},
29+
addressSearch: {
30+
unnamed: 'Unbenannt',
31+
},
2932
attributions: {
3033
[openStreetMap]: `$t(textLocator.layers.${openStreetMap}): © <a href='https://www.openstreetmap.org/copyright' target='_blank'>OpenStreetMap</a> contributors`,
3134
[openSeaMap]: `$t(textLocator.layers.${openSeaMap}): © <a href='https://www.openseamap.org/ target='_blank''>OpenSeaMap</a>`,
@@ -38,7 +41,8 @@ const locales: LanguageOption[] = [
3841
'<br><a href="https://github.com/Dataport/polar/blob/main/LEGALNOTICE.md">Impressum</a>',
3942
},
4043
error: {
41-
searchCoastalGazetteer: 'Die Suche ist fehlgeschlagen. TODO',
44+
searchCoastalGazetteer:
45+
'Die Suche ist mit einem unbekannten Fehler fehlgeschlagen. Bitte versuchen Sie es später erneut.',
4246
},
4347
},
4448
plugins: {
@@ -61,17 +65,29 @@ const locales: LanguageOption[] = [
6165
[wmtsTopplusOpenLight]: 'TopPlusOpen (Light)',
6266
[wmtsTopplusOpenLightGrey]: 'TopPlusOpen (Light, Grey)',
6367
},
68+
addressSearch: {
69+
unnamed: 'Unnamed',
70+
},
71+
attributions: {
72+
[openStreetMap]: `$t(textLocator.layers.${openStreetMap}): © <a href='https://www.openstreetmap.org/copyright' target='_blank'>OpenStreetMap</a> contributors`,
73+
[openSeaMap]: `$t(textLocator.layers.${openSeaMap}): © <a href='https://www.openseamap.org/ target='_blank''>OpenSeaMap</a>`,
74+
[mdiSeaNames]: `$t(textLocator.layers.${mdiSeaNames}): © <a href='https://projekt.mdi-de.org/kuesten-gazetteer/ target='_blank''>MDI DE</a>`,
75+
[wmtsTopplusOpenWeb]: `$t(textLocator.layers.${wmtsTopplusOpenWeb}): © Bundesamt für Kartographie und Geodäsie <YEAR>`,
76+
[wmtsTopplusOpenWebGrey]: `$t(textLocator.layers.${wmtsTopplusOpenWebGrey}): © Bundesamt für Kartographie und Geodäsie <YEAR>`,
77+
[wmtsTopplusOpenLight]: `$t(textLocator.layers.${wmtsTopplusOpenLight}): © Bundesamt für Kartographie und Geodäsie <YEAR>`,
78+
[wmtsTopplusOpenLightGrey]: `$t(textLocator.layers.${wmtsTopplusOpenLightGrey}): © Bundesamt für Kartographie und Geodäsie <YEAR>`,
79+
static:
80+
'<br><a href="https://github.com/Dataport/polar/blob/main/LEGALNOTICE.md">Legal notice (Impressum)</a>',
81+
},
82+
error: {
83+
searchCoastalGazetteer:
84+
'The search failed with an unknown error. Please try again later.',
85+
},
6486
},
65-
attributions: {
66-
[openStreetMap]: `$t(textLocator.layers.${openStreetMap}): © <a href='https://www.openstreetmap.org/copyright' target='_blank'>OpenStreetMap</a> contributors`,
67-
[openSeaMap]: `$t(textLocator.layers.${openSeaMap}): © <a href='https://www.openseamap.org/ target='_blank''>OpenSeaMap</a>`,
68-
[mdiSeaNames]: `$t(textLocator.layers.${mdiSeaNames}): © <a href='https://projekt.mdi-de.org/kuesten-gazetteer/ target='_blank''>MDI DE</a>`,
69-
[wmtsTopplusOpenWeb]: `$t(textLocator.layers.${wmtsTopplusOpenWeb}): © Bundesamt für Kartographie und Geodäsie <YEAR>`,
70-
[wmtsTopplusOpenWebGrey]: `$t(textLocator.layers.${wmtsTopplusOpenWebGrey}): © Bundesamt für Kartographie und Geodäsie <YEAR>`,
71-
[wmtsTopplusOpenLight]: `$t(textLocator.layers.${wmtsTopplusOpenLight}): © Bundesamt für Kartographie und Geodäsie <YEAR>`,
72-
[wmtsTopplusOpenLightGrey]: `$t(textLocator.layers.${wmtsTopplusOpenLightGrey}): © Bundesamt für Kartographie und Geodäsie <YEAR>`,
73-
static:
74-
'<br><a href="https://github.com/Dataport/polar/blob/main/LEGALNOTICE.md">Legal notice (Impressum)</a>',
87+
plugins: {
88+
addressSearch: {
89+
defaultGroup: 'Location search',
90+
},
7591
},
7692
},
7793
},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { WKT, GeoJSON } from 'ol/format'
2+
3+
export const ignoreIds = {
4+
global: ['EuroNat-33'],
5+
geometries: [
6+
'EuroNat-33',
7+
'SH-WATTENMEER-DM-1',
8+
'SH-WATTENMEER-1',
9+
'Ak2006-51529',
10+
'Landsg-2016-110',
11+
],
12+
}
13+
export const wellKnownText = new WKT()
14+
export const geoJson = new GeoJSON()
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { CoreState } from '@polar/lib-custom-types'
2+
import { Store } from 'vuex'
3+
import { MakeRequestUrlParameters, ResponsePayload } from './types'
4+
import { makeRequestUrl } from './makeRequestUrl'
5+
6+
const getEmptyResponsePayload = (): ResponsePayload => ({
7+
count: '',
8+
currentpage: '',
9+
pages: '',
10+
keyword: '',
11+
querystring: '',
12+
results: [],
13+
time: NaN,
14+
})
15+
16+
const mergeResponses = (
17+
initialResponse: ResponsePayload,
18+
responses: ResponsePayload[]
19+
) => ({
20+
...initialResponse,
21+
currentpage: 'merged',
22+
results: [
23+
initialResponse.results,
24+
...responses.map(({ results }) => results),
25+
].flat(1),
26+
time: NaN, // not used, setting NaN to indicate it's not the actual time
27+
})
28+
29+
export async function getAllPages(
30+
this: Store<CoreState>,
31+
signal: AbortSignal,
32+
url: string,
33+
params: Partial<MakeRequestUrlParameters>,
34+
epsg: `EPSG:${string}`
35+
): Promise<ResponsePayload> {
36+
const response = await fetch(makeRequestUrl(url, params, epsg), {
37+
method: 'GET',
38+
signal,
39+
})
40+
41+
if (!response.ok) {
42+
this.dispatch('plugin/toast/addToast', {
43+
type: 'error',
44+
text: 'textLocator.error.searchCoastalGazetteer',
45+
})
46+
console.error('Gazetteer response:', response)
47+
return getEmptyResponsePayload()
48+
}
49+
const responsePayload: ResponsePayload = await response.json()
50+
const pages = parseInt(responsePayload.pages, 10)
51+
const initialRequestMerge = typeof params.page === 'undefined' && pages > 1
52+
53+
if (!initialRequestMerge) {
54+
return responsePayload
55+
}
56+
57+
return mergeResponses(
58+
responsePayload,
59+
await Promise.all(
60+
Array.from(Array(pages - 1)).map((_, index) =>
61+
getAllPages.call(
62+
this,
63+
signal,
64+
url,
65+
{
66+
...params,
67+
page: `${index + 2}`,
68+
},
69+
epsg
70+
)
71+
)
72+
)
73+
)
74+
}
Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
import { Geometry } from 'geojson'
2-
import { GeoJSON, WKT } from 'ol/format'
3-
4-
const geoJson = new GeoJSON()
5-
const wellKnownText = new WKT()
6-
7-
interface RequestPayload {
8-
keyword: string
9-
searchType: 'like' | 'exact' | 'id'
10-
lang: '-' | string
11-
sdate: string
12-
edate: string
13-
type: '-' | string
14-
page?: string // numerical
15-
geom?: string
16-
}
1+
import { wgs84ProjectionCode } from '../common'
2+
import { geoJson, wellKnownText } from './common'
3+
import { MakeRequestUrlParameters, RequestPayload } from './types'
174

185
const searchRequestDefaultPayload: Partial<RequestPayload> = {
196
searchType: 'like',
@@ -25,23 +12,22 @@ const searchRequestDefaultPayload: Partial<RequestPayload> = {
2512

2613
export const makeRequestUrl = (
2714
url: string,
28-
inputValue: string,
29-
page: string | undefined,
30-
geometry: Geometry | undefined,
15+
{ keyword, page, geometry, ...rest }: Partial<MakeRequestUrlParameters>,
3116
epsg: string
3217
): string =>
3318
`${url}?${new URLSearchParams({
3419
...searchRequestDefaultPayload,
35-
keyword: inputValue ? `*${inputValue}*` : '',
20+
keyword: keyword ? `*${keyword}*` : '',
3621
...(typeof page !== 'undefined' ? { page } : {}),
3722
...(typeof geometry !== 'undefined'
3823
? {
3924
geom: wellKnownText.writeGeometry(
4025
geoJson.readGeometry(geometry, {
4126
dataProjection: epsg,
42-
featureProjection: 'EPSG:4326',
27+
featureProjection: wgs84ProjectionCode,
4328
})
4429
),
4530
}
4631
: {}),
32+
...rest,
4733
}).toString()}`

0 commit comments

Comments
 (0)