windkit.polygons_to_lines

windkit.polygons_to_lines(gdf, lctable=None, map_type='roughness', return_lctable=False, external_roughness=None, check_errors=True, snap=True)[source]

Convert a GeoDataFrame of polygons into line segments.

This function processes a GeoDataFrame containing polygon geometries and converts them into line segments. It ensures that the input data is valid, optionally snaps geometries to align vertices, and creates a landcover table if not provided.

Parameters:
  • gdf (geopandas.GeoDataFrame) – A GeoDataFrame containing polygon geometries. Must have at least a ‘z0’ column. If a landcover table is not provided, the GeoDataFrame can also contain a ‘d’ column for displacement height and a ‘desc’ column for descriptions of the landcover.

  • lctable (windkit.LandCoverTable or None, optional) – A landcover table specifying the mapping of landcover classes to roughness and displacement height. If None, a new landcover table is created from the unique (‘z0’, ‘d’) pairs in the GeoDataFrame.

  • map_type ({"roughness","landcover"}) – Whether the output is a geopandas dataframe with roughness (z0) or id change lines, default “roughness”.

  • return_lctable (bool) – Whether to return the landcover table, default False

  • external_roughness (float or None, optional) – The roughness value to assign to areas outside the polygons. If None, segments bordering unspecified areas are removed.

  • check_errors (bool, optional) – Whether to check for errors in the map. If True, the following checks are performed: 1) No polygons are allowed to overlap. 2) If ‘external_roughness’ is None, the bounding box of the polygons must not contain holes. 3) If ‘external_roughness’ is a float, a warning is issued if there are holes in the polygons.

  • snap (bool, optional) – If True, inserts extra vertices to align polygons that touch but do not share vertices. Default is True.

Returns:

  • gdf (geopandas.GeoDataFrame) – A GeoDataFrame with LineString geometries and columns ‘id_left’ and ‘id_right’.

  • lctable (windkit.LandCoverTable) – A landcover table containing the mapping of ‘id_left’ and ‘id_right’ to roughness length (‘z0’), displacement height (‘d’), and a description (‘desc’).

Raises:
  • ValueError – If no line segments remain after processing or if the GeoDataFrame contains invalid geometries.

  • KeyError – If there are duplicate (‘z0’, ‘d’) pairs with different IDs in the landcover table.

Notes

  • This function is heavily inspired by the code available in the QGIS plugin where a similar conversion is performed.

  • If ‘external_roughness’ is specified, an additional landcover class is added to the landcover table for areas outside the polygons.

  • Identical functionality written in Fortran is available in the WAsP core at Rvea0287/poly2lines.f90, which is faster than this implementation.