shapelib.utils package#

Submodules#

shapelib.utils.line_utils module#

shapelib.utils.line_utils.get_perpendicular_lines(line, perpendicular_line_length=0.5, distance_between_points=0.02, distance_between_neighbors=0.3)[source]#

This function returns a list of perpendicular lines to the input line.

Args:#

line: LineString

The input line given by the user.

perpendicular_line_length: float

The length of the perpendicular lines.

distance_between_points: float

The distance between the points of the given line to be used as reference to create the perpendicular lines.

distance_between_neighbors: float

The distance between the current point and its neighbors to be used as reference to create the perpendicular lines.

Return type:

list[LineString]

Parameters:

shapelib.utils.utils module#

shapelib.utils.utils.multiline_to_single_line(geometry)[source]#

Flatten a MultiLineString into a single LineString.

The vertices of every part are concatenated, in part order, into one continuous LineString. If the geometry is already a LineString it is returned unchanged.

Parameters:

geometry (LineString | MultiLineString) – The geometry to flatten.

Returns:

A single LineString built from all the input vertices.

Return type:

LineString

Examples

>>> from shapely.geometry import MultiLineString
>>> mls = MultiLineString([[(0, 0), (1, 0)], [(1, 0), (2, 0)]])
>>> multiline_to_single_line(mls).wkt
'LINESTRING (0 0, 1 0, 1 0, 2 0)'
shapelib.utils.utils.get_most_common_geometry_type(gdf)[source]#

Identify the most common geometry type in the given shapefile or GeoDataFrame.

This function analyzes the geometries in a GeoDataFrame or a shapefile and identifies the most frequent geometry type (e.g., “Point”, “LineString”, “Polygon”). It ignores invalid or missing geometries during the analysis.

Parameters:

gdf (str | Path | GeoDataFrame) – The path to a shapefile (as a string or Path) or an existing GeoDataFrame. If a file path is provided, it will be loaded as a GeoDataFrame.

Returns:

The most common geometry type in the input GeoDataFrame or shapefile. If no valid geometries are found or the input is invalid, it returns None.

Return type:

str | None

Examples

>>> gdf = gpd.read_file("/path/to/shapefile.shp")
>>> most_common_geom = get_most_common_geometry_type(gdf)
>>> print(most_common_geom)
'Polygon'

Notes

The function only processes valid geometries and skips invalid or missing entries. It uses the geom_type property of GeoDataFrame geometries to identify the type.

shapelib.utils.utils.assume_wgs84_if_lonlat(gdf, source)[source]#

Assign WGS84 (EPSG:4326) to data read without a CRS when its coordinates are lon/lat.

A file can come without CRS information — a shapefile whose .prj is missing or empty, a FlatGeobuf or GeoPackage written without one. When every coordinate fits the lon/lat range (x in [-180, 180], y in [-90, 90]) the data is taken as WGS84 and a warning is logged; otherwise it cannot be placed on the map and a ValueError is raised. Data with no coordinates at all (no non-empty geometry) is taken as WGS84 too, since there is nothing to misplace.

This is a reading policy: it belongs to the file handlers, not to BaseShapes, which keeps rejecting in-memory data built without a CRS.

Parameters:
  • gdf (GeoDataFrame) – The data as read from the file.

  • source (str) – The file the data was read from, used in the log and error messages.

Returns:

gdf unchanged if it already has a CRS (or no active geometry column), otherwise a copy with EPSG:4326 assigned.

Return type:

GeoDataFrame

Raises:

ValueError – If the data has no CRS and its coordinates do not fit the lon/lat range.

Examples

>>> gdf = gpd.read_file("prescription.zip")  # shapefile without .prj
>>> gdf.crs is None
True
>>> assume_wgs84_if_lonlat(gdf, "prescription.zip").crs.to_epsg()
4326