Skip to content

Commit

Permalink
fix inverted hillshade & misleading tile reference (#229) (#232)
Browse files Browse the repository at this point in the history
* fix inverted hillshade & misleading tile reference (#229)
  • Loading branch information
ungarj committed Dec 3, 2019
1 parent e7c6bb7 commit 4a292f6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Changelog
----
* don't raise exception when one of the registered processes cannot be imported (#225)
* don't close pool between zoom levels (#227)
* ``_validate`` module renamed to ``validate``
* ``_validate`` module renamed to ``validate`` (#230)
* fix inverted hillshade & misleading tile reference (#229)

----
0.30
Expand Down
4 changes: 3 additions & 1 deletion mapchete/_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ def hillshade(
-------
hillshade : array
"""
return commons_hillshade.hillshade(elevation, self, azimuth, altitude, z, scale)
return commons_hillshade.hillshade(
elevation, self.tile, azimuth, altitude, z, scale
)

def contours(
self, elevation, interval=100, field='elev', base=0
Expand Down
4 changes: 2 additions & 2 deletions mapchete/commons/contours.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def extract_contours(array, tile, interval=100, field='elev', base=0):
----------
array : array
input elevation data
tile : Tile
tile covering the array
mp : mapchete.MapcheteProcess
process object
interval : integer
elevation value interval when drawing contour lines
field : string
Expand Down
17 changes: 10 additions & 7 deletions mapchete/commons/hillshade.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def hillshade(elevation, tile, azimuth=315.0, altitude=45.0, z=1.0, scale=1.0, )
altitude = float(altitude)
z = float(z)
scale = float(scale)
xres = tile.tile.pixel_x_size
yres = -tile.tile.pixel_y_size
xres = tile.pixel_x_size
yres = -tile.pixel_y_size
slope, aspect = calculate_slope_aspect(
elevation,
xres,
Expand All @@ -117,13 +117,16 @@ def hillshade(elevation, tile, azimuth=315.0, altitude=45.0, z=1.0, scale=1.0, )
scale=scale
)
deg2rad = math.pi / 180.0
# shaded has values between -1.0 and +1.0
shaded = np.sin(altitude * deg2rad) * np.sin(slope) \
+ np.cos(altitude * deg2rad) * np.cos(slope) \
* np.cos((azimuth - 90.0) * deg2rad - aspect)
# shaded now has values between -1.0 and +1.0
# stretch to 0 - 255 and invert
shaded = (((shaded + 1.0) / 2) * -255.0).astype("uint8")
# add one pixel padding using the edge values
# stretch to 0 - 255 and add one pixel padding using the edge values
return ma.masked_array(
data=np.pad(shaded, 1, mode='edge'), mask=elevation.mask
data=np.pad(
np.clip(shaded * 255.0, 1, 255).astype("uint8"),
1,
mode='edge'
),
mask=elevation.mask
)
10 changes: 4 additions & 6 deletions mapchete/formats/default/png_hillshade.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,12 @@ def empty(self, process_tile):
return ma.masked_values(np.zeros(process_tile.shape), 0)

def _prepare_array(self, data):
data = prepare_array(
-(data - 255), dtype="uint8", masked=False, nodata=0)
data = prepare_array(-(data - 255), dtype="uint8", masked=False, nodata=0)[0]
zeros = np.zeros(data.shape)
if self.old_band_num:
data = np.stack((
np.zeros(data[0].shape), np.zeros(data[0].shape),
np.zeros(data[0].shape), data[0]))
data = np.stack([zeros, zeros, zeros, data])
else:
data = np.stack((np.zeros(data[0].shape), data[0]))
data = np.stack([zeros, data])
return prepare_array(data, dtype="uint8", masked=True, nodata=255)


Expand Down
14 changes: 7 additions & 7 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,27 +593,27 @@ def test_serve(client, mp_tmpdir):
response = client.get(url)
assert response.status_code == 200
for url in [
tile_base_url+"5/30/62.png",
tile_base_url+"5/30/63.png",
tile_base_url+"5/31/62.png",
tile_base_url+"5/31/63.png",
tile_base_url + "5/30/62.png",
tile_base_url + "5/30/63.png",
tile_base_url + "5/31/62.png",
tile_base_url + "5/31/63.png",
]:
response = client.get(url)
assert response.status_code == 200
img = response.data
with MemoryFile(img) as memfile:
with memfile.open() as dataset:
data = dataset.read()
# get alpha band and assert not all are masked
assert not data[3].all()
# get alpha band and assert some pixels are masked
assert data[3].any()
# test outside zoom range
response = client.get(tile_base_url + "6/31/63.png")
assert response.status_code == 200
img = response.data
with MemoryFile(img) as memfile:
with memfile.open() as dataset:
data = dataset.read()
assert not data.any()
assert not data.all()
# test invalid url
response = client.get(tile_base_url + "invalid_url")
assert response.status_code == 404
Expand Down

0 comments on commit 4a292f6

Please sign in to comment.