Source code for hypso.rgb.maps

"""RGB images on map grids, and map figures.

- :func:`regrid_camera`: a georeferenced RGB camera image
  (:func:`hypso.rgb.georeference_camera`) on a map grid - the capture's UTM
  zone, any CRS, or another product's grid - by cell averaging (default; the
  camera's ~30 m pixels are finer than most grids) or nearest neighbour.
- :func:`decorate_map`: graticule, scale bar, north arrow and coastlines on
  axes in projected coordinates (shared with :mod:`hypso.maps`).
- :func:`plot_rgb_map`: a gridded RGB image (from :func:`regrid_camera`, or a
  regridded hypso.Product) as a map, with optional map elements: latitude/
  longitude graticule, scale bar, north arrow, coastlines and title.

matplotlib is needed for the figures, and cartopy (with its Natural Earth
data) for coastlines; neither is a dependency of the package.
"""
import warnings

import numpy as np
import pyproj
import xarray as xr

from hypso.grid.areas import coordinate_attrs, grid_area, grid_name, utm_crs

CHANNELS = ("R", "G", "B")
#: HSI bands used for an RGB rendering of a hypso.Product (nm).
RGB_WAVELENGTHS = (640, 550, 460)


[docs] def camera_gsd(geo: xr.Dataset) -> float: """Median distance (m) between neighbouring camera pixel centres.""" lat, lon = geo["latitude"].values, geo["longitude"].values crs = utm_crs(lat[::50, ::50], lon[::50, ::50]) x, y = pyproj.Transformer.from_crs("EPSG:4326", crs, always_xy=True).transform( lon[::20, ::20].astype(float), lat[::20, ::20].astype(float)) return float(np.median(np.hypot(np.diff(x, axis=1), np.diff(y, axis=1))) / 20)
[docs] def regrid_camera(geo: xr.Dataset, target="utm", resolution: float = None, method: str = "auto", max_gb: float = 2.0) -> xr.Dataset: """A georeferenced camera image on a map grid. Parameters ---------- geo : xarray.Dataset From :func:`hypso.rgb.georeference_camera` (R, G, B, latitude, longitude). target : str, int, pyproj.CRS or pyresample.geometry.AreaDefinition, default "utm" ``"utm"`` (zone of the image centre), any CRS, or a grid - e.g. the HSI product's after regridding, ``l1c.regrid(...).area_definition()``, to put both on the same grid. resolution : float, optional Pixel size in the CRS's units; default the camera's own (~30 m). Not with an AreaDefinition. method : {"auto", "average", "nearest"}, default "auto" Cell means of the camera pixels in each cell, or the nearest pixel; "auto" averages when cells are at least twice the camera's pixel size and takes the nearest pixel otherwise (averaging onto cells as small as the pixels leaves empty cells). max_gb : float, default 2 Refuse grids larger than this. Returns ------- xarray.Dataset ``R``, ``G``, ``B`` on ``(y, x)`` with CF projection coordinates and a ``crs`` grid mapping (NaN where the camera has no data), and the camera's attributes plus ``grid``. """ from pyresample.geometry import AreaDefinition, SwathDefinition from hypso.grid.resample import Averager, Resampler lat, lon = geo["latitude"].values, geo["longitude"].values if isinstance(target, AreaDefinition): if resolution is not None: raise ValueError("resolution is set by the AreaDefinition; do not pass it as well") area = target else: crs = utm_crs(lat, lon) if target == "utm" else pyproj.CRS.from_user_input(target) if resolution is None: metres = camera_gsd(geo) resolution = round(metres / 111_320.0, 5) if crs.is_geographic else float(max(1, round(metres))) area = grid_area(crs, resolution, lat, lon, area_id="camera_grid") size_gb = area.shape[0] * area.shape[1] * 3 * 4 / 1e9 if size_gb > max_gb: raise ValueError(f"the {area.shape[0]} x {area.shape[1]} grid would need {size_gb:.1f} GB (max_gb={max_gb})") cube = np.stack([geo[c].values for c in CHANNELS], axis=-1).astype(np.float32) if method == "auto": cell = abs(area.pixel_size_x) * (111_320.0 if area.crs.is_geographic else 1) method = "average" if cell >= 2 * camera_gsd(geo) else "nearest" if method == "average": data = Averager(lat, lon, area).mean(cube) elif method == "nearest": radius = 1.5 * max(camera_gsd(geo), abs(area.pixel_size_x) * (111_320.0 if area.crs.is_geographic else 1)) data = Resampler(SwathDefinition(lons=lon.astype(np.float64), lats=lat.astype(np.float64)), area, radius, bilinear=False).nearest(cube) else: raise ValueError(f"method must be 'average' or 'nearest', got {method!r}") x, y = area.get_proj_vectors() x_attrs, y_attrs = coordinate_attrs(area.crs) size_x, size_y = abs(float(area.pixel_size_x)), abs(float(area.pixel_size_y)) out = xr.Dataset({c: (("y", "x"), data[..., i].astype(np.float32), dict(geo[c].attrs)) for i, c in enumerate(CHANNELS)}, coords={"x": ("x", x, x_attrs), "y": ("y", y, y_attrs)}, attrs={**{k: v for k, v in geo.attrs.items()}, "grid": grid_name(area.crs, size_x, size_y), "grid_crs": area.crs.to_string(), "grid_resolution_x": size_x, "grid_resolution_y": size_y, "grid_method": method}) out["crs"] = ((), np.int32(0), area.crs.to_cf()) for c in CHANNELS: out[c].attrs["grid_mapping"] = "crs" return out
def _rgb_and_grid(source, stretch, exposure=None): """(H x W x 3 image in 0-1 with NaN outside, x, y in metres, CRS).""" if isinstance(source, xr.Dataset): rgb = np.stack([source[c].values for c in CHANNELS], axis=-1).astype(float) crs = pyproj.CRS.from_cf(dict(source["crs"].attrs)) x, y = source["x"].values, source["y"].values else: # a regridded hypso.Product if source.grid is None: raise ValueError("plot_rgb_map needs a gridded product: regrid() it first") bands = [int(np.argmin(np.abs(source.wavelengths - w))) for w in RGB_WAVELENGTHS] rgb = source.cube.values[..., bands].astype(float) area = source.area_definition() crs = area.crs x, y = area.get_proj_vectors() if exposure is not None: # one gain for all channels (colours kept); a camera image's white is 255, a reflectance cube's 1 from hypso.rgb.exposure import adjust_exposure full_scale = 255.0 if isinstance(source, xr.Dataset) else 1.0 image = adjust_exposure(rgb, full_scale=full_scale, normalize=True, **exposure) return np.where(np.isnan(rgb), np.nan, image), np.asarray(x), np.asarray(y), crs low, high = np.nanpercentile(rgb, stretch, axis=(0, 1)) image = np.clip((rgb - low) / (high - low), 0, 1) return image, np.asarray(x), np.asarray(y), crs def _nice_length(span): exponent = 10 ** np.floor(np.log10(span)) return max(m * exponent for m in (1, 2, 5) if m * exponent <= span) def _fill_lonlat(ax, geometry, to_map, color): """Fill a (Multi)Polygon in longitude/latitude on projected axes, holes kept, below the data (zorder -1).""" from matplotlib.path import Path as MplPath from matplotlib.patches import PathPatch import shapely geometry = shapely.segmentize(geometry, 0.02) # straight lon/lat edges curve in the projection vertices, codes = [], [] for polygon in getattr(geometry, "geoms", [geometry]): if polygon.geom_type != "Polygon" or polygon.is_empty: continue for ring in [polygon.exterior, *polygon.interiors]: lon, lat = np.asarray(ring.coords).T x, y = to_map.transform(lon, lat) vertices += list(zip(x, y)) codes += [MplPath.MOVETO] + [MplPath.LINETO] * (len(x) - 2) + [MplPath.CLOSEPOLY] if vertices: ax.add_patch(PathPatch(MplPath(vertices, codes), facecolor=color, edgecolor="none", zorder=-1))
[docs] def decorate_map(ax, extent, crs, graticule: bool = True, scale_bar: bool = True, north_arrow: bool = True, coastlines: bool = False, graticule_step: float = None, ink: str = "white", coast_color: str = "yellow", water: bool = False, water_color: str = "#c6dbef"): """Map elements on axes in projected coordinates: latitude/longitude graticule, scale bar, north arrow and Natural Earth coastlines. Parameters ---------- ax : matplotlib.axes.Axes extent : sequence of float ``[xmin, xmax, ymin, ymax]`` in the CRS's units; the axes are set to it. crs : pyproj.CRS The axes' projection. graticule, scale_bar, north_arrow, coastlines, graticule_step As in :func:`plot_rgb_map`. ink : str, default "white" Colour of lines and labels ("white" on images, "black" on a light background). coast_color : str, default "yellow" water : bool, default False Fill the sea and lakes (Natural Earth 10 m ocean and lakes) in ``water_color``, below anything drawn at zorder >= 0 (images, data). water_color : str, default "#c6dbef" (light blue) """ from matplotlib import patheffects halo = "black" if ink == "white" else "white" outline = [patheffects.withStroke(linewidth=2.5, foreground=halo)] # readable on any background ax.set_xlim(extent[0], extent[1]) ax.set_ylim(extent[2], extent[3]) to_lonlat = pyproj.Transformer.from_crs(crs, "EPSG:4326", always_xy=True) to_map = pyproj.Transformer.from_crs("EPSG:4326", crs, always_xy=True) xx, yy = np.meshgrid(np.linspace(extent[0], extent[1], 50), np.linspace(extent[2], extent[3], 50)) lons, lats = to_lonlat.transform(xx, yy) if graticule: span = max(np.ptp(lons), np.ptp(lats)) step = graticule_step or _nice_length(span / 3) for value in np.arange(np.ceil(lats.min() / step) * step, lats.max(), step): lx, ly = to_map.transform(np.linspace(lons.min(), lons.max(), 200), np.full(200, value)) ax.plot(lx, ly, color=ink, lw=0.6, alpha=0.8, path_effects=[patheffects.withStroke(linewidth=1.4, foreground=halo, alpha=0.5)]) ax.annotate(f"{value:.6g}°N", (extent[0], ly[np.argmin(np.abs(lx - extent[0]))]), color=ink, fontsize=8, xytext=(3, 2), textcoords="offset points", path_effects=outline) for value in np.arange(np.ceil(lons.min() / step) * step, lons.max(), step): lx, ly = to_map.transform(np.full(200, value), np.linspace(lats.min(), lats.max(), 200)) ax.plot(lx, ly, color=ink, lw=0.6, alpha=0.8, path_effects=[patheffects.withStroke(linewidth=1.4, foreground=halo, alpha=0.5)]) ax.annotate(f"{value:.6g}°E", (lx[np.argmin(np.abs(ly - extent[2]))], extent[2]), color=ink, fontsize=8, xytext=(2, 3), textcoords="offset points", rotation=90, path_effects=outline) ax.set_xlim(extent[0], extent[1]) ax.set_ylim(extent[2], extent[3]) if water or coastlines: try: from cartopy.io import shapereader from shapely.geometry import box as lonlat_box region = lonlat_box(lons.min() - 0.5, lats.min() - 0.5, lons.max() + 0.5, lats.max() + 0.5) if water: for name in ("ocean", "lakes"): path = shapereader.natural_earth(resolution="10m", category="physical", name=name) for geometry in shapereader.Reader(path).geometries(): if geometry.intersects(region): _fill_lonlat(ax, geometry.intersection(region), to_map, water_color) if coastlines: lines = [] path = shapereader.natural_earth(resolution="10m", category="physical", name="coastline") lines += [g for g in shapereader.Reader(path).geometries() if g.intersects(region)] # lake shores (Natural Earth's coastline layer has only the sea's) path = shapereader.natural_earth(resolution="10m", category="physical", name="lakes") for lake in shapereader.Reader(path).geometries(): if lake.intersects(region): lines += [ring for part in getattr(lake, "geoms", [lake]) for ring in [part.exterior, *part.interiors]] for geometry in lines: for line in getattr(geometry, "geoms", [geometry]): lon_line, lat_line = np.asarray(line.coords).T cx, cy = to_map.transform(lon_line, lat_line) ax.plot(cx, cy, color=coast_color, lw=0.7) ax.set_xlim(extent[0], extent[1]) ax.set_ylim(extent[2], extent[3]) except Exception as error: # cartopy or its data missing warnings.warn(f"coastlines/water skipped: {error}", UserWarning, stacklevel=2) width = extent[1] - extent[0] if scale_bar: length = _nice_length(width / 4) x0, y0 = extent[0] + 0.05 * width, extent[2] + 0.05 * (extent[3] - extent[2]) ax.plot([x0, x0 + length], [y0, y0], color=ink, lw=3, solid_capstyle="butt") ax.plot([x0, x0 + length], [y0, y0], color=halo, lw=1, solid_capstyle="butt") ax.annotate(f"{length / 1000:g} km", (x0 + length / 2, y0), color=ink, ha="center", fontsize=9, xytext=(0, 5), textcoords="offset points", path_effects=outline) if north_arrow: cx, cy = extent[1] - 0.08 * width, extent[3] - 0.15 * (extent[3] - extent[2]) lon_c, lat_c = to_lonlat.transform(cx, cy) nx, ny = to_map.transform(lon_c, lat_c + 0.01) direction = np.array([nx - cx, ny - cy]) / np.hypot(nx - cx, ny - cy) tip = np.array([cx, cy]) + direction * 0.06 * width ax.annotate("N", tip, xytext=(cx, cy), color=ink, ha="center", fontsize=11, fontweight="bold", path_effects=outline, arrowprops={"arrowstyle": "-|>", "color": ink, "lw": 2, "path_effects": outline}) ax.set_xticks([]) ax.set_yticks([]) return ax
[docs] def plot_rgb_map(source, ax=None, graticule: bool = True, scale_bar: bool = True, north_arrow: bool = True, coastlines: bool = False, title: str = None, stretch=(2, 98), graticule_step: float = None, exposure: dict = None): """Plot a gridded RGB image as a map. Parameters ---------- source : xarray.Dataset or hypso.Product A gridded camera image (:func:`regrid_camera`) or a regridded product (its bands nearest 640, 550 and 460 nm). ax : matplotlib.axes.Axes, optional Axes to draw on; default a new figure. graticule : bool, default True Latitude/longitude lines with labels. scale_bar : bool, default True A scale bar (km) in the lower left. north_arrow : bool, default True An arrow to true north (in a UTM grid, true north is not straight up). coastlines : bool, default False Natural Earth 10 m coastlines (needs cartopy and its data; skipped with a warning otherwise). title : str, optional stretch : tuple of float, default (2, 98) Percentiles mapped to black and white per channel. graticule_step : float, optional Degrees between graticule lines; default chosen from the extent. exposure : dict, optional Use :func:`hypso.rgb.adjust_exposure` (one gain for all channels, colours kept) instead of the per-channel ``stretch``, with these arguments, e.g. ``{}`` (automatic), ``{"ev": 1}`` or ``{"gamma": 2.2}``. Returns ------- matplotlib.axes.Axes """ try: import matplotlib.pyplot as plt except ImportError as error: raise ImportError("plot_rgb_map needs matplotlib (pip install matplotlib)") from error image, x, y, crs = _rgb_and_grid(source, stretch, exposure) image = np.where(np.isnan(image), 1.0, image) if ax is None: _, ax = plt.subplots(figsize=(7, 7)) dx, dy = abs(x[1] - x[0]) if x.size > 1 else 1.0, abs(y[1] - y[0]) if y.size > 1 else 1.0 extent = [x.min() - dx / 2, x.max() + dx / 2, y.min() - dy / 2, y.max() + dy / 2] ax.imshow(image, extent=extent, origin="upper" if y[0] > y[-1] else "lower", interpolation="nearest") decorate_map(ax, extent, crs, graticule=graticule, scale_bar=scale_bar, north_arrow=north_arrow, coastlines=coastlines, graticule_step=graticule_step) if title: ax.set_title(title) return ax