Skip to content

Instantly share code, notes, and snippets.

@albertsun
Created November 1, 2011 21:09
Show Gist options
  • Save albertsun/1331916 to your computer and use it in GitHub Desktop.
Save albertsun/1331916 to your computer and use it in GitHub Desktop.
LoadCensusBlockTiles
/* Author: Albert Sun
WSJ.com News Graphics
*/
/*jslint white: false, nomen: false, debug: false, devel: true, onevar: false, plusplus: false, browser: true, bitwise: false, es5: true, maxerr: 200 */
/*global jQuery: false, $: false, log: false, window: false, WSJNG: false, _: false, google: false, localStorage: false */
// Necessary functions
if (!window.typeOf) {
window.typeOf = function(b){var a=typeof b;if(a==="object")if(b){if(b instanceof Array)a="array"}else a="null";return a};
}
// ***************************************
// Just in case there's a console.log hanging around....
// ***************************************
if (!window.console) { window.console = { "log": jQuery.noop }; }
// ***************************************
// Set up a Global Namespace
// ***************************************
var WSJNG = WSJNG || {};
WSJNG.query = WSJNG.query || {};
WSJNG.query.LoadCensusBlockTiles = (function() {
var self = {};
var loaded = new WSJNG.Dictionary();
activexhr = [];
self.setController = function(controller) {
this.controller = controller;
};
self.handleDjangoResponse = function(data, textStatus, jqXHR) {
var i, len, GEOID10, geom, info;
var url = this.url.split(/\/|\./);
var tile = [url[4], url[5], url[3]]; //[x,y,z]
data = data.features;
for (i=0, len=data.length; i<len; i++) {
GEOID10 = data[i].id;
geom = WSJNG.geom.ParseGeoJSONMultiPolygon(data[i].geometry.coordinates);
if (data[i].properties !== undefined) {
info = data[i].properties;
}
if (!WSJNG.Blocks[GEOID10]) {
WSJNG.Blocks[GEOID10]= new WSJNG.Feature({"GEOID10": GEOID10, "multipolygon": geom, "fields": info, "map": WSJNG.map, "controller": self.controller});
if (self.controller.SelectionController.containsFeature(GEOID10) === true) {
WSJNG.Blocks[GEOID10].selected = true;
}
}
WSJNG.Blocks[GEOID10].tiles[tile.join("_")] = true;
}
};
self.LoadTile = function(tile) {
var key = "", xhrobj;
key = tile.join("_");
if (loaded[key] !== true) {
xhrobj = $.getJSON("/geom/CensusBlockTile/"+tile[2]+"/"+tile[0]+"/"+tile[1]+".json")
.complete(function(jqXHR, textStatus) {
for (var i=0, len=activexhr.length; i<len; i++) {
if (activexhr[i] === jqXHR) {
//console.log("removing xhr "+activexhr.length);
activexhr.splice(i,1);
return;
}
}
});
activexhr.push(xhrobj);
$.when(xhrobj).then(
this.handleDjangoResponse,
(function(k) { return function() {
//fail handler: if the loadtile request fails, mark it as unloaded so we know to try again
loaded[k] = false;
}; }(key))
);
loaded[key] = true;
} else {
console.log('tile already loaded');
}
};
self.RemoveTile = function(tilekey) {
// 1. remove tileid from the query loaded object
// 2. go through all blocks in the tile and remove self from them
// 3. if a block that is getting the tile removed has no tiles remaining, remove it
// loaded may not even be neccessary if google takes care of not requesting any tiles that have already been added
// let's keep it for now anyways
// it might be too slow to loop through all thousands of blocks every time a tile is unloaded
// should save the GEOIDS associated with that tile with the tilekey for a quicker lookup
loaded[tilekey] = false;
var keys = WSJNG.Blocks.keys();
for (var i=0,len=keys.length; i<len; i++) {
if (self.controller.SelectionController.containsFeature(keys[i]) !== true) {
delete WSJNG.Blocks[keys[i]].tiles[tilekey];
if (WSJNG.Blocks[keys[i]].tiles.length == 0) {
WSJNG.Blocks[keys[i]].remove();
delete WSJNG.Blocks[keys[i]];
}
}
}
};
/**
* Remove all tiles from the map, except for selected ones
*/
self.ReleaseBlocks = function() {
$.when.apply(this, activexhr).always(function() {
var counter = 0;
var keys = WSJNG.Blocks.keys();
for (var i=0,len=keys.length; i<len; i++) {
if (self.controller.SelectionController.containsFeature(keys[i]) !== true) {
WSJNG.Blocks[keys[i]].remove();
delete WSJNG.Blocks[keys[i]];
counter++;
}
}
loaded = new WSJNG.Dictionary();
//console.log("removed "+counter+" blocks");
});
//console.log("aborting xhr "+activexhr.length);
_.each(activexhr, function(el, i, list) {
el.abort();
});
};
return self;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment