Skip to content

Commit

Permalink
Use new Leaflet Map component
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kallekleiv committed Sep 7, 2020
1 parent c147444 commit 80625e2
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 137 deletions.
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@
"defusedxml>=0.6.0",
"ecl2df>=0.6.1; sys_platform=='linux'",
"fmu-ensemble>=1.2.3",
"matplotlib>=3.0",
"opm>=2020.10.1; sys_platform=='linux'",
"pandas>=0.24",
"pillow>=6.1",
"pyscal>=0.4.1",
"scipy>=1.2",
"webviz-config>=0.0.55",
"webviz-subsurface-components>=0.0.27",
"webviz-subsurface-components>=0.2.0",
"xtgeo>=2.8",
],
tests_require=TESTS_REQUIRE,
Expand Down
11 changes: 1 addition & 10 deletions tests/unit_tests/data_input/test_image_processing.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import numpy as np

from webviz_subsurface._datainput.image_processing import (
array_to_png,
get_colormap,
)
from webviz_subsurface._datainput.image_processing import array_to_png

with open("tests/data/surface_png.txt", "r") as f:
BASE64_SURFACE = f.read()
with open("tests/data/colormap.txt", "r") as f:
BASE64_COLORMAP = f.read()


def test_array_to_png():
data = np.loadtxt("tests/data/surface_zarr.np.gz")
assert array_to_png(data) == BASE64_SURFACE


def test_colormap():
assert get_colormap("viridis") == BASE64_COLORMAP
7 changes: 0 additions & 7 deletions webviz_subsurface/_datainput/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import base64

import numpy as np
from matplotlib import cm
from PIL import Image


Expand Down Expand Up @@ -76,9 +75,3 @@ def array_to_png(
base64_data = base64.b64encode(byte_io.read()).decode("ascii")

return f"data:image/png;base64,{base64_data}"


def get_colormap(colormap):
return array_to_png(
cm.get_cmap(colormap, 256)([np.linspace(0, 1, 256)]), colormap=True
)
59 changes: 14 additions & 45 deletions webviz_subsurface/_datainput/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from webviz_config.common_cache import CACHE
from PIL import Image

from .image_processing import array_to_png, get_colormap
from .image_processing import array_to_png


@CACHE.memoize(timeout=CACHE.TIMEOUT)
Expand Down Expand Up @@ -33,43 +33,11 @@ def get_surface_fence(fence, surface):
def make_surface_layer(
surface,
name="surface",
min_val=None,
max_val=None,
color="viridis",
hillshading=False,
unit="",
):
"""Make LayeredMap surface image base layer"""
zvalues = get_surface_arr(surface)[2]
bounds = [[surface.xmin, surface.ymin], [surface.xmax, surface.ymax]]
min_val = min_val if min_val is not None else np.nanmin(zvalues)
max_val = max_val if max_val is not None else np.nanmax(zvalues)
return {
"name": name,
"checked": True,
"base_layer": True,
"data": [
{
"type": "image",
"url": array_to_png(zvalues.copy()),
"colormap": get_colormap(color),
"bounds": bounds,
"allowHillshading": hillshading,
"minvalue": f"{min_val:.2f}" if min_val is not None else None,
"maxvalue": f"{max_val:.2f}" if max_val is not None else None,
"unit": str(unit),
}
],
}


def new_make_surface_layer(
surface,
name="surface",
updatemode="update",
min_val=None,
max_val=None,
color=None,
shader_type="hillshading",
shader_type="soft-hillshading",
unit="",
):
"""Make NewLayeredMap surface image base layer
Expand All @@ -84,12 +52,11 @@ def new_make_surface_layer(
Returns:
A surface layer that can be plotted in NewLayeredMap
"""

zvalues = get_surface_arr(surface)[2]
bounds = [[surface.xmin, surface.ymin], [surface.xmax, surface.ymax]]
min_val = min_val if min_val is not None else np.nanmin(zvalues)
max_val = max_val if max_val is not None else np.nanmax(zvalues)
image = base64.b64decode(array_to_png(zvalues.copy())[22:])
img = Image.open(io.BytesIO(image))
img = Image.open(io.BytesIO(base64.b64decode(array_to_png(zvalues.copy())[22:])))
width, height = img.size
if width * height >= 300 * 300:
scale = 1.0
Expand All @@ -99,6 +66,8 @@ def new_make_surface_layer(
return {
"name": name,
"checked": True,
"id": name,
"action": updatemode,
"baseLayer": True,
"data": [
{
Expand All @@ -124,17 +93,17 @@ def new_make_surface_layer(
"cutPointMin": min_val,
"cutPointMax": max_val,
},
"bounds": bounds,
"shader": {
"type": shader_type,
"shadows": False,
"shadowIterations": 2,
"elevationScale": 0.05,
"shadows": True,
"shadowIterations": 128,
"elevationScale": 1.0,
"pixelScale": 1000,
"setBlackToAlpha": True,
},
"minvalue": min_val.round(2),
"maxvalue": max_val.round(2),
"bounds": [[surface.xmin, surface.ymin], [surface.xmax, surface.ymax]],
"minvalue": round(min_val, 4) if min_val else None,
"maxvalue": round(max_val, 4) if max_val else None,
"unit": str(unit),
"imageScale": scale,
}
Expand Down Expand Up @@ -165,7 +134,7 @@ def get_surface_layers(switch, surface_name, surfaces, min_val=None, max_val=Non
layers = []
for i, surface in enumerate(surfaces):
if surface is not None:
s_layer = new_make_surface_layer(
s_layer = make_surface_layer(
surface,
name=depth_list[i],
min_val=min_val,
Expand Down
Loading

0 comments on commit 80625e2

Please sign in to comment.