• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a package to create and plot Voronoi regions within geographic boundaries

geovoronoi – a package to create and plot Voronoi regions inside geographic areas

PyPI version DOI

Markus Konrad [email protected] / [email protected], March 2022

This package is currently not maintained. You may prefer to use the voronoi_polygons function from the shapely package instead, which didn't exist when I created the geovoronoi package, but is now considered to be a faster and more reliable option for generating Voronoi regions.

Overview

Voronoi regions of random points across Spain and their respective area

geovoronoi helps generating Voronoi regions for 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 using SciPy. 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. geovoronoi uses shapely for these operations. The package furthermore implements some functions for easy plotting of the resulting Voronoi regions.

Installation

This package is available on PyPI. You can install the latest version via pip as follows:

# install with "plotting" dependencies (recommended):
pip install -U geovoronoi[plotting]

# or install base version:
pip install -U geovoronoi

Usage

You have a geographic area that contains some points for which you want to generate Voronoi regions. This geographic area is a shapely Polygon/MultiPolygon object (that you, for example, obtained from a GeoJSON file that you loaded with GeoPandas or Fiona). The N points you have are either in the form of a Nx2 NumPy array, or a list of shapely Point objects (they can be converted with the functions coords_to_points and points_to_coords). Both the points and the surrounding geographic area must be in the same CRS (coordinate reference system).

Let's take for example these randomly generated points in Italy (in World Mercator CRS):

import numpy as np

# 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:

import geopandas as gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
area = world[world.name == 'Italy']

area = area.to_crs(epsg=3395)    # convert to World Mercator CRS
area_shape = area.iloc[0].geometry   # get the Polygon

Now we can calculate the Voronoi regions, cut them with the geographic area shape and assign the points to them:

from geovoronoi import voronoi_regions_from_coords

region_polys, region_pts = voronoi_regions_from_coords(coords, area_shape)

region_polys is 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 the shapely API provides. You can, for example, get each Voronoi region's area (there's also a helper function calculate_polygon_areas in geovoronoi for that).

region_pts is a dict that maps the same Voronoi region IDs as in region_polys to a list of indices into coords, 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 the plotting sub-module:

import matplotlib.pyplot as plt
from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area

fig, 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:

Voronoi regions of random points across Italy

See the full example source code in examples/random_points_across_italy.py. See also the other examples in the examples/ directory that show how to calculate the area of the Voronoi regions, handle duplicate points or interact with the GeoPandas or Fiona packages.

Dependencies

geovoronoi requires Python 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 like pip):

  • NumPy
  • SciPy
  • shapely
  • matplotlib (only necessary for plotting)
  • geopandas (only necessary for plotting)
  • descartes (only necessary for plotting)

License

Licensed under Apache License 2.0. See LICENSE.txt file.

More Repositories

1

pdftabextract

A set of tools for extracting tables from PDF files helping to do data mining on (OCR-processed) scanned documents.
Python
2,199
star
2

tmtoolkit

Text Mining and Topic Modeling Toolkit for Python with parallel processing power
Python
192
star
3

pdf2xml-viewer

A simple viewer and inspection tool for text boxes in PDF documents
HTML
91
star
4

germalemma

A lemmatizer for German language text
Python
86
star
5

plz_geocoord

Dataset of all German postal codes and their geographic center as geo-coordinates.
34
star
6

otreeutils

Facilitate oTree experiment implementation with extensions for custom data models, surveys, understanding questions, timeout warnings and more.
Python
17
star
7

otree_custom_models

Example project showing how to use custom models in oTree for recording complex decisions in experiments
Python
8
star
8

pandas-excel-styler

Styling individual cells in Excel output files created with pandas.
Python
8
star
9

otree_iat

Implicit Association Test (IAT) experiment for oTree
Python
6
star
10

mdb-twitter-network

Twitter network of members of the 19th German Bundestag
R
5
star
11

tm_bundestag

An example topic model for debates from the 18th German Bundestag
Jupyter Notebook
5
star
12

gemeindeverzeichnis

Python-Modul zum Einlesen von Gemeindeverzeichnisdaten des Statistischen Bundesamts als pandas DataFrame
Python
5
star
13

r-geodata-workshop

Workshop held at WZB: Working with geo-spatial data in R - Obtaining, linking and plotting geographic data
R
5
star
14

tm_corona

A small showcase for topic modeling with the tmtoolkit Python package. I use a corpus of articles from the German online news website Spiegel Online (SPON) to create a topic model for before and during the COVID-19 pandemic.
Jupyter Notebook
4
star
15

spatially_weighted_avg

Code for "Spatially weighted averages in R with sf"
R
2
star
16

r_clustered_se

Code for blog post "Clustered standard errors with R: Three ways, one result".
R
2
star
17

d3-balloon

d3.js extension for interactive balloon plots
HTML
2
star
18

covid19-placesapi

Code to obtain and analyse "popular times" data from Google Places. Also contains data fetched between March 22nd and April 15th 2020 for different places world-wide.
R
2
star
19

r_simplify_features

Code for blog post showing how to simplify spatial features with R.
R
2
star
20

wzb_r_tutorial

Documents for R tutorial given at WZB accompanying the lecture "Studying Social Stratification with Big Data" (Hipp, Ulbricht) in winter semester 2018
1
star