LineUtil.js (Leaflet-1.8.0) | : | LineUtil.js (Leaflet-1.9.0) | ||
---|---|---|---|---|
import {Point} from './Point'; | import {Point, toPoint} from './Point'; | |||
import * as Util from '../core/Util'; | import * as Util from '../core/Util'; | |||
import {toLatLng} from '../geo/LatLng'; | ||||
/* | /* | |||
* @namespace LineUtil | * @namespace LineUtil | |||
* | * | |||
* Various utility functions for polyline points processing, used by Leaflet int ernally to make polylines lightning-fast. | * Various utility functions for polyline points processing, used by Leaflet int ernally to make polylines lightning-fast. | |||
*/ | */ | |||
// Simplify polyline with vertex reduction and Douglas-Peucker simplification. | // Simplify polyline with vertex reduction and Douglas-Peucker simplification. | |||
// Improves rendering performance dramatically by lessening the number of points to draw. | // Improves rendering performance dramatically by lessening the number of points to draw. | |||
skipping to change at line 241 | skipping to change at line 242 | |||
// @function isFlat(latlngs: LatLng[]): Boolean | // @function isFlat(latlngs: LatLng[]): Boolean | |||
// Returns true if `latlngs` is a flat array, false is nested. | // Returns true if `latlngs` is a flat array, false is nested. | |||
export function isFlat(latlngs) { | export function isFlat(latlngs) { | |||
return !Util.isArray(latlngs[0]) || (typeof latlngs[0][0] !== 'object' && typeof latlngs[0][0] !== 'undefined'); | return !Util.isArray(latlngs[0]) || (typeof latlngs[0][0] !== 'object' && typeof latlngs[0][0] !== 'undefined'); | |||
} | } | |||
export function _flat(latlngs) { | export function _flat(latlngs) { | |||
console.warn('Deprecated use of _flat, please use L.LineUtil.isFlat inste ad.'); | console.warn('Deprecated use of _flat, please use L.LineUtil.isFlat inste ad.'); | |||
return isFlat(latlngs); | return isFlat(latlngs); | |||
} | } | |||
/* @function polylineCenter(latlngs: LatLng[], crs: CRS): LatLng | ||||
* Returns the center ([centroid](http://en.wikipedia.org/wiki/Centroid)) of the | ||||
passed LatLngs (first ring) from a polyline. | ||||
*/ | ||||
export function polylineCenter(latlngs, crs) { | ||||
var i, halfDist, segDist, dist, p1, p2, ratio, center; | ||||
if (!latlngs || latlngs.length === 0) { | ||||
throw new Error('latlngs not passed'); | ||||
} | ||||
if (!isFlat(latlngs)) { | ||||
console.warn('latlngs are not flat! Only the first ring will be u | ||||
sed'); | ||||
latlngs = latlngs[0]; | ||||
} | ||||
var points = []; | ||||
for (var j in latlngs) { | ||||
points.push(crs.project(toLatLng(latlngs[j]))); | ||||
} | ||||
var len = points.length; | ||||
for (i = 0, halfDist = 0; i < len - 1; i++) { | ||||
halfDist += points[i].distanceTo(points[i + 1]) / 2; | ||||
} | ||||
// The line is so small in the current view that all points are on the sa | ||||
me pixel. | ||||
if (halfDist === 0) { | ||||
center = points[0]; | ||||
} else { | ||||
for (i = 0, dist = 0; i < len - 1; i++) { | ||||
p1 = points[i]; | ||||
p2 = points[i + 1]; | ||||
segDist = p1.distanceTo(p2); | ||||
dist += segDist; | ||||
if (dist > halfDist) { | ||||
ratio = (dist - halfDist) / segDist; | ||||
center = [ | ||||
p2.x - ratio * (p2.x - p1.x), | ||||
p2.y - ratio * (p2.y - p1.y) | ||||
]; | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
return crs.unproject(toPoint(center)); | ||||
} | ||||
End of changes. 3 change blocks. | ||||
1 lines changed or deleted | 2 lines changed or added |