swisslandstats package#
LandDataFrame class#
- class swisslandstats.LandDataFrame(data, *, index_column=None, x_column=None, y_column=None, crs=None, res=None, **df_init_kwargs)[source]#
Land data frame.
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 LandDataFrame 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 data columns.
- 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:
- 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, default 1) – max number of results to return and which to process upon receipt
- Returns:
result
- Return type:
- get_geoseries()[source]#
Get the geometry of the LandDataFrame as a geopandas GeoSeries
- Returns:
result
- Return type:
geopandas GeoSeries
- get_transform()[source]#
Get the affine transform of the current land data frame.
- Returns:
transform
- Return type:
Affine
- 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 False) –
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 kewyord arguments passed to pandas.merge) –
- Returns:
result
- Return type:
- plot(column, *, cmap=None, legend=False, figsize=None, ax=None, **show_kwargs)[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, default False) – If
True, display the legendfigsize (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_kwargs (dict-like, 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 data 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 data column.
nodata (numeric, default 0) – value to be assigned to pixels with no data.
dtype (str or numpy dtype, default) – the data type.
- to_ndarray(column, *, nodata=0, dtype='uint8')[source]#
Convert a data column to a numpy array.
- Parameters:
column (str) – name of the data column.
nodata (numeric, default 0) – value to be assigned to pixels with no data.
dtype (str or numpy dtype, default uint8) – the data type.
- Returns:
arr – A raster array.
- Return type:
np.ndarray
- to_xarray(columns, *, dim_name='time', nodata=0, dtype='uint8')[source]#
Convert a data column to a xarray data array.
- Parameters:
columns (str or list of str) – name or names of the data columns.
dim_name (str, default time) – name of the outermost dimension set by the columns argument.
nodata (numeric, default 0) – value to be assigned to pixels with no data.
dtype (str or numpy dtype, default uint8) – the data type.
- Returns:
da – A xarray data array.
- Return type:
xr.DataArray
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 False) –
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 kewyord arguments passed to pandas.merge) –
- Returns:
result
- Return type:
- swisslandstats.read_csv(filepath_or_buffer, *, index_column=None, x_column=None, y_column=None, columns=None, sep=None, crs=None, res=None, read_csv_kwargs=None, df_init_kwargs=None)[source]#
Read a CSV file into a LandDataFrame.
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.
columns (list of str, optional) – List of column names to use. If None is provided, all columns will be read.
sep (str, optional) – Delimiter to use. If None is provided, the value set in settings.DEFAULT_SEP 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_kwargs (dict-like, optional) – Keyword arguments to be passed to pandas.read_csv, except sep.
df_init_kwargs (dict-like, optional) – Keyword arguments to be passed to LandDataFrame.__init__
- Returns:
result
- Return type: