Skip to content

Commit

Permalink
Added output for a Hypsographic curve.
Browse files Browse the repository at this point in the history
A small bit of polish.
  • Loading branch information
tcld committed Nov 18, 2015
1 parent 3e8030e commit 9208508
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 20 deletions.
3 changes: 2 additions & 1 deletion tests/blessed_images/generated_blessed_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def main(blessed_images_dir, tests_data_dir):
draw_temperature_levels_on_file(w, "%s/temperature_28070.png" % blessed_images_dir)
draw_biome_on_file(w, "%s/biome_28070.png" % blessed_images_dir)
draw_scatter_plot_on_file(w, "%s/scatter_28070.png" % blessed_images_dir)
draw_hypsographic_plot_on_file(w, "%s/hypsographic_28070.png" % blessed_images_dir)
draw_satellite_on_file(w, "%s/satellite_28070.png" % blessed_images_dir)
draw_ancientmap_on_file(w, "%s/ancientmap_28070_factor3.png" % blessed_images_dir, resize_factor=3)
draw_ancientmap_on_file(w, "%s/ancientmap_28070_factor2.png" % blessed_images_dir, resize_factor=2)

img = PNGWriter.rgba_from_dimensions(w.width * 2, w.height * 2, "%s/rivers_28070_factor2.png" % blessed_images_dir)
draw_rivers_on_image(w, img, factor=2)
Expand Down
11 changes: 10 additions & 1 deletion tests/draw_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import numpy
from worldengine.draw import _biome_colors, draw_simple_elevation, elevation_color, \
draw_elevation, draw_riversmap, draw_grayscale_heightmap, draw_ocean, draw_precipitation, \
draw_world, draw_temperature_levels, draw_biome, draw_scatter_plot, draw_satellite
draw_world, draw_temperature_levels, draw_biome, draw_scatter_plot, draw_hypsographic_plot, \
draw_satellite
from worldengine.biome import Biome
from worldengine.world import World
from worldengine.image_io import PNGWriter, PNGReader
Expand Down Expand Up @@ -154,6 +155,14 @@ def test_draw_scatter_plot(self):
draw_scatter_plot(w, 512, target)
self._assert_img_equal("scatter_28070", target)

def test_draw_hypsographic_plot(self):
x_bins = 800
y_bins = 600
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PNGWriter.grayscale_from_dimensions(x_bins, y_bins)
draw_hypsographic_plot(w, target, x_bins, y_bins)
self._assert_img_equal("hypsographic_28070", target)

def test_draw_satellite(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
Expand Down
7 changes: 4 additions & 3 deletions tests/drawing_functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ def setUp(self):
self.w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)

def test_draw_ancient_map(self):
target = PNGWriter.rgba_from_dimensions(self.w.width * 3, self.w.height * 3)
draw_ancientmap(self.w, target, resize_factor=3)
self._assert_img_equal("ancientmap_28070_factor3", target)
factor = int(2)
target = PNGWriter.rgba_from_dimensions(self.w.width * factor, self.w.height * factor)
draw_ancientmap(self.w, target, resize_factor=factor)
self._assert_img_equal("ancientmap_28070_factor%i" % factor, target)

