leaflet-geometryutil
Advanced tools
Comparing version 0.7.0 to 0.7.1
{ | ||
"name": "leaflet-geometryutil", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "Leaflet utility functions on geometries", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -13,4 +13,4 @@ describe('Closest on path with precision', function() { | ||
closest = L.GeometryUtil.closest(map, line, [1.7, 1.7]); | ||
assert.almostequal(closest.lat, 1.7, 2); | ||
assert.almostequal(closest.lng, 1.7, 2); | ||
assert.almostEqual(closest.lat, 1.7, 2); | ||
assert.almostEqual(closest.lng, 1.7, 2); | ||
done(); | ||
@@ -24,4 +24,4 @@ }); | ||
// TODO: should not be almost equal | ||
assert.almostequal(closest.lat, 0, 2); | ||
assert.almostequal(closest.lng, 0, 2); | ||
assert.almostEqual(closest.lat, 0, 2); | ||
assert.almostEqual(closest.lng, 0, 2); | ||
done(); | ||
@@ -33,3 +33,3 @@ }); | ||
var ll = L.latLng([5, 10]), | ||
line = L.polyline([[-50, -10], [30, 40]]).addTo(map), | ||
line = L.polyline([[-50, -10], [30, 40]])/*.addTo(map)*/, | ||
closest = L.GeometryUtil.closest(map, line, ll); | ||
@@ -67,7 +67,8 @@ assert.isTrue(closest.distance > 0); | ||
it('It should work with last segment of polygon', function(done) { | ||
var polygon = L.polygon([[0, 0], [10, 10], [0, 10]]), | ||
ll = [-1, 5], | ||
var polygon = L.polygon([[0, 0], [10, 10], [0, 10]])/*.addTo(map)*/, | ||
ll = [0, 5], | ||
marker = L.marker(ll)/*.addTo(map)*/, | ||
closest = L.GeometryUtil.closest(map, polygon, ll); | ||
assert.almostequal(closest.lat, 0, 2); | ||
assert.almostequal(closest.lng, 5, 2); | ||
assert.almostEqual(closest.lat, 0, 2); | ||
assert.almostEqual(closest.lng, 5, 2); | ||
done(); | ||
@@ -134,16 +135,6 @@ }); | ||
}, | ||
layers = L.geoJson(campus, { | ||
pointToLayer: function (feature, latlng) { | ||
return L.circleMarker(latlng, { | ||
radius: 8, | ||
fillColor: "#ff7800", | ||
color: "#000", | ||
weight: 1, | ||
opacity: 1, | ||
fillOpacity: 0.8 | ||
}); | ||
} | ||
}),ll = [-1, 5], | ||
closest = L.GeometryUtil.closest(map, layers.getLayers()[0], ll); | ||
layers = L.geoJson(campus)/*.addTo(map)*/, | ||
ll = [-1, 5], | ||
marker = L.marker(ll)/*.addTo(map)*/, | ||
closest = L.GeometryUtil.closest(map, layers.getLayers()[0], ll); | ||
// if layers.getLayers()[0] is a LayerGroup, we are in Leaflet 0.7.7 | ||
@@ -156,3 +147,3 @@ // so there is no result | ||
} else { | ||
assert.isOk(true); | ||
assert.isNotNull(closest); | ||
} | ||
@@ -176,2 +167,68 @@ done(); | ||
it('It should work with nested arrays and last segment of polygon', function(done) { | ||
var campus = { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "MultiPolygon", | ||
"coordinates": [ | ||
[ | ||
[ | ||
[0, 0], [50, 50], [100, 0] | ||
], | ||
[ | ||
[20,10], [50, 40], [80, 10] | ||
] | ||
] | ||
] | ||
} | ||
}, | ||
layers = L.geoJson(campus)/*.addTo(map)*/, | ||
ll = L.latLng([0, 50]), | ||
closest = L.GeometryUtil.closest(map, layers.getLayers()[0], ll); | ||
// if layers.getLayers()[0] is a LayerGroup, we are in Leaflet 0.7.7 | ||
// so there is no result | ||
if (layers.getLayers()[0] instanceof L.LayerGroup) { | ||
assert.isNull(closest); | ||
} else { | ||
assert.latLngEqual(L.latLng(0,2), closest); | ||
assert.equal(closest.distance, 0); | ||
} | ||
done(); | ||
}); | ||
it('It must not return a point on a segment between the last point of a polygon and the first point of his follower', function(done) { | ||
var campus = { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "MultiPolygon", | ||
"coordinates": [ | ||
[ | ||
[ | ||
[0, 0], [50, 50], [100, 0] | ||
], | ||
[ | ||
[20,10], [50, 40], [80, 10] | ||
] | ||
] | ||
] | ||
} | ||
}, | ||
layers = L.geoJson(campus)/*.addTo(map)*/, | ||
ll = L.latLng(5,10), | ||
marker = L.marker(ll)/*.addTo(map)*/, | ||
closest = L.GeometryUtil.closest(map, layers.getLayers()[0], ll); | ||
// if layers.getLayers()[0] is a LayerGroup, we are in Leaflet 0.7.7 | ||
// so there is no result | ||
if (layers.getLayers()[0] instanceof L.LayerGroup) { | ||
assert.isNull(closest); | ||
} else { | ||
// L.marker(closest).addTo(map) | ||
assert.almostNotEqual(ll.lat, closest.lat, .5); | ||
assert.almostNotEqual(ll.lng, closest.lng, .5); | ||
assert.almostEqual('7.8', closest.lat, .5); | ||
assert.almostEqual('6.75', closest.lng, .5); | ||
} | ||
done(); | ||
}); | ||
}); |
@@ -36,8 +36,8 @@ describe('Closest snap', function() { | ||
assert.equal(corner.layer, square); | ||
assert.almostequal(corner.latlng.lat, w); | ||
assert.almostequal(corner.latlng.lng, -w); | ||
assert.almostEqual(corner.latlng.lat, w); | ||
assert.almostEqual(corner.latlng.lng, -w); | ||
var snap = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([w-d, -w-d]), Infinity, false); | ||
assert.almostequal(snap.latlng.lat, w-d); | ||
assert.almostequal(snap.latlng.lng, -w); | ||
assert.almostEqual(snap.latlng.lat, w-d); | ||
assert.almostEqual(snap.latlng.lng, -w); | ||
done(); | ||
@@ -49,10 +49,10 @@ }); | ||
assert.equal(corner.layer, square); | ||
assert.almostequal(corner.latlng.lat, w); | ||
assert.almostequal(corner.latlng.lng, -w); | ||
assert.almostEqual(corner.latlng.lat, w); | ||
assert.almostEqual(corner.latlng.lng, -w); | ||
var snap = L.GeometryUtil.closestLayerSnap(map, layers, L.latLng([w-d-d/2, -w-d]), d); | ||
assert.almostequal(snap.latlng.lat, w-d-d/2); | ||
assert.almostequal(snap.latlng.lng, -w); | ||
assert.almostEqual(snap.latlng.lat, w-d-d/2); | ||
assert.almostEqual(snap.latlng.lng, -w); | ||
done(); | ||
}); | ||
}); |
@@ -13,6 +13,6 @@ describe('Closest on segment', function() { | ||
// TODO: should not be almost equal | ||
assert.almostequal(0, closest.lat, 2); | ||
assert.almostequal(0, closest.lng, 2); | ||
assert.almostEqual(0, closest.lat, 2); | ||
assert.almostEqual(0, closest.lng, 2); | ||
done(); | ||
}); | ||
}); |
@@ -15,7 +15,7 @@ describe('Locate on line', function() { | ||
it('It should return ratio of point', function(done) { | ||
assert.almostequal(0.5, L.GeometryUtil.locateOnLine(map, line, L.latLng([1, 1])), 4); | ||
assert.almostequal(0.25, L.GeometryUtil.locateOnLine(map, line, L.latLng([0.5, 0.5])), 4); | ||
assert.almostequal(0.85, L.GeometryUtil.locateOnLine(map, line, L.latLng([1.7, 1.7])), 4); | ||
assert.almostEqual(0.5, L.GeometryUtil.locateOnLine(map, line, L.latLng([1, 1])), 4); | ||
assert.almostEqual(0.25, L.GeometryUtil.locateOnLine(map, line, L.latLng([0.5, 0.5])), 4); | ||
assert.almostEqual(0.85, L.GeometryUtil.locateOnLine(map, line, L.latLng([1.7, 1.7])), 4); | ||
done(); | ||
}); | ||
}); |
@@ -20,2 +20,7 @@ // Packaging/modules magic dance. | ||
L.Polyline._flat = L.Polyline._flat || function (latlngs) { | ||
// true if it's a flat array of latlngs; false if nested | ||
return !L.Util.isArray(latlngs[0]) || (typeof latlngs[0][0] !== 'object' && typeof latlngs[0][0] !== 'undefined'); | ||
}; | ||
/** | ||
@@ -170,3 +175,3 @@ * @fileOverview Leaflet Geometry utilities for distances and linear referencing. | ||
closest: function (map, layer, latlng, vertices) { | ||
var latlngs, | ||
@@ -202,44 +207,57 @@ mindist = Infinity, | ||
/** | ||
* Flat an array upon a predicate, saying if we have to dig digger or not | ||
* Specific use for Polygon, Polyline, MultiPolyline (0.7.7) | ||
* Leaflet 1.0 change the structure of Polyline (could be MultiPolyline > nested arrays), idem for Polygon | ||
*/ | ||
function flattenArray(array, predicate, result) { | ||
latlngs = layer.getLatLngs().slice(0); | ||
var index = -1, | ||
length = array.length; | ||
predicate || ( predicate = function(v) { return true } ); | ||
result || ( result = []); | ||
if (predicate(array)) { | ||
while (++index < length) { | ||
var value = array[index]; | ||
if (predicate(value)) { | ||
flattenArray(value, predicate, result); | ||
} else { | ||
result.push(value); | ||
// add the last segment for L.Polygon | ||
if (layer instanceof L.Polygon) { | ||
// add the last segment for each child that is a nested array | ||
var addLastSegment = function(latlngs) { | ||
if (L.Polyline._flat(latlngs)) { | ||
latlngs.push(latlngs[0]); | ||
} else { | ||
for (var i = 0; i < latlngs.length; i++) { | ||
addLastSegment(latlngs[i]); | ||
} | ||
} | ||
} else { | ||
result.push(array); | ||
} | ||
addLastSegment(latlngs); | ||
} | ||
// we have a multi polygon / multi polyline / polygon with holes | ||
// use recursive to explore and return the good result | ||
if ( ! L.Polyline._flat(latlngs) ) { | ||
for (var i = 0; i < latlngs.length; i++) { | ||
// if we are at the lower level, and if we have a L.Polygon, we add the last segment | ||
var subResult = L.GeometryUtil.closest(map, latlngs[i], latlng, vertices); | ||
if (subResult.distance < mindist) { | ||
mindist = subResult.distance; | ||
result = subResult; | ||
} | ||
} | ||
return result; | ||
} | ||
latlngs = flattenArray(layer.getLatLngs().slice(0), function isFlattenable(value) { | ||
return ( ( value instanceof Array && typeof value[0] !== 'number' ) && ! ( value instanceof L.LatLng ) ) | ||
}); | ||
} else { | ||
// Lookup vertices | ||
if (vertices) { | ||
for(i = 0, n = latlngs.length; i < n; i++) { | ||
var ll = latlngs[i]; | ||
distance = L.GeometryUtil.distance(map, latlng, ll); | ||
if (distance < mindist) { | ||
// Lookup vertices | ||
if (vertices) { | ||
for(i = 0, n = latlngs.length; i < n; i++) { | ||
var ll = latlngs[i]; | ||
distance = L.GeometryUtil.distance(map, latlng, ll); | ||
if (distance < mindist) { | ||
mindist = distance; | ||
result = ll; | ||
result.distance = distance; | ||
} | ||
} | ||
return result; | ||
} | ||
// Keep the closest point of all segments | ||
for (i = 0, n = latlngs.length; i < n-1; i++) { | ||
var latlngA = latlngs[i], | ||
latlngB = latlngs[i+1]; | ||
distance = L.GeometryUtil.distanceSegment(map, latlng, latlngA, latlngB); | ||
if (distance <= mindist) { | ||
mindist = distance; | ||
result = ll; | ||
result = L.GeometryUtil.closestOnSegment(map, latlng, latlngA, latlngB); | ||
result.distance = distance; | ||
@@ -250,20 +268,3 @@ } | ||
} | ||
// add the first point to close the polygon | ||
if (layer instanceof L.Polygon) { | ||
latlngs.push(latlngs[0]); | ||
} | ||
// Keep the closest point of all segments | ||
for (i = 0, n = latlngs.length; i < n-1; i++) { | ||
var latlngA = latlngs[i], | ||
latlngB = latlngs[i+1]; | ||
distance = L.GeometryUtil.distanceSegment(map, latlng, latlngA, latlngB); | ||
if (distance <= mindist) { | ||
mindist = distance; | ||
result = L.GeometryUtil.closestOnSegment(map, latlng, latlngA, latlngB); | ||
result.distance = distance; | ||
} | ||
} | ||
return result; | ||
}, | ||
@@ -270,0 +271,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1602077
2105