Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 172 lines (120 sloc) 4.627 kb
3e3965f @danni Mark lots of tests as skipped if they required plotting or the net
authored
1 import os
2
2e03e76 @danni A verification using Google's elevation API
authored
3 import numpy as np
988b3b5 @danni Plot outputs from STRM grid
authored
4 import pytest
5 from matplotlib import pyplot as plt
f08d344 @danni strm: extract_track method
authored
6 from numpy.testing import assert_almost_equal, assert_allclose
5f0f991 @danni Load a GA SRTM DEM ESRI grid into a numpy array
authored
7
3b50f0e @danni Big rearrange of source code for deployment to OpenShift
authored
8 from cyclerouter.processing.srtm import SRTM
5f0f991 @danni Load a GA SRTM DEM ESRI grid into a numpy array
authored
9
f08d344 @danni strm: extract_track method
authored
10 from tests.test_json import json as track_json
11
3e3965f @danni Mark lots of tests as skipped if they required plotting or the net
authored
12
13 NO_PLOTS = not (os.environ.get('PLOTS', 'no') == 'yes')
14 NO_NET = not (os.environ.get('NET', 'no') == 'yes')
15
16
832a182 @danni Handle masked out values using a np.ma.array
authored
17 @pytest.fixture(scope='module')
988b3b5 @danni Plot outputs from STRM grid
authored
18 def grid():
19 return SRTM()
5f0f991 @danni Load a GA SRTM DEM ESRI grid into a numpy array
authored
20
f08d344 @danni strm: extract_track method
authored
21 @pytest.fixture
22 def track(track_json):
3b50f0e @danni Big rearrange of source code for deployment to OpenShift
authored
23 from cyclerouter.processing.track import RKJSON
f08d344 @danni strm: extract_track method
authored
24
25 return RKJSON(track_json)
26
3e3965f @danni Mark lots of tests as skipped if they required plotting or the net
authored
27
988b3b5 @danni Plot outputs from STRM grid
authored
28 def test_load_srtm(grid):
5f0f991 @danni Load a GA SRTM DEM ESRI grid into a numpy array
authored
29 # assert grid makes sense shapewise
30 assert grid.ndim == 2
31 assert grid.lats.ndim == grid.lons.ndim == 1
32 assert grid.shape == (grid.lats.shape[0], grid.lons.shape[0])
33
34 # 3 arcsecond grid
35 assert_almost_equal(grid.binsize, 3./3600)
36
37 # origin is top-left
38 assert grid.lats[0] > grid.lats[1]
39 assert_almost_equal(grid.lats[0] - grid.lats[1], grid.binsize)
40
41 assert grid.lons[0] < grid.lons[1]
42 assert_almost_equal(grid.lons[1] - grid.lons[0], grid.binsize)
43
44 # Check our grid contains places interesting to us:
45 # Melbourne
46 assert grid.lats[0] >= -37.81361 >= grid.lats[-1] and \
47 grid.lons[0] <= 144.96306 <= grid.lons[-1]
48 # Altona
49 assert grid.lats[0] >= -37.868 >= grid.lats[-1] and \
50 grid.lons[0] <= 144.83 <= grid.lons[-1]
51 # Brunswick
52 assert grid.lats[0] >= -37.767 >= grid.lats[-1] and \
53 grid.lons[0] <= 144.960 <= grid.lons[-1]
988b3b5 @danni Plot outputs from STRM grid
authored
54
3e3965f @danni Mark lots of tests as skipped if they required plotting or the net
authored
55
56 @pytest.mark.skipif('NO_PLOTS')
988b3b5 @danni Plot outputs from STRM grid
authored
57 def test_plot_srtm(grid):
58
59 fig, (ax1, ax2) = plt.subplots(2, 1)
60
61 ax1.set_title("Meridional Elevation (Lon = {})".format(grid.lons[0]))
62 ax1.set_xlabel("Latitude")
63 ax1.plot(grid.lats, grid[:, 0])
64
65 ax2.set_title("Zonal Elevation (Lat = {})".format(grid.lats[0]))
66 ax2.set_xlabel("Longitude")
67 ax2.plot(grid.lons, grid[0, :])
68
69 plt.show()
2e03e76 @danni A verification using Google's elevation API
authored
70
3e3965f @danni Mark lots of tests as skipped if they required plotting or the net
authored
71
72 @pytest.mark.skipif('NO_NET')
73 @pytest.mark.skipif('NO_PLOTS')
2e03e76 @danni A verification using Google's elevation API
authored
74 def test_against_google(grid):
75
76 import json
3b50f0e @danni Big rearrange of source code for deployment to OpenShift
authored
77 from cyclerouter.rk import Client
2e03e76 @danni A verification using Google's elevation API
authored
78
79 client = Client()
80
81 # make a request to the Google Elevation API
82 _, content = client.request(
83 'http://maps.googleapis.com/maps/api/elevation/json',
84 data=dict(path='{},{}|{},{}'.format(grid.lats[0], grid.lons[0],
85 grid.lats[-1], grid.lons[0]),
86 samples=100,
87 sensor='false'))
88
89 content = json.loads(content)
90
91 assert content['status'] == 'OK'
92 assert 'results' in content
93
94 data = np.empty(shape=(len(content['results']), 3))
95
96 for i, rec in enumerate(content['results']):
97 data[i] = [rec['location']['lat'],
98 rec['location']['lng'],
99 rec['elevation']]
100
101 data = data.view(type=np.recarray,
102 dtype=[('lats', float),
103 ('lons', float),
104 ('elev', float)])
105
106 fig, ax1 = plt.subplots(1, 1)
107
108 ax1.set_title("SRTM v Google (Lon = {})".format(grid.lons[0]))
109 ax1.set_xlabel("Latitude")
110 ax1.plot(grid.lats, grid[:, 0])
111 ax1.plot(data.lats, data.elev, 'r')
112 ax1.set_xlim(-38.5, -33.5)
113 ax1.set_ylim(-20, 200)
114
115 plt.show()
d48a6e8 @danni srtm: implement extract_point method
authored
116
3e3965f @danni Mark lots of tests as skipped if they required plotting or the net
authored
117
118 @pytest.mark.skipif('NO_NET')
d48a6e8 @danni srtm: implement extract_point method
authored
119 @pytest.mark.parametrize(('lat', 'lon'), [
120 (-37.81361, 144.96306), # Melbourne
121 (-37.868, 144.83), # Altona
122 (-37.767, 144.960), # Brunswick
123 ])
124 def test_extract_point(grid, lat, lon):
125 import json
3b50f0e @danni Big rearrange of source code for deployment to OpenShift
authored
126 from cyclerouter.rk import Client
d48a6e8 @danni srtm: implement extract_point method
authored
127
128 calc = grid.extract_point(lat, lon)
129
130 # make a request to the Google Elevation API
131 client = Client()
132
133 _, content = client.request(
134 'http://maps.googleapis.com/maps/api/elevation/json',
135 data=dict(locations='{},{}'.format(lat, lon),
136 sensor='false'))
137
138 content = json.loads(content)
139
140 assert content['status'] == 'OK'
141 assert 'results' in content
142 assert len(content['results']) == 1
143
144 result = content['results'][0]
145 desr = result['elevation']
146 res = result['resolution']
147
148 # assert the calculated value is within the resolution of Google's value
f08d344 @danni strm: extract_track method
authored
149 assert_allclose(calc, desr, atol=res / 2)
150
3e3965f @danni Mark lots of tests as skipped if they required plotting or the net
authored
151
152 @pytest.mark.skipif('NO_PLOTS')
f08d344 @danni strm: extract_track method
authored
153 def test_extract_track_against_rk(grid, track):
154 elevs = grid.extract_track(track)
155
156 assert len(elevs) == len(track)
157
158 # calculate RMS error
159 rmse = np.sqrt(np.mean((elevs - track.elev) ** 2))
160 nrms = rmse / (elevs.max() - elevs.min())
d48a6e8 @danni srtm: implement extract_point method
authored
161
f08d344 @danni strm: extract_track method
authored
162 print "RMS error", rmse
163 print "NRMS", nrms
164
165 fig, ax1 = plt.subplots(1, 1)
166
167 ax1.set_title("SRTM v RK")
168 ax1.plot(elevs)
169 ax1.plot(track.elev, 'r')
170
171 plt.show()
Something went wrong with that request. Please try again.