environmentaltools.spatial.create_polygon
- environmentaltools.spatial.create_polygon(data, crs='epsg:25830', sides=[])[source]
Create a closed polygon from coordinates and optional additional sides.
Constructs a polygon geometry from a primary set of coordinates, optionally adding additional side segments. Useful for creating polygons from multiple line segments or closing open geometries.
- Parameters:
data (pd.DataFrame) – Primary polygon coordinates containing columns ‘x’ and ‘y’. These points define the main boundary of the polygon.
crs (str, optional) – Coordinate reference system in EPSG format (e.g., ‘epsg:25830’ for ETRS89 UTM Zone 30N, ‘epsg:4326’ for WGS84). Default is ‘epsg:25830’.
sides (list of pd.DataFrame or pd.DataFrame, optional) – Additional side segment(s) to append to the polygon boundary. Each DataFrame should contain ‘x’ and ‘y’ coordinate columns. Coordinates are appended in the order provided. Default is empty list.
- Returns:
GeoDataFrame containing single polygon geometry with specified CRS. Has one row with the polygon geometry.
- Return type:
gpd.GeoDataFrame
Notes
Coordinates are concatenated in order: data, then each side in sides list
The shapely Polygon constructor automatically closes the polygon
Ensure coordinate order forms a valid polygon (no self-intersections)
All input DataFrames should use the same coordinate system
Examples
>>> import pandas as pd >>> import numpy as np >>> # Define main boundary >>> data = pd.DataFrame({'x': [0, 10, 10, 0], 'y': [0, 0, 10, 10]}) >>> # Create closed polygon >>> poly = create_polygon(data, crs='epsg:4326') >>> print(poly.geometry[0]) POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))
>>> # Add additional side >>> side = pd.DataFrame({'x': [10, 5], 'y': [10, 15]}) >>> poly = create_polygon(data, crs='epsg:4326', sides=[side])