-
Notifications
You must be signed in to change notification settings - Fork 4
/
cluster.min.js
1 lines (1 loc) · 5.1 KB
/
cluster.min.js
1
dojo.provide("modules.ClusterLayer"),dojo.addOnLoad(function(){dojo.declare("modules.ClusterLayer",null,{constructor:function(a,b){this.options=b||{},this.data=a,this.size=b.size||8,this.pxgrid=this.options.pixelsSquare||128,this.intervals=this.options.intervals||5,this.rgb=this.options.rgb||[165,76,91],this.textrgb=this.options.textrgb||[5,5,5,1],this.pattern=[],this.gravity=!0,this.test=[],this.options.hasOwnProperty("gravity")&&(this.gravity=this.options.gravity),this._map=this.options.map;var c=this.options.id||"ClusterGraphicsLayer"+Math.ceil(Math.random(100)*100);this.graphics=new esri.layers.GraphicsLayer({id:c}),this.options.hasOwnProperty("visible")&&(this.graphics.visible=this.options.visible),this._map.loaded?this._map.addLayer(this.graphics):dojo.connect(this._map,"onLoad",dojo.hitch(this,function(){this._map.addLayer(this.graphics)})),this.globalMax=!0,this.options.hasOwnProperty("globalMax")&&(this.globalMax=this.options.globalMax),dojo.connect(this._map,"onZoomEnd",this,this.regrid),this._draw(),this.loaded=!0,this.onLoad(this)},setOpacity:function(a){this.opacity!=a&&this.onOpacityChange(this.opacity=a)},regrid:function(){this.setData(this.lastDataset)},onLoad:function(){},onUpdateStart:function(){},onUpdateEnd:function(){},setData:function(a){this.onUpdateStart(),this.lastDataset=a,this.count=a.length;var b={},c=dojo.coords(this._map.id).w*1/(this.pxgrid*1),d=(this._map.extent.xmax-this._map.extent.xmin)/c;b.gridsquare=d;var e=999999,f=0;dojo.forEach(a,function(a){var c=Math.round(a.y/d)+"|"+Math.round(a.x/d);b[c]?(b[c].count+=1,b[c].avgx+=(a.x-b[c].avgx)/b[c].count,b[c].avgy+=(a.y-b[c].avgy)/b[c].count):b[c]={count:1,avgx:a.x,avgy:a.y},b[c].count<e&&(e=b[c].count),b[c].count>f&&(f=b[c].count)}),this.data={data:b,noDataValue:[0],minCluster:e,maxCluster:f,points:a.length},b={},this.pattern=[];var g=e,h=f,i=Math.round((h-g)/this.intervals);if(g==1||this.showSingle)this.pattern[0]={min:0,max:1};for(var j=0;j<=this.intervals;j++)this.pattern[j+1]={},this.pattern[j+1].min=g+j*i,this.pattern[j+1].max=g+(j+1)*i;this._getColors(),this._draw()},_getMax:function(a){return Math.max.apply(Math,a)},_getMin:function(a){return Math.min.apply(Math,a)},_getEqualInterval:function(a,b){var c=a.length/b,d=1,e=a.length,f=[],g=0;for(var h=g;h<b;h++)f[h]={},f[h].min=d+h*c,f[h].max=d+Math.floor((h+1)*c);return f},clear:function(){this.graphics.clear(),this.test=[]},getColorValues:function(){var a=[],b=.25;return dojo.forEach(this.pattern,function(c){a.push("rgba("+this.rgb.concat(b).join(",")+")"),b+=.15}),a},getRange:function(){var a=this.data;if(!a)return;var b=a.data,c=a.noDataValue[0],d=0,e=0,f=this._map;for(var g in b)if(b.hasOwnProperty(g)){var h=b[g];if(h==c)continue;if(!this.globalMax){var i;if(g.split("|").length==4)i=f.toScreen(esri.geometry.Point((g.split("|")[0]*1+g.split("|")[1]*1)*b.gridsquare/2,(g.split("|")[2]*1+g.split("|")[3]*1)*b.gridsquare/2,f.spatialReference));else if(g.split("|").length==2)try{i=f.toScreen(esri.geometry.Point(g.split("|")[1]*b.gridsquare/2,g.split("|")[0]*b.gridsquare/2),f.spatialReference)}catch(j){continue}if(!i)continue}h>d&&(d=h),h<e&&(e=h)}return{min:e,max:d}},setVisibility:function(a){this.graphics.setVisibility(a)},show:function(){this.graphics.show()},hide:function(){this.graphics.hide()},_draw:function(){this.clear();if(!this.data)return;var a=this.data,b=a.noDataValue[0],c=a.data,d=this.getRange(),e=d.min,f=d.max;if(e==f&&f==0)return;var g=this._map;for(var h in c)if(c.hasOwnProperty(h)){if(h.indexOf("|")==-1)continue;var i=new esri.geometry.Extent({xmin:c.gridsquare*h.split("|")[1]-c.gridsquare/2,ymin:c.gridsquare*h.split("|")[0]-c.gridsquare/2,xmax:c.gridsquare*h.split("|")[1]+c.gridsquare/2,ymax:c.gridsquare*h.split("|")[0]+c.gridsquare/2,spatialReference:g.spatialReference}),j=c[h].count,k=null,l="9pt",m=!0;for(var n=0;n<this.pattern.length;n++){m=!0;if(j<=this.pattern[n].max){k=this.pattern[n].symbol,l=this.pattern[n].textsize,n===0&&(m=!1);break}}var o=c.gridsquare*h.split("|")[1],p=c.gridsquare*h.split("|")[0];if(this.gravity){var q=this.size,r=k.size,s=q/this.pxgrid*c.gridsquare,t=c.gridsquare*r/this.pxgrid;o+c.gridsquare/2-c[h].avgx<=s&&(c[h].avgx=o+t),c[h].avgx-(o-c.gridsquare/2)<=s&&(c[h].avgx=o-t),p+c.gridsquare/2-c[h].avgy<=s&&(c[h].avgy=p+t),c[h].avgy-(p-c.gridsquare/2)<=s&&(c[h].avgy=p-t)}else c[h].avgx=o,c[h].avgy=p;var u=new esri.geometry.Point(c[h].avgx,c[h].avgy,g.spatialReference);this.graphics.add(new esri.Graphic(u,k,{extent:i,count:j})),m&&this.graphics.add(new esri.Graphic(u,(new esri.symbol.TextSymbol(j)).setColor(new dojo.Color(this.textrgb)).setFont(new esri.symbol.Font(l)).setOffset(0,-5),{extent:i,count:j}))}c=null,a=null,this.onUpdateEnd()},_getColors:function(){var a=1,b=.9,c=7,d=this.pattern.length,e=parseInt(this.pxgrid/d)>this.size?parseInt(this.pxgrid/d-this.size):parseInt(this.size/2),f=this.size,g=.11,h=1-g*d;for(var i=0;i<d;i++)this.pattern[i].symbol=new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE,f,new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,new dojo.Color(this.rgb.concat(h)),a),new dojo.Color(this.rgb.concat(b))),this.pattern[i].size=f,this.pattern[i].textsize=c++ +"pt",f+=e,a+=2}})});