114 lines
2.8 KiB
TypeScript
114 lines
2.8 KiB
TypeScript
import { Component, createEffect, createSignal, onCleanup } from 'solid-js';
|
|
|
|
import dispatch, { cancelDispatch } from '../../workers/dispatcher-main';
|
|
import VectorSource from 'ol/source/Vector';
|
|
import GeoJSON from 'ol/format/GeoJSON';
|
|
import {
|
|
createCachedSignal,
|
|
destroyCachedSignal,
|
|
} from '../../workers/cached-signals';
|
|
|
|
interface Props {
|
|
trksegId: string;
|
|
vectorSource: VectorSource;
|
|
context: any;
|
|
}
|
|
|
|
export const Trkseg: Component<Props> = ({
|
|
vectorSource,
|
|
trksegId,
|
|
context,
|
|
}) => {
|
|
const params = {
|
|
id: trksegId,
|
|
method: 'getTrkseg',
|
|
};
|
|
const trkseg = createCachedSignal(params);
|
|
|
|
const clearFeatures = () => {
|
|
[`${trksegId}/start`, trksegId, `${trksegId}/end`].forEach((id) => {
|
|
const feature = vectorSource.getFeatureById(id);
|
|
if (feature) {
|
|
vectorSource.removeFeature(feature);
|
|
}
|
|
});
|
|
};
|
|
|
|
onCleanup(() => {
|
|
console.log({
|
|
caller: 'Trkseg / onCleanup',
|
|
trksegId,
|
|
trkseg: trkseg(),
|
|
params,
|
|
});
|
|
destroyCachedSignal(params);
|
|
clearFeatures();
|
|
});
|
|
|
|
createEffect(() => {
|
|
console.log({ caller: 'Trkseg', vectorSource, trksegId, trkseg: trkseg() });
|
|
|
|
if (trkseg()) {
|
|
let geo: any = {
|
|
type: 'FeatureCollection',
|
|
features: [
|
|
{
|
|
type: 'Feature',
|
|
geometry: {
|
|
type: 'Point',
|
|
coordinates: [trkseg().trkpt[0].$.lon, trkseg().trkpt[0].$.lat],
|
|
},
|
|
properties: {
|
|
type: 'trkseg-start',
|
|
id: trksegId,
|
|
context: { ...context, trkseg, trksegId },
|
|
},
|
|
id: `${trksegId}/start`,
|
|
},
|
|
{
|
|
type: 'Feature',
|
|
geometry: {
|
|
type: 'LineString',
|
|
coordinates: trkseg().trkpt.map((trkpt: any) => [
|
|
trkpt.$.lon,
|
|
trkpt.$.lat,
|
|
]),
|
|
},
|
|
properties: {
|
|
type: 'trkseg',
|
|
id: trksegId,
|
|
context: { ...context, trkseg, trksegId },
|
|
},
|
|
id: trksegId,
|
|
},
|
|
{
|
|
type: 'Feature',
|
|
geometry: {
|
|
type: 'Point',
|
|
coordinates: [
|
|
trkseg().trkpt.at(-1).$.lon,
|
|
trkseg().trkpt.at(-1).$.lat,
|
|
],
|
|
},
|
|
properties: {
|
|
type: 'trkseg-finish',
|
|
id: trksegId,
|
|
context: { ...context, trkseg, trksegId },
|
|
},
|
|
id: `${trksegId}/end`,
|
|
},
|
|
],
|
|
};
|
|
|
|
const features = new GeoJSON().readFeatures(geo);
|
|
console.log({ caller: 'Trkseg', features });
|
|
clearFeatures();
|
|
vectorSource.addFeatures(features);
|
|
}
|
|
});
|
|
|
|
return <></>;
|
|
};
|
|
|
|
export default Trkseg;
|