Displaying trksegs through geojson.

This commit is contained in:
Eric van der Vlist 2022-11-27 17:12:25 +01:00
parent ab7374f360
commit 37fd31a040
4 changed files with 41 additions and 10 deletions

View File

@ -5,6 +5,8 @@ import OlMap from 'ol/Map';
import dispatch from '../../workers/dispatcher-main'; import dispatch from '../../workers/dispatcher-main';
import Trk from '../trk'; import Trk from '../trk';
import getUri from '../../lib/ids'; import getUri from '../../lib/ids';
import VectorSource from 'ol/source/Vector';
import VectorLayer from 'ol/layer/Vector';
interface Props { interface Props {
gpxId: string; gpxId: string;
@ -23,8 +25,12 @@ export const Gpx: Component<Props> = ({ map, gpxId }) => {
}) })
); );
const vectorSource = new VectorSource();
const vectorLayer = new VectorLayer({ source: vectorSource });
createEffect(() => map()?.addLayer(vectorLayer));
createEffect(() => { createEffect(() => {
console.log({ caller: 'Gpx', map: map(), gpxId, gpx: gpx() }); console.log({ caller: 'Gpx', map: map(), gpxId, gpx: gpx(), vectorLayer });
}); });
return ( return (
@ -32,7 +38,7 @@ export const Gpx: Component<Props> = ({ map, gpxId }) => {
<For each={gpx() ? gpx().trk || [] : []}> <For each={gpx() ? gpx().trk || [] : []}>
{(trkId: string) => { {(trkId: string) => {
console.log({ caller: 'Gpx / loop', trkId }); console.log({ caller: 'Gpx / loop', trkId });
return <Trk map={map} trkId={trkId} />; return <Trk vectorSource={vectorSource} trkId={trkId} />;
}} }}
</For> </For>
); );

View File

@ -6,12 +6,13 @@ import dispatch from '../../workers/dispatcher-main';
import getUri from '../../lib/ids'; import getUri from '../../lib/ids';
import Trkseg from '../trkseg'; import Trkseg from '../trkseg';
import VectorSource from 'ol/source/Vector';
interface Props { interface Props {
trkId: string; trkId: string;
map: () => OlMap | null; vectorSource: VectorSource;
} }
export const Trk: Component<Props> = ({ map, trkId }) => { export const Trk: Component<Props> = ({ vectorSource, trkId }) => {
const [trk] = createResource( const [trk] = createResource(
trkId, trkId,
async () => async () =>
@ -24,7 +25,7 @@ export const Trk: Component<Props> = ({ map, trkId }) => {
); );
createEffect(() => { createEffect(() => {
console.log({ caller: 'Trk', map: map(), trkId, trk: trk() }); console.log({ caller: 'Trk', vectorSource, trkId, trk: trk() });
}); });
return ( return (
@ -32,7 +33,7 @@ export const Trk: Component<Props> = ({ map, trkId }) => {
<For each={trk() ? trk().trkseg || [] : []}> <For each={trk() ? trk().trkseg || [] : []}>
{(trksegId: string) => { {(trksegId: string) => {
console.log({ caller: 'Trk / loop', trksegId }); console.log({ caller: 'Trk / loop', trksegId });
return <Trkseg map={map} trksegId={trksegId} />; return <Trkseg vectorSource={vectorSource} trksegId={trksegId} />;
}} }}
</For> </For>
); );

View File

@ -4,13 +4,15 @@ import OlMap from 'ol/Map';
import dispatch from '../../workers/dispatcher-main'; import dispatch from '../../workers/dispatcher-main';
import getUri from '../../lib/ids'; import getUri from '../../lib/ids';
import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';
interface Props { interface Props {
trksegId: string; trksegId: string;
map: () => OlMap | null; vectorSource: VectorSource;
} }
export const Trkseg: Component<Props> = ({ map, trksegId }) => { export const Trkseg: Component<Props> = ({ vectorSource, trksegId }) => {
const [trkseg] = createResource( const [trkseg] = createResource(
trksegId, trksegId,
async () => async () =>
@ -23,7 +25,29 @@ export const Trkseg: Component<Props> = ({ map, trksegId }) => {
); );
createEffect(() => { createEffect(() => {
console.log({ caller: 'Trkseg', map: map(), trksegId, trkseg: trkseg() }); console.log({ caller: 'Trkseg', vectorSource, trksegId, trkseg: trkseg() });
if (trkseg()) {
let geo: any = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: trkseg().trkpt.map((trkpt: any) => [
trkpt.$.lon,
trkpt.$.lat,
]),
},
},
],
};
const features = new GeoJSON().readFeatures(geo);
console.log({ caller: 'Trkseg', features });
vectorSource.addFeatures(features);
}
}); });
return <></>; return <></>;

View File

@ -40,7 +40,7 @@ export const getTrkseg = async (params: any) => {
//level 1 //level 1
if (row.doc.type === 'trkpt') { if (row.doc.type === 'trkpt') {
target.splice(1); target.splice(1);
appendToArray(target.at(-1), row.doc.type, row.doc._id); appendToArray(target.at(-1), row.doc.type, row.doc.doc);
target.push(row.doc.doc); target.push(row.doc.doc);
} }
}); });