hmpps-open-layers-map
Version:
A native Web Component for displaying maps using OpenLayers.
51 lines (50 loc) • 1.74 kB
JavaScript
// Convert MDSS positions into a GeoJSON FeatureCollection.
export function mdssPositionsToGeoJson(positions) {
if (!positions || positions.length === 0) {
return { type: 'FeatureCollection', features: [] };
}
const pointFeatures = positions.map(pos => ({
type: 'Feature',
id: pos.positionId.toString(),
geometry: {
type: 'Point',
coordinates: [pos.longitude, pos.latitude],
},
properties: {
'@id': pos.positionId.toString(),
confidence: pos.precision,
speed: pos.speed,
direction: pos.direction,
timestamp: pos.timestamp,
geolocationMechanism: pos.geolocationMechanism,
sequenceNumber: pos.sequenceNumber,
type: 'mdss-location',
},
}));
const lineFeatures = [];
positions.forEach((current, index) => {
if (index < positions.length - 1) {
const next = positions[index + 1];
lineFeatures.push({
type: 'Feature',
id: `${current.positionId}-${next.positionId}`,
geometry: {
type: 'LineString',
coordinates: [
[current.longitude, current.latitude],
[next.longitude, next.latitude],
],
},
properties: {
'@id': `${current.positionId}-${next.positionId}`,
direction: current.direction,
type: 'mdss-line',
},
});
}
});
return {
type: 'FeatureCollection',
features: [...pointFeatures, ...lineFeatures],
};
}