Advertisement
sharthZ

Untitled

Mar 26th, 2015
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;var HeatmapImageryProvider = (function CreateHeatmapImageryProvider(){
  2.  
  3.     var defaultHeatmapConfig = {
  4.         radius: 40,
  5.         renderer: 'canvas2d',
  6.         gradient: { 0.25: "rgb(0,0,255)", 0.55: "rgb(0,255,0)", 0.85: "yellow", 1.0: "rgb(255,0,0)"},
  7.         maxOpacity: 1,
  8.         minOpacity: 0,
  9.         blur: 0.85,
  10.         xField: 'x',
  11.         yField: 'y',
  12.         valueField: 'value'
  13.     };
  14.  
  15.     var defaultOptions = {
  16.         useLocalMaximum: true,
  17.         canvasWidth: 256,
  18.         canvasHeight: 256
  19.     };
  20.  
  21.     function lon2x(a) { return (a + 180) / 360; }
  22.     function lat2y(a) { return 1 - (Math.log(Math.tan(Math.PI/4)+a*Math.PI/360))/Math.PI; }
  23.  
  24.     function lon2tilex(a) { return (a + 180) / 360; }
  25.     function lat2tiley(a) { return 1 - (Math.log(Math.tan(a*Math.PI/180)+1/Math.cos(a*Math.PI/360)))/Math.PI; }
  26.  
  27.     function preprocessingData(data){
  28.         var obj = JSON.parse(data);
  29.         var object = obj["tokens.geopoints_full.geohash_type"];
  30.         var arrayname = object["_type"];
  31.         var points = object[arrayname];
  32.         var maxValue = -1;
  33.         var minValue = Number.MAX_VALUE;
  34.         var value = 0, x = -1, y = -1;
  35.         var latlng = null;
  36.         var newpoints = [];
  37.         for(var p in points) {
  38.             value = 0;
  39.             latlng = null;
  40.             for (var key in p) {
  41.                 if (p.hasOwnProperty(key)) {
  42.                     if(key == "count") {
  43.                         value = p[key];
  44.                         maxValue = value > maxValue? value : maxValue;
  45.                         minValue = value < minValue? value : minValue;
  46.                     }
  47.                     else if(key == "term") {
  48.                         arr = p[key].split('_');
  49.                         latlng = decodeGeoHash(arr[0]);
  50.                         x = lon2x(latlng.longitude);
  51.                         y = lat2y(latlng.latitude);
  52.                     }
  53.                     else {
  54.  
  55.                     }
  56.                 }
  57.             }
  58.             newpoints.push({x: x, y: y, tilex: lon2tilex(latlng.longitude), tiley: lat2tiley(latlng.latitude), value: value});
  59.         }
  60.  
  61.         return {min: minValue, max: maxValue, data: newpoints};
  62.     }
  63.  
  64.     var HeatmapImageryProvider = function HeatmapImageryProvider(hmconfig, options, data) {    
  65.         d = preprocessingData(data);
  66.         this._data = d.data;
  67.         this._maxValue = d.max;
  68.         this._minValue = d.min;
  69.  
  70.         this._canvas = document.createElement('canvas');
  71.         this._canvas.width = options.canvasWidth;
  72.         this._canvas.height = options.canvasHeight;
  73.  
  74.         this._heatmap = h337.create(this._canvas);  
  75.         this._heatmap.configure(hmonfig);
  76.  
  77.     };
  78.  
  79.     Object.defineProperty(HeatmapImageryProvider, "ready", {      
  80.         get: function () {
  81.             return true;
  82.         },
  83.     });
  84.  
  85.     Object.defineProperty(HeatmapImageryProvider, "hasAlphaChannel", {      
  86.         get: function () {
  87.             return true;
  88.         },
  89.     });
  90.  
  91.     Object.defineProperty(HeatmapImageryProvider, "credit", {      
  92.         get: function () {
  93.             return undefined;
  94.         },
  95.     });
  96.  
  97.     Object.defineProperty(HeatmapImageryProvider, "rectangle", {      
  98.         get: function () {
  99.             return undefined;
  100.         },
  101.     });
  102.  
  103.     Object.defineProperty(HeatmapImageryProvider, "tileWidth", {      
  104.         get: function () {
  105.             return this._canvas.width;
  106.         },
  107.     });
  108.  
  109.     Object.defineProperty(HeatmapImageryProvider, "tileHeight", {      
  110.         get: function () {
  111.             return this._canvas.height;
  112.         },
  113.     });
  114.  
  115.     Object.defineProperty(HeatmapImageryProvider, "maximumLevel", {      
  116.         get: function () {
  117.             return undefined;
  118.         },
  119.     });
  120.  
  121.     Object.defineProperty(HeatmapImageryProvider, "minimumLevel", {      
  122.         get: function () {
  123.             return undefined;
  124.         },
  125.     });
  126.  
  127.     Object.defineProperty(HeatmapImageryProvider, "tilingScheme", {      
  128.         get: function () {
  129.             return undefined;
  130.         },
  131.     });
  132.  
  133.     HeatmapImageryProvider.prototype._drawTile = function(x, y, level) {
  134.         var context = this._canvas.getContext('2d');
  135.         var newdata = [];
  136.         var zoom = Math.pow(2, level - 1);
  137.         for(var p in this._data){
  138.             if((Math.abs(Math.floor(p.tilex*2*zoom) - x) < 2)&&(Math.abs(Math.floor(p.tiley*zoom) - y) < 2)){
  139.                 newdata.push({
  140.                     x: p.x*2*zoom - this._canvas.width*x,
  141.                     y: p.y*zoom - this._canvas.height*y,
  142.                     value: p.value
  143.                 });
  144.                 //maxValue = value > maxValue? value : maxValue;
  145.                 //minValue = value < minValue? value : minValue;
  146.             }
  147.  
  148.         }
  149.         this._heatmap.setData({max: this._maxValue, min: this._minValue, data:newdata});
  150.         return this._canvas;
  151.     };
  152.  
  153.     HeatmapImageryProvider.prototype.getTileCredits = function(x, y, level) {
  154.         return undefined;
  155.     };
  156.  
  157.  
  158.     HeatmapImageryProvider.prototype.requestImage = function(x, y, level) {
  159.         return this._drawTile(x, y, level);
  160.     };
  161.  
  162.  
  163.     HeatmapImageryProvider.prototype.pickFeatures = function() {
  164.         return undefined;
  165.     };
  166.  
  167.     return HeatmapImageryProvider;
  168. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement