-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlandgateapitestmap.py
More file actions
127 lines (99 loc) · 6.07 KB
/
landgateapitestmap.py
File metadata and controls
127 lines (99 loc) · 6.07 KB
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
""" LandgateAPITest Web App
Map plotting module
Created by Aiden Price,
Curtin University Masters of Geospatial Science candidate,
Submitted June 2016"""
# Libraries available on Google cloud service.
import webapp2
# Google's appengine python libraries.
from google.appengine.ext import ndb
# Local model imports
from landgateapitestmodel import TestCampaign
from landgateapitestmodel import ResultObject
from landgateapitestmodel import TestMaster
from landgateapitestmodel import TestEndpoint
from landgateapitestmodel import NetworkResult
from landgateapitestmodel import LocationResult
from landgateapitestmodel import PingResult
from landgateapitestmodel import ReferenceObject
from landgateapitestmodel import Vector
from landgateapitestmodel import CampaignStats
# Constants and helper classes and functions
DEFAULT_CAMPAIGN_NAME = 'production_campaign'
PRETEXT = "<!DOCTYPE html><html><head><title>LandgateAPITest Web Map</title><link rel='stylesheet' href='https://cdn.jsdelivr.net/leaflet/1.0.0-rc.1/leaflet.css' /><script src='https://cdn.jsdelivr.net/leaflet/1.0.0-rc.1/leaflet-src.js'></script><style>html, body {height: 100%; width: 100%; }#map { width: 100%; height: 100%; }</style></head><body><div id='map'></div><script src='https://rawgit.com/Leaflet/Leaflet.heat/gh-pages/dist/leaflet-heat.js'></script><script>var map = L.map('map').setView([-27, 148], 5);var tiles = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {attribution: '© <a href=\"http://osm.org/copyright\">OpenStreetMap</a> contributors',}).addTo(map);testPoints = "
POSTTEXT = ';var heat = L.heatLayer(testPoints).addTo(map);</script></body></html>'
def getCampaignKey(database_name=DEFAULT_CAMPAIGN_NAME):
key = ndb.Key(TestCampaign, database_name)
if key is None:
return TestCampaign(key=database_name, campaignName=database_name).put()
else:
return key
class MapPlotter(webapp2.RequestHandler):
"""Returns an interactive Leaflet map with the locations."""
def get(self):
try:
campaignName = self.request.get('campaignName')
campaignKey = getCampaignKey(campaignName)
mapName = self.request.get('mapName')
except Exception as e:
self.response.set_status(555, message="Custom error response code.")
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Missing or invalid parameter in request.\n' +
'Please provide ?campaignName=&mapName=\n\n' +
e.message + '\n\n')
else:
try:
listVectors = Vector.query(ancestor=campaignKey, projection=[Vector.preTestLocation.location, Vector.referenceCheckSuccess, Vector.onDeviceSuccess, Vector.referenceCheckValid]).fetch()
listAll = [(vector.preTestLocation.location.lat, vector.preTestLocation.location.lon, vector.referenceCheckSuccess, vector.onDeviceSuccess, vector.referenceCheckValid) for vector in listVectors]
listFiltered = [vector for vector in listAll if vector[4]]
listLatsAndLongs = []
if mapName == 'All':
listLatsAndLongs = [[vector[0], vector[1], 1.0] for vector in listFiltered]
elif mapName == 'Success':
listLatsAndLongs = [[vector[0], vector[1], 1.0] for vector in listFiltered if vector[2] and vector[3]]
elif mapName == 'FailedOnDevice':
listLatsAndLongs = [[vector[0], vector[1], 1.0] for vector in listFiltered if not vector[3]]
elif mapName == 'FailedReferenceCheck':
listLatsAndLongs = [[vector[0], vector[1], 1.0] for vector in listFiltered if not vector[2]]
elif mapName == 'AllFailures':
listLatsAndLongs = [[vector[0], vector[1], 1.0] for vector in listFiltered if not (vector[2] and vector[3])]
outString = PRETEXT + str(listLatsAndLongs) + POSTTEXT
self.response.headers['Content-Type'] = 'text/html'
self.response.write(outString)
except Exception as e:
self.response.set_status(555, message="Custom error response code.")
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Sorry, mapping error condition ' +
'encountered!\nNo map for you!\n\n' +
e.message + '\n\n')
class StaticMapPlotter(webapp2.RequestHandler):
"""Returns a static Leaflet map with the locations."""
def get(self):
try:
campaignName = self.request.get('campaignName')
campaignKey = getCampaignKey(campaignName)
except Exception as e:
self.response.set_status(555, message="Custom error response code.")
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Missing or invalid parameter in request.\n' +
'Please provide ?campaignName=\n\n' +
e.message + '\n\n')
else:
try:
listVectors = Vector.query(ancestor=campaignKey, projection=[Vector.preTestLocation.location]).fetch()
listLatsAndLongs = [[vector.preTestLocation.location.lat, vector.preTestLocation.location.lon, 1.0] for vector in listVectors]
outString = PRETEXT + str(listLatsAndLongs) + POSTTEXT
self.response.headers['Content-Type'] = 'text/html'
self.response.write(outString)
except Exception as e:
self.response.set_status(555, message="Custom error response code.")
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Sorry, mapping error condition ' +
'encountered!\nNo map for you!\n\n' +
e.message + '\n\n')
# WSGI app
# Handles incoming requests according to supplied URL.
app = webapp2.WSGIApplication([
('/map', MapPlotter),
('/staticmap', StaticMapPlotter)
], debug=True)