-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender_simulations.py
executable file
·683 lines (612 loc) · 18.9 KB
/
render_simulations.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
AUTHOR: Brendan Harmon <brendan.harmon@gmail.com>
PURPOSE: Rendering 2D and 3D maps of landscape evolution simulations
COPYRIGHT: (C) 2017 Brendan Harmon
LICENSE: This program is free software under the GNU General Public
License (>=v2).
"""
import os
import sys
import atexit
import grass.script as gscript
from grass.exceptions import CalledModuleError
# set graphics driver
driver = "cairo"
# temporary region
gscript.use_temp_region()
# set environment
env = gscript.gisenv()
overwrite = True
env['GRASS_OVERWRITE'] = overwrite
env['GRASS_VERBOSE'] = False
env['GRASS_MESSAGE_FORMAT'] = 'standard'
gisdbase = env['GISDBASE']
location = env['LOCATION_NAME']
mapset = env['MAPSET']
region = 'elevation_2012_1m@PERMANENT'
res=1
# set 2D rendering parameters
legend_coord = (2, 32, 2, 4)
border = 400
width = 1600
height = 1600
font = 'Lato-Regular'
fontsize = 24
# temporal paramters
end_time = '2016_01_01_02_00_00'
# color tables
elevation_colors = """\
0% 0 132 132
89 0 191 191
93.6 0 255 0
98.2 255 255 0
102.8 255 127 0
107.4 191 127 63
112 200 200 200
100% 200 200 200
nv white
default white
"""
flux_colors = """\
0 255:255:255
0.0001 255:255:0
0.125 255:127:0
0.25 191:127:63
100% 0:0:0
nv 255:255:255
default 255:255:255
"""
def main():
"""try to install dependencies"""
# dependencies()
"""render maps"""
render_region_2d(mapset)
# render_region_3d(mapset)
# render_legends(mapset)
render_subregion_2d(mapset)
# render_fortbragg_2d()
atexit.register(cleanup)
sys.exit(0)
def render_region_2d(mapset):
"""2D rendering of region"""
# create rendering directory
render = os.path.join(gisdbase, 'images', mapset)
if not os.path.exists(render):
os.makedirs(render)
# set region and mask
gscript.run_command('g.region',
raster=region,
res=res)
gscript.run_command('r.mask', vector='watershed')
# set color tables
gscript.write_command(
'r.colors',
map='elevation_{time}'.format(time=end_time),
rules='-',
stdin=elevation_colors)
# compute and render shaded relief
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'shaded_relief'+'.png'),
overwrite=overwrite)
gscript.run_command('r.relief',
input='elevation_{time}'.format(time=end_time),
output='shaded_relief',
altitude=90,
azimuth=45,
zscale=3,
overwrite=overwrite)
gscript.run_command('d.shade',
shade='shaded_relief',
color='elevation_{time}'.format(time=end_time),
brighten=5,
overwrite=overwrite)
gscript.run_command('d.legend',
raster='elevation_{time}'.format(time=end_time),
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# render net difference
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'net_difference'+'.png'),
overwrite=overwrite)
gscript.run_command('d.rast',
map='net_difference')
gscript.run_command('d.legend',
raster='net_difference',
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# compute and render landforms
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'landforms'+'.png'),
overwrite=overwrite)
gscript.run_command('r.geomorphon',
elevation='elevation_{time}'.format(time=end_time),
forms='landforms',
search=64,
skip=0,
flat=1,
dist=0,
step=0,
start=0,
overwrite=overwrite)
# gscript.run_command('d.shade',
# shade='shaded_relief',
# color='landforms',
# brighten=0)
gscript.run_command('d.rast',
map='landforms')
gscript.run_command('d.legend',
raster='landforms',
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# render water depth with shaded relief
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'depth'+'.png'),
overwrite=overwrite)
gscript.run_command('d.shade',
shade='shaded_relief',
color='depth_{time}'.format(time=end_time),
brighten=20)
gscript.run_command('d.legend',
raster='depth_{time}'.format(time=end_time),
fontsize=fontsize,
font=font,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# check for erosion-deposition
find_raster = gscript.find_file('erosion_deposition_{time}'.format(time=end_time),
element='cell')
if find_raster['file']:
# render erosion-deposition
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'erdep'+'.png'),
overwrite=overwrite)
gscript.run_command('d.shade',
shade='shaded_relief',
color='erosion_deposition_{time}'.format(time=end_time),
brighten=20)
gscript.run_command('d.legend',
raster='erosion_deposition_{time}'.format(time=end_time),
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# check for flux
find_raster = gscript.find_file('flux_{time}'.format(time=end_time),
element='cell')
if find_raster['file']:
# render flux
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'flux'+'.png'),
overwrite=overwrite)
gscript.write_command(
'r.colors',
map='flux_{time}'.format(time=end_time),
rules='-',
stdin=flux_colors)
gscript.run_command('d.rast',
map='flux_{time}'.format(time=end_time))
gscript.run_command('d.legend',
raster='flux_{time}'.format(time=end_time),
font=font,
fontsize=fontsize,
labelnum='2',
at=legend_coord,
flags='l')
gscript.run_command('d.mon', stop=driver)
# remove mask
gscript.run_command('r.mask', flags='r')
def render_subregion_2d(mapset):
"""2D rendering of region"""
# create rendering directory
render = os.path.join(gisdbase, 'images', mapset+'_detail')
if not os.path.exists(render):
os.makedirs(render)
# set region and mask
gscript.run_command('g.region',
vector='subwatershed',
res=res)
gscript.run_command('r.mask', vector='subwatershed')
# set color tables
gscript.write_command(
'r.colors',
map='elevation_{time}'.format(time=end_time),
rules='-',
stdin=elevation_colors)
# compute and render shaded relief
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'shaded_relief'+'.png'),
overwrite=overwrite)
gscript.run_command('r.relief',
input='elevation_{time}'.format(time=end_time),
output='shaded_relief',
altitude=90,
azimuth=45,
zscale=3,
overwrite=overwrite)
gscript.run_command('d.shade',
shade='shaded_relief',
color='elevation_{time}'.format(time=end_time),
brighten=5,
overwrite=overwrite)
gscript.run_command('d.legend',
raster='elevation_{time}'.format(time=end_time),
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# render net difference
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'net_difference'+'.png'),
overwrite=overwrite)
gscript.run_command('d.rast',
map='net_difference')
gscript.run_command('d.legend',
raster='net_difference',
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# compute and render landforms
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'landforms'+'.png'),
overwrite=overwrite)
gscript.run_command('r.geomorphon',
elevation='elevation_{time}'.format(time=end_time),
forms='landforms',
search=64,
skip=0,
flat=1,
dist=0,
step=0,
start=0,
overwrite=overwrite)
# gscript.run_command('d.shade',
# shade='shaded_relief',
# color='landforms',
# brighten=0)
gscript.run_command('d.rast',
map='landforms')
gscript.run_command('d.legend',
raster='landforms',
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# render water depth with shaded relief
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'depth'+'.png'),
overwrite=overwrite)
gscript.run_command('d.shade',
shade='shaded_relief',
color='depth_{time}'.format(time=end_time),
brighten=20)
gscript.run_command('d.legend',
raster='depth_{time}'.format(time=end_time),
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# check for erosion-deposition
find_raster = gscript.find_file('erosion_deposition_{time}'.format(time=end_time),
element='cell')
if find_raster['file']:
# render erosion-deposition
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'erdep'+'.png'),
overwrite=overwrite)
gscript.run_command('d.shade',
shade='shaded_relief',
color='erosion_deposition_{time}'.format(time=end_time),
brighten=20)
gscript.run_command('d.legend',
raster='erosion_deposition_{time}'.format(time=end_time),
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# check for flux
find_raster = gscript.find_file('flux_{time}'.format(time=end_time),
element='cell')
if find_raster['file']:
# render flux
gscript.run_command('d.mon',
start=driver,
width=width,
height=height,
output=os.path.join(render, 'flux'+'.png'),
overwrite=overwrite)
gscript.write_command(
'r.colors',
map='flux_{time}'.format(time=end_time),
rules='-',
stdin=flux_colors)
gscript.run_command('d.rast',
map='flux_{time}'.format(time=end_time))
gscript.run_command('d.legend',
raster='flux_{time}'.format(time=end_time),
font=font,
fontsize=fontsize,
labelnum='2',
at=legend_coord,
flags='l')
gscript.run_command('d.mon', stop=driver)
# remove mask
gscript.run_command('r.mask', flags='r')
def render_fortbragg_2d():
# create rendering directory
render = os.path.join(gisdbase, 'images', mapset)
if not os.path.exists(render):
os.makedirs(render)
# set region
gscript.run_command('g.region', region='fortbragg', res=10)
# set mask
gscript.run_command('r.mask', raster='fortbragg_cfactor')
# compute relief
gscript.run_command('r.relief',
input='fortbragg_elevation_10m_2012',
output='relief',
altitude=90,
azimuth=45,
zscale=3,
overwrite=overwrite)
# compute skyview
gscript.run_command('r.skyview',
input='fortbragg_elevation_10m_2012',
output='skyview',
ndir=16,
colorized_output='colorized_skyview',
overwrite=overwrite)
# render net difference
gscript.run_command('d.mon',
start=driver,
width=width*2,
height=height,
output=os.path.join(render, 'net_difference'+'.png'),
overwrite=overwrite)
gscript.run_command('d.shade',
shade='skyview',
color='net_difference',
brighten=20,
overwrite=overwrite)
gscript.run_command('d.legend',
raster='net_difference',
font=font,
fontsize=fontsize,
at=legend_coord)
gscript.run_command('d.mon', stop=driver)
# remove mask
gscript.run_command('r.mask', flags='r')
def render_region_3d(mapset):
"""3D rendering of region with nviz"""
# set 3d rendering parameters
camera_height = 750
perspective = 15
position = 1.0, 1.0
light_position = (0.68, -0.68, 0.95)
fringe = "se"
fringe_color = "255:255:255"
fringe_elevation = 85
size = (1200, 1200)
zexag = 3
vwidth = 3
# create rendering directory
render = os.path.join(gisdbase, 'images', mapset+'_3d')
if not os.path.exists(render):
os.makedirs(render)
# set region
gscript.run_command('g.region',
region='region',
res=res)
# mask rasters
gscript.run_command('r.mask', vector='watershed')
gscript.run_command('r.mapcalc',
expression="depth = {raster}".format(raster='depth_'+end_time),
overwrite=True)
find_raster = gscript.find_file('flux_{time}'.format(time=end_time),
element='cell')
if find_raster['file']:
gscript.run_command('r.mapcalc',
expression="flux = {raster}".format(raster='flux_'+end_time),
overwrite=True)
find_raster = gscript.find_file('erosion_deposition_{time}'.format(time=end_time),
element='cell')
if find_raster['file']:
gscript.run_command('r.mapcalc',
expression="erosion_deposition = {raster}".format(raster='erosion_deposition_'+end_time),
overwrite=True)
gscript.run_command('r.mapcalc',
expression="difference = {raster}".format(raster='net_difference'),
overwrite=True)
gscript.run_command('r.mask', flags='r')
# list of rasters to render
rasters = ['elevation_'+end_time,
'colorized_skyview',
'landforms',
'depth',
'flux',
'erosion_deposition',
'difference']
for raster in rasters:
# check if raster exists
find_raster = gscript.find_file(raster,
element='cell')
if find_raster['file']:
# 3D render raster
gscript.run_command('m.nviz.image',
elevation_map='elevation_'+end_time,
color_map=raster,
resolution_fine=1,
vline='watershed',
vline_width=vwidth,
vline_color='black',
height=camera_height,
position=position,
perspective=perspective,
zexag=zexag,
light_position=light_position,
fringe=fringe,
fringe_color=fringe_color,
fringe_elevation=fringe_elevation,
output=os.path.join(render,raster),
format='tif',
size=size,
errors='ignore'
)
# remove temporary maps
gscript.run_command(
'g.remove',
type='raster',
name=['depth',
'erdep',
'flux',
'difference'],
flags='f')
def render_legends(mapset):
"""render raster map legends"""
# create rendering directory
render = os.path.join(gisdbase, 'images', mapset+'_3d')
if not os.path.exists(render):
os.makedirs(render)
# set region
gscript.run_command('g.region',
region='region',
res=res)
# mask rasters
gscript.run_command('r.mask', vector='watershed')
# export legends
gscript.run_command('r.out.legend',
raster='elevation_'+end_time,
file=os.path.join(render,'legend_elevation.png'),
filetype='cairo',
dimensions=[0.8,6],
resolution=300,
color='none',
digits=3,
font='Lato-Regular',
overwrite=overwrite)
gscript.run_command('r.out.legend',
raster='depth_'+end_time,
file=os.path.join(render,'legend_depth.png'),
filetype='cairo',
dimensions=[0.8,6],
resolution=300,
color='none',
range=[0,1],
digits=3,
font='Lato-Regular',
overwrite=overwrite)
gscript.run_command('r.out.legend',
raster='net_difference',
file=os.path.join(render,'legend_difference.png'),
filetype='cairo',
dimensions=[0.8,6],
resolution=300,
color='none',
digits=4,
font='Lato-Regular',
overwrite=overwrite)
find_raster = gscript.find_file('erosion_deposition_{time}'.format(time=end_time),
element='cell')
if find_raster['file']:
gscript.run_command('r.out.legend',
raster='erosion_deposition_'+end_time,
file=os.path.join(render,'legend_erosion_deposition.png'),
filetype='cairo',
dimensions=[0.8,6],
resolution=300,
color='none',
range=[-0.25,0.25],
digits=4,
font='Lato-Regular',
overwrite=overwrite)
find_raster = gscript.find_file('flux_{time}'.format(time=end_time),
element='cell')
if find_raster['file']:
gscript.run_command('r.out.legend',
raster='flux_'+end_time,
file=os.path.join(render,'legend_flux.png'),
filetype='cairo',
dimensions=[0.8,6],
resolution=300,
color='none',
range=[0,25],
digits=3,
font='Lato-Regular',
overwrite=overwrite)
# remove mask
gscript.run_command('r.mask', flags='r')
def dependencies():
"""try to install required add-ons"""
try:
gscript.run_command('g.extension',
extension='r.skyview',
operation='add')
except CalledModuleError:
pass
try:
gscript.run_command('g.extension',
extension='r.geomorphon',
operation='add')
except CalledModuleError:
pass
try:
gscript.run_command('g.extension',
extension='r.out.legend',
operation='add')
except CalledModuleError:
pass
def cleanup():
try:
# stop cairo monitor
gscript.run_command('d.mon', stop=driver)
except CalledModuleError:
pass
try:
# remove mask
gscript.run_command('r.mask', flags='r')
except CalledModuleError:
pass
if __name__ == "__main__":
atexit.register(cleanup)
sys.exit(main())