-
Notifications
You must be signed in to change notification settings - Fork 0
/
surveyor.py
executable file
·399 lines (307 loc) · 12.7 KB
/
surveyor.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
#! /usr/bin/env python
"""
surveyor.py
- a python program, which shows an example use of projekt_anarres.py
by letting the user survey a planet using different projections.
(C) 2013 Ida-Sofia Skyman (skymandr@fripost.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see [0].
Usage:
$ ./surveyor.py <rectangular map image>
Thie program lets you survey a map using the different projections implemented
in projekt_anarres.py. The followin projections are implemented:
- azimuthal orthographic
- azimuthal equal-area ("Lambert")
- azimuthal equidistant
- azimuthal stereographic
- equirectangular
The map data is taken from an image file assumed by the program to contain
simple rectangular projection data. The program works best with png-format.
The azimuthal views only support views along the standard (equatorial)
parallel, while the rectangular view can be centered on any coordinate.
To update redraw meridians and parallels, press "Update". To reset the view,
press "Reset".
For more information on the different projections and options, please see
the documentation for projekt_anarres.py!
------
[0]: http://www.gnu.org/licenses/gpl-3.0.html
"""
# Initial imports:
import os
import sys
import projekt_anarres as p
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons, Cursor
from testgrids import make_testgrids as grids
class PlanetarySurveyor(object):
"""
The PlanetarySurveyor creates a Matplotlib "widget" letting the user
navigate map data loaded from an image file.
"""
def __init__(self, filename):
"""
Initialized with filename of image file containing the equirectangular
map data.
"""
self.filename = filename
self.load_image()
# Setup display:
self.fig = plt.figure(1)
self.ax = plt.subplot(111)
plt.clf()
plt.subplots_adjust(left=0.1, bottom=0.25)
self.meridian = 90
self.parallel = 90
self.parallels = 16
self.meridians = 16
self.mode = "azimuthal"
self.projection = "orthographic"
self.setup_display()
# Setup mouse interaction:
self.click = self.display.figure.canvas.mpl_connect(
'button_press_event', self.mouseclick)
self.cursor = Cursor(self.display.axes, useblit=True, color='red',
linewidth=1)
# Setup axes:
self.axes_step = plt.axes([0.13, 0.15, 0.60, 0.03])
self.axes_meridians = plt.axes([0.13, 0.10, 0.60, 0.03])
self.axes_parallels = plt.axes([0.13, 0.05, 0.60, 0.03])
self.update_axes = plt.axes([0.79, 0.095, 0.08, 0.04])
self.reset_axes = plt.axes([0.79, 0.05, 0.08, 0.04])
self.radio_axes = plt.axes([0.88, 0.05, 0.11, 0.15])
# Setup sliders:
self.step = 22.5
self.slider_step = Slider(self.axes_step, 'Step', 0, 90,
valinit=self.step, valfmt='%2.1f')
self.slider_meridians = Slider(self.axes_meridians, 'Meridians', 0,
64, valinit=self.parallels,
valfmt='%2d')
self.slider_parallels = Slider(self.axes_parallels, 'Parallels', 0,
64, valinit=self.parallels,
valfmt='%2d')
self.slider_step.on_changed(self.update)
self.slider_meridians.on_changed(self.update)
self.slider_parallels.on_changed(self.update)
# Setup button(s):
self.update_button = Button(self.update_axes, 'Update')
self.update_button.on_clicked(self.update_display)
self.reset_button = Button(self.reset_axes, 'Reset')
self.reset_button.on_clicked(self.reset)
# Setup radio buttons:
self.radio = RadioButtons(self.radio_axes, ('ortho', 'eq.area',
'eq.dist', 'stereo', 'rect'), active=0)
self.radio.on_clicked(self.set_mode)
self.projections = {"ortho": ("orthographic", "azimuthal"),
"eq.area": ("lambert", "azimuthal"),
"eq.dist": ("equidistant", "azimuthal"),
"stereo": ("stereographic", "azimuthal"),
"rect": ("rectangular", "rectangular")}
# Almost ready:
self.update()
plt.show()
def load_image(self):
"""
Load and flatten specified image file. If this fails, the default map
is loaded. """
try:
map_image = plt.imread(self.filename)
except IOError as e:
print "Could not load file {0} ({1})".format(
self.filename, e.strerror)
print "Using default image..."
self.filename = "templates/nowwhat.png"
map_image = plt.imread(self.filename)
while len(map_image.shape) > 2:
map_image = map_image.mean(-1)
self.map_image = map_image
def setup_display(self):
"""
Setup parameters and map display.
"""
self.R = 180
self.padding = self.R / 10
if self.mode == 'azimuthal':
self.hemisphere = p.get_azimuthal_hemisphere(
self.map_image, self.meridian, 90,
self.R, self.projection, 0, self.padding)
self.display = plt.imshow(self.hemisphere, cmap=plt.cm.gray,
extent=[-1.5, 1.5, -1.5, 1.5])
plt.axis([-1.5, 1.5, -1.5, 1.5])
plt.axis('off')
elif self.mode == 'rectangular':
plt.axis([-180, 180, -90, 90])
pass
if self.meridians > 0 or self.parallels > 0:
self.draw_graticules()
def update_display(self, val=0):
"""
Update map display.
"""
if self.mode == 'azimuthal':
self.hemisphere = p.get_azimuthal_hemisphere(
self.map_image, self.meridian, 90,
self.R, self.projection, 0, self.padding)
ax = self.display.axes
self.display.axes.cla()
self.display = ax.imshow(self.hemisphere, cmap=plt.cm.gray,
extent=[-1.5, 1.5, -1.5, 1.5])
plt.axis([-1.5, 1.5, -1.5, 1.5])
plt.axis('off')
elif self.mode == 'rectangular':
self.hemisphere = p.get_rectangular_hemisphere(
self.map_image, self.meridian, self.parallel,
False)
ax = self.display.axes
self.display.axes.cla()
self.display = ax.imshow(self.hemisphere, cmap=plt.cm.gray,
extent=[
self.meridian - 90, self.meridian + 90,
self.parallel - 90, self.parallel + 90])
plt.axis([self.meridian - 90, self.meridian + 90,
self.parallel - 90, self.parallel + 90])
self.fix_coordinates()
if self.meridians > 0 or self.parallels > 0:
self.draw_graticules()
self.update()
def update(self, val=0):
"""
Update internal parameters from sliders, update coordiantes and draw.
"""
if self.step != self.slider_step.val:
self.step = np.round(self.slider_step.val / 0.5) * 0.5
self.slider_step.set_val(self.step)
if (self.meridians != self.slider_meridians.val or
self.parallels != self.slider_parallels.val):
self.meridians = np.round(self.slider_meridians.val
).astype(np.int)
self.parallels = np.round(self.slider_parallels.val
).astype(np.int)
self.fix_coordinates()
plt.draw()
def set_mode(self, val="ortho"):
"""
Set projection and mode.
"""
self.projection = self.projections[val][0]
self.mode = self.projections[val][1]
if self.mode == "azimuthal":
self.parallel = 90
self.update_display()
def reset(self, event):
"""
Reset widget
"""
self.slider_step.reset()
self.slider_meridians.reset()
self.slider_parallels.reset()
self.meridian = 90
self.parallel = 90
self.update()
self.set_mode()
def mouseclick(self, event):
"""
Handle mouse navigation of map display for the different projections.
"""
if event.inaxes == self.display.axes:
if event.button == 1:
if self.mode == "azimuthal":
self.meridian += self.step * np.round(event.xdata)
elif self.mode == "rectangular":
self.parallel = 180 - np.round(event.ydata / 0.5) * 0.5
self.meridian = np.round(event.xdata / 0.5) * 0.5
self.update()
self.update_display()
def fix_coordinates(self):
"""
Fix coordinates so they comply to standard representation for maps.
"""
self.parallel %= 180
self.meridian %= 360
self.display.axes.set_title("{0}: {1}".format(self.projection,
self.get_coordinates()))
def get_coordinates(self):
"""
Return string representation of coordinates in N-S/E-W standard.
"""
parallel = self.parallel
meridian = self.meridian - 180
if parallel > 90.0:
parallel = 180.0 - parallel
elif parallel < -90.0:
parallel = -180.0 - parallel
if meridian > 180.0:
meridian = 360.0 - meridian
elif meridian < -180.0:
meridian = -360.0 - meridian
if parallel >= 0:
NS = "N"
else:
NS = "S"
if meridian >= 0:
EW = "E"
else:
EW = "W"
return "{0} {1}, {2} {3}".format(np.abs(parallel), NS,
np.abs(meridian), EW)
def get_graticule(self):
"""
Return resolution of the current graticule (distances between parallel
and meridian lines).
"""
try:
dLat = 180.0 / self.parallels
except ZeroDivisionError:
dLat = None
try:
dLon = 360.0 / self.meridians
except ZeroDivisionError:
dLon = 0
return dLat, dLon
def draw_graticules(self):
"""
Draw parallel and meridian lines using testgrids-module.
"""
dLat, dLon = self.get_graticule()
ax = self.display.axes
if self.meridians > 0:
if self.mode == "azimuthal":
x_mer, z_mer = grids.get_meridians(self.meridian, dLon, 1,
1.5 / 1.1, self.projection)
ax.plot(x_mer, z_mer, ':k', label="meridians")
ax.axis('off')
elif self.mode == "rectangular":
mer_min = np.ceil((self.meridian - 90) / dLon) * dLon
mer_max = np.floor((self.meridian + 90) / dLon) * dLon
ax.set_xticks(np.arange(mer_min, mer_max + 1, dLon))
ax.grid(True)
if self.parallels > 0:
if self.mode == "azimuthal":
x_par, z_par = grids.get_parallels(self.parallel, dLat, 1,
1.5 / 1.1, self.projection)
ax.plot(x_par, z_par, ':k', label="parallels")
ax.axis('off')
elif self.mode == "rectangular":
par_min = np.ceil((self.parallel - 90) / dLat) * dLat
par_max = np.floor((self.parallel + 90) / dLat) * dLat
ax.set_yticks(np.arange(par_min, par_max + 1, dLat))
ax.grid(True)
def main():
"""
Parse commandline arguments and start widget.
"""
try:
filename = sys.argv[1]
except IndexError:
filename = ""
Surveyor = PlanetarySurveyor(filename)
if __name__ == "__main__":
sys.exit(main())