Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 72083082fe14da1a9b090bb5066e54e90cdd3d69 @mheadd committed Dec 19, 2012
Showing with 11,611 additions and 0 deletions.
  1. +100 −0 index.html
  2. +210 −0 lib/backbone.cartodb.js
  3. +1,159 −0 lib/backbone.js
  4. BIN lib/blank_tile.png
  5. +2,799 −0 lib/carto.js
  6. +542 −0 lib/cartodb-gmapsv3.js
  7. +26 −0 lib/cartodb.css
  8. +66 −0 lib/class.js
  9. +94 −0 lib/dat.gui.min.js
  10. +2,945 −0 lib/modestmaps.js
  11. BIN lib/sprite.png
  12. +31 −0 lib/underscore-min.js
  13. +2,766 −0 lib/wax.g.js
  14. +7 −0 readme.md
  15. +94 −0 src/canvas_tile_layer.js
  16. +416 −0 src/grid_layer.js
  17. +356 −0 src/torque.js
@@ -0,0 +1,100 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
+ <title>Philadelphia Homicides: 2006-2012</title>
+ <link rel="shortcut icon" href="http://cartodb.com/favicon/favicon_32x32.ico" />
+ <link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
+ <link rel="stylesheet" href="lib/cartodb.css">
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
+ <script type="text/javascript" src="lib/wax.g.js"></script>
+ <script type="text/javascript" src="lib/cartodb-gmapsv3.js"></script>
+ <script type="text/javascript" src="lib/dat.gui.min.js"></script>
+ <script type="text/javascript" src="lib/underscore-min.js"></script>
+ <script type="text/javascript" src="lib/backbone.js"></script>
+ <script type="text/javascript" src="lib/class.js"></script>
+ <script type="text/javascript" src="lib/backbone.cartodb.js"></script>
+ <script type="text/javascript" src="src/canvas_tile_layer.js"></script>
+ <script type="text/javascript" src="src/grid_layer.js"></script>
+ <script type="text/javascript" src="src/torque.js"></script>
+ <script type="text/javascript">
+ var gui;
+ function initialize() {
+ // initialise the google map
+ var map = new google.maps.Map(document.getElementById('map_canvas'), {
+ center: new google.maps.LatLng(39.987642831840844,-75.14717102050781),
+ zoom: 12,
+ mapTypeId:google.maps.MapTypeId.SATELLITE,
+ mapTypeControl:false,
+ minZoom:1,
+ scrollwheel: false,
+ panControl: false,
+ zoomControl: false,
+ scaleControl: false,
+ streetViewControl: false,
+ overviewMapControl: false,
+ });
+
+ var map_style = {};
+ map_style.google_maps_customization_style = [
+ {
+ stylers:[
+ { invert_lightness:true },
+ { weight:1 },
+ { saturation:-50 },
+ { lightness:-10 }
+ ]
+ },
+ {
+ elementType:"labels",
+ stylers:[
+ { visibility:"simplified" }
+ ]
+ }
+ ];
+
+ var Soft = function () {
+ this.Soft = function () {
+ map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
+ map.setOptions({styles:map_style.google_maps_customization_style});
+ }
+ }
+
+
+ map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
+ map.setOptions({styles:map_style.google_maps_customization_style});
+
+ var TorqueOptions = {
+ user: "markh",
+ table: "crimes",
+ column: "dispatch_date_time",
+ cumulative: true,
+ resolution: 2,
+ steps: 285,
+ fps: 24,
+ fitbounds: false,
+ clock: true,
+ blendmode: 'source-over',
+ trails: true,
+ point_type:'circle',
+ cellsize:2
+ }
+
+ var torque = null;
+ Torque(function (env) {
+ Torque.app = new env.app.Instance();
+ torque = new Torque.app.addLayer(map, TorqueOptions);
+ Torque.env = env;
+ });
+
+ }
+ </script>
+</head>
+<body onload="initialize()">
+ <div id="map_canvas"></div>
+ <div class="torque_time"></div>
+ <a class="cartodb_logo" href="http://www.cartodb.com" target="_blank">CartoDB</a>
+</body>
+</html>
@@ -0,0 +1,210 @@
+
+/**
+ *
+ * backbone cartodb adapter
+ *
+ * this is a small library that allows to use Backbone with models
+ * to work with data stored in CartoDB (a geospatial database on
+ * the cloud, see more info at http://cartodb.com).
+ *
+ * it does NOT overrride Backbone.sync
+ *
+ */
+
+Backbone.CartoDB = function(options, query, cache) {
+
+ options = _.defaults(options, {
+ USE_PROXY: false,
+ user: ''
+ });
+
+ function _SQL(sql) {
+ this.sql = sql;
+ }
+ function SQL(sql) {
+ return new _SQL(sql);
+ }
+
+ // SQL("{0} is {1}").format("CartoDB", "epic!");
+ _SQL.prototype.format = function() {
+ var str = this.sql,
+ len = arguments.length+1;
+ var safe, arg;
+ for (i=0; i < len; arg = arguments[i++]) {
+ safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
+ str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
+ }
+ return str;
+ };
+
+
+ var resource_path= options.user + '.cartodb.com/api/v2/sql';
+ var resource_url = 'https://' + resource_path;
+
+ /**
+ * fetch sql from the server
+ *
+ * this function should be changed if you're working on node
+ *
+ */
+ query = query || function(sql, callback, proxy) {
+ var url = resource_url;
+ var crossDomain = true;
+ if(proxy) {
+ url = 'api/v0/proxy/' + resource_url;
+ crossDomain = false;
+ }
+ if(sql.length > 1500) {
+ $.ajax({
+ url: url,
+ crossDomain: crossDomain,
+ type: 'POST',
+ dataType: 'json',
+ data: 'q=' + encodeURIComponent(sql),
+ success: callback,
+ error: function(){
+ if(proxy) {
+ callback();
+ } else {
+ //try fallback
+ if(USE_PROXY) {
+ query(sql, callback, true);
+ }
+ }
+ }
+ });
+ } else {
+ // TODO: add timeout
+ $.getJSON(resource_url + '?q=' + encodeURIComponent(sql) + '&callback=?')
+ .success(callback)
+ .fail(function(){
+ callback();
+ }).complete(function() {
+ });
+ }
+ };
+
+ var dummy_cache = {
+ setItem: function(key, value) { },
+ getItem: function(key) { return null; },
+ removeItem: function(key) { }
+ };
+
+ cache = cache && dummy_cache;
+
+
+ var CartoDBModel = Backbone.Model.extend({
+
+ _create_sql: function() {
+ var where = SQL(" where {0} = '{1}'").format(
+ this.columns[this.what],
+ this.get(this.what).replace("'", "''")
+ );
+ var select = this._sql_select();
+ var sql = 'select ' + select.join(',') + ' from ' + this.table + where;
+ return sql;
+ },
+
+ _sql_select: function() {
+ var select = [];
+ for(var k in this.columns) {
+ var w = this.columns[k];
+ if(w.indexOf('ST_') !== -1 || w === "the_geom") {
+ select.push(SQL('ST_AsGeoJSON({1}) as {0}').format(k,w));
+ } else {
+ select.push(SQL('{1} as {0}').format(k, w));
+ }
+ }
+ return select;
+ },
+
+ _parse_columns: function(row) {
+ var parsed = {};
+ for(var k in row) {
+ var v = row[k];
+ var c = this.columns[k];
+ if (c.indexOf('ST_') !== -1 || c === "the_geom") {
+ parsed[k] = JSON.parse(v);
+ } else {
+ parsed[k] = row[k];
+ }
+ }
+ return parsed;
+ },
+
+ fetch: function() {
+ var self = this;
+ query(this._create_sql(), function(data) {
+ self.set(self._parse_columns(data.rows[0]));
+ });
+ }
+ });
+
+
+ /**
+ * cartodb collection created from a sql composed using 'columns' and
+ * 'table' attributes defined in a child class
+ *
+ * var C = CartoDBCollection.extend({
+ * table: 'table',
+ * columns: ['c1', 'c2']
+ * });
+ * var c = new C();
+ * c.fetch();
+ */
+ var CartoDBCollection = Backbone.Collection.extend({
+
+ _create_sql: function() {
+ var tables = this.table;
+ if(!_.isArray(this.table)) {
+ tables = [this.table];
+ }
+ tables = tables.join(',');
+ var select = CartoDBModel.prototype._sql_select.call(this);
+ var sql = 'select ' + select.join(',') + ' from ' + this.table;
+ if (this.where) {
+ sql += " WHERE " + this.where;
+ }
+ return sql;
+ },
+
+ fetch: function() {
+ var self = this;
+ var sql = this.sql || this._create_sql();
+ if(typeof(sql) === "function") {
+ sql = sql.call(this);
+ }
+ var item = this.cache ? cache.getItem(sql): false;
+ if(!item) {
+ query(sql, function(data) {
+ if(this.cache) {
+ try {
+ cache.setItem(sql, JSON.stringify(data.rows));
+ } catch(e) {}
+ }
+ var rows;
+ if(!self.sql) {
+ rows = _.map(data.rows, function(r) {
+ return CartoDBModel.prototype._parse_columns.call(self, r);
+ });
+ } else {
+ rows = data.rows;
+ }
+ self.reset(rows);
+ });
+ } else {
+ self.reset(JSON.parse(item));
+ }
+ }
+
+ });
+
+
+ return {
+ query: query,
+ CartoDBCollection: CartoDBCollection,
+ CartoDBModel: CartoDBModel,
+ SQL: SQL
+ };
+
+};
Oops, something went wrong.

0 comments on commit 7208308

Please sign in to comment.