-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapify.py
366 lines (301 loc) · 13.6 KB
/
Mapify.py
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# API (Note: this must be the first thing in the script)
import urllib
import json
# noinspection PyUnresolvedReferences
import clr
clr.AddReference('System.Web.Extensions')
# clr.AddReference('System.Collections.Generic')
# noinspection PyUnresolvedReferences
from System.Web.Script.Serialization import JavaScriptSerializer
# noinspection PyUnresolvedReferences
from System.Collections.Generic import Dictionary, List
xhrLimit = 500
googleRegionBias = "us"
global Data, model, q
model.Title = "Mapify"
if model.Data.p == "" and model.Data.fams == "": # Blue Toolbar Page load
mapData = model.DynamicData()
mapData.cesiumKey = model.Setting("CesiumKey", "")
mapData.xhrLimit = xhrLimit
mapData.count = q.BlueToolbarCount()
template = """
<script src="https://cesium.com/downloads/cesiumjs/releases/1.116/Build/Cesium/Cesium.js"></script>
<link href="https://cesium.com/downloads/cesiumjs/releases/1.116/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/gh/TenthPres/TouchPointScripts/Mapify/style.min.css" rel="stylesheet">
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h2>Loading...</h2></div>
<div id="toolbar"></div>
<div id="statusInfo" title="People who aren't shown probably could not be located on the map.">
Showing <span id="showingCount">0</span> of {{count}} people
</div>
<script>
Cesium.Ion.defaultAccessToken = '{{{cesiumKey}}}';
const viewer = new Cesium.Viewer('cesiumContainer', {
animation : false,
fullscreenButton: false, // will be manually created later in the toolbar
geocoder : false,
infoBox: true,
skyAtmosphere: false,
skyBox: false,
timeline : false,
scene: {
fog: {
enabled: false
},
globe: {
enableLighting: false,
showGroundAtmosphere: false
}
}
});
var flyHome = function() {
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(-75.169899, 39.947262, 85000.0), // TODO replace with church lat/lng setting
duration: 4
});
};
function clearInfobox() {
viewer.infoBox.frame.contentDocument.body.removeChild(viewer.infoBox.frame.contentDocument.body.firstChild)
viewer.infoBox.frame.removeEventListener('load', clearInfobox);
}
viewer.homeButton.viewModel._command = Cesium.createCommand(flyHome);
viewer.baseLayerPicker.viewModel.selectedImagery = viewer.baseLayerPicker.viewModel.imageryProviderViewModels[8];
viewer.infoBox.frame.sandbox = "allow-same-origin allow-top-navigation allow-pointer-lock allow-popups allow-forms allow-scripts";
viewer.infoBox.frame.src = "/HomeWidgets/Embed/0";
viewer.infoBox.frame.addEventListener('load', clearInfobox);
viewer.infoBox.viewModel.enableCamera = false;
viewer.scene.globe.showGroundAtmosphere = false;
// Prevent camera from getting locked to entity via double-click
viewer.cesiumWidget.screenSpaceEventHandler.setInputAction(function() {}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
// Handle Entity Selection
viewer.cesiumWidget.screenSpaceEventHandler.setInputAction(function(movement) {
var clickedOn = viewer.scene.pick(movement.position),
clickedEntity = (Cesium.defined(clickedOn)) ? clickedOn.id : undefined;
if (Cesium.defined(clickedEntity)) {
viewer.selectedEntity = handleSelectedEntity(clickedEntity);
} else {
viewer.selectedEntity = undefined;
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
new Cesium.FullscreenButton(viewer._toolbar);
let entities = {},
showCount = 0,
requestsNeeded = Math.ceil({{{count}}} / {{{xhrLimit}}}),
colorAssignments = {},
categoricalColors = [
new Cesium.Color(0, 1, 0, 0.5),
new Cesium.Color(1, 0, 0, 0.5),
new Cesium.Color(0, 0, 1, 0.5),
new Cesium.Color(1, 1, 0, 0.5),
new Cesium.Color(0, 1, 1, 0.5),
new Cesium.Color(1, 0, 1, 0.5),
new Cesium.Color(0, .4, 0, .5)
],
nullColor = new Cesium.Color(0, 0, 0, 0.5);
function reqListener() {
let start = this.responseText.indexOf(">>DATA>") + 7,
data = JSON.parse(this.responseText.substr(start, this.responseText.indexOf("<DATA<<") - start))
for (const hsh in data) {
if (!entities.hasOwnProperty(hsh)) {
entities[hsh] = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(data[hsh].lng, data[hsh].lat, 0),
name: data[hsh].addr,
point: {
pixelSize: Math.sqrt(data[hsh].cnt) * 10,
color: nullColor
},
_data: {
loaded: false,
hash: hsh,
famIds: data[hsh].families,
resCode: data[hsh].resCode
}
});
entities[hsh].point.color = computeColor(entities[hsh]);
showCount += data[hsh].cnt;
} else {
console.warn("ERROR: hash points need to be merged across requests."); // TODO
}
}
// Update count of number of people shown
document.getElementById("showingCount").innerHTML = "" + showCount;
// Fully Loaded
requestsNeeded--
if (requestsNeeded <= 0) {
flyHome();
document.getElementById('loadingOverlay').style.opacity = 0;
}
}
for (var r = 1; r <= requestsNeeded; r++) {
let url = window.location.origin + window.location.pathname + "?p=" + r,
xhr = new XMLHttpRequest();
xhr.addEventListener("load", reqListener);
xhr.open("GET", url);
xhr.send();
}
function colorForCategory(field, category) {
if (!colorAssignments.hasOwnProperty(field)) {
colorAssignments[field] = {
values: {},
count: 0
}
}
if (!colorAssignments[field].values.hasOwnProperty(category)) {
let index = colorAssignments[field].count % categoricalColors.length;
colorAssignments[field].values[category] = categoricalColors[index];
colorAssignments[field].count++;
}
return colorAssignments[field].values[category];
}
function computeColor(entity) {
colorBasis = "resCode";
if (["resCode"].indexOf(colorBasis) > -1) { // categorical fields
return colorForCategory(colorBasis, entity._data[colorBasis]);
} else { // quantified fields
console.warn("Quantified colors not yet available")
}
}
function handleSelectedEntity(selectedEntity) {
if (selectedEntity._data.loaded === false) {
let url = window.location.origin + getPyScriptAddress(),
xhr = new XMLHttpRequest();
selectedEntity._data.loaded = undefined; // loading
selectedEntity.description = "loading...";
xhr.addEventListener("load", function() {
updateEntityWithPersonalData(selectedEntity, this);
});
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send("hsh=" + selectedEntity._data.hash + "&fams=" + JSON.stringify(selectedEntity._data.famIds));
} else {
correctIframeHeight();
}
return selectedEntity;
}
function getPyScriptAddress() {
let path = window.location.pathname;
path = "/PyScriptForm/" + path.substr(18, path.length - 54);
return path;
}
function updateEntityWithPersonalData(entity, xhrResp) {
let start = xhrResp.responseText.indexOf(">>DATA>") + 7,
data = xhrResp.responseText.substr(start, xhrResp.responseText.indexOf("<DATA<<") - start)
entity._data.loaded = true;
entity.description = data;
correctIframeHeight();
}
function correctIframeHeight() {
let iframe = viewer.infoBox.frame;
setTimeout(() => {
iframe.style.height = parseFloat(iframe.style.height.split('p')[0]) + 20 + "px";
}, 50);
}
function imageLoaded(that) {
that.parentElement.style.backgroundImage = "url(" + that.src + ")";
that.parentElement.style.opacity = 1;
}
</script>
"""
print model.RenderTemplate(template, mapData)
elif model.Data.p != "": # XHR Map Data Request
model.Data.p = int(model.Data.p)
geoSql = "SELECT g.id geoId, g.Latitude lat, g.Longitude lng FROM People p LEFT JOIN Geocodes g ON g.Address = CONCAT(p.PrimaryAddress, ' ', p.PrimaryAddress2, ' ', p.PrimaryCity, ' ', p.PrimaryState , ' ', p.PrimaryZip) WHERE p.PeopleId = {}"
pts = Dictionary[str, Dictionary[str, object]]()
for p in q.BlueToolbarReport(None, xhrLimit, xhrLimit * (model.Data.p - 1)):
geo = q.QuerySqlTop1(geoSql.format(p.PeopleId))
lat = geo.lat
lng = geo.lng
hsh = "{}|{}".format(geo.lat, geo.lng)
if lat == "" or lng == "":
continue
if not pts.ContainsKey(hsh):
resCode = p.ResidentCode or p.Family.ResidentCode
if resCode is not None:
resCode = resCode.Code
pts.Add(hsh, Dictionary[str, object]({
'cnt': 0,
'hash': hsh,
'families': Dictionary[str, object](),
'addr': p.AddressLineOne or p.Family.AddressLineOne,
'lat': lat,
'lng': lng,
'resCode': resCode
}))
if not pts[hsh]['families'].ContainsKey(str(p.Family.FamilyId)):
pts[hsh]['families'][str(p.Family.FamilyId)] = []
pts[hsh]['families'][str(p.Family.FamilyId)].Add(p.PeopleId)
pts[hsh]['cnt'] += 1
print "<!-- >>>>>DATA>" + JavaScriptSerializer().Serialize(pts) + "<DATA<<<<< -->"
elif model.HttpMethod == 'post' and model.Data.fams != '' and model.Data.hsh != '': # XHR Point data request
included = JavaScriptSerializer().DeserializeObject(model.Data.fams)
includedPeop = []
for f in included.Values:
for p in f:
includedPeop.append(p)
includedFams = JavaScriptSerializer().Serialize(included.Keys).replace('"', '')[1:-1]
includedPeop = JavaScriptSerializer().Serialize(includedPeop).replace('"', '')[1:-1]
sql = """
-- noinspection SqlResolveForFile
SELECT *, 1 as [includedFamily], (IIF(p.PeopleId IN ({3}), 1, 0)) as [includedPerson]
FROM People p
LEFT JOIN Families f on p.FamilyId = f.FamilyId
LEFT JOIN PeopleExtra pe ON pe.Field = '{0}' AND pe.PeopleId = p.PeopleId
WHERE p.FamilyId IN ({2})
UNION
SELECT *, 0 as [includedFamily], (IIF(p.PeopleId IN ({3}), 1, 0)) as [includedPerson]
FROM People p
LEFT JOIN Families f on p.FamilyId = f.FamilyId
LEFT JOIN PeopleExtra pe ON pe.Field = '{0}' AND pe.PeopleId = p.PeopleId
WHERE pe.Data = '{1}' AND p.FamilyId NOT IN ({2})
ORDER BY includedFamily DESC, p.FamilyId ASC, includedPerson DESC, p.PositionInFamilyId
""".format("null", model.Data.hsh, includedFams, includedPeop)
famId = 0
out = '''
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/gh/TenthPres/TouchPointScripts/Mapify/style-infobox.min.css" rel="stylesheet">
'''
template = """
{{IfNotEqual d.FamilyId d.PrevFamilyId}}
{{IfGt d.PrevFamilyId 0}}
</div></div>
{{/IfGt}}
<div class="family{{IfEqual d.includedFamily 1}} included{{else}} excluded{{/IfEqual}}">
<h2>
The {{ p.Family.HeadOfHousehold.Name }} Family
{{IfNotEqual d.HomePhone ''}}
<a href="tel:{{d.HomePhone}}" class="fa fa-phone" title="Call Home Phone"></a>
{{/IfNotEqual}}
</h2><div class="family-members">
{{/IfNotEqual}}
<div class="person{{IfEqual d.includedPerson 1}} included{{else}} excluded{{/IfEqual}}">
<div class="person-photo"
{{IfNotEqual pHasPhoto 0}}
style="background-position: {{p.Picture.X}}% {{p.Picture.Y}}%; opacity:0;">
<img onload="parent.imageLoaded(this)" src="/Portrait/{{ p.Picture.SmallId }}"
/{{/IfNotEqual}}>
</div>
<h3>
<a href="/Person2/{{d.PeopleId}}" target="_blank">{{d.Name}}</a>
</h3>
{{IfNotEqual d.includedPerson 1}}<p class="note">(not included in search)</p>{{/IfNotEqual}}
<p>
{{IfNotEqual d.EmailAddress ''}}
<a href="mailto:{{d.EmailAddress}}" class="fa fa-envelope" title="Send Email"></a>
{{/IfNotEqual}}
{{IfNotEqual d.CellPhone ''}}
<a href="tel:{{d.CellPhone}}" class="fa fa-mobile" title="Call Mobile Phone"></a>
{{/IfNotEqual}}
{{IfNotEqual d.WorkPhone ''}}
<a href="tel:{{d.WorkPhone}}" class="fa fa-phone" title="Call Work Phone"></a>
{{/IfNotEqual}}
</p>
</div>
"""
for Data.d in q.QuerySql(sql):
Data.d.PrevFamilyId = famId
Data.p = model.GetPerson(Data.d.PeopleId)
Data.pHasPhoto = 1 * (Data.p.Picture is not None)
out += model.RenderTemplate(template)
famId = Data.d.FamilyId
out += "</div></div>"
print "<!-- >>>>>DATA>" + out + "<DATA<<<<< -->"