StorymapperArrow/MapArrow.js
2025-05-27 16:01:42 +02:00

205 lines
6.8 KiB
JavaScript

mapboxgl.accessToken = 'pk.eyJ1Ijoib3V0ZG9vcm1hcHBpbmdjb21wYW55IiwiYSI6ImNqYmh3cDdjYzNsMnozNGxsYzlvMmk2bTYifQ.QqcZ4LVoLWnXafXdjZxnZg';
const map = new mapboxgl.Map({
container: 'map',
center: [10, 50],
zoom: 5
});
import * as turf from "@turf/turf";
import { ARROW_BODY_STYLE_CONSTANT, ARROW_BODY_STYLE_LINEAR, ARROW_BODY_STYLE_EXPONENTIAL } from "./Arrow.js";
map.on('load', () => {
const points = [
[1.42076, 40.08804],
[358.4050, 50.52]
];
const fullPolygon = getArrowPolygon(points, 20000); // offset 20 km
map.addSource("arrow-shape", { type: "geojson", data: fullPolygon });
map.addLayer({
id: "arrow-shape",
type: "fill",
source: "arrow-shape",
paint: {
"fill-color": "#ff0000",
"fill-opacity": 0.7
}
});
const grid = generateLatLonGrid(10);
map.addSource("latLonGrid", { type: "geojson", data: grid });
map.addLayer({
id: "latLonGrid",
type: "line",
source: "latLonGrid",
paint: {
"line-color": "#888",
"line-width": 1,
"line-opacity": 0.5
}
});
// Obrys (černý)
map.addLayer({
id: "arrow-outline",
type: "line",
source: "arrow-shape",
paint: {
"line-color": "#000000",
"line-width": 2,
"line-opacity": 1
}
});
});
const points = [
{ x: 70, y: 38 },
{ x: 71, y: 45},
{ x: 65, y: 50 },
{ x: 70, y: 53}
];
const arrowData = {
points: points,
splineStep: 0.01,
spacing: 0.01,
offsetDistance: 10000
};
const style = {
calculation: ARROW_BODY_STYLE_LINEAR,
range: 1,
minValue: 0.1
};
const arrowHeadData = {
widthArrow: 1,
lengthArrow: 1
};
// Cubic interpolation source from https://www.paulinternet.nl/?page=bicubic
function cubicInterpolate(p, x) {
return p[1] + 0.5 * x * (p[2] - p[0] + x * (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] + x * (3.0 * (p[1] - p[2]) + p[3] - p[0])));
}
/**
* @param {Object} arrowData - Object with data for arrow
* @param {{x: number, y: number}[]} arrowData.points - List of points defining the arrow's path.
* @param {number} arrowData.splineStep - The step size for the spline interpolation.
* @param {number} arrowData.spacing - The spacing between the points along the arrow.
* @param {number} arrowData.offsetDistance - The offset distance for the arrow's path (width).
*
* @param {Object} style - Object with data for the calculation style.
* @param {number} style.calculation - The style for the calculation
* @param {number} style.range - The range for the calculation style.
* @param {number} style.minValue - The minimum value used in the calculation.
*
* @param {Object|undefined} arrowHeadData - Optional data for the arrowhead.
* @param {number} arrowHeadData.widthArrow - The width of the arrowhead.
* @param {number} arrowHeadData.lengthArrow - The length of the arrowhead.
*
* @returns {{x: number, y: number}[]} - An array of points representing the arrow polygon.
*/
export function getArrowPolygon(arrowData, style= undefined, arrowHeadData = undefined) {
const splineStep = 20;
const smooth = computeSplinePoints(arrowData.points, splineStep);
const { leftSidePoints, rightSidePoints } = computeSideOffsets(smooth, arrowData.offsetDistance);
const end = smooth[smooth.length -1];
const bearing = averageBearing(smooth, 3);
const triangle = createIsoscelesTriangleCoords(turf.point(end), arrowData.offsetDistance * 5, arrowData.offsetDistance * 5, bearing);
const polygonCoords = [
...leftSidePoints,
...triangle,
...rightSidePoints.reverse(),
leftSidePoints[0]
];
return turf.polygon([[...polygonCoords]]);
}
function averageBearing(points, count = 3) {
const bearings = [];
for (let i = points.length - count; i < points.length -1; i++) {
if (i >= 0) {
const b = turf.bearing(turf.point(points[i]), turf.point(points[i + 1]));
bearings.push(b);
}
}
// Průměr s korekcí kruhového rozsahu
const sinSum = bearings.reduce((sum, b) => sum + Math.sin(b * Math.PI / 180), 0);
const cosSum = bearings.reduce((sum, b) => sum + Math.cos(b * Math.PI / 180), 0);
return Math.atan2(sinSum, cosSum) * 180 / Math.PI;
}
function computeSplinePoints(points, segments = 10) {
if (points.length < 2) return points;
const result = [];
for (let i = 0; i < points.length - 1; i++) {
const p0 = points[i === 0 ? i : i - 1];
const p1 = points[i];
const p2 = points[i + 1];
const p3 = points[i + 2] || p2;
for (let j = 0; j < segments; j++) {
const t = j / segments;
const lon = cubicInterpolate([p0[0], p1[0], p2[0], p3[0]], t);
const lat = cubicInterpolate([p0[1], p1[1], p2[1], p3[1]], t);
result.push([lon, lat]);
}
}
result.push(points[points.length - 1]);
return result;
}
function computeSideOffsets(points, offsetMeters) {
let leftSidePoints = [];
let rightSidePoints = [];
for (let i = 1; i < points.length; i++) {
const previousPoint = points[i - 1];
const currentPoint = points[i];
const bearing = turf.bearing(turf.point(previousPoint), turf.point(currentPoint));
leftSidePoints.push(turf.destination(turf.point(currentPoint), offsetMeters, bearing - 90, { units: 'meters' }).geometry.coordinates);
rightSidePoints.push(turf.destination(turf.point(currentPoint), offsetMeters, bearing + 90, { units: 'meters' }).geometry.coordinates);
}
return { leftSidePoints, rightSidePoints };
}
function createIsoscelesTriangleCoords(center, baseLengthMeters, heightMeters, bearing = 0) {
const halfBase = baseLengthMeters / 2;
const left = turf.destination(center, halfBase, bearing - 90, { units: 'meters' }).geometry.coordinates;
const right = turf.destination(center, halfBase, bearing + 90, { units: 'meters' }).geometry.coordinates;
const tip = turf.destination(center, heightMeters, bearing, { units: 'meters' }).geometry.coordinates;
return [left, tip, right];
}
function generateLatLonGrid(step = 10) {
const features = [];
for (let lat = -80; lat <= 80; lat += step) {
features.push({
type: "Feature",
geometry: {
type: "LineString",
coordinates: Array.from({ length: 37 }, (_, i) => [-180 + i * 10, lat])
}
});
}
for (let lon = -180; lon <= 180; lon += step) {
features.push({
type: "Feature",
geometry: {
type: "LineString",
coordinates: Array.from({ length: 17 }, (_, i) => [lon, -80 + i * 10])
}
});
}
return {
type: "FeatureCollection",
features
};
}