-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode_Web.py
592 lines (445 loc) · 20.3 KB
/
code_Web.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# import the GUI forms that we create with Qt Creator
import form_Web
import code_MapHtml
# import the Qt components we'll use
# do this so later we won't have to clutter our code with references to parent Qt classes
from PyQt5.QtGui import (
QCursor,
QIcon,
QPixmap
)
from PyQt5.QtCore import (
Qt,
QUrl,
pyqtSignal,
QIODevice,
QByteArray,
QBuffer
)
from PyQt5.QtWidgets import (
# QApplication,
QMdiSubWindow
)
from math import (
floor
)
from PyQt5.QtWebEngineWidgets import (
QWebEngineView,
QWebEngineSettings,
)
from collections import (
defaultdict
)
import base64
class Web(QMdiSubWindow, form_Web.Ui_frmWeb):
resized = pyqtSignal()
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.setAttribute(Qt.WA_DeleteOnClose,True)
self.mdiParent = ""
self.setWindowIcon(QIcon(QPixmap(1,1)))
self.contentType = "Web Page"
self.resized.connect(self.resizeMe)
self.webView = QWebEngineView(self)
self.webView.setObjectName("webView")
self.webView.loadFinished.connect(self.LoadFinished)
self.webView.loadProgress.connect(self.showLoadProgress)
self.title = ""
def resizeEvent(self, event):
#routine to handle events on objects, like clicks, lost focus, gained forcus, etc.
self.resized.emit()
return super(self.__class__, self).resizeEvent(event)
def resizeMe(self):
windowWidth = self.frameGeometry().width()
windowHeight = self.frameGeometry().height()
self.scrollArea.setGeometry(5, 27, windowWidth -10 , windowHeight-35)
self.webView.setGeometry(5, 27, windowWidth - 10, windowHeight-35)
if self.contentType == "Map":
self.webView.adjustSize()
self.LoadLocationsMap(self.filter)
def html(self):
# QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
html = """
<!DOCTYPE html>
<html>
<head>
</head>
<body>
"""
myPixmap = self.webView.grab()
myPixmap = myPixmap.scaledToWidth(600, Qt.SmoothTransformation)
myByteArray = QByteArray()
myBuffer = QBuffer(myByteArray)
myBuffer.open(QIODevice.WriteOnly)
myPixmap.save(myBuffer, "PNG")
encodedImage = base64.b64encode(myByteArray)
html = html + ("""
<img src="data:image/png;base64,
""")
html = html + str(encodedImage)[1:]
html = html + ("""
<font size>
</body>
</html>
""")
# QApplication.restoreOverrideCursor()
return(html)
def scaleMe(self):
fontSize = self.mdiParent.fontSize
settings = QWebEngineSettings.globalSettings()
settings.setFontSize(QWebEngineSettings.DefaultFontSize, floor(fontSize * 1.6))
scaleFactor = self.mdiParent.scaleFactor
windowWidth = 800 * scaleFactor
windowHeight = 580 * scaleFactor
self.resize(windowWidth, windowHeight)
def loadAboutYearbird(self):
self.title= "About Yearbird"
self.contentType = "About"
html = """
<!DOCTYPE html>
<html>
<head>
<title>About Yearbird</title>
<meta charset="utf-8">
<style>
* {
font-family: "Times New Roman", Times, serif;
}
</style>
</head>
<body>
<h1>
Yearbird
</h1>
"""
html = html + "<h3>Version: " + self.mdiParent.versionNumber + "</h3>"
html = html + "<h3>Date: " + self.mdiParent.versionDate+ "</h3>"
html = html + """
<font size='4'>
<b>
Yearbird is a free, open-source application to analyze personal eBird sightings.
<br><br>
Created by Richard Trinkner.
</b>
<h3>
Licenses
</h3>
<p>
<ul>
<li>
Yearbird is licensed under the GNU General Public License, version 3.
</li>
<li>
PyQt, by Riverbank Computing, is licensed under the GNU General Public License.
</li>
<li>
Map base layers are retrieved from Google.
</li>
<li>
Map layers that include points and location labels are generated using OpenLayers. OpenLayers is free, Open Source JavaScript, released under the 2-clause BSD License (also known as the FreeBSD).
</li>
<li>
PyInstaller, by the PyInstaller Development Team, Giovanni Bajo and McMillan Enterprise, is licensed under the GPL General Public License.
</li>
</ul>
</font size>
</body>
</html>
"""
self.webView.setHtml(html)
self.setWindowTitle("About Yearbird")
return(True)
def LoadWebPage(self, url):
# QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
self.webView.load(QUrl(url))
self.resizeMe()
self.scaleMe()
def LoadFinished(self):
# QApplication.restoreOverrideCursor()
return()
def LoadLocationsMap(self, filter):
self.title= "Location Map"
coordinatesDict = defaultdict()
mapWidth = self.frameGeometry().width() -10
mapHeight = self.frameGeometry().height() -35
self.scrollArea.setGeometry(5, 27, mapWidth + 2, mapHeight + 2)
self.webView.setGeometry(5, 27, mapWidth + 2, mapHeight + 2)
self.contentType = "Map"
self.filter = filter
locations = self.mdiParent.db.GetLocations(filter)
if len(locations) == 0:
return(False)
for l in locations:
coordinates = self.mdiParent.db.GetLocationCoordinates(l)
coordinatesDict[l] = coordinates
thisMap = code_MapHtml.MapHtml()
thisMap.mapHeight = mapHeight
thisMap.mapWidth = mapWidth
thisMap.coordinatesDict = coordinatesDict
html = thisMap.html()
self.webView.setHtml(html)
# set window title to descriptive map name
locationName = filter.getLocationName() # str name of region or location or ""
locationType = filter.getLocationType()
startDate = filter.getStartDate() # str format yyyy-mm-dd or ""
endDate = filter.getEndDate() # str format yyyy-mm-dd or ""
startSeasonalMonth = filter.getStartSeasonalMonth() # str format mm
startSeasonalDay = filter.getStartSeasonalDay() # str format dd
endSeasonalMonth = filter.getEndSeasonalMonth() # str format dd
endSeasonalDay = filter.getEndSeasonalDay() # str format dd
speciesName = filter.getSpeciesName() # str speciesName
family = filter.getFamily() # str family name
# set main location label, using "All Locations" if none others are selected
windowTitle = speciesName
if locationName != "":
if locationType == "Country":
locationName = self.mdiParent.db.GetCountryName(locationName)
if locationType == "State":
locationName = self.mdiParent.db.GetStateName(locationName)
windowTitle = windowTitle + "; " + locationName
if startDate != "":
dateTitle = startDate + " to " + endDate
if startDate == endDate:
dateTitle = startDate
windowTitle = windowTitle + "; " + dateTitle
# set main seasonal range label, if specified
if not ((startSeasonalMonth == "") or (endSeasonalMonth == "")):
monthRange = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
rangeTitle = monthRange[int(startSeasonalMonth)-1] + "-" + startSeasonalDay + " to " + monthRange[int(endSeasonalMonth)-1] + "-" + endSeasonalDay
windowTitle = windowTitle + "; " + rangeTitle
if family != "":
family = family[0:family.index("(") - 1]
windowTitle = windowTitle + "; " + family
if windowTitle == "":
windowTitle = "All species, locations, dates and families"
#remove leading "; " if needed
if windowTitle[0:2] == "; ":
windowTitle = windowTitle [2:]
# add location count to window title
windowTitle = "Map: " + windowTitle + " (" + str(len(coordinatesDict.keys())) + ")"
self.setWindowTitle(windowTitle)
self.title = windowTitle
icon = QIcon()
icon.addPixmap(QPixmap(":/icon_map.png"), QIcon.Normal, QIcon.Off)
self.setWindowIcon(icon)
return(True)
def loadChoroplethUSStates(self, filter):
from copy import deepcopy
import folium
from branca.colormap import LinearColormap
self.title= "US States Choropleth"
self.filter = deepcopy(filter)
# find states in filtered sightings
stateDict = defaultdict()
minimalSightingList = self.mdiParent.db.GetMinimalFilteredSightingsList(filter)
for s in minimalSightingList:
# Consider only full species, not slash or spuh or hybrid entries
commonName = s["commonName"]
if "/" not in commonName and "sp." not in commonName and " x " not in commonName:
if self.mdiParent.db.TestSighting(s, filter):
if s["state"][3:5] not in stateDict.keys():
stateDict[s["state"][3:5]] = [s]
else:
stateDict[s["state"][3:5]].append(s)
# check if no sightings were found. Return false if none found. Abort and display message.
if len(stateDict) == 0:
return(False)
stateTotals = defaultdict()
largestTotal = 0
for state in stateDict.keys():
stateSpecies = set()
for s in stateDict[state]:
stateSpecies.add(s["commonName"])
stateTotals[state] = len(stateSpecies)
if len(stateSpecies) > largestTotal:
largestTotal = len(stateSpecies)
# Load the shape of the zone (US counties)
geo_file = self.mdiParent.db.state_geo
#add the state values to the geojson so we can access them for tooltips
for f in geo_file["features"]:
if f["id"] in stateTotals.keys():
f["properties"]["speciesTotal"] = stateTotals[f["id"]]
else:
f["properties"]["speciesTotal"] = 0
stateTotals[f["id"]] = 0
#create color range for map, using the maximum state value found above
colormap = LinearColormap(
colors=[(255, 240, 227), (255, 119, 0)],
index=[0, round(largestTotal * .75)],
vmin=0,
vmax=largestTotal,
)
# Initialize the folium map
state_map = folium.Map(location=[39.5, -98.3], zoom_start=4)
# Configure the chloropleth layer and add to map
folium.GeoJson(
geo_file,
style_function=lambda feature: {
'fillColor': 'rgb(240, 240, 240)' if stateTotals[feature['id']] == 0 else colormap(stateTotals[feature['id']]),
'color': 'black',
'weight': .2,
'fillOpacity': .8,
},
tooltip=folium.features.GeoJsonTooltip(
fields=['name', 'speciesTotal'],
aliases=["State", "Species"]
)
).add_to(state_map)
# make the layer control box visible
folium.LayerControl().add_to(state_map)
# get the html string from the map
html = state_map.get_root().render()
self.webView.setHtml(html)
return(True)
def loadChoroplethUSCounties(self, filter):
from copy import deepcopy
import folium
from branca.colormap import LinearColormap
self.title= "US Counties Choropleth"
self.filter = deepcopy(filter)
# find states in filtered sightings
countyDict = defaultdict()
minimalSightingList = self.mdiParent.db.GetMinimalFilteredSightingsList(filter)
for s in minimalSightingList:
# only count US sightings since we're only showing the US choropleth
if s["country"] == "US" and s["state"] not in ["US-HI", "US-AK"]:
#only use sightings that have a county code assigned to them
# some US sightings won't have them, such as if a checklist is for
# an entire state, not localized down to a location or county
if "countyCode" in s.keys():
# Consider only full species, not slash or spuh or hybrid entries
commonName = s["commonName"]
if "/" not in commonName and "sp." not in commonName and " x " not in commonName:
if self.mdiParent.db.TestSighting(s, filter):
if s["countyCode"] not in countyDict.keys():
countyDict[s["countyCode"]] = [s]
else:
countyDict[s["countyCode"]].append(s)
# check if no sightings were found. Return false if none found. Abort and display message.
if len(countyDict) == 0:
return(False)
countyTotals = defaultdict()
largestTotal = 0
for county in countyDict.keys():
countySpecies = set()
for s in countyDict[county]:
countySpecies.add(s["commonName"])
countyTotals[county] = len(countySpecies)
if len(countySpecies) > largestTotal:
largestTotal = len(countySpecies)
# Load the shape of the zone (US counties)
geo_file = self.mdiParent.db.county_geo
#add the county values to the geojson so we can access them for tooltips
for f in geo_file["features"]:
if f["id"] in countyTotals.keys():
f["properties"]["speciesTotal"] = countyTotals[f["id"]]
else:
f["properties"]["speciesTotal"] = 0
countyTotals[f["id"]] = 0
#create color range for map, using the maximum state value found above
colormap = LinearColormap(
colors=[(255, 240, 227), (255, 119, 0)],
index=[0, round(largestTotal * .75)],
vmin=0,
vmax=largestTotal,
)
# Initialize the folium map
county_map = folium.Map(location=[39.5, -98.3], zoom_start=4)
# Configure the chloropleth layer and add to map
folium.GeoJson(
geo_file,
style_function=lambda feature: {
'fillColor': 'rgb(240, 240, 240)' if countyTotals[feature['id']] == 0 else colormap(countyTotals[feature['id']]),
'color': 'black',
'weight': 1,
'fillOpacity': .8,
'nan_fill_color': 'white'
},
tooltip=folium.features.GeoJsonTooltip(
fields=['name', 'state', 'speciesTotal'],
aliases=["County", "State", "Species"]
)
).add_to(county_map)
# make the layer control box visible
folium.LayerControl().add_to(county_map)
# get the html string from the map
html = county_map.get_root().render()
self.webView.setHtml(html)
return(True)
def loadChoroplethWorldCountries(self, filter):
from copy import deepcopy
import folium
from branca.colormap import LinearColormap
self.title= "World Choropleth"
self.filter = deepcopy(filter)
# find states in filtered sightings
countryDict = defaultdict()
minimalSightingList = self.mdiParent.db.GetMinimalFilteredSightingsList(filter)
for s in minimalSightingList:
# Consider only full species, not slash or spuh or hybrid entries
commonName = s["commonName"]
if "/" not in commonName and "sp." not in commonName and " x " not in commonName:
if self.mdiParent.db.TestSighting(s, filter):
if s["country"] not in countryDict.keys():
countryDict[s["country"]] = [s]
else:
countryDict[s["country"]].append(s)
# check if no sightings were found. Return false if none found. Abort and display message.
if len(countryDict) == 0:
return(False)
countryTotals = defaultdict()
largestTotal = 0
for country in countryDict.keys():
countrySpecies = set()
for s in countryDict[country]:
countrySpecies.add(s["commonName"])
countryTotals[country] = len(countrySpecies)
if len(countrySpecies) > largestTotal:
largestTotal = len(countrySpecies)
# Load the shape of the zone (US counties)
geo_file = self.mdiParent.db.country_geo
#add the country values to the geojson so we can access them for tooltips
for f in geo_file["features"]:
if f["id"] in countryTotals.keys():
f["properties"]["speciesTotal"] = countryTotals[f["id"]]
else:
f["properties"]["speciesTotal"] = 0
countryTotals[f["id"]] = 0
#create color range for map, using the maximum country value found above
colormap = LinearColormap(
colors=[(255, 240, 227), (255, 119, 0)],
index=[0, round(largestTotal * .75)],
vmin=0,
vmax=largestTotal,
)
# Initialize the folium map
choro_map = folium.Map(location=[1, 1], zoom_start=1)
# Configure the chloropleth layer and add to map
folium.GeoJson(
geo_file,
style_function=lambda feature: {
'fillColor': 'rgb(240, 240, 240)' if countryTotals[feature['id']] == 0 else colormap(countryTotals[feature['id']]),
'color': 'black',
'weight': 1,
'fillOpacity': .8,
'nan_fill_color': 'white'
},
tooltip=folium.features.GeoJsonTooltip(
fields=['name', 'speciesTotal'],
aliases=["Country", "Species"]
)
).add_to(choro_map)
# make the layer control box visible
folium.LayerControl().add_to(choro_map)
# get the html string from the map
html = choro_map.get_root().render()
self.webView.setHtml(html)
return(True)
def loadChoroplethWorldSubregion1(self, filter):
return()
def showLoadProgress(self, percent):
if percent < 100:
self.setWindowTitle(self.title + ": " + str(percent) + "%")
else:
self.setWindowTitle(self.title)