def test_gradient(self):
self._assert_are_colors_equal((10, 20, 40),
Expand Down
45 changes: 31 additions & 14 deletions worldengine/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from worldengine.draw import draw_ancientmap_on_file, draw_biome_on_file, draw_ocean_on_file, \
draw_precipitation_on_file, draw_grayscale_heightmap_on_file, draw_simple_elevation_on_file, \
draw_temperature_levels_on_file, draw_riversmap_on_file, draw_scatter_plot_on_file, \
draw_satellite_on_file, draw_icecaps_on_file
draw_hypsographic_plot_on_file, draw_satellite_on_file, draw_icecaps_on_file
from worldengine.plates import world_gen, generate_plates_simulation
from worldengine.imex import export
from worldengine.step import Step
Expand Down Expand Up @@ -84,18 +84,27 @@ def generate_rivers_map(world, filename):
draw_riversmap_on_file(world, filename)
print("+ rivers map generated in '%s'" % filename)

def draw_scatter_plot(world, filename):
draw_scatter_plot_on_file(world, filename)
print("+ scatter plot generated in '%s'" % filename)

def draw_satellite_map(world, filename):
def generate_satellite_map(world, filename):
draw_satellite_on_file(world, filename)
print("+ satellite map generated in '%s'" % filename)

def draw_icecaps_map(world, filename):

def generate_icecaps_map(world, filename):
draw_icecaps_on_file(world, filename)
print("+ icecap map generated in '%s'" % filename)


def generate_scatter_plot(world, filename):
draw_scatter_plot_on_file(world, filename)
print("+ scatter plot generated in '%s'" % filename)


def generate_hypsographic_plot(world, filename):
draw_hypsographic_plot_on_file(world, filename)
print("+ hypsographic plot generated in '%s'" % filename)


def generate_plates(seed, world_name, output_dir, width, height,
num_plates=10):
"""
Expand Down Expand Up @@ -317,12 +326,14 @@ def main():
g_generate.add_argument('--not-fade-borders', dest='fade_borders', action="store_false",
help="Not fade borders",
default=True)
g_generate.add_argument('--scatter', dest='scatter_plot',
action="store_true", help="generate scatter plot")
g_generate.add_argument('--sat', dest='satelite_map',
action="store_true", help="generate satellite map")
g_generate.add_argument('--ice', dest='icecaps_map',
action="store_true", help="generate ice caps map")
g_generate.add_argument('--scatter', dest='scatter_plot',
action="store_true", help="generate scatter plot")
g_generate.add_argument('--hypsographic', dest='hypsographic_plot',
action="store_true", help="generate hypsographic curve")

# -----------------------------------------------------
g_ancient_map = parser.add_argument_group(
Expand Down Expand Up @@ -468,7 +479,9 @@ def main():
for x in range(0,len(humids)):
humids[x] = 1 - float(humids[x])
if args.scatter_plot and not generation_operation:
usage(error="Scatter plot can be produced only during world generation")
usage(error="Scatter plot can be produced only during world generation.")
if args.hypsographic_plot and not generation_operation:
usage(error="Hypsograpic curve can be produced only during world generation.")

print('Worldengine - a world generator (v. %s)' % VERSION)
print('-----------------------')
Expand All @@ -486,6 +499,7 @@ def main():
print(' icecaps heightmap : %s' % args.icecaps_map)
print(' rivers map : %s' % args.rivers_map)
print(' scatter plot : %s' % args.scatter_plot)
print(' hypsographic plot : %s' % args.hypsographic_plot)
print(' satellite map : %s' % args.satelite_map)
print(' fade borders : %s' % args.fade_borders)
if args.temps:
Expand Down Expand Up @@ -542,15 +556,18 @@ def main():
if args.rivers_map:
generate_rivers_map(world,
'%s/%s_rivers.png' % (args.output_dir, world_name))
if args.scatter_plot:
draw_scatter_plot(world,
'%s/%s_scatter.png' % (args.output_dir, world_name))
if args.satelite_map:
draw_satellite_map(world,
generate_satellite_map(world,
'%s/%s_satellite.png' % (args.output_dir, world_name))
if args.icecaps_map:
draw_icecaps_map(world,
generate_icecaps_map(world,
'%s/%s_icecaps.png' % (args.output_dir, world_name))
if args.scatter_plot:
generate_scatter_plot(world,
'%s/%s_scatter.png' % (args.output_dir, world_name))
if args.hypsographic_plot:
generate_hypsographic_plot(world,
'%s/%s_hypsographic.png' % (args.output_dir, world_name))

elif operation == 'plates':
print('') # empty line
Expand Down
52 changes: 51 additions & 1 deletion worldengine/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,49 @@ def draw_scatter_plot(world, size, target):
ny = (size - 1) * ((p - min_humidity) / humidity_delta)

target.set_pixel(int(nx), (size - 1) - int(ny), (r, 128, b, 255))



def draw_hypsographic_plot(world, target, x_bins, y_bins):
# Will draw a Hypsograhpic plot that contains information about the distribution of elevations in the world.
# y-axis shows the elevation, x-axis shows how often a certain elevation occurs.
# For further details see:
# https://en.wikipedia.org/wiki/Elevation#Hypsography
# http://www.ngdc.noaa.gov/mgg/global/etopo1_surface_histogram.html

assert int(x_bins) > 0 and int(y_bins) > 0, "Hypsographic curve needs more than 0 bins."

# set colors
color_none = target.get_max_colors() # white
color_full = int(color_none / 2) # gray
color_line = 0 # black

# Prepare a list of available elevations. Scale them to the y-axis and put them into a sorted list,
# smallest point to highest.
e = world.elevation['data']
e_min = e.min()
e_max = e.max()
e = (e - e_min) / (e_max - e_min) # normalize to [0, 1]
e *= int(y_bins) - 0.01 # instead of "- 1", small trick to make the last bin contain more than one point
e = numpy.sort(e, axis=None) # flatten the array and order values by height
e = e.astype(dtype=numpy.uint16) # do not round (the graph wouldn't change, just shift)

# Fill the plot.
# Start by calculating how many points of the heightmap have to be represented by a single bin (n).
# Then step through the bins and fill in points from highest to lowest. Every bin is set to the average height of
# the n points it represents.
# Instead of drawing to [y, x] draw to [y_bins - y - 1, x_bins - x - 1] to flip the output in x- and y-direction.
n = e.size / float(x_bins)
for x in range(x_bins):
avg = sum(e[int(x * n):int((x + 1) * n)])
avg = int(avg / float(n)) # average height of n height-points
for y in range(y_bins):
target[y_bins - y - 1, x_bins - x - 1] = color_none if y > avg else color_full

# Draw sea-level (a black line at sea-level).
sea = int(numpy.interp(world.sea_level(), [e_min, e_max], [0, y_bins])) # map sea-level to appropriate y-position
for x in range(x_bins):
target[y_bins - sea - 1, x] = color_line


# -------------
# Draw on files
Expand Down Expand Up @@ -811,6 +853,14 @@ def draw_scatter_plot_on_file(world, filename):
img.complete()


def draw_hypsographic_plot_on_file(world, filename):
x_bins = 800
y_bins = 600
img = PNGWriter.grayscale_from_dimensions(x_bins, y_bins, filename)
draw_hypsographic_plot(world, img, x_bins, y_bins)
img.complete()


def draw_satellite_on_file(world, filename):
img = PNGWriter.rgba_from_dimensions(world.width, world.height, filename)
draw_satellite(world, img)
Expand Down

0 comments on commit 9208508

Please sign in to comment.