Python map viewing: read conf file, and take world folder as input

This commit is contained in:
Gaël C 2021-07-24 14:44:54 +02:00
parent c2c397c2a5
commit 31c5ea1025
3 changed files with 65 additions and 12 deletions

42
readconfig.py Normal file
View File

@ -0,0 +1,42 @@
def read_conf_file(filename):
f = open(filename, 'r')
return read_conf(f)
def read_conf(f, end_tag=None):
conf = {}
while True:
line = f.readline()
if len(line) == 0:
return conf
line = line.strip()
if line == end_tag:
return conf
if len(line) == 0 or line[0] == '#':
continue
eqpos = line.find('=')
if eqpos < 0:
continue
name, value = line[:eqpos].rstrip(), line[eqpos+1:].lstrip()
if value == '{':
# Group
conf[name] = read_conf(f, end_tag='}')
elif value == '"""':
# Multiline
conf[value] = read_multiline(f)
else:
conf[name] = value
def read_multiline(f):
mline = ''
while True:
line = f.readline()
if len(line) == 0:
return mline
line = line.strip()
if line == '"""':
return mline
mline += line + '\n'

View File

@ -19,7 +19,7 @@ except ImportError: # No module matplotlib
has_matplotlib = False
if has_matplotlib:
def view_map(dem, lakes, scale=1, sea_level=0.0, title=None):
def view_map(dem, lakes, scale=1, center=False, sea_level=0.0, title=None):
lakes_sea = np.maximum(lakes, sea_level)
water = np.maximum(lakes_sea - dem, 0)
max_elev = dem.max()
@ -31,7 +31,10 @@ if has_matplotlib:
rgb = ls.shade(dem, cmap=cmap1, vert_exag=1/scale, blend_mode='soft', norm=norm_ground)
(X, Y) = dem.shape
extent = (0, Y*scale, 0, X*scale)
if center:
extent = (-(Y+1)*scale/2, (Y-1)*scale/2, -(X+1)*scale/2, (X-1)*scale/2)
else:
extent = (-0.5*scale, (Y-0.5)*scale, -0.5*scale, (X-0.5)*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')

View File

@ -6,12 +6,19 @@ import sys
import os
from view import stats, plot
from readconfig import read_conf_file
scale = 1
if len(sys.argv) > 1:
os.chdir(sys.argv[1])
if len(sys.argv) > 2:
scale = int(sys.argv[2])
conf = read_conf_file('mapgen_rivers.conf')
if 'center' in conf:
center = conf['center'] == 'true'
else:
center = True
if 'blocksize' in conf:
blocksize = float(conf['blocksize'])
else:
blocksize = 15.0
def load_map(name, dtype, shape):
dtype = np.dtype(dtype)
@ -21,9 +28,10 @@ def load_map(name, dtype, shape):
data = zlib.decompress(data)
return np.frombuffer(data, dtype=dtype).reshape(shape)
shape = np.loadtxt('size', dtype='u4')
dem = load_map('dem', '>i2', shape)
lakes = load_map('lakes', '>i2', shape)
shape = np.loadtxt('river_data/size', dtype='u4')
shape = (shape[1], shape[0])
dem = load_map('river_data/dem', '>i2', shape)
lakes = load_map('river_data/lakes', '>i2', shape)
stats(dem, lakes, scale=scale)
plot(dem, lakes, scale=scale)
stats(dem, lakes, scale=blocksize)
plot(dem, lakes, scale=blocksize, center=center)