feat(generator/biomes): generate input for biome models

This commit is contained in:
Mahdi Dibaiee 2019-05-16 09:56:13 +04:30
parent 0d348b6276
commit f79c63abf8
3 changed files with 82 additions and 6 deletions

View File

@ -1,11 +1,14 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors, cm
from matplotlib.collections import PatchCollection
import scipy.interpolate as interpolate
from scipy import ndimage
import math
from io import BytesIO
import base64
import pandas as pd
from shapely.geometry import Point, MultiPoint
from descartes import PolygonPatch
parameters = {
'width': {
@ -102,6 +105,10 @@ parameters = {
'max': 1,
'step': 0.1
},
'biomes': {
'default': False,
'type': 'bool'
},
'seed': {
'default': '',
'type': 'int',
@ -244,7 +251,7 @@ def constant_filter(a):
return max(1, a[0])
return 0
def generate_map(**kwargs):
def generate_map(biomes=False, **kwargs):
plt.clf()
p.update(kwargs)
@ -291,14 +298,74 @@ def generate_map(**kwargs):
print(np.count_nonzero(ground) / ground.size)
plt.gca().invert_yaxis()
plt.imshow(ground.T, cmap=greys, norm=norm)
plt.gca().invert_yaxis()
figfile = BytesIO()
plt.savefig(figfile, format='png')
figfile.seek(0)
if biomes:
generate_biomes(ground)
return figfile
def to_range(omin, omax, nmin, nmax):
orange = omax - omin
nrange = nmax - nmin
return lambda x: ((x - omin) * nrange / orange) + nmin
def generate_biomes(ground):
width, height = p['width'], p['height']
height_to_latitude = to_range(0, height, -90, 90)
width_to_longitude = to_range(0, width, -180, 180)
print('generate_biomes')
INPUTS = ['elevation', 'distance_to_water', 'latitude']
data = {}
for col in ['longitude', 'latitude', 'elevation', 'distance_to_water']:
data[col] = []
points = []
for x, y in np.ndindex(ground.shape):
v = ground[x,y]
if v > p['water_level']:
points.append((x, y))
data['longitude'].append(width_to_longitude(x))
data['latitude'].append(height_to_latitude(y))
data['elevation'].append(v)
points = MultiPoint(points)
boundary = points.buffer(1e-0).boundary
for x, y in np.ndindex(ground.shape):
if ground[x,y] > p['water_level']:
# print(x,y, Point(x,y).distance(boundary))
data['distance_to_water'].append(Point(x, y).distance(boundary))
df = pd.DataFrame(data)
print(df)
# fig = plt.figure()
# ax = fig.add_subplot(111)
# minx, miny, maxx, maxy = boundary.bounds
# w, h = maxx - minx, maxy - miny
# ax.set_xlim(minx - 0.2 * w, maxx + 0.2 * w)
# ax.set_ylim(miny - 0.2 * h, maxy + 0.2 * h)
# ax.set_aspect(1)
# ax.add_collection(PatchCollection([PolygonPatch(boundary.buffer(0.1), fc='red', ec='black', zorder=1)], match_original=True))
# plt.show()
if __name__ == "__main__":
generate_map()
plt.show()

View File

@ -26,10 +26,16 @@
{% for k, v in parameters.items() %}
<div class='form-group'>
<label name='{{ k }}'>{{ k | replace('_', ' ') | title }}</label>
{% if v["type"] == "bool" %}
<input id='{{ k }}' name='{{ k }}'
type='checkbox'
class='form-control'>
{% else %}
<input id='{{ k }}' name='{{ k }}'
type="number"
class='form-control'
min='{{ v["min"] }}' value='{{ v["default"] }}' max='{{ v["max"] }}' step='{{ v["step"] }}'>
{% endif %}
{% if v["description"] %}
<small class="form-text text-muted">{{ v["description"] }}</small>
{% endif %}

View File

@ -13,6 +13,8 @@ def parse(key, value):
return int(value)
elif t == 'float':
return float(value)
elif t == 'bool':
return value == 'on'
else:
return value
@ -21,3 +23,4 @@ def get_map():
params = { key: parse(key, request.args[key]) for key in request.args }
res = send_file(generate_map(**params), mimetype='image/png')
return res