forked from mapbox/leaflet-knn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-knn.min.js
1 lines (1 loc) · 4.45 KB
/
leaflet-knn.min.js
1
(function(e){if("function"==typeof bootstrap)bootstrap("leafletknn",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeLeafletKnn=e}else"undefined"!=typeof window?window.leafletKnn=e():global.leafletKnn=e()})(function(){return function(e,t,n){function r(n,i){if(!t[n]){if(!e[n]){var s="function"==typeof require&&require;if(!i&&s)return s(n,!0);throw Error("Cannot find module '"+n+"'")}var o=t[n]={exports:{}};e[n][0](function(t){var i=e[n][1][t];return r(i?i:t)},o,o.exports)}return t[n].exports}for(var i=0;n.length>i;i++)r(n[i]);return r}({1:[function(require,module){var sphereKnn=require("sphere-knn");module.exports=function(layer){"use strict";if(!(layer instanceof L.GeoJSON))throw Error("must be L.GeoJSON");var points=[];layer.eachLayer(function(l){if(l instanceof L.Marker){var ll=l.getLatLng();ll.layer=l,points.push(ll)}});var sknn=sphereKnn(points);return sknn.nearest=function(p,n,max_distance){return p instanceof L.LatLng&&(p=[p.lng,p.lat]),sknn(p[1],p[0],n,max_distance)},sknn}},{"sphere-knn":2}],2:[function(require,module){var spherekd=require("./lib/spherekd");module.exports=function(points){var root=spherekd.build(points);return function(lat,lon,n,max){return spherekd.lookup(lat,lon,root,n,max)}}},{"./lib/spherekd":3}],3:[function(require,module,exports){function spherical2cartesian(lat,lon){lat*=rad,lon*=rad;var cos=Math.cos(lat);return[cos*Math.cos(lon),Math.sin(lat),cos*Math.sin(lon)]}function Position(object){var lat,lon;object.hasOwnProperty("lat")?lat=object.lat:object.hasOwnProperty("latitude")?lat=object.latitude:object.hasOwnProperty("location")&&Array.isArray(object.location)&&2===object.location.length&&(lat=object.location[0]),object.hasOwnProperty("lon")?lon=object.lon:object.hasOwnProperty("longitude")?lon=object.longitude:object.hasOwnProperty("lng")?lon=object.lng:object.hasOwnProperty("long")?lon=object.long:object.hasOwnProperty("location")&&Array.isArray(object.location)&&2===object.location.length&&(lon=object.location[1]),this.object=object,this.position=spherical2cartesian(lat,lon)}function build(array){for(var i=array.length,out=Array(i);i--;)out[i]=new Position(array[i]);return kd.build(out)}function lookup(lat,lon,node,n,max){for(var array=kd.lookup(spherical2cartesian(lat,lon),node,n,max>0?2*Math.sin(max*invEarthDiameter):void 0),i=array.length;i--;)array[i]=array[i].object;return array}var kd=require("./kd"),rad=Math.PI/180,invEarthDiameter=1/12742018;exports.build=build,exports.lookup=lookup},{"./kd":4}],4:[function(require,module,exports){function Node(axis,split,left,right){this.axis=axis,this.split=split,this.left=left,this.right=right}function distance(a,b){for(var k,i=Math.min(a.length,b.length),d=0;i--;)k=b[i]-a[i],d+=k*k;return d}function byDistance(a,b){return a.dist-b.dist}function buildrec(array,depth){if(0===array.length)return null;if(1===array.length)return array[0];var axis=depth%array[0].position.length;array.sort(function(a,b){return a.position[axis]-b.position[axis]});var i=Math.floor(.5*array.length);return++depth,new Node(axis,array[i].position[axis],buildrec(array.slice(0,i),depth),buildrec(array.slice(i),depth))}function build(array){return buildrec(array,0)}function lookup(position,node,n,max){max>0||(max=Number.POSITIVE_INFINITY);var array=[];if(null===node||0>=n)return array;for(var dist,i,stack=[node,0];stack.length;)if(dist=stack.pop(),node=stack.pop(),!(dist>max||array.length===n&&dist*dist>array[array.length-1].dist)){for(;node instanceof Node;)position[node.axis]<node.split?(stack.push(node.right,node.split-position[node.axis]),node=node.left):(stack.push(node.left,position[node.axis]-node.split),node=node.right);dist=distance(position,node.position),max*max>=dist&&binary.insert({object:node,dist:dist},array,byDistance),array.length>n&&array.pop()}for(i=array.length;i--;)array[i]=array[i].object;return array}var binary=require("./binary");exports.build=build,exports.lookup=lookup},{"./binary":5}],5:[function(require,module,exports){function defaultComparator(a,b){return a-b}exports.search=function(item,array,comparator){comparator||(comparator=defaultComparator);for(var mid,comp,low=0,high=array.length-1;high>=low;)if(mid=low+high>>>1,comp=comparator(array[mid],item),0>comp)low=mid+1;else{if(!(comp>0))return mid;high=mid-1}return-(low+1)},exports.insert=function(item,array,comparator){var i=exports.search(item,array,comparator);0>i&&(i=-(i+1)),array.splice(i,0,item)}},{}]},{},[1])(1)});