Zonal analysis#

class pylandstats.ZonalAnalysis(landscape_filepath, zones, *, zone_index=None, zone_nodata=0, neighborhood_rule=None)[source]#

Zonal analysis.

__init__(landscape_filepath, zones, *, zone_index=None, zone_nodata=0, neighborhood_rule=None)[source]#

Initialize the zonal analysis.

Parameters:
  • landscape_filepath (str, file-like object or pathlib.Path object) – A string/file-like object/pathlib.Path object with the landscape data.

  • zones (geopandas.GeoSeries, geopandas.GeoDataFrame, list-like, str, file-like object, pathlib.Path object, numpy.ndarray, optional) –

    This can either be:

    • A geopandas geo-series or geo-data frame.

    • A list-like of shapely geometries, in the CRS of the landscape.

    • A filename or URL, a file-like object opened in binary (‘rb’) mode, or a Path object that will be passed to geopandas.read_file.

    • A numpy array of the same shape as the landscape raster image, where each zone is labelled by a unique integer value. The values will be used to identify the zones (i.e., as index) - unless a different zone_index is provided.

  • zone_index (list-like or str, optional) –

    Index used to identify zones (i.e., index of the metrics data frames). This can either be:

    • A list-like of index labels that will be positionally mapped to each zone.

    • A str with the name of a column only when zones is a geo-data frame or a geo-data frame file, e.g., a shapefile.

  • zone_nodata (numeric, optional, default 0) – Value of the zones array that corresponds to no data. Only considered if zones is a numpy array of integer types.

  • neighborhood_rule ({'8', '4'}, optional) – Neighborhood rule to determine patch adjacencies, i.e: ‘8’ (queen’s case/Moore neighborhood) or ‘4’ (rook’s case/Von Neumann neighborhood). Ignored if landscape is a Landscape instance. If no value is provided, the default value set in settings.DEFAULT_NEIGHBORHOOD_RULE will be taken.

compute_class_metrics_df(*, metrics=None, classes=None, metrics_kws=None, fillna=None)#

Compute the data frame of class-level metrics, which is multi-indexed by the class and attribute value.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed in the context of this analysis case.

  • classes (list-like, optional) – A list-like of ints or strings with the class values that should be considered in the context of this analysis case.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. The default empty dictionary will compute each metric according to FRAGSTATS defaults.

  • fillna (bool, optional) – Whether NaN values representing landscapes with no occurrences of patches of the provided class should be replaced by zero when appropriate, e.g., area and edge metrics (no ocurrences mean zero area/edge). If the provided value is None (default), the value will be taken from settings.CLASS_METRICS_DF_FILLNA.

Returns:

df – Dataframe with the values computed for each class, attribute value (multi-index) and metric (columns).

Return type:

pandas.DataFrame

compute_landscape_metrics_df(*, metrics=None, metrics_kws=None)#

Computes the data frame of landscape-level metrics, which is indexed by the attribute value.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed. If None, all the implemented landscape-level metrics will be computed.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. The default empty dictionary will compute each metric according to FRAGSTATS defaults.

Returns:

df – Dataframe with the values computed at the landscape level for each attribute value (index) and metric (columns).

Return type:

pandas.DataFrame

compute_zonal_statistics_gdf(metrics, *, class_val=None, metrics_kws=None)[source]#

Compute the zonal statistics geo-data frame over the landscape raster.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed. If None, all the implemented class-level metrics will be computed.

  • class_val (int, optional) – If provided, the zonal statistics will be computed at the level of the corresponding class, otherwise they will be computed at the landscape level.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. If None, each metric will be computed according to FRAGSTATS defaults.

Returns:

zonal_statistics_gdf – Geo-data frame with the computed zonal statistics.

Return type:

geopandas.GeoDataFrame

plot_landscapes(*, cmap=None, legend=True, subplots_kws=None, show_kws=None, subplots_adjust_kws=None)#

Plot each landscape snapshot in a dedicated matplotlib axis.

Uses the Landscape.plot_landscape method of each instance.

Parameters:
  • cmap (str or ~matplotlib.colors.Colormap, optional) – A Colormap instance.

  • legend (bool, optional) – If True, display the legend of the land use/cover color codes.

  • subplots_kws (dict, default None) – Keyword arguments to be passed to matplotlib.pyplot.subplots.

  • show_kws (dict, default None) – Keyword arguments to be passed to rasterio.plot.show.

  • subplots_adjust_kws (dict, default None) – Keyword arguments to be passed to matplotlib.pyplot.subplots_adjust.

Returns:

fig – The figure with its corresponding plots drawn into its axes.

Return type:

matplotlib.figure.Figure

plot_metric(metric, *, class_val=None, ax=None, metric_legend=True, metric_label=None, fmt='--o', plot_kws=None, subplots_kws=None, metric_kws=None)#

Plot the metric.

Parameters:
  • metric (str) – A string indicating the name of the metric to plot.

  • class_val (int, optional) – If provided, the metric will be plotted at the level of the corresponding class, otherwise it will be plotted at the landscape level.

  • ax (axis object, optional) – Plot in given axis; if None creates a new figure.

  • metric_legend (bool, default True) – Whether the metric label should be displayed within the plot (as label of the y-axis).

  • metric_label (str, optional) – Label of the y-axis to be displayed if metric_legend is True. If the provided value is None, the label will be taken from the settings module.

  • fmt (str, default '--o') – A format string for matplotlib.pyplot.plot.

  • plot_kws (dict, default None) – Keyword arguments to be passed to matplotlib.pyplot.plot.

  • subplots_kws (dict, default None) – Keyword arguments to be passed to matplotlib.pyplot.plot.subplots only if no axis is given (through the ax argument).

  • metric_kws (dict, default None) – Keyword arguments to be passed to the method that computes the metric (specified in the metric argument) for each landscape.

Returns:

ax – Returns the Axes object with the plot drawn onto it.

Return type:

matplotlib.axes.Axes

class pylandstats.BufferAnalysis(landscape_filepath, base_geom, buffer_dists, *, buffer_rings=False, base_geom_crs=None, neighborhood_rule=None)[source]#

Buffer analysis around a feature of interest.

__init__(landscape_filepath, base_geom, buffer_dists, *, buffer_rings=False, base_geom_crs=None, neighborhood_rule=None)[source]#

Initialize the buffer analysis.

Parameters:
  • landscape_filepath (str, file-like object or pathlib.Path object) – A string/file-like object/pathlib.Path object with the landscape data.

  • base_geom (shapely geometry, geopandas.GeoSeries or geopandas.GeoDataFrame) – Geometry that will serve as a base to buffer around.

  • buffer_dists (list-like) – Buffer distances, in units of the landscape CRS.

  • buffer_rings (bool, default Falsenn) – If False, each buffer zone will consist of the whole region that lies within the respective buffer distance around the base geometry. If True, buffer zones will take the form of rings around the base geometry.

  • base_geom_crs (str, dict or pyproj.CRS, optional) – The coordinate reference system of the base geometry. Required if the base geometry is a shapely geometry or a geopandas GeoSeries without the crs attribute set.

  • neighborhood_rule ({'8', '4'}, optional) – Neighborhood rule to determine patch adjacencies, i.e: ‘8’ (queen’s case/Moore neighborhood) or ‘4’ (rook’s case/Von Neumann neighborhood). Ignored if landscape is a Landscape instance. If no value is provided, the default value set in settings.DEFAULT_NEIGHBORHOOD_RULE will be taken.

compute_class_metrics_df(*, metrics=None, classes=None, metrics_kws=None, fillna=None)[source]#

Compute the data frame of class-level metrics, which is multi-indexed by the class and buffer distance.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed in the context of this analysis case.

  • classes (list-like, optional) – A list-like of ints or strings with the class values that should be considered in the context of this analysis case.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. The default empty dictionary will compute each metric according to FRAGSTATS defaults.

  • fillna (bool, optional) – Whether NaN values representing landscapes with no occurrences of patches of the provided class should be replaced by zero when appropriate, e.g., area and edge metrics (no ocurrences mean zero area/edge). If the provided value is None (default), the value will be taken from settings.CLASS_METRICS_DF_FILLNA.

Returns:

df – Dataframe with the values computed for each class, buffer distance (multi-index) and metric (columns).

