2022-12-09 14:18:14 +00:00
|
|
|
import { Component, createEffect, createSignal, onCleanup } from 'solid-js';
|
2022-11-27 15:18:50 +00:00
|
|
|
|
2022-12-09 14:18:14 +00:00
|
|
|
import dispatch, { cancelDispatch } from '../../workers/dispatcher-main';
|
2022-11-27 16:12:25 +00:00
|
|
|
import VectorSource from 'ol/source/Vector';
|
|
|
|
import GeoJSON from 'ol/format/GeoJSON';
|
2022-11-27 15:18:50 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
trksegId: string;
|
2022-11-27 16:12:25 +00:00
|
|
|
vectorSource: VectorSource;
|
2022-11-27 15:18:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 16:12:25 +00:00
|
|
|
export const Trkseg: Component<Props> = ({ vectorSource, trksegId }) => {
|
2022-12-09 14:18:14 +00:00
|
|
|
const [trkseg, setTrkseg] = createSignal<Trkseg>();
|
|
|
|
const [dispatchId, setDispatchId] = createSignal<number>(-1);
|
|
|
|
|
|
|
|
const trksegCallBack = (error: any, result: any, id?: number) => {
|
|
|
|
if (error) {
|
|
|
|
console.error({ caller: 'trksegCallBack', error });
|
|
|
|
} else {
|
|
|
|
console.log({ caller: 'trksegCallBack', result });
|
|
|
|
setTrkseg(result);
|
|
|
|
}
|
|
|
|
if (id) {
|
|
|
|
setDispatchId(id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
{
|
|
|
|
action: 'getAndWatch',
|
|
|
|
params: {
|
|
|
|
id: trksegId,
|
|
|
|
method: 'getTrkseg',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
trksegCallBack,
|
|
|
|
true
|
2022-11-27 15:18:50 +00:00
|
|
|
);
|
|
|
|
|
2022-12-09 14:18:14 +00:00
|
|
|
onCleanup(() => {
|
|
|
|
dispatch({
|
|
|
|
action: 'cancelWatch',
|
|
|
|
params: {
|
|
|
|
id: trksegId,
|
|
|
|
method: 'getTrkseg',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
cancelDispatch(dispatchId());
|
|
|
|
});
|
|
|
|
|
2022-11-27 15:18:50 +00:00
|
|
|
createEffect(() => {
|
2022-11-27 16:12:25 +00:00
|
|
|
console.log({ caller: 'Trkseg', vectorSource, trksegId, trkseg: trkseg() });
|
|
|
|
|
2022-12-08 20:27:40 +00:00
|
|
|
if (trkseg()) {
|
2022-11-27 16:12:25 +00:00
|
|
|
let geo: any = {
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: [
|
2022-11-27 18:17:33 +00:00
|
|
|
{
|
|
|
|
type: 'Feature',
|
|
|
|
geometry: {
|
|
|
|
type: 'Point',
|
|
|
|
coordinates: [trkseg().trkpt[0].$.lon, trkseg().trkpt[0].$.lat],
|
|
|
|
},
|
2022-11-28 20:36:12 +00:00
|
|
|
properties: { type: 'trkseg-start', id: trksegId },
|
2022-12-14 07:03:42 +00:00
|
|
|
id: `${trksegId}/start`,
|
2022-11-27 18:17:33 +00:00
|
|
|
},
|
2022-11-27 16:12:25 +00:00
|
|
|
{
|
|
|
|
type: 'Feature',
|
|
|
|
geometry: {
|
|
|
|
type: 'LineString',
|
|
|
|
coordinates: trkseg().trkpt.map((trkpt: any) => [
|
|
|
|
trkpt.$.lon,
|
|
|
|
trkpt.$.lat,
|
|
|
|
]),
|
|
|
|
},
|
2022-11-28 20:36:12 +00:00
|
|
|
properties: { type: 'trkseg', id: trksegId },
|
2022-12-14 07:03:42 +00:00
|
|
|
id: trksegId,
|
2022-11-27 18:17:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'Feature',
|
|
|
|
geometry: {
|
|
|
|
type: 'Point',
|
|
|
|
coordinates: [
|
|
|
|
trkseg().trkpt.at(-1).$.lon,
|
|
|
|
trkseg().trkpt.at(-1).$.lat,
|
|
|
|
],
|
|
|
|
},
|
2022-11-28 20:36:12 +00:00
|
|
|
properties: { type: 'trkseg-finish', id: trksegId },
|
2022-12-14 07:03:42 +00:00
|
|
|
id: `${trksegId}/end`,
|
2022-11-27 16:12:25 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const features = new GeoJSON().readFeatures(geo);
|
|
|
|
console.log({ caller: 'Trkseg', features });
|
2022-12-14 07:03:42 +00:00
|
|
|
[`${trksegId}/start`, trksegId, `${trksegId}/end`].forEach((id) => {
|
|
|
|
const feature = vectorSource.getFeatureById(id);
|
|
|
|
if (feature) {
|
|
|
|
vectorSource.removeFeature(feature);
|
|
|
|
}
|
|
|
|
});
|
2022-11-27 16:12:25 +00:00
|
|
|
vectorSource.addFeatures(features);
|
|
|
|
}
|
2022-11-27 15:18:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Trkseg;
|