diff --git a/modules/actions/straighten.js b/modules/actions/straighten.js index c7de86011..60a49416e 100644 --- a/modules/actions/straighten.js +++ b/modules/actions/straighten.js @@ -1,4 +1,5 @@ import { actionDeleteNode } from './delete_node'; +import { geoEuclideanDistance } from '../geo/index'; /* @@ -60,7 +61,7 @@ export function actionStraighten(wayId, projection) { points = nodes.map(function(n) { return projection(n.loc); }), startPoint = points[0], endPoint = points[points.length-1], - threshold = 0.2 * Math.sqrt(Math.pow(startPoint[0] - endPoint[0], 2) + Math.pow(startPoint[1] - endPoint[1], 2)), + threshold = 0.2 * geoEuclideanDistance(startPoint, endPoint), i; if (threshold === 0) { diff --git a/modules/geo/index.js b/modules/geo/index.js index 2030bc689..e7cbfbbf7 100644 --- a/modules/geo/index.js +++ b/modules/geo/index.js @@ -259,12 +259,9 @@ export function geoPolygonIntersectsPolygon(outer, inner, checkSegments) { export function geoPathLength(path) { - var length = 0, - dx, dy; + var length = 0; for (var i = 0; i < path.length - 1; i++) { - dx = path[i][0] - path[i + 1][0]; - dy = path[i][1] - path[i + 1][1]; - length += Math.sqrt(dx * dx + dy * dy); + length += geoEuclideanDistance(path[i], path[i + 1]); } return length; } diff --git a/modules/svg/labels.js b/modules/svg/labels.js index 8e62d18ea..fb16566ac 100644 --- a/modules/svg/labels.js +++ b/modules/svg/labels.js @@ -1,7 +1,7 @@ import * as d3 from 'd3'; import _ from 'lodash'; import rbush from 'rbush'; -import { geoPathLength } from '../geo/index'; +import { geoEuclideanDistance, geoPathLength } from '../geo/index'; import { osmEntity } from '../osm/index'; import { utilDetect } from '../util/detect'; import { utilDisplayName, utilEntitySelector } from '../util/index'; @@ -44,7 +44,6 @@ export function svgLabels(projection, context) { ]; - function blacklisted(preset) { var noIcons = ['building', 'landuse', 'natural']; return _.some(noIcons, function(s) { @@ -372,8 +371,10 @@ export function svgLabels(projection, context) { if (start < 0 || start + width > length) continue; - var sub = subpath(nodes, start, start + width), - isReverse = reverse(sub), + var sub = subpath(nodes, start, start + width); + if (!sub) continue; + + var isReverse = reverse(sub), bbox = { minX: Math.min(sub[0][0], sub[sub.length - 1][0]) - margin, minY: Math.min(sub[0][1], sub[sub.length - 1][1]) - margin, @@ -403,36 +404,33 @@ export function svgLabels(projection, context) { } function subpath(nodes, from, to) { - function segmentLength(i) { - var dx = nodes[i][0] - nodes[i + 1][0]; - var dy = nodes[i][1] - nodes[i + 1][1]; - return Math.sqrt(dx * dx + dy * dy); - } - var sofar = 0, start, end, i0, i1; + for (var i = 0; i < nodes.length - 1; i++) { - var current = segmentLength(i); + var a = nodes[i], + b = nodes[i + 1]; + var current = geoEuclideanDistance(a, b); var portion; if (!start && sofar + current >= from) { portion = (from - sofar) / current; start = [ - nodes[i][0] + portion * (nodes[i + 1][0] - nodes[i][0]), - nodes[i][1] + portion * (nodes[i + 1][1] - nodes[i][1]) + a[0] + portion * (b[0] - a[0]), + a[1] + portion * (b[1] - a[1]) ]; i0 = i + 1; } if (!end && sofar + current >= to) { portion = (to - sofar) / current; end = [ - nodes[i][0] + portion * (nodes[i + 1][0] - nodes[i][0]), - nodes[i][1] + portion * (nodes[i + 1][1] - nodes[i][1]) + a[0] + portion * (b[0] - a[0]), + a[1] + portion * (b[1] - a[1]) ]; i1 = i + 1; } sofar += current; - } + var ret = nodes.slice(i0, i1); ret.unshift(start); ret.push(end);