Geomtry types

Check and set geometry type.

get_geom_type(gdf)[source]

Returns a string of the geometry type in a GeoDataFrame or GeoSeries.

Parameters:

gdf (GeoDataFrame | GeoSeries) – GeoDataFrame or GeoSeries

Return type:

str

Returns:

A string that is either “polygon”, “line”, “point”, or “mixed”.

Raises:

TypeError – If ‘gdf’ is not of type GeoDataFrame or GeoSeries.

Examples:

>>> from sgis import to_gdf, get_geom_type
>>> gdf = to_gdf([0, 0])
>>> gdf
                  geometry
0  POINT (0.00000 0.00000)
>>> get_geom_type(gdf)
'point'
is_single_geom_type(gdf)[source]

Returns True if all geometries in a GeoDataFrame are of the same type.

The types are either polygon, line or point. Multipart and singlepart are considered the same type.

Parameters:

gdf (GeoDataFrame | GeoSeries) – GeoDataFrame or GeoSeries

Return type:

bool

Returns:

True if all geometries are the same type, False if not.

Raises:

TypeError – If ‘gdf’ is not of type GeoDataFrame or GeoSeries.

Examples:

>>> from sgis import to_gdf, get_geom_type
>>> gdf = to_gdf([0, 0])
>>> gdf
                                            geometry
0                            POINT (0.00000 0.00000)
>>> is_single_geom_type(gdf)
True
make_all_singlepart(gdf, index_parts=False, ignore_index=False)[source]

Make all geometries single part.

This means doing either 0, 1 or 2 calls to the explode method.

Parameters:
  • gdf (GeoDataFrame | GeoSeries) – GeoDataFrame

  • index_parts (bool) – Defaults to False.

  • ignore_index (bool) – Defaults to False.

Return type:

GeoDataFrame | GeoSeries

Returns:

A GeoDataFrame of singlepart geometries.

to_single_geom_type(gdf, geom_type, ignore_index=False)[source]

Returns only the specified geometry type in a GeoDataFrame or GeoSeries.

GeometryCollections are first exploded, then only the rows with the given geometry_type is kept. Both multipart and singlepart geometries are kept. LinearRings are considered lines.

Parameters:
  • gdf (GeoDataFrame | GeoSeries | Geometry | GeometryArray | ndarray) – GeoDataFrame or GeoSeries

  • geom_type (str) – the geometry type to keep, either ‘point’, ‘line’ or ‘polygon’. Both multi- and singlepart geometries are included.

  • ignore_index (bool) – If True, the resulting axis will be labeled 0, 1, …, n - 1. Defaults to False.

Return type:

GeoDataFrame | GeoSeries

Returns:

A GeoDataFrame with a single geometry type.

Raises:
  • TypeError – If incorrect gdf type.

  • ValueError – If ‘geom_type’ is neither ‘polygon’, ‘line’ or ‘point’.

Examples:

First create a GeoDataFrame of mixed geometries.

>>> from sgis import to_gdf, to_single_geom_type
>>> from shapely.geometry import LineString, Polygon
>>> gdf = to_gdf([
...     (0, 0),
...     LineString([(1, 1), (2, 2)]),
...     Polygon([(3, 3), (4, 4), (3, 4), (3, 3)])
...     ])
>>> gdf
                                            geometry
0                            POINT (0.00000 0.00000)
1      LINESTRING (1.00000 1.00000, 2.00000 2.00000)
2  POLYGON ((3.00000 3.00000, 4.00000 4.00000, 3....
>>> to_single_geom_type(gdf, "line")
                                        geometry
1  LINESTRING (1.00000 1.00000, 2.00000 2.00000)
>>> to_single_geom_type(gdf, "polygon")
                                            geometry
2  POLYGON ((3.00000 3.00000, 4.00000 4.00000, 3....

Also keeps multigeometries and geometries within GeometryCollections.

>>> gdf = gdf.dissolve()
>>> gdf
                                            geometry
0  GEOMETRYCOLLECTION (POINT (0.00000 0.00000), L...
>>> to_single_geom_type(gdf, "line")
                                        geometry
2  LINESTRING (1.00000 1.00000, 2.00000 2.00000)