-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
272 lines (227 loc) · 9.2 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>LegisGraphSpatial</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v2.2.3/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v2.2.3/mapbox.css' rel='stylesheet' />
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="/lib/neo4j-web.min.js"></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:100px; bottom:0; width:100%; }
#desc{
position:absolute;
margin-left:5px;
margin-right: 5px;
height: 100px;
width:100%;
}
.committees {
margin-left: 0px;
}
.heading {
margin-top: 0px;
margin-bottom: 0px;
padding-top: 25px;
}
.top-row {
background: lightgray;
}
</style>
</head>
<body>
<div id="desc">
<div class="row top-row">
<div class="col-xs-6">
<h3 class="heading">Legis-graph-spatial <small>Click anywhere in the US to learn more.</small></h3>
</div>
<div class="col-xs-6">
<p class="lead">Do you know who your representative is? Click anywhere in the US to learn more about your representative and the subjects over which they have influence. <a href="https://github.com/legis-graph/legis-graph-spatial">How does it work?</a></p>
</div>
</div>
</div>
<div id='map'></div>
<script>
$(function(){
/**
* Constants
*/
var boltURI = 'bolt://138.68.21.245';
var MB_API_TOKEN = "pk.eyJ1IjoibHlvbndqIiwiYSI6IndwUXlLUjQifQ.DIiytYUASOcXjKdpXW8S-Q";
var driver = neo4j.v1.driver(boltURI);
var session = driver.session();
var subjectsQuery =
"CALL spatial.withinDistance('geom', {latitude: {lat}, longitude: {lon}}, 1) YIELD node AS d \n" +
"WITH d, d.wkt AS wkt, d.state AS state, d.district AS district LIMIT 1 \n" +
"MATCH (d)<-[:REPRESENTS]-(l:Legislator) \n" +
"MATCH (l)-[:SERVES_ON]->(c:Committee) \n" +
"MATCH (c)<-[:REFERRED_TO]-(b:Bill) \n" +
"MATCH (b)-[:DEALS_WITH]->(s:Subject) \n" +
"WITH wkt, state, district, l.govtrackID AS govtrackID, l.lastName AS lastName, l.firstName AS firstName, l.currentParty AS party, s.title AS subject, count(*) AS strength, collect(DISTINCT c.name) AS committees ORDER BY strength DESC LIMIT 10 \n" +
"WITH wkt, state, district, {lastName: lastName, firstName: firstName, govtrackID: govtrackID, party: party, committees: committees} AS legislator, collect({subject: subject, strength: strength}) AS subjects \n" +
"RETURN {legislator: legislator, subjects: subjects, state: state, district: district} AS r, wkt LIMIT 1";
var districtQuery =
"CALL spatial.withinDistance('geom', {latitude:{lat}, longitude:{lon}}, 1) YIELD node AS d \n" +
"RETURN d.wkt AS wkt, d.state AS state, d.district AS district LIMIT 1;"
/**
* Clears the map. Remove all currently shown layers
* */
function clearMap(m) {
for(var i in m._layers) {
if(m._layers[i]._path != undefined) {
try {
m.removeLayer(m._layers[i]);
}
catch(e) {
console.log("problem with " + e + m._layers[i]);
}
}
}
}
/**
* Converts Polygon WKT string to an array of arrays of [x,y] points.
* Each outer array represents one polygon of the multi-polygon set.
* Each inner array is a collection of points on the map for a single polygon.
*/
function parseWKTPolygons(wkt) {
var pointArr = [];
var points = wkt.slice(16, -4).split(",");
// The geometries are MULTIPOLYGONS therefore there can be multiple polygons in each geometry
var polygons = wkt.slice(16, -4).split(")), ((").map(function (polygon) {
return polygon.split(", ").map(function (pair) {
return pair.split(" ").reverse().map(function (num) {
return parseFloat(num);
})
}).filter(function (point) {
return !(isNaN(point[0]) || isNaN(point[1]));
});
});
return polygons;
}
function getDistricts(latlng, distance) {
var districtParams = {
"layer": "geom",
"lon": latlng.lng,
"lat": latlng.lat,
"distanceInKm": distance
}
session
.run(subjectsQuery, districtParams)
.then(function(result){
result.records.forEach(function(record) {
console.log(record);
drawPolygons(parseWKTPolygons(record.get("wkt")), 'brown');
addDistrictPopup(record.get("r"), latlng);
});
})
}
/**
* Generate the content for the popup
* @param d
* @returns {string}
*/
function buildPopup(d) {
var legislator = d["legislator"];
var committees = legislator["committees"];
var legislatorName = "Rep. " + legislator["firstName"] + " " + legislator["lastName"];
var imgURL= "https://www.govtrack.us/data/photos/" + legislator["govtrackID"] + "-50px.jpeg";
var subjects = d["subjects"];
var text =
'<ul class="nav nav-tabs" role="tablist">' +
'<li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Info</a></li>' +
'<li role="presentation"><a href="#code" aria-controls="code" role="tab" data-toggle="tab">Code</a></li>' +
'</ul>' +
'<div class="tab-content">' +
'<div role="tabpanel" class="tab-pane active" id="home">' +
'<div class="row">' +
'<div class="col-xs-3">' +
'<img src="'+ imgURL + '">' +
'</div>' +
'<div class="col-xs-9">' +
'<ul class="list-unstyled">' +
'<li><strong>' + legislatorName + '</strong></li>' +
'<li>District: ' + d["district"] + '</li>' +
'<li>State: ' + d["state"] + '</li>' +
'</div>' +
'</div>' +
'<div class="row committees">' +
'<dl><dt>Committees</dt>';
$.each(committees, function(i,v) {
text = text + '<dd>' + v + '</dd>';
});
text = text +
'</dl>' +
'</div>' +
'<div class="row">' +
'<ul>';
$.each(subjects, function(i,v) {
text = text + '<li>' + v["subject"] + " (" + v["strength"] + ")";
});
text = text +
'</ul>' +
'</div>' +
'</div>' +
'<div role="tabpanel" class="tab-pane" id="code">' +
'<pre class="pre-scrollable">' + subjectsQuery + '</pre>' +
'</div>' +
'</div>'
;
return text;
}
/**
* Add District polygon and legislator popup to map
*
* @param data
* @param latlng
*/
function addDistrictPopup(data, latlng) {
popuptext = buildPopup(data);
var popup = L.popup({keepInView: true, minWidth: 350, maxWidth: 1000});
popup.setLatLng(latlng)
.setContent(popuptext)
.openOn(map);
popup.update();
}
function drawPolygons(polygons, color) {
var bounds = null;
console.log("Drawing " + polygons.length + " polygons");
$.each(polygons, function (i, points) {
try {
console.log("Drawing polygon " + (i + 1) + "/" + polygons.length + " of " + points.length + " points");
var polyline = L.polygon(points, {color: color}).addTo(map);
if (bounds == null) {
bounds = polyline.getBounds();
} else {
bounds.extend(polyline.getBounds());
}
} catch (err) {
console.log("Failed to draw polygon " + (i + 1) + "/" + polygons.length + ": " + err);
}
});
if (bounds) {
map.fitBounds(bounds);
}
}
/**
* Event handler for map click
*
* @param event
*/
function getClosestDistrict(event) {
getDistricts(event.latlng, 1);
}
L.mapbox.accessToken = MB_API_TOKEN;
var map = L.mapbox.map('map', 'mapbox.streets')
.setView([39.8282, -98.5795], 5);
map.on('click', function(e) {
clearMap(map);
getClosestDistrict(e);
});
});
</script>
</body>
</html>