forked from mgax/gistmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
162 lines (146 loc) · 5.24 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css">
<style>
html, body, #map { height: 100%; margin: 0; }
</style>
</head>
<body>
<div id="map"></div>
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/tabletop.js/1.1.0/tabletop.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
(function() {
'use strict';
var layerRender = {
osm: function(map) {
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">' +
'OpenStreetMap</a> contributors'
}).addTo(map);
},
choropleth: function(map, layerConfig) {
var colorRange = layerConfig.colorRange;
var color = d3.scale.linear()
.domain([colorRange.min.value, colorRange.max.value])
.range([colorRange.min.color, colorRange.max.color])
.interpolate(d3.interpolateLab);
var data = {};
var layer = L.geoJson([], {
style: function(feature) {
var code = feature.properties[layerConfig.join.featureProperty];
var value = data[code];
return {
weight: 1,
fillColor: color(value),
fillOpacity: 0.5
};
},
onEachFeature: function(feature, layer) {
var code = feature.properties[layerConfig.join.featureProperty];
layer.bindPopup("" + data[code]);
}
});
layer.addTo(map);
var googleMatch = layerConfig.data.match(/^google-spreadsheet (.+)$/);
if(googleMatch) {
var spreadsheetKey = googleMatch[1];
Tabletop.init({
key: spreadsheetKey,
simpleSheet: true,
callback: function(row_list) {
_.forEach(row_list, function(row) {
var value = parseFloat(row[layerConfig.join.dataValue]);
var key = row[layerConfig.join.dataKey];
data[key] = value;
});
$.getJSON(layerConfig.features, function(resp) {
var coll = topojson.feature(resp, _.values(resp.objects)[0]);
layer.addData(coll.features);
map.fitBounds(layer.getBounds());
});
}
});
}
else {
console.log("Can't understand data source", layerConfig.data);
return;
}
}
};
function renderLayer(map, layerConfig) {
if(layerConfig.type == 'osm') {
renderOsmLayer(map);
}
}
function renderMap(config) {
var map = L.map('map');
_.forEach(config.layers, function(layerConfig) {
var renderFunction = layerRender[layerConfig.type];
if(renderFunction) {
renderFunction(map, layerConfig);
}
else {
console.log("Unknown layer type", layerConfig.type);
}
});
}
var providerMap = {
gist: function(options) {
configFromGist(options.id, function(config) {
renderMap(config);
});
},
path: function(options) {
$.getJSON(options.path, function(config) {
renderMap(config);
});
}
};
function loadConfig() {
var source = parseQueryString(window.location.search);
if(source && providerMap[source.provider]) {
providerMap[source.provider](source);
}
else {
console.log("Could not match a provider");
}
}
function parseQueryString(queryString) {
var gistMatch = queryString.match(/^\?gist=(.+)$/);
if(gistMatch) { return {provider: 'gist', id: gistMatch[1]}; }
var pathMatch = queryString.match(/^\?path=(.+)$/);
if(pathMatch) { return {provider: 'path', path: pathMatch[1]}; }
return null;
}
function configFromGist(id, callback) {
$.ajax({
type: 'GET',
url: 'https://api.github.com/gists/' + id,
dataType: 'jsonp',
success: function(resp) {
callback(JSON.parse(resp.data.files['map.json'].content));
}
});
}
loadConfig();
})();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-48378796-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>