swisslandstats package

LandDataFrame class

class swisslandstats.LandDataFrame(data, index_column=None, x_column=None, y_column=None, crs=None, res=None, **df_init_kws)[source]

A LandDataFrame object is a pandas.DataFrame extended to deal with the land statistics files provided by the Swiss Federal Statistical Office (SFSO). Each row of a SLSDataFrame represents a pixel of a raster landscape, with the ‘x’ and ‘y’ that depict the centroid of the pixel, as well as a set of land use/land cover (LULC) information columns.

The default parameters are defined to work with SFSO data out-of-the-box, but they can be modified through the following keyword arguments:

Parameters:
  • data (ndarray (structured or homogeneous), Iterable, dict or DataFrame) – Data that will be passed to the initialization method of pd.DataFrame
  • index_column (str, optional) – Label of the index column. If None is provided, the value set in settings.DEFAULT_INDEX_COLUMN will be taken.
  • x_column (str, optional) – Label of the x-coordinates column. If None is provided, the value set in settings.DEFAULT_X_COLUMN will be taken.
  • y_column (str, optional) – Label of the y-coordinates column. If None is provided, the value set in settings.DEFAULT_Y_COLUMN will be taken.
  • crs (rasterio CRS, optional) – Coordinate reference system, as a rasterio CRS object. If None is provided, the value set in settings.DEFAULT_CRS will be taken.
  • res (tuple, optional) – The (x, y) resolution of the dataset. If None is provided, the value set in settings.DEFAULT_RES will be taken.
clip_by_geometry(geometry, geometry_crs=None)[source]

Clip a LandDataFrame by a geometry

Parameters:
  • geometry (shapely Polygon or MultiPolygon) – the geometry used to clip the dataframe
  • geometry_crs (dict, optional) – the starting coordinate reference system of the passed-in geometry. If not given, it will take the default crs from the settings.
Returns:

result

Return type:

LandDataFrame

clip_by_nominatim(query, which_result=1)[source]

Clip a LandDataFrame by a single place name query to Nominatim. See also the documentation for osmnx.gdf_from_place

Parameters:
  • query (string or dict) – query string or structured query dict to geocode/download
  • which_result (int) – max number of results to return and which to process upon receipt
Returns:

result

Return type:

LandDataFrame

get_geoseries()[source]

Get the geometry of the LandDataFrame as a geopandas GeoSeries

Returns:result
Return type:geopandas GeoSeries
merge(right, duplicate_columns=False, how='outer', left_index=True, right_index=True, **kwargs)[source]

Merges LandDataFrame objects The default parameter values will do an outer join using the indices of both dataframes as join keys, and will avoid duplicating columns. See also the documentation for pandas.merge

Parameters:
  • right (LandDataFrame) –
  • duplicate_columns (boolean, default True) –
  • how ({'left', 'right', 'outer', 'inner'}, default 'outer') – parameter passed to pandas.merge
  • left_index (boolean, default True) – parameter passed to pandas.merge
  • right_index (boolean, default True) – parameter passed to pandas.merge
  • **kwargs (additional keyord arguments passed to pandas.merge) –
Returns:

result

Return type:

LandDataFrame

plot(column, cmap=None, legend=False, figsize=None, ax=None, **show_kws)[source]

Plots a land statistics column with a categorical legend by means of rasterio.plot.show

Parameters:
  • column (str) – data column to display
  • cmap (str or ~matplotlib.colors.Colormap, optional) – A Colormap instance
  • legend (bool, optional) – If True, display the legend
  • figsize (tuple of two ints, optional) – Size of the figure to create.
  • ax (axis object, optional) – Plot in given axis; if None creates a new figure
  • **show_kws (optional) – Keyword arguments to be passed to rasterio.plot.show
Returns:

ax – axis with plot data

Return type:

matplotlib axis

to_geodataframe(drop_xy_columns=True)[source]

Transform the LandDataFrame to a geopandas GeoDataFrame with the points represented by the x and y columns as geometry

Parameters:drop_xy_columns (boolean, default True) – whether the LandDataFrame x and y columns should be deleted from the geopandas GeoDataFrame
Returns:result
Return type:geopandas GeoDataFrame
to_geotiff(fp, column, nodata=0, dtype='uint8')[source]

Export a LULC column to a GeoTIFF file

Parameters:
  • fp (str, file object or pathlib.Path object) – A filename or URL, a file object opened in binary (‘rb’) mode, or a Path object.
  • column (str) – name of the LULC column
  • nodata (numeric) – value to be assigned to pixels with no data
  • dtype (str or numpy dtype) – the data type
to_ndarray(column, nodata=0, dtype=<class 'numpy.uint8'>)[source]

Convert a LULC column to a numpy array

Parameters:
  • column (str) – name of the LULC column
  • nodata (numeric) – value to be assigned to pixels with no data
  • dtype (str or numpy dtype) – the data type
Returns:

lulc_arr – A LULC array

Return type:

np.ndarray

Other functions

swisslandstats.merge(left, right, duplicate_columns=False, how='outer', left_index=True, right_index=True, **kwargs)[source]

Merges LandDataFrame objects The default parameter values will do an outer join using the indices of both dataframes as join keys, and will avoid duplicating columns. See also the documentation for pandas.merge

Parameters:
  • left (LandDataFrame) –
  • right (LandDataFrame) –
  • duplicate_columns (boolean, default True) –
  • how ({'left', 'right', 'outer', 'inner'}, default 'outer') – parameter passed to pandas.merge
  • left_index (boolean, default True) – parameter passed to pandas.merge
  • right_index (boolean, default True) – parameter passed to pandas.merge
  • **kwargs (additional keyord arguments passed to pandas.merge) –
Returns:

result

Return type:

LandDataFrame

swisslandstats.read_csv(filepath_or_buffer, index_column=None, x_column=None, y_column=None, crs=None, res=None, read_csv_kws=None, df_init_kws=None)[source]

Convert a LULC column to a numpy array. See also the documentation for pandas.read_csv.

Parameters:
  • filepath_or_buffer (str, pathlib.Path, py._path.local.LocalPath or any object with a read() method (such as a file handle or StringIO)) – The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. For instance, a local file could be file://localhost/path/to/table.csv
  • index_column (str, optional) – Label of the index column. If None is provided, the value set in settings.DEFAULT_INDEX_COLUMN will be taken.
  • x_column (str, optional) – Label of the x-coordinates column. If None is provided, the value set in settings.DEFAULT_X_COLUMN will be taken.
  • y_column (str, optional) – Label of the y-coordinates column. If None is provided, the value set in settings.DEFAULT_Y_COLUMN will be taken.
  • crs (rasterio CRS, optional) – Coordinate reference system, as a rasterio CRS object. If None is provided, the value set in settings.DEFAULT_CRS will be taken.
  • res (tuple, optional) – The (x, y) resolution of the dataset. If None is provided, the value set in settings.DEFAULT_RES will be taken.
  • read_csv_kws (dict-like, optional) – Keyword arguments to be passed to pandas.read_csv
  • df_init_kws (dict-like, optional) – Keyword arguments to be passed to pandas.read_csv
Returns:

result

Return type:

LandDataFrame