Skip to content

Commit

Permalink
sel a smaller region for each tile to render to
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiannucci committed Nov 7, 2024
1 parent b8d19cc commit 57ff2ac
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions xpublish_wms/wms/get_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,29 +279,48 @@ def render(
if minmax_only:
logger.warning("Falling back to default minmax")
return {"min": float(da.min()), "max": float(da.max())}

# x and y are only set for triangle grids, we dont subset the data for triangle grids
# at this time.
if x is None:
try:
# Grab a buffer around the bbox to ensure we have enough data to render
# TODO: Base this on actual data resolution?
if self.crs == "EPSG:4326":
coord_buffer = 0.5 # degrees
elif self.crs == "EPSG:3857":
coord_buffer = 30000 # meters
else:
# Default to 0.5, this should never happen
coord_buffer = 0.5

# Filter the data to only include the data within the bbox + buffer so
# we don't have to render a ton of empty space or pull down more chunks
# than we need
da = filter_data_within_bbox(da, self.bbox, coord_buffer)
except Exception as e:
print(f"Error filtering data within bbox: {e}")
print("Falling back to full layer")

# Squeeze single value dimensions
da = da.squeeze()

logger.debug(f"Projection time: {time.time() - projection_start}")
logger.warning(f"Projection time: {time.time() - projection_start}")

start_dask = time.time()

da = da.persist()
da = da.compute()
if x is not None and y is not None:
x = x.persist()
y = y.persist()
else:
da["x"] = da.x.persist()
da["y"] = da.y.persist()
x = x.compute()
y = y.compute()

logger.debug(f"dask compute: {time.time() - start_dask}")
logger.warning(f"dask compute: {time.time() - start_dask}")

if minmax_only:
da = da.persist()
data_sel = filter_data_within_bbox(da, self.bbox, self.BBOX_BUFFER)

try:
return {
"min": float(np.nanmin(data_sel)),
"max": float(np.nanmax(data_sel)),
"min": float(np.nanmin(da)),
"max": float(np.nanmax(da)),
}
except Exception as e:
logger.error(
Expand All @@ -322,9 +341,6 @@ def render(
y_range=(self.bbox[1], self.bbox[3]),
)

# Squeeze single value dimensions
da = da.squeeze()

if ds.gridded.render_method == RenderMethod.Quad:
mesh = cvs.quadmesh(
da,
Expand Down Expand Up @@ -354,7 +370,7 @@ def render(
how="linear",
span=(vmin, vmax),
)
logger.debug(f"Shade time: {time.time() - start_shade}")
logger.warning(f"Shade time: {time.time() - start_shade}")

im = shaded.to_pil()
im.save(buffer, format="PNG")
Expand Down

0 comments on commit 57ff2ac

Please sign in to comment.