!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self),n.RTree=e()}}(function(){return function e(n,t,r){function o(a,s){if(!t[a]){if(!n[a]){var h="function"==typeof require&&require;if(!s&&h)return h(a,!0);if(i)return i(a,!0);throw new Error("Cannot find module '"+a+"'")}var c=t[a]={exports:{}};n[a][0].call(c.exports,function(e){return o(n[a][1][e]||e)},c,c.exports,e,n,t,r)}return t[a].exports}for(var i="function"==typeof require&&require,a=0;aa.max[0]&&(a.max[0]=o[r][0]),o[r][1]a.max[1]&&(a.max[1]=o[r][1]),r++;var s={x:a.min[0],y:a.min[1],w:a.max[0]-a.min[0],h:a.max[1]-a.min[1]};return n&&(s.leaf=n),s},i={point:function(e,n){return n.insertSubtree({x:e.geometry.coordinates[0],y:e.geometry.coordinates[1],w:0,h:0,leaf:e},n.root)},multiPointLineString:function(e,n){return n.insertSubtree(o(e.geometry.coordinates,e),n.root)},multiLineStringPolygon:function(e,n){return n.insertSubtree(o(Array.prototype.concat.apply([],e.geometry.coordinates),e),n.root)},multiPolygon:function(e,n){return n.insertSubtree(o(Array.prototype.concat.apply([],Array.prototype.concat.apply([],e.geometry.coordinates)),e),n.root)},makeRec:function(e){return r(e.x,e.y,e.w,e.h)},geometryCollection:function(e,n){if(e.bbox)return n.insertSubtree({leaf:e,x:e.bbox[0],y:e.bbox[1],w:e.bbox[2]-e.bbox[0],h:e.bbox[3]-e.bbox[1]},n.root);for(var t,r=e.geometry.geometries,a=0,s=r.length,h=[];a=t.x()&&n<=t.y2()&&a>=t.y():et.x()&&nt.y()},this.expand=function(r){var s,h,c=r.x(),u=r.y(),l=r.x2(),f=r.y2();return s=e>c?c:e,h=n>u?u:n,t=i>l?i-s:l-s,o=a>f?a-h:f-h,e=s,n=h,this}}r.overlapRectangle=function(e,n){return 0===e.h&&0===e.w||0===n.h&&0===n.w?e.x<=n.x+n.w&&e.x+e.w>=n.x&&e.y<=n.y+n.h&&e.y+e.h>=n.y:e.xn.x&&e.yn.y},r.containsRectangle=function(e,n){return e.x+e.w<=n.x+n.w&&e.x>=n.x&&e.y+e.h<=n.y+n.h&&e.y>=n.y},r.expandRectangle=function(e,n){var t,r,o=e.x+e.w,i=n.x+n.w,a=e.y+e.h,s=n.y+n.h;return t=e.x>n.x?n.x:e.x,r=e.y>n.y?n.y:e.y,e.w=o>i?o-t:i-t,e.h=a>s?a-r:s-r,e.x=t,e.y=r,e},r.makeMBR=function(e,n){if(!e.length)return{x:0,y:0,w:0,h:0};(n=n||{}).x=e[0].x,n.y=e[0].y,n.w=e[0].w,n.h=e[0].h;for(var t=1,o=e.length;t0;)if(i=c.pop(),s=u.pop()-1,"target"in x)for(;s>=0;){if(r.overlapRectangle(x,h=i.nodes[s])){if(x.target&&"leaf"in h&&h.leaf===x.target||!x.target&&("leaf"in h||r.containsRectangle(h,x))){y="nodes"in h?a(i.nodes.splice(s,1)):i.nodes.splice(s,1),r.makeMBR(i.nodes,i),delete x.target;break}"nodes"in h&&(u.push(s),c.push(i),i=h,s=h.nodes.length)}s--}else if("nodes"in x){i.nodes.splice(s+1,1),i.nodes.length>0&&r.makeMBR(i.nodes,i);for(var p=0;p0&&i.nodes.length0;)c(e,n[0],n[1]);return n},c=function(e,t,o){for(var i,a,s,h=r.squarifiedRatio(t.w,t.h,t.nodes.length+1),c=r.squarifiedRatio(o.w,o.h,o.nodes.length+1),u=e.length-1;u>=0;u--){var l=e[u],f={};f.x=Math.min(t.x,l.x),f.y=Math.min(t.y,l.y),f.w=Math.max(t.x+t.w,l.x+l.w)-f.x,f.h=Math.max(t.y+t.h,l.y+l.h)-f.y;var y=Math.abs(r.squarifiedRatio(f.w,f.h,t.nodes.length+2)-h),x={};x.x=Math.min(o.x,l.x),x.y=Math.min(o.y,l.y),x.w=Math.max(o.x+o.w,l.x+l.w)-x.x,x.h=Math.max(o.y+o.h,l.y+l.h)-x.y;var p=Math.abs(r.squarifiedRatio(x.w,x.h,o.nodes.length+2)-c);(!a||!i||Math.abs(p-y)=0;s--){var h=e[s];h.x>e[o].x?o=s:h.x+h.we[a].y?a=s:h.y+h.hMath.abs(e[i].y+e[i].h-e[a].y)?r>o?(n=e.splice(r,1)[0],t=e.splice(o,1)[0]):(t=e.splice(o,1)[0],n=e.splice(r,1)[0]):i>a?(n=e.splice(i,1)[0],t=e.splice(a,1)[0]):(t=e.splice(a,1)[0],n=e.splice(i,1)[0]),[{x:n.x,y:n.y,w:n.w,h:n.h,nodes:[n]},{x:t.x,y:t.y,w:t.w,h:t.h,nodes:[t]}]},l=function(e,n,t,o){var i=[];if(!r.overlapRectangle(e,o))return t;for(i.push(o.nodes);i.length>0;)for(var a=i.pop(),s=a.length-1;s>=0;s--){var h=a[s];r.overlapRectangle(e,h)&&("nodes"in h?i.push(h.nodes):"leaf"in h&&t.push(n?h:h.leaf))}return t},f=function(e,n){var o;if(0===n.nodes.length)return n.x=e.x,n.y=e.y,n.w=e.w,n.h=e.h,void n.nodes.push(e);for(var i,a=function(e,n){var t,o=-1,i=[],a=!0;i.push(n);for(var s=n.nodes;a||-1!==o;){a?a=!1:(i.push(s[o]),s=s[o].nodes,o=-1);for(var h=s.length-1;h>=0;h--){var c=s[h];if("leaf"in c){o=-1;break}var u=r.squarifiedRatio(c.w,c.h,c.nodes.length+1),l=Math.max(c.x+c.w,e.x+e.w)-Math.min(c.x,e.x),f=Math.max(c.y+c.h,e.y+e.h)-Math.min(c.y,e.y),y=r.squarifiedRatio(l,f,c.nodes.length+2);(o<0||Math.abs(y-u)0;){if(o&&"nodes"in o&&0===o.nodes.length){i=o,o=a.pop();for(var c=0;c0;)t=(n=s(e,!1,i)).length,r=r.concat(n);return r}(e)},this.insert=function(e,n){return f({x:e.x,y:e.y,w:e.w,h:e.h,leaf:n},i)}}o.prototype.toJSON=function(e){return JSON.stringify(this.root,!1,e)},o.fromJSON=function(e){var n=new o;return n.setTree(JSON.parse(e)),n},n.exports=o,"function"!=typeof Array.isArray&&(Array.isArray=function(e){return"object"==typeof e&&"[object Array]"==={}.toString.call(e)})},{"./rectangle":3}]},{},[2])(2)});