Package: geovoronoi
geovoronoi – a package to create and plot Voronoi regions inside geographic areasMarkus Konradmarkus.konrad@wzb.eu, March 2022Overviewgeovoronoihelps generatingVoronoi regionsfor geographic data, for example coordinates of public universities in a certain country. This in turn may be used to estimate some kind of "coverage". The usage is not confined to geographic data, though. This package allows you to generate finite Voronoi regions inside any valid surrounding polygonal shape.The main function of this package,voronoi_regions_from_coords(), takes a list of coordinates and calculates the Voronoi regions from them usingSciPy. At the edges, these regions go to infinity. We can then take the shape of the surrounding area (e.g. the shape of a country as polygon) to cut the Voronoi regions so that they fit into the provided shape, making the regions at the edges finite.geovoronoiusesshapelyfor these operations. The package furthermore implements some functions for easy plotting of the resulting Voronoi regions.InstallationThis package is available onPyPI. You can install the latest version viapipas follows:# install with "plotting" dependencies (recommended):
pip install -U geovoronoi[plotting]

# or install base version:
pip install -U geovoronoiUsageYou have a geographic area that contains some points for which you want to generate Voronoi regions. This geographic area is ashapely Polygon/MultiPolygon object(that you, for example, obtained from a GeoJSON file that you loaded withGeoPandasorFiona). TheNpoints you have are either in the form of aNx2 NumPy array, or a list of shapely Point objects (they can be converted with the functionscoords_to_pointsandpoints_to_coords). Both the points and the surrounding geographic area must in the sameCRS (coordinate reference system).Let's take for example these randomly generated points in Italy (inWorld Mercator CRS):importnumpyasnp# coords = ... generate some coordinates with np.random.uniform ...print(coords)array([[1690891.43454513, 4865911.53550427],
       [1303898.2749075 , 5398659.4816214 ],
       [1379407.32051822, 5701267.51923313],
       [1703402.05850744, 4916559.63783754],
       ...
       ]])The surrounding shape of Italy was obtained beforehand from GeoPandas:importgeopandasasgpdworld=gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))area=world[world.name=='Italy']area=area.to_crs(epsg=3395)# convert to World Mercator CRSarea_shape=area.iloc[0].geometry# get the PolygonNow we can calculate the Voronoi regions, cut them with the geographic area shape and assign the points to them:fromgeovoronoiimportvoronoi_regions_from_coordsregion_polys,region_pts=voronoi_regions_from_coords(coords,area_shape)region_polysis a dict that maps Voronoi region IDs to shapely Polygon objects that represent the shape of the respective Voronoi region. With them, you can do everything that theshapely APIprovides. You can, for example, get each Voronoi region'sarea(there's also a helper functioncalculate_polygon_areasingeovoronoifor that).region_ptsis a dict that maps the same Voronoi region IDs as inregion_polysto a list of indices intocoords, i.e. these indices represent the points that belong to this Voronoi region. Usually, this is only a single point. However, in case of duplicate points (i.e. two or more points have exactly the same coordinates) all duplicate points are listed for the respective Voronoi region.You can plot the results with the functions from theplottingsub-module:importmatplotlib.pyplotaspltfromgeovoronoi.plottingimportsubplot_for_map,plot_voronoi_polys_with_points_in_areafig,ax=subplot_for_map()plot_voronoi_polys_with_points_in_area(ax,area_shape,region_polys,coords,region_pts)plt.show()This would be an example output:See the full example source code inexamples/random_points_across_italy.py. See also the other examples in theexamples/directory that show how to calculate the area of the Voronoi regions, handle duplicate points or interact with the GeoPandas or Fiona packages.DependenciesgeovoronoirequiresPython 3.6 or newer(the package is tested for up to Python 3.10). The following packages need to be installed (if not, they will be automatically installed if you use a Python package manager likepip):NumPySciPyshapelymatplotlib (only necessary for plotting)geopandas (only necessary for plotting)descartes (only necessary for plotting)LicenseLicensed underApache License 2.0. SeeLICENSE.txtfile.