geoutils.geovector

geoutils.vectortools provides a toolset for working with vector data.

Functions

extract_vertices

geoutils.geovector.extract_vertices(gdf)[source]

Function to extract the exterior vertices of all shapes within a gpd.GeoDataFrame.

param gdf

The GeoDataFrame from which the vertices need to be extracted.

returns

A list containing a list of (x, y) positions of the vertices. The length of the primary list is equal

to the number of geometries inside gdf, and length of each sublist is the number of vertices in the geometry.

generate_voronoi_polygons

geoutils.geovector.generate_voronoi_polygons(gdf)[source]

Generate Voronoi polygons (tessellation) from the vertices of all geometries in a GeoDataFrame.

Uses scipy.spatial.voronoi.

Param

The GeoDataFrame from whose vertices are used for the Voronoi polygons.

Return type

GeoDataFrame

Returns

A GeoDataFrame containing the Voronoi polygons.

generate_voronoi_with_bounds

geoutils.geovector.generate_voronoi_with_bounds(gdf, bound_poly)[source]

Generate Voronoi polygons that are bounded by the polygon bound_poly, to avoid Voronoi polygons that extend far beyond the original geometry.

Voronoi polygons are created using generate_voronoi_polygons, cropped to the extent of bound_poly and gaps are filled with new polygons.

Param

The GeoDataFrame from whose vertices are used for the Voronoi polygons.

Param

A shapely Polygon to be used for bounding the Voronoi diagrams.

Return type

GeoDataFrame

Returns

A GeoDataFrame containing the Voronoi polygons.

Classes

Vector

class geoutils.geovector.Vector(filename)[source]

Bases: object

Create a Vector object from a fiona-supported vector dataset.

__init__(filename)[source]

Load a fiona-supported dataset, given a filename.

Parameters

filename – The filename or GeoDataFrame of the dataset.

Returns

A Vector object

property bounds: rasterio.coords.BoundingBox

Get a bounding box of the total bounds of the Vector.

Return type

BoundingBox

buffer_without_overlap(buffer_size, plot=False)[source]

Returns a Vector object containing self’s geometries extended by a buffer, without overlapping each other.

The algorithm is based upon this tutorial: https://statnmap.com/2020-07-31-buffer-area-for-nearest-neighbour/. The buffered polygons are created using Voronoi polygons in order to delineate the “area of influence” of each geometry. The buffer is slightly inaccurate where two geometries touch, due to the nature of the Voronoi polygons,hence one geometry “steps” slightly on the neighbor buffer in some cases. The algorithm may also yield unexpected results on very simple geometries.

Note: A similar functionality is provided by momepy (http://docs.momepy.org) and is probably more robust. It could be implemented in GeoPandas in the future: https://github.com/geopandas/geopandas/issues/2015

Examples

>>> outlines = gu.Vector(gu.datasets.get_path('glacier_outlines'))
>>> outlines = gu.Vector(outlines.ds.to_crs('EPSG:32645'))
>>> buffer = outlines.buffer_without_overlap(500)
>>> ax = buffer.ds.plot()
>>> outlines.ds.plot(ax=ax, ec='k', fc='none')
>>> plt.show()
Parameters
  • buffer_size – Buffer size in self’s coordinate system units.

  • plot – Set to True to show intermediate plots, useful for understanding or debugging.

Returns

A Vector containing the buffered geometries.

copy()[source]

Return a copy of the Vector.

Return type

~VectorType

create_mask(rst=None, crs=None, xres=None, yres=None, bounds=None, buffer=0)[source]

Rasterize the vector features into a boolean raster which has the extent/dimensions of the provided raster file.

Alternatively, user can specify a grid to rasterize on using xres, yres, bounds and crs. Only xres is mandatory, by default yres=xres and bounds/crs are set to self’s.

Vector features which fall outside the bounds of the raster file are not written to the new mask file.

Parameters
  • rst – A Raster object or string to filename

  • crs – A pyproj or rasterio CRS object (Default to rst.crs if not None then self.crs)

  • xres – Output raster spatial resolution in x. Only is rst is None.

  • yres – Output raster spatial resolution in y. Only if rst is None. (Default to xres)

  • bounds – Output raster bounds (left, bottom, right, top). Only if rst is None (Default to self bounds)

  • buffer – Size of buffer to be added around the features, in the raster’s projection units.

If a negative value is set, will erode the features.

Returns

array containing the mask

crop2raster(rst)[source]

Update self so that features outside the extent of a raster file are cropped.

Reprojection is done on the fly if both data set have different projections.

Parameters

rst (Raster) – A Raster object or string to filename

Return type

None

info()[source]

Returns string of information about the vector (filename, coordinate system, number of layers, features, etc.).

Returns

text information about Vector attributes.

Return type

str

query(expression, inplace=False)[source]

Query the Vector dataset with a valid Pandas expression.

Parameters
  • expression (str) – A python-like expression to evaluate. Example: “col1 > col2”

  • inplace (bool) – Whether the query should modify the data in place or return a modified copy.

Return type

~VectorType

Returns

Vector resulting from the provided query expression or itself if inplace=True.

rasterize(rst=None, crs=None, xres=None, yres=None, bounds=None, in_value=None, out_value=0)[source]

Return an array with input geometries burned in.

By default, output raster has the extent/dimensions of the provided raster file. Alternatively, user can specify a grid to rasterize on using xres, yres, bounds and crs. Only xres is mandatory, by default yres=xres and bounds/crs are set to self’s.

Burn value is set by user and can be either a single number, or an iterable of same length as self.ds. Default is an index from 1 to len(self.ds).

Parameters
  • rst – A raster to be used as reference for the output grid

  • crs – A pyproj or rasterio CRS object (Default to rst.crs if not None then self.crs)

  • xres – Output raster spatial resolution in x. Only is rst is None.

  • yres – Output raster spatial resolution in y. Only if rst is None. (Default to xres)

  • bounds – Output raster bounds (left, bottom, right, top). Only if rst is None (Default to self bounds)

  • in_value – Value(s) to be burned inside the polygons (Default is self.ds.index + 1)

  • out_value – Value to be burned outside the polygons (Default is 0)

Returns

array containing the burned geometries