You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
5.4 KiB
JavaScript

!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,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module){function collapse(results){for(var l=[],included={},i=0;i<results.length;i++)void 0==included[L.stamp(results[i].layer)]&&(l.push(results[i]),included[L.stamp(results[i].layer)]=!0);return l}function reformat(coords,layer){for(var l=[],i=0;i<coords.length;i++)l.push({lon:coords[i][0],lat:coords[i][1],layer:layer});return l}function flat(coords){var i=0,j=0,k=0,l=[];if("object"==typeof coords[0]&&"object"==typeof coords[0][0]&&"object"==typeof coords[0][0][0]){for(;i<coords.length;i++)for(;j<coords[i].length;j++)for(;k<coords[i][j].length;k++)l.push(coords[i][j][k]);return l}if("object"==typeof coords[0]&&"object"==typeof coords[0][0]){for(;i<coords.length;i++)for(;j<coords[i].length;j++)l.push(coords[i][j]);return l}return"object"==typeof coords[0]?coords:[coords]}var sphereKnn=require("sphere-knn");module.exports=function(layer){"use strict";function collectPoints(l){points=points.concat(reformat(flat(l.feature.geometry.coordinates),l))}if(!(layer instanceof L.GeoJSON))throw new Error("must be L.GeoJSON");var points=[];layer.eachLayer(collectPoints);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.nearestLayer=function(p,n,max_distance){return p instanceof L.LatLng&&(p=[p.lng,p.lat]),collapse(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":5}],3:[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)}},{}],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&&array[array.length-1].dist<dist*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":3}],5:[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=new 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}]},{},[1])(1)});