Adding buttons to reverse geocode routes.

This commit is contained in:
Eric van der Vlist 2023-01-05 18:27:20 +01:00
parent 69148f6e1d
commit 97f11239e8
4 changed files with 66 additions and 11 deletions

View File

@ -1,12 +1,16 @@
import { Component, createResource } from 'solid-js'; import { Component, createResource, Show } from 'solid-js';
import { peekCachedSignal } from '../../workers/cached-signals'; import { peekCachedSignal } from '../../workers/cached-signals';
import RteIcon from '../../icons/directions-svgrepo-com.svg?component-solid'; import RteIcon from '../../icons/directions-svgrepo-com.svg?component-solid';
import SearchIcon from '@suid/icons-material/Search';
import { useI18n } from '@solid-primitives/i18n'; import { useI18n } from '@solid-primitives/i18n';
import Tree from '../tree'; import Tree from '../tree';
import { findAddress, getVillageOrTown } from '../../lib/osm'; import { findAddress, getVillageOrTown } from '../../lib/osm';
import { getLength } from 'ol/sphere'; import { getLength } from 'ol/sphere';
import { Geometry, LineString } from 'ol/geom'; import { Geometry, LineString } from 'ol/geom';
import { IconButton } from '@suid/material';
import { cloneDeep } from 'lodash';
import dispatch from '../../workers/dispatcher-main';
interface Props { interface Props {
rteId: string; rteId: string;
@ -21,12 +25,12 @@ const RteViewer: Component<Props> = ({ rteId }) => {
return rte().name; return rte().name;
}; };
const [startAddress] = createResource(rte, (r: Rte) => // const [startAddress] = createResource(rte, (r: Rte) =>
findAddress(r.rtept[0].$.lon, r.rtept[0].$.lat, locale) // findAddress(r.rtept[0].$.lon, r.rtept[0].$.lat, locale)
); // );
const [endAddress] = createResource(rte, (r: Rte) => // const [endAddress] = createResource(rte, (r: Rte) =>
findAddress(r.rtept.at(-1).$.lon, r.rtept.at(-1).$.lat, locale) // findAddress(r.rtept.at(-1).$.lon, r.rtept.at(-1).$.lat, locale)
); // );
const formatLength = (length: number) => { const formatLength = (length: number) => {
if (length > 1000) { if (length > 1000) {
@ -48,6 +52,24 @@ const RteViewer: Component<Props> = ({ rteId }) => {
const length = getLength(geometry, { projection: 'EPSG:4326' }); const length = getLength(geometry, { projection: 'EPSG:4326' });
const findClickHandlerFactory = (target: string) => async () => {
const index = target === 'from' ? 0 : -1;
const address = await findAddress(
rte().rtept.at(index).$.lon,
rte().rtept.at(index).$.lat,
locale
);
const updatedRte = cloneDeep(rte());
if (updatedRte.extensions === undefined) {
updatedRte.extensions = {};
}
updatedRte.extensions[target] = address;
dispatch({
action: 'putRte',
params: { id: rteId, rte: updatedRte },
});
};
return ( return (
<Tree <Tree
title={ title={
@ -57,9 +79,32 @@ const RteViewer: Component<Props> = ({ rteId }) => {
} }
content={ content={
<> <>
<div>{`${formatLength(length)} ${t('from')} ${getVillageOrTown( <div>
startAddress() {formatLength(length)} {t('from')}{' '}
)} ${t('to')} ${getVillageOrTown(endAddress())} `}</div> <Show
when={!rte()?.extensions?.from}
fallback={<>{getVillageOrTown(rte()?.extensions?.from)}</>}
>
<IconButton
color='primary'
onClick={findClickHandlerFactory('from')}
>
<SearchIcon />
</IconButton>
</Show>{' '}
{t('to')}{' '}
<Show
when={!rte()?.extensions?.to}
fallback={<>{getVillageOrTown(rte()?.extensions?.to)}</>}
>
<IconButton
color='primary'
onClick={findClickHandlerFactory('to')}
>
<SearchIcon />
</IconButton>
</Show>
</div>
</> </>
} }
subTree={undefined} subTree={undefined}

View File

@ -52,3 +52,10 @@ export const getRte = async (params: any) => {
}); });
return rte; return rte;
}; };
export const putRte = async (params: any) => {
const { id, rte } = params;
rte.rtept = undefined;
await put(id, 'rte', (doc) => rte, rte);
return rte;
};

View File

@ -8,7 +8,7 @@ export const _findAddress = async (
try { try {
const response = await fetch( const response = await fetch(
`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2&addressdetails=1&extratags=1&namedetails=1&accept-language=${locale()}`, `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2&addressdetails=1&extratags=1&namedetails=1&accept-language=${locale()}`,
{} { headers: { 'User-Agent': 'Dyomedea' } }
); );
const data = await response.json(); const data = await response.json();
console.log({ caller: 'findAddress', lon, lat, data }); console.log({ caller: 'findAddress', lon, lat, data });

View File

@ -12,6 +12,7 @@ import {
getAllGpxes, getAllGpxes,
getAllGpxesWithSummary, getAllGpxesWithSummary,
} from '../db/gpx'; } from '../db/gpx';
import { putRte } from '../db/rte';
import { getSettings, putSettings } from '../db/settings'; import { getSettings, putSettings } from '../db/settings';
import { getState, setState } from '../db/state'; import { getState, setState } from '../db/state';
import { getTrk, putNewTrk } from '../db/trk'; import { getTrk, putNewTrk } from '../db/trk';
@ -47,6 +48,8 @@ onmessage = async function (e) {
getWpt, getWpt,
putWpt, putWpt,
putRte,
getState, getState,
setState, setState,