pygeoutils.geotools
#
Some utilities for manipulating GeoSpatial data.
Module Contents#
- class pygeoutils.geotools.Coordinates#
Generate validated and normalized coordinates in WGS84.
- Parameters:
Examples
>>> from pygeoutils import Coordinates >>> c = Coordinates([460, 20, -30], [80, 200, 10]) >>> c.points.x.tolist() [100.0, -30.0]
- property points: geopandas.GeoSeries#
Get validate coordinate as a
geopandas.GeoSeries
.
- class pygeoutils.geotools.GeoBSpline(points, npts_sp, degree=3)#
Create B-spline from a geo-dataframe of points.
- Parameters:
points (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – Input points as aGeoDataFrame
orGeoSeries
in a projected CRS.npts_sp (
int
) – Number of points in the output spline curve.degree (
int
, optional) – Degree of the spline. Should be less than the number of points and greater than 1. Default is 3.
Examples
>>> from pygeoutils import GeoBSpline >>> import geopandas as gpd >>> xl, yl = zip( ... *[ ... (-97.06138, 32.837), ... (-97.06133, 32.836), ... (-97.06124, 32.834), ... (-97.06127, 32.832), ... ] ... ) >>> pts = gpd.GeoSeries(gpd.points_from_xy(xl, yl, crs=4326)) >>> sp = GeoBSpline(pts.to_crs("epsg:3857"), 5).spline >>> pts_sp = gpd.GeoSeries(gpd.points_from_xy(sp.x, sp.y, crs="epsg:3857")) >>> pts_sp = pts_sp.to_crs(4326) >>> list(zip(pts_sp.x, pts_sp.y)) [(-97.06138, 32.837), (-97.06135, 32.83629), (-97.06131, 32.83538), (-97.06128, 32.83434), (-97.06127, 32.83319)]
- property spline: Spline#
Get the spline as a
Spline
object.
- pygeoutils.geotools.break_lines(lines, points, tol=0.0)#
Break lines at specified points at given direction.
- Parameters:
lines (
geopandas.GeoDataFrame
) – Lines to break at intersection points.points (
geopandas.GeoDataFrame
) – Points to break lines at. It must contain a column nameddirection
with valuesup
ordown
. This column is used to determine which part of the lines to keep, i.e., upstream or downstream of points.tol (
float
, optional) – Tolerance for snapping points to the nearest lines in meters. The default is 0.0.
- Returns:
geopandas.GeoDataFrame
– Original lines except for the parts that have been broken at the specified points.
- pygeoutils.geotools.coords_list(coords)#
Convert a single coordinate or list of coordinates to a list of coordinates.
- pygeoutils.geotools.geo2polygon(geometry, geo_crs=None, crs=None)#
Convert a geometry to a Shapely’s Polygon and transform to any CRS.
- Parameters:
- Returns:
shapely.Polygon
orshapely.MultiPolygon
– A (Multi)Polygon in the target CRS, if different from the input CRS.
- pygeoutils.geotools.geometry_list(geometry)#
Get a list of polygons, points, and lines from a geometry.
- pygeoutils.geotools.multi2poly(gdf)#
Convert multipolygons to polygon and fill holes, if any.
Notes
This function tries to convert multipolygons to polygons by first checking if multiploygons can be directly converted using their exterior boundaries. If not, will try to remove those small sub-polygons that their area is less than 1% of the total area of the multipolygon. If this fails, the original multipolygon will be returned.
- Parameters:
gdf (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – A GeoDataFrame or GeoSeries with (multi)polygons in a projected coordinate system.- Returns:
geopandas.GeoDataFrame
orgeopandas.GeoSeries
– A GeoDataFrame or GeoSeries with polygons.
- pygeoutils.geotools.nested_polygons(gdf)#
Get nested polygons in a GeoDataFrame.
- Parameters:
gdf (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – A GeoDataFrame or GeoSeries with (multi)polygons.- Returns:
dict
– A dictionary where keys are indices of larger ploygons and values are a list of indices of smaller polygons that are contained within the larger polygons.
- pygeoutils.geotools.query_indices(tree_gdf, input_gdf, predicate='intersects')#
Find the indices of the input_geo that intersect with the tree_geo.
- Parameters:
tree_gdf (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – The tree geodataframe.input_gdf (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – The input geodataframe.predicate (
str
, optional) – The predicate to use for the query operation, defaults tointesects
.
- Returns:
dict
– A dictionary of the indices of theinput_gdf
that intersect with thetree_gdf
. Keys are the index ofinput_gdf
and values are a list of indices of the intersectingtree_gdf
.
- pygeoutils.geotools.snap2nearest(lines, points, tol)#
Find the nearest points on a line to a set of points.
- Parameters:
lines (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – Lines.points (
geopandas.GeoDataFrame
orgeopandas.GeoSeries
) – Points to snap to lines.tol (
float
, optional) – Tolerance for snapping points to the nearest lines in meters. It must be greater than 0.0.
- Returns:
geopandas.GeoDataFrame
orgeopandas.GeoSeries
– Points snapped to lines.