Return type:

pandas.DataFrame

compute_landscape_metrics_df(*, metrics=None, metrics_kws=None)[source]#

Computes the data frame of landscape-level metrics, which is indexed by the buffer distance.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed. If None, all the implemented landscape-level metrics will be computed.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. The default empty dictionary will compute each metric according to FRAGSTATS defaults.

Returns:

df – Dataframe with the values computed at the landscape level for each buffer distance (index) and metric (columns).

Return type:

pandas.DataFrame

compute_zonal_statistics_gdf(metrics, *, class_val=None, metrics_kws=None)#

Compute the zonal statistics geo-data frame over the landscape raster.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed. If None, all the implemented class-level metrics will be computed.

  • class_val (int, optional) – If provided, the zonal statistics will be computed at the level of the corresponding class, otherwise they will be computed at the landscape level.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. If None, each metric will be computed according to FRAGSTATS defaults.

Returns:

zonal_statistics_gdf – Geo-data frame with the computed zonal statistics.

Return type:

geopandas.GeoDataFrame

plot_landscapes(*, cmap=None, legend=True, subplots_kws=None, show_kws=None, subplots_adjust_kws=None)#

Plot each landscape snapshot in a dedicated matplotlib axis.

Uses the Landscape.plot_landscape method of each instance.

Parameters:
  • cmap (str or ~matplotlib.colors.Colormap, optional) – A Colormap instance.

  • legend (bool, optional) – If True, display the legend of the land use/cover color codes.

  • subplots_kws (dict, default None) – Keyword arguments to be passed to matplotlib.pyplot.subplots.

  • show_kws (dict, default None) – Keyword arguments to be passed to rasterio.plot.show.

  • subplots_adjust_kws (dict, default None) – Keyword arguments to be passed to matplotlib.pyplot.subplots_adjust.

Returns:

fig – The figure with its corresponding plots drawn into its axes.

Return type:

matplotlib.figure.Figure

plot_metric(metric, *, class_val=None, ax=None, metric_legend=True, metric_label=None, fmt='--o', plot_kws=None, subplots_kws=None, metric_kws=None)#

Plot the metric.

Parameters:
  • metric (str) – A string indicating the name of the metric to plot.

  • class_val (int, optional) – If provided, the metric will be plotted at the level of the corresponding class, otherwise it will be plotted at the landscape level.

  • ax (axis object, optional) – Plot in given axis; if None creates a new figure.

  • metric_legend (bool, default True) – Whether the metric label should be displayed within the plot (as label of the y-axis).

  • metric_label (str, optional) – Label of the y-axis to be displayed if metric_legend is True. If the provided value is None, the label will be taken from the settings module.

  • fmt (str, default '--o') – A format string for matplotlib.pyplot.plot.

  • plot_kws (dict, default None) – Keyword arguments to be passed to matplotlib.pyplot.plot.

  • subplots_kws (dict, default None) – Keyword arguments to be passed to matplotlib.pyplot.plot.subplots only if no axis is given (through the ax argument).

  • metric_kws (dict, default None) – Keyword arguments to be passed to the method that computes the metric (specified in the metric argument) for each landscape.

Returns:

ax – Returns the Axes object with the plot drawn onto it.

Return type:

matplotlib.axes.Axes

class pylandstats.ZonalGridAnalysis(landscape_filepath, *, num_zone_cols=None, num_zone_rows=None, zone_width=None, zone_height=None, offset=None, neighborhood_rule=None)[source]#

Zonal analysis over a grid.

__init__(landscape_filepath, *, num_zone_cols=None, num_zone_rows=None, zone_width=None, zone_height=None, offset=None, neighborhood_rule=None)[source]#

Initialize the zonal grid analysis.

