mirror of
https://gitlab.com/gaelysam/mapgen_rivers.git
synced 2024-11-14 14:50:22 +01:00
Dynamic map displaying
Map is displayed at every iteration if matplotlib library is installed
This commit is contained in:
parent
3fda369fb5
commit
8a15bc924d
|
@ -9,6 +9,8 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
|
import view_map
|
||||||
|
|
||||||
# Always place in this script's parent directory
|
# Always place in this script's parent directory
|
||||||
os.chdir(os.path.dirname(sys.argv[0]))
|
os.chdir(os.path.dirname(sys.argv[0]))
|
||||||
argc = len(sys.argv)
|
argc = len(sys.argv)
|
||||||
|
@ -65,6 +67,7 @@ nn = n*vscale + offset
|
||||||
# Initialize landscape evolution model
|
# Initialize landscape evolution model
|
||||||
print('Initializing model')
|
print('Initializing model')
|
||||||
model = EvolutionModel(nn, K=1, m=0.35, d=1, sea_level=0, flex_radius=flex_radius)
|
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
|
dt = time/niter
|
||||||
|
|
||||||
|
@ -73,7 +76,9 @@ print('Initial flow calculation')
|
||||||
model.calculate_flow()
|
model.calculate_flow()
|
||||||
|
|
||||||
for i in range(niter):
|
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')
|
print('Diffusion')
|
||||||
model.diffusion(dt)
|
model.diffusion(dt)
|
||||||
print('Advection')
|
print('Advection')
|
||||||
|
@ -83,7 +88,7 @@ for i in range(niter):
|
||||||
print('Flow calculation')
|
print('Flow calculation')
|
||||||
model.calculate_flow()
|
model.calculate_flow()
|
||||||
|
|
||||||
print('Done')
|
print('Done!')
|
||||||
|
|
||||||
# Twist the grid
|
# Twist the grid
|
||||||
bx, by = bounds.make_bounds(model.dirs, model.rivers)
|
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:
|
with open('size', 'w') as sfile:
|
||||||
sfile.write('{:d}\n{:d}'.format(mapsize+1, mapsize+1))
|
sfile.write('{:d}\n{:d}'.format(mapsize+1, mapsize+1))
|
||||||
|
|
||||||
# Display the map if matplotlib is found
|
view_map.stats(model.dem, model.lakes)
|
||||||
try:
|
view_map.plot(model.dem, model.lakes, title='Final map')
|
||||||
from view_map import view_map
|
|
||||||
view_map(model.dem, model.lakes, model.rivers)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
67
view_map.py
67
view_map.py
|
@ -20,33 +20,53 @@ try:
|
||||||
except ImportError: # No module matplotlib
|
except ImportError: # No module matplotlib
|
||||||
has_matplotlib = False
|
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):
|
ls = mcl.LightSource(azdeg=315, altdeg=45)
|
||||||
if not has_matplotlib:
|
rgb = ls.shade(lakes_sea, cmap=cmap1, vert_exag=1/scale, blend_mode='soft', vmin=0, vmax=max_elev)
|
||||||
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)
|
(X, Y) = dem.shape
|
||||||
rgb = ls.shade(lakes_sea, cmap=cmap1, vert_exag=1/scale, blend_mode='soft', vmin=0, vmax=max_elev)
|
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
|
sm1 = plt.cm.ScalarMappable(cmap=cmap1, norm=plt.Normalize(vmin=0, vmax=max_elev))
|
||||||
extent = (0, Y*scale, 0, X*scale)
|
plt.colorbar(sm1).set_label('Elevation')
|
||||||
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))
|
sm2 = plt.cm.ScalarMappable(cmap=cmap2, norm=plt.Normalize(vmin=0, vmax=max_depth))
|
||||||
plt.colorbar(sm1).set_label('Altitude')
|
plt.colorbar(sm2).set_label('Water depth')
|
||||||
|
|
||||||
sm2 = plt.cm.ScalarMappable(cmap=cmap2, norm=plt.Normalize(vmin=0, vmax=max_depth))
|
plt.xlabel('X')
|
||||||
plt.colorbar(sm2).set_label('Profondeur d\'eau')
|
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
|
surface = dem.size
|
||||||
|
|
||||||
continent = lake_dem >= 0
|
continent = lake_dem >= 0
|
||||||
|
@ -57,7 +77,8 @@ def map_stats(dem, lake_dem, scale):
|
||||||
|
|
||||||
print('--- General ---')
|
print('--- General ---')
|
||||||
print('Grid size: {:5d}x{:5d}'.format(dem.shape[0], dem.shape[1]))
|
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()
|
||||||
print('--- Surfaces ---')
|
print('--- Surfaces ---')
|
||||||
print('Continents: {:6.2%}'.format(continent_surface/surface))
|
print('Continents: {:6.2%}'.format(continent_surface/surface))
|
||||||
|
@ -94,5 +115,5 @@ if __name__ == "__main__":
|
||||||
dem = load_map('dem', '>i2', shape)
|
dem = load_map('dem', '>i2', shape)
|
||||||
lakes = load_map('lakes', '>i2', shape)
|
lakes = load_map('lakes', '>i2', shape)
|
||||||
|
|
||||||
map_stats(dem, lakes, scale)
|
stats(dem, lakes, scale=scale)
|
||||||
view_map(dem, lakes, scale)
|
plot(dem, lakes, scale)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user