Dynamic map displaying

Map is displayed at every iteration if matplotlib library is installed
This commit is contained in:
Gael-de-Sailly 2020-11-14 12:05:52 +01:00
parent 3fda369fb5
commit 8a15bc924d
2 changed files with 53 additions and 31 deletions

View File

@ -9,6 +9,8 @@ import os
import sys
import settings
import view_map
# Always place in this script's parent directory
os.chdir(os.path.dirname(sys.argv[0]))
argc = len(sys.argv)
@ -65,6 +67,7 @@ nn = n*vscale + offset
# Initialize landscape evolution model
print('Initializing model')
model = EvolutionModel(nn, K=1, m=0.35, d=1, sea_level=0, flex_radius=flex_radius)
view_map.update(model.dem, model.lakes, t=5, title='Initializing...')
dt = time/niter
@ -73,7 +76,9 @@ print('Initial flow calculation')
model.calculate_flow()
for i in range(niter):
print('Iteration {:d} of {:d}'.format(i+1, niter))
disp_niter = 'Iteration {:d} of {:d}...'.format(i+1, niter)
view_map.update(model.dem, model.lakes, title=disp_niter)
print(disp_niter)
print('Diffusion')
model.diffusion(dt)
print('Advection')
@ -83,7 +88,7 @@ for i in range(niter):
print('Flow calculation')
model.calculate_flow()
print('Done')
print('Done!')
# Twist the grid
bx, by = bounds.make_bounds(model.dirs, model.rivers)
@ -108,9 +113,5 @@ save(model.rivers, 'rivers', dtype='>u4')
with open('size', 'w') as sfile:
sfile.write('{:d}\n{:d}'.format(mapsize+1, mapsize+1))
# Display the map if matplotlib is found
try:
from view_map import view_map
view_map(model.dem, model.lakes, model.rivers)
except:
pass
view_map.stats(model.dem, model.lakes)
view_map.plot(model.dem, model.lakes, title='Final map')

View File

@ -20,33 +20,53 @@ try:
except ImportError: # No module matplotlib
has_matplotlib = False
if has_matplotlib:
def view_map(dem, lakes, scale=1, title=None):
if not has_matplotlib:
return
lakes_sea = np.maximum(lakes, 0)
water = np.maximum(lakes_sea - dem, 0)
max_elev = lakes_sea.max()
max_depth = water.max()
def view_map(dem, lakes, scale):
if not has_matplotlib:
return
lakes_sea = np.maximum(lakes, 0)
water = np.maximum(lakes_sea - dem, 0)
max_elev = lakes_sea.max()
max_depth = water.max()
ls = mcl.LightSource(azdeg=315, altdeg=45)
rgb = ls.shade(lakes_sea, cmap=cmap1, vert_exag=1/scale, blend_mode='soft', vmin=0, vmax=max_elev)
ls = mcl.LightSource(azdeg=315, altdeg=45)
rgb = ls.shade(lakes_sea, cmap=cmap1, vert_exag=1/scale, blend_mode='soft', vmin=0, vmax=max_elev)
(X, Y) = dem.shape
extent = (0, Y*scale, 0, X*scale)
plt.imshow(np.flipud(rgb), extent=extent, interpolation='antialiased')
alpha = (water > 0).astype('u1')
plt.imshow(np.flipud(water), alpha=np.flipud(alpha), cmap=cmap2, extent=extent, vmin=0, vmax=max_depth, interpolation='antialiased')
(X, Y) = dem.shape
extent = (0, Y*scale, 0, X*scale)
plt.imshow(np.flipud(rgb), extent=extent, interpolation='antialiased')
alpha = (water > 0).astype('u1')
plt.imshow(np.flipud(water), alpha=np.flipud(alpha), cmap=cmap2, extent=extent, vmin=0, vmax=max_depth, interpolation='antialiased')
sm1 = plt.cm.ScalarMappable(cmap=cmap1, norm=plt.Normalize(vmin=0, vmax=max_elev))
plt.colorbar(sm1).set_label('Elevation')
sm1 = plt.cm.ScalarMappable(cmap=cmap1, norm=plt.Normalize(vmin=0, vmax=max_elev))
plt.colorbar(sm1).set_label('Altitude')
sm2 = plt.cm.ScalarMappable(cmap=cmap2, norm=plt.Normalize(vmin=0, vmax=max_depth))
plt.colorbar(sm2).set_label('Water depth')
sm2 = plt.cm.ScalarMappable(cmap=cmap2, norm=plt.Normalize(vmin=0, vmax=max_depth))
plt.colorbar(sm2).set_label('Profondeur d\'eau')
plt.xlabel('X')
plt.ylabel('Z')
plt.show()
if title is not None:
plt.title(title, fontweight='bold')
def map_stats(dem, lake_dem, scale):
def update(*args, t=0.01, **kwargs):
plt.clf()
view_map(*args, **kwargs)
plt.pause(t)
def plot(*args, **kwargs):
plt.clf()
view_map(*args, **kwargs)
plt.show()
else:
def update(*args, **kwargs):
pass
def plot(*args, **kwargs):
pass
def stats(dem, lake_dem, scale=1):
surface = dem.size
continent = lake_dem >= 0
@ -57,7 +77,8 @@ def map_stats(dem, lake_dem, scale):
print('--- General ---')
print('Grid size: {:5d}x{:5d}'.format(dem.shape[0], dem.shape[1]))
print('Map size: {:5d}x{:5d}'.format(int(dem.shape[0]*scale), int(dem.shape[1]*scale)))
if scale > 1:
print('Map size: {:5d}x{:5d}'.format(int(dem.shape[0]*scale), int(dem.shape[1]*scale)))
print()
print('--- Surfaces ---')
print('Continents: {:6.2%}'.format(continent_surface/surface))
@ -94,5 +115,5 @@ if __name__ == "__main__":
dem = load_map('dem', '>i2', shape)
lakes = load_map('lakes', '>i2', shape)
map_stats(dem, lakes, scale)
view_map(dem, lakes, scale)
stats(dem, lakes, scale=scale)
plot(dem, lakes, scale)