Skip to content

Commit

Permalink
Merge pull request #16 from xpublish-community/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
mpiannucci committed Jun 21, 2023
2 parents b91984e + 683274f commit f2158b0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 53 deletions.
2 changes: 1 addition & 1 deletion xpublish_wms/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .wms import wms_handler

logger = logging.getLogger("cf_wms")
logger = logging.getLogger("uvicorn")


class CfWmsPlugin(Plugin):
Expand Down
13 changes: 12 additions & 1 deletion xpublish_wms/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import contextlib
import logging
from typing import Tuple, Union

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
from pyproj import Transformer

from xpublish_wms.grid import GridType

logger = logging.getLogger(__name__)
logger = logging.getLogger("uvicorn")
matplotlib.use("Agg")


def lower_case_keys(d: dict) -> dict:
Expand Down Expand Up @@ -103,3 +107,10 @@ def ds_bbox(ds: xr.Dataset) -> Tuple[float, float, float, float]:
]

return bbox


@contextlib.contextmanager
def figure_context(*args, **kwargs):
fig = plt.figure(*args, **kwargs)
yield fig
plt.close(fig)
104 changes: 53 additions & 51 deletions xpublish_wms/wms/get_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
import cachey
import cartopy.crs as ccrs
import cf_xarray # noqa
import matplotlib
import matplotlib.cm as cm
import matplotlib.tri as tri
import numpy as np
import pandas as pd
import xarray as xr
from fastapi.responses import StreamingResponse
from matplotlib import cm
from matplotlib.figure import Figure
from PIL import Image
from rasterio.enums import Resampling
from rasterio.transform import from_bounds

from xpublish_wms.grid import GridType
from xpublish_wms.utils import to_lnglat
from xpublish_wms.utils import figure_context, to_lnglat

logger = logging.getLogger(__name__)
logger = logging.getLogger("uvicorn")
matplotlib.use("Agg")


class GetMap:
Expand Down Expand Up @@ -368,8 +369,8 @@ def render_sgrid(
data_sel = data[inds]
if minmax_only:
return {
"min": float(data_sel.min()),
"max": float(data_sel.max()),
"min": float(np.nanmin(data_sel)),
"max": float(np.nanmax(data_sel)),
}

tris = tri.Triangulation(x_sel, y_sel)
Expand All @@ -381,50 +382,51 @@ def render_sgrid(
projection = ccrs.Mercator() if self.crs == "EPSG:3857" else ccrs.PlateCarree()

dpi = 80
fig = Figure(dpi=dpi, facecolor="none", edgecolor="none")
fig.set_alpha(0)
fig.set_figheight(self.height / dpi)
fig.set_figwidth(self.width / dpi)
ax = fig.add_axes(
[0.0, 0.0, 1.0, 1.0],
xticks=[],
yticks=[],
projection=projection,
)
ax.set_axis_off()
ax.set_frame_on(False)
ax.set_clip_on(False)
ax.set_position([0, 0, 1, 1])

if not self.autoscale:
vmin, vmax = self.colorscalerange
else:
vmin, vmax = [None, None]

try:
# ax.tripcolor(tris, data_sel, transform=ccrs.PlateCarree(), cmap=cmap, shading='flat', vmin=vmin, vmax=vmax)
ax.tricontourf(
tris,
data_sel,
transform=ccrs.PlateCarree(),
cmap=self.palettename,
vmin=vmin,
vmax=vmax,
levels=80,
with figure_context(dpi=dpi, facecolor="none", edgecolor="none") as fig:
fig.set_alpha(0)
fig.set_figheight(self.height / dpi)
fig.set_figwidth(self.width / dpi)
ax = fig.add_axes(
[0.0, 0.0, 1.0, 1.0],
xticks=[],
yticks=[],
projection=projection,
)
# ax.pcolormesh(x, y, data, transform=ccrs.PlateCarree(), cmap=cmap, vmin=vmin, vmax=vmax)
except Exception as e:
print(e)
print(bbox)

ax.set_extent(bbox, crs=ccrs.PlateCarree())
ax.axis("off")

fig.savefig(
buffer,
format="png",
transparent=True,
pad_inches=0,
bbox_inches="tight",
)
ax.set_axis_off()
ax.set_frame_on(False)
ax.set_clip_on(False)
ax.set_position([0, 0, 1, 1])

if not self.autoscale:
vmin, vmax = self.colorscalerange
else:
vmin, vmax = [None, None]

try:
# ax.tripcolor(tris, data_sel, transform=ccrs.PlateCarree(), cmap=cmap, shading='flat', vmin=vmin, vmax=vmax)
ax.tricontourf(
tris,
data_sel,
transform=ccrs.PlateCarree(),
cmap=self.palettename,
vmin=vmin,
vmax=vmax,
levels=80,
)
# ax.pcolormesh(x, y, data, transform=ccrs.PlateCarree(), cmap=cmap, vmin=vmin, vmax=vmax)
except Exception as e:
print(e)
print(bbox)

ax.set_extent(bbox, crs=ccrs.PlateCarree())
ax.axis("off")

fig.savefig(
buffer,
format="png",
transparent=True,
pad_inches=0,
bbox_inches="tight",
)

return True

0 comments on commit f2158b0

Please sign in to comment.