polars_shapely_function#

add_buffer(geo_str: Expr, buffer_size: float) Expr[source]#

Add a buffer to geometries in a Polars expression.

Parameters:
  • geo_str (pl.Expr) – The Polars expression containing geometries in WKT format.

  • buffer_size (float) – The buffer size.

Returns:

A Polars expression with buffered geometries in WKT format.

Return type:

pl.Expr

calculate_line_length(line_str: Expr) Expr[source]#

Calculate the length of LineString geometries in a Polars expression.

Parameters:

line_str (pl.Expr) – The Polars expression containing LineString geometries in WKT format.

Returns:

A Polars expression with the lengths of the LineString geometries.

Return type:

pl.Expr

combine_shape(geometry_list_str: Expr) Expr[source]#

Combine multiple geometries into a single geometry in a Polars expression.

Parameters:

geometry_list_str (pl.Expr) – The Polars expression containing geometry lists in WKT format.

Returns:

A Polars expression with combined geometries in WKT format.

Return type:

pl.Expr

generate_linestring_from_coordinates_list(coord_list: Expr) Expr[source]#

Generate LineString geometries from coordinate lists in a Polars expression.

Parameters:

coord_list (pl.Expr) – The Polars expression containing coordinate lists.

Returns:

A Polars expression with LineString geometries.

Return type:

pl.Expr

generate_linestring_from_nearest_points_col(point: Expr, multi_point: MultiPoint)[source]#

Generate a LineString geometry from the nearest points within a specified distance in a column containing geometric data.

Parameters:
  • point (pl.Expr) – The expression representing the column containing the reference points.

  • multi_point (MultiPoint) – The MultiPoint geometry containing the points to search.

Returns:

An expression with LineString geometries created from the nearest points.

Return type:

pl.Expr

Example:

>>> data = {'geometry': [Point(0, 0).wkt, Point(2, 2).wkt]}
... df = pl.DataFrame(data)
... multi_point = MultiPoint([Point(0, 1), Point(1, 1), Point(2, 1)])
... df.with_column(
...     c(geometry).pipe(generate_linestring_from_nearest_points_col, multi_point=multi_point)
...     .alias('linestring')
... )
shape: (3, 2)
┌──────────────────┬─────────────────────────┐
│ geometry         ┆ linestring              │
│ ---              ┆ ---                     │
│ str              ┆ str                     │
╞══════════════════╪═════════════════════════╡
│ POINT (0 0)      ┆ LINESTRING (0 0, 0 1)   │
│ POINT (2 2)      ┆ LINESTRING (2 2, 2 1)   │
└──────────────────┴─────────────────────────┘
generate_point_from_coordinates(x: Expr, y: Expr) Expr[source]#

Generate Point geometries from x and y coordinates in Polars expressions.

Parameters:
  • x (pl.Expr) – The Polars expression containing x coordinates.

  • y (pl.Expr) – The Polars expression containing y coordinates.

Returns:

A Polars expression with Point geometries in WKT format.

Return type:

pl.Expr

geoalchemy2_to_shape_col(geo_str: Expr) Expr[source]#

Convert GeoAlchemy2 strings in a Polars expression to geometries.

Parameters:

geo_str (pl.Expr) – The Polars expression containing GeoAlchemy2 strings.

Returns:

A Polars expression with geometries.

Return type:

pl.Expr

geoalchemy2_to_wkt_col(geo_str: Expr, srid_from: int | None, srid_to: int | None) Expr[source]#

Convert GeoAlchemy2 strings in a Polars expression to WKT format.

Parameters:
  • geo_str (pl.Expr) – The Polars expression containing GeoAlchemy2 strings.

  • srid_from (Optional[int]) – The source spatial reference system identifier.

  • srid_to (Optional[int]) – The target spatial reference system identifier.

Returns:

A Polars expression with geometries in WKT format.

Return type:

pl.Expr

Raises:

ValueError – If only one of srid_from or srid_to is provided.

geojson_to_wkt_col(geometry: Expr) Expr[source]#

Convert GeoJSON strings in a Polars expression to WKT format.

Parameters:

geometry (pl.Expr) – The Polars expression containing GeoJSON strings.

Returns:

A Polars expression with geometries in WKT format.

Return type:

pl.Expr

get_coordinates_col(col: Expr) Expr[source]#

Add a new column to the DataFrame with coordinates extracted from a specified column containing geometric data.

Parameters:

col (pl.Expr) – The column containing geometric data.

Returns:

The DataFrame with an additional column of coordinates.

Return type:

polars.DataFrame

get_coordinates_list_from_col(df: DataFrame, col_name: str = 'geometry') list[tuple[float, float]][source]#

Extract a list of coordinates from a specified column containing geometric data.

Parameters:
  • df (pl.DataFrame) – The DataFrame to process.

  • col_name (str) – The name of the column containing geometric data.

Returns:

A list of coordinates extracted from the specified column.

Return type:

list

get_geometry_list(df: DataFrame, col_name: str = 'geometry') list[Point | LineString | Polygon][source]#

Get a list of geometries from a Polars DataFrame.

Parameters:
  • df (pl.DataFrame) – The Polars DataFrame.

  • col_name (str, optional) – The name of the geometry column. Defaults to “geometry”.

Returns:

The list of geometries.

Return type:

list[Union[Point, LineString, Polygon]]

get_linestring_boundaries_col(line_str: Expr) Expr[source]#

Get the boundary nodes of geometries in a Polars expression.

Parameters:

line_str (pl.Expr) – The Polars expression containing linestring in WKT format.

Returns:

A Polars expression with lists of boundary nodes in WKT format.

Return type:

pl.Expr

get_linestring_from_point_list(point_list_str: Expr) Expr[source]#

Generate LineString geometries from point lists in a Polars expression.

Parameters:

point_list_str (pl.Expr) – The Polars expression containing point lists in WKT format.

Returns:

A Polars expression with LineString geometries in WKT format.

Return type:

pl.Expr

get_multigeometry_from_col(df: DataFrame, col_name: str = 'geometry') MultiPoint | MultiLineString | MultiPolygon[source]#

Get a MultiGeometry object from a Polars DataFrame column.

Parameters:
  • df (pl.DataFrame) – The Polars DataFrame.

  • col_name (str, optional) – The name of the geometry column. Defaults to “geometry”.

Returns:

The MultiGeometry object.

Return type:

Union[MultiPoint, MultiLineString, MultiPolygon]

get_multilinestring_from_wkt_list_col(linestring_list: Expr) Expr[source]#

Convert a column containing WKT representations of lines into a MultiLineString geometry.

Parameters:

linestring_list (pl.Expr) – The expression representing the column containing list of linestring.

Returns:

An expression with MultiLineString geometries.

Return type:

pl.Expr

get_multipoint_from_wkt_list_col(point_list: Expr) Expr[source]#

Convert a column containing WKT representations of lines into a Multipoint geometry.

Parameters:

point_list (pl.Expr) – The expression representing the column containing list of Point.

Returns:

An expression with MultiPoint geometries.

Return type:

pl.Expr

get_nearest_point_within_distance_col(point: Expr, point_list: MultiPoint, min_distance: float) Expr[source]#

Find the nearest point within a specified minimum distance from each point in a column containing geometric data.

Parameters:
  • point (pl.Expr) – The expression representing the column containing the reference points.

  • point_list (MultiPoint) – The MultiPoint geometry containing the points to search.

  • min_distance (float) – The minimum distance to search for the nearest point.

Returns:

An expression with the nearest points within the specified minimum distance.

Return type:

pl.Expr

Examples:#

>>> data = {'geometry': [Point(0, 0).wkt, Point(2, 2).wkt, Point(4, 4).wkt]}
... df = pl.DataFrame(data)
... multi_point = MultiPoint([Point(1, 1), Point(10, 1), Point(2, 1)])
... df.with_column(get_nearest_point_within_distance_col(pl.col('geometry'), multi_point, 2).alias('nearest_point'))
shape: (3, 2)
┌─────────────────┬──────────────────┐
│ geometry        ┆ nearest_point    │
│ ---             ┆ ---              │
│ str             ┆ str              │
╞═════════════════╪══════════════════╡
│ POINT (0 0)     ┆ POINT (1 1)      │
│ POINT (2 2)     ┆ POINT (2 1)      │
│ POINT (4 4)     ┆ None             │
└─────────────────┴──────────────────┘
get_point_list_centroid_col(point_list: Expr) Expr[source]#

Calculate the centroid of a list of points from a specified column containing geometric data.

Parameters:

point_list (pl.Expr) – The expression representing the column containing the list of points.

Returns:

An expression with the centroid points.

Return type:

pl.Expr

Example:

>>> data = {'points': [Point(0, 0).wkt, Point(1, 1).wkt, Point(2, 2).wkt]}
... df = pl.DataFrame(data)
... df.with_column(get_point_list_centroid_col(pl.col('points')).alias('centroid'))
shape: (1, 1)
┌────────────────┐
│ centroid       │
│ ---            │
│ str            │
╞════════════════╡
│ POINT (1 1)    │
└────────────────┘
linestring_is_ring_col(linestring: Expr) Expr[source]#

Check if the LineString geometries in a specified column form a closed ring.

Parameters:

linestring (pl.Expr) – The expression representing the column containing LineString geometries.

Returns:

An expression indicating whether each LineString is a closed ring.

Return type:

pl.Expr

Example:

>>> data = {
...     'geometry': [
...         LineString([(0, 0), (1, 1), (1, 0), (0, 0)]).wkt,
...         LineString([(0, 0), (1, 1), (1, 0)]).wkt
...     ]}
... df = pl.DataFrame(data)
... df.with_column(linestring_is_ring_col(pl.col('geometry')).alias('is_ring'))
shape: (2, 2)
┌───────────────────────────────────────┬──────────┐
│ geometry                              ┆ is_ring  │
│ ---                                   ┆ ---      │
│ str                                   ┆ bool     │
╞═══════════════════════════════════════╪══════════╡
│ LINESTRING (0 0, 1 1, 1 0, 0 0)       ┆ true     │
│ LINESTRING (0 0, 1 1, 1 0)            ┆ false    │
└───────────────────────────────────────┴──────────┘
move_geometry_col(geometry: Expr, angle: Expr, distance: Expr) Expr[source]#
shape_coordinate_transformer_col(shape_col: Expr, srid_from: int, srid_to: int) Expr[source]#

Transform the coordinates of geometries in a Polars expression from one CRS to another.

Parameters:
  • shape_col (pl.Expr) – The Polars expression containing geometries.

  • srid_from (int) – The source spatial reference system identifier.

  • srid_to (int) – The target spatial reference system identifier.

Returns:

A Polars expression with transformed geometries.

Return type:

pl.Expr

shape_intersect_polygon(geo_str: Expr, polygon: Polygon) Expr[source]#

Check if geometries in a Polars expression intersect with a given polygon.

Parameters:
  • geo_str (pl.Expr) – The Polars expression containing geometries in WKT format.

  • polygon (Polygon) – The polygon to check for intersection.

Returns:

A Polars expression with boolean values indicating intersection.

Return type:

pl.Expr

shape_intersect_shape_col(geo_str: Expr, geometry: Geometry) Expr[source]#

Check if geometries in a Polars expression intersect with a given polygon.

Parameters:
  • geo_str (pl.Expr) – The Polars expression containing geometries in WKT format.

  • polygon (Polygon) – The polygon to check for intersection.

Returns:

A Polars expression with boolean values indicating intersection.

Return type:

pl.Expr

shape_to_geoalchemy2_col(geo: Expr) Expr[source]#

Convert geometries in a Polars expression to GeoAlchemy2 format.

Parameters:

geo (pl.Expr) – The Polars expression containing geometries.

Returns:

A Polars expression with geometries in GeoAlchemy2 format.

Return type:

pl.Expr

shape_to_wkt_col(geometry: Expr) Expr[source]#

Convert geometries in a Polars expression to WKT format.

Parameters:

geometry (pl.Expr) – The Polars expression containing geometries.

Returns:

A Polars expression with geometries in WKT format.

Return type:

pl.Expr

wkt_to_geoalchemy_col(geo_str: Expr, srid_from: int | None, srid_to: int | None) Expr[source]#

Convert WKT strings in a Polars expression to GeoAlchemy2 format.

Parameters:
  • geo_str (pl.Expr) – The Polars expression containing WKT strings.

  • srid_from (Optional[int]) – The source spatial reference system identifier.

  • srid_to (Optional[int]) – The target spatial reference system identifier.

Returns:

A Polars expression with geometries in GeoAlchemy2 format.

Return type:

pl.Expr

Raises:

ValueError – If only one of srid_from or srid_to is provided.

wkt_to_shape_col(geometry: Expr) Expr[source]#

Convert WKT strings in a Polars expression to geometries.

Parameters:

geometry (pl.Expr) – The Polars expression containing WKT strings.

Returns:

A Polars expression with geometries.

Return type:

pl.Expr