Parameters:
  • landscape_filepath (str, file-like object or pathlib.Path object) – A string/file-like object/pathlib.Path object with the landscape data.

  • num_zone_cols (int, optional) – The number of zone columns/rows into which the landscape will be separated. If the landscape dimensions and the desired zones do not divide evenly, the zones will be defined for the minimum superset that covers the landscape bounds. If not provided, then num_width/num_height must be provided.

  • num_zone_rows (int, optional) – The number of zone columns/rows into which the landscape will be separated. If the landscape dimensions and the desired zones do not divide evenly, the zones will be defined for the minimum superset that covers the landscape bounds. If not provided, then num_width/num_height must be provided.

  • zone_width (numeric, optional) – The width/height of each zone (in units of the landscape CRS). If the landscape dimensions and the desired zones do not divide evenly, the zones will be defined for the minimum superset that covers the landscape bounds. If not provided, then num_width/num_height must be provided.

  • zone_height (numeric, optional) – The width/height of each zone (in units of the landscape CRS). If the landscape dimensions and the desired zones do not divide evenly, the zones will be defined for the minimum superset that covers the landscape bounds. If not provided, then num_width/num_height must be provided.

  • offset (str, optional) – If set to “center”, the and the landscape dimensions and the desired zones do not divide evenly, the grid is offsetted so that the landscape bounds are in the center of the grid. Otherwise, the grid starts at the top-left corner of the landscape. Ignored if the landscape dimensions and the desired zones divide evenly.

  • neighborhood_rule ({'8', '4'}, optional) – Neighborhood rule to determine patch adjacencies, i.e: ‘8’ (queen’s case/Moore neighborhood) or ‘4’ (rook’s case/Von Neumann neighborhood). If no value is provided, the default value set in settings.DEFAULT_NEIGHBORHOOD_RULE will be taken.

compute_class_metrics_df(*, metrics=None, classes=None, metrics_kws=None, fillna=None)#

Compute the data frame of class-level metrics, which is multi-indexed by the class and attribute value.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed in the context of this analysis case.

  • classes (list-like, optional) – A list-like of ints or strings with the class values that should be considered in the context of this analysis case.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. The default empty dictionary will compute each metric according to FRAGSTATS defaults.

  • fillna (bool, optional) – Whether NaN values representing landscapes with no occurrences of patches of the provided class should be replaced by zero when appropriate, e.g., area and edge metrics (no ocurrences mean zero area/edge). If the provided value is None (default), the value will be taken from settings.CLASS_METRICS_DF_FILLNA.

Returns:

df – Dataframe with the values computed for each class, attribute value (multi-index) and metric (columns).

Return type:

pandas.DataFrame

compute_landscape_metrics_df(*, metrics=None, metrics_kws=None)#

Computes the data frame of landscape-level metrics, which is indexed by the attribute value.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed. If None, all the implemented landscape-level metrics will be computed.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. The default empty dictionary will compute each metric according to FRAGSTATS defaults.

Returns:

df – Dataframe with the values computed at the landscape level for each attribute value (index) and metric (columns).

Return type:

pandas.DataFrame

compute_zonal_statistics_gdf(metrics, *, class_val=None, metrics_kws=None)#

Compute the zonal statistics geo-data frame over the landscape raster.

Parameters:
  • metrics (list-like, optional) – A list-like of strings with the names of the metrics that should be computed. If None, all the implemented class-level metrics will be computed.

  • class_val (int, optional) – If provided, the zonal statistics will be computed at the level of the corresponding class, otherwise they will be computed at the landscape level.

  • metrics_kws (dict, optional) – Dictionary mapping the keyword arguments (values) that should be passed to each metric method (key), e.g., to exclude the boundary from the computation of total_edge, metric_kws should map the string ‘total_edge’ (method name) to {‘count_boundary’: False}. If None, each metric will be computed according to FRAGSTATS defaults.

Returns:

zonal_statistics_gdf – Geo-data frame with the computed zonal statistics.

Return type:

geopandas.GeoDataFrame

plot_landscapes(*, cmap=None, ax=None, figsize=None, **plot_kws)[source]#

Plot the spatial distribution of the landscape zones.

Parameters:
  • cmap (str or ~matplotlib.colors.Colormap, optional) – A Colormap instance.

  • ax (axis object, optional) – Plot in given axis; if None creates a new figure.

  • figsize (tuple of two numeric types, optional) – Size of the figure to create. Ignored if axis ax is provided.

  • **plot_kws (optional) – Keyword arguments to be passed to geopandas.GeoSeries.plot.

Returns:

ax – Returns the Axes object with the plot drawn onto it.

Return type:

matplotlib.axes.Axes