-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into felix/google-rightclick-changelog
- Loading branch information
Showing
2 changed files
with
49 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as turf from '@turf/turf'; | ||
|
||
/* Utility to add rotated labels along lines and assign collision priorities */ | ||
export default function calculateLabels(data, pointSpacing) { | ||
const routes = data.features.filter(d => d.geometry.type !== 'Point'); | ||
const result = []; | ||
|
||
function addPoint(lineLength, lineString, dAlong, name, priority) { | ||
let offset = 1; | ||
if (dAlong > 0.5 * lineLength) offset *= -1; | ||
const feature = turf.along(lineString, dAlong); | ||
const nextFeature = turf.along(lineString, dAlong + offset); | ||
const {coordinates} = feature.geometry; | ||
const next = nextFeature.geometry.coordinates; | ||
if (coordinates[0] === next[0] && coordinates[1] === next[1]) return; | ||
|
||
let angle = 90 - turf.rhumbBearing(coordinates, next); | ||
if (Math.abs(angle) > 90) angle += 180; | ||
|
||
result.push({position: coordinates, text: name, priority, angle}); | ||
} | ||
|
||
// Add points along the lines | ||
for (const feature of routes) { | ||
const lineLength = Math.floor(turf.lineDistance(feature.geometry)); | ||
const {name} = feature.properties; | ||
|
||
feature.geometry.coordinates.forEach(c => { | ||
const lineString = turf.lineString(c); | ||
|
||
// Add labels to minimize overlaps, pick odd values from each level | ||
// 1 <- depth 1 | ||
// 1 2 3 <- depth 2 | ||
// 1 2 3 4 5 6 7 <- depth 3 | ||
let delta = 0.5 * lineLength; // Spacing between points at level | ||
let depth = 1; | ||
while (delta > pointSpacing) { | ||
for (let i = 1; i < 2 ** depth; i += 2) { | ||
addPoint(lineLength, lineString, i * delta, name, 100 - depth); // Top levels have highest priority | ||
} | ||
depth++; | ||
delta /= 2; | ||
} | ||
}); | ||
} | ||
|
||
return result; | ||
} |