Skip to content

Commit

Permalink
Refactoring to make more systematic use of Mixin classes. Note this m…
Browse files Browse the repository at this point in the history
…akes the Raster class larger, but it is easier to understand because the logic for the Raster and RasterLayer classes are more contained
  • Loading branch information
stevenpawley committed Jun 2, 2024
1 parent f6c2aad commit 63a0dba
Show file tree
Hide file tree
Showing 9 changed files with 500 additions and 504 deletions.
24 changes: 0 additions & 24 deletions pyspatialml/_extraction.py

This file was deleted.

124 changes: 123 additions & 1 deletion pyspatialml/_plotting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math

import rasterio
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -34,7 +35,7 @@ def discrete_cmap(N, base_cmap=None):
return base.from_list(cmap_name, color_list, N)


class RasterPlot(object):
class RasterPlotMixin(object):
def plot(
self,
cmap=None,
Expand Down Expand Up @@ -264,3 +265,124 @@ def plot(
cbar.ax.tick_params(labelsize=legend_fontsize)

return axs


class RasterLayerPlotMixin(object):
def plot(
self,
cmap=None,
norm=None,
ax=None,
cax=None,
figsize=None,
out_shape=(500, 500),
categorical=None,
legend=False,
vmin=None,
vmax=None,
fig_kwds=None,
legend_kwds=None,
):
"""Plot a RasterLayer using matplotlib.pyplot.imshow
Parameters
----------
cmap : str (default None)
The name of a colormap recognized by matplotlib.
Overrides the cmap attribute of the RasterLayer.
norm : matplotlib.colors.Normalize (opt)
A matplotlib.colors.Normalize to apply to the RasterLayer.
This overrides the norm attribute of the RasterLayer.
ax : matplotlib.pyplot.Artist (optional, default None)
axes instance on which to draw to plot.
cax : matplotlib.pyplot.Artist (optional, default None)
axes on which to draw the legend.
figsize : tuple of integers (optional, default None)
Size of the matplotlib.figure.Figure. If the ax argument is
given explicitly, figsize is ignored.
out_shape : tuple, default=(500, 500)
Number of rows, cols to read from the raster datasets for
plotting.
categorical : bool (optional, default False)
if True then the raster values will be considered to
represent discrete values, otherwise they are considered to
represent continuous values. This overrides the
RasterLayer 'categorical' attribute. Setting the argument
categorical to True is ignored if the
RasterLayer.categorical is already True.
legend : bool (optional, default False)
Whether to plot the legend.
vmin, xmax : scale (optional, default None)
vmin and vmax define the data range that the colormap
covers. By default, the colormap covers the complete value
range of the supplied data. vmin, vmax are ignored if the
norm parameter is used.
fig_kwds : dict (optional, default None)
Additional arguments to pass to the
matplotlib.pyplot.figure call when creating the figure
object. Ignored if ax is passed to the plot function.
legend_kwds : dict (optional, default None)
Keyword arguments to pass to matplotlib.pyplot.colorbar().
Returns
-------
ax : matplotlib axes instance
"""

# some checks
if fig_kwds is None:
fig_kwds = {}

if ax is None:
if cax is not None:
raise ValueError("'ax' can not be None if 'cax' is not.")
fig, ax = plt.subplots(figsize=figsize, **fig_kwds)

ax.set_aspect("equal")

if norm:
if not isinstance(norm, mpl.colors.Normalize):
raise AttributeError(
"norm argument should be a " "matplotlib.colors.Normalize object"
)

if cmap is None:
cmap = self.cmap

if norm is None:
norm = self.norm

if legend_kwds is None:
legend_kwds = {}

arr = self.read(masked=True, out_shape=out_shape)

if categorical is True:
if self.categorical is False:
N = np.bincount(arr)
cmap = discrete_cmap(N, base_cmap=cmap)
vmin, vmax = None, None

im = ax.imshow(
X=arr,
extent=rasterio.plot.plotting_extent(self.ds),
cmap=cmap,
norm=norm,
vmin=vmin,
vmax=vmax,
)

if legend is True:
plt.colorbar(im, cax=cax, ax=ax, **legend_kwds)

return ax
167 changes: 0 additions & 167 deletions pyspatialml/_rasterbase.py

This file was deleted.

Loading

0 comments on commit 63a0dba

Please sign in to comment.