Skip to content

Commit

Permalink
minor speed optimizations
Browse files Browse the repository at this point in the history
README update
  • Loading branch information
internaut committed Feb 10, 2021
1 parent 7cef54b commit 0646bed
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Markus Konrad <markus.konrad@wzb.eu>, February 2021

![Voronoi regions of random points across Spain and their respective area](https://raw.githubusercontent.com/WZBSocialScienceCenter/geovoronoi/master/examples/random_points_and_area.png)

*geovoronoi* helps generating [Voronoi regions](https://en.wikipedia.org/wiki/Voronoi_diagram) 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".
*geovoronoi* helps generating [Voronoi regions](https://en.wikipedia.org/wiki/Voronoi_diagram) 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.

It takes a list of coordinates and calculates the Voronoi regions from them using [SciPy](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Voronoi.html#scipy.spatial.Voronoi). 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](http://toblerity.org/shapely/) for these operations. The package furthermore implements some functions for easy plotting of the resulting Voronoi regions.
The main function of this package, `voronoi_regions_from_coords()`, takes a list of coordinates and calculates the Voronoi regions from them using [SciPy](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Voronoi.html#scipy.spatial.Voronoi). 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](http://toblerity.org/shapely/) for these operations. The package furthermore implements some functions for easy plotting of the resulting Voronoi regions.

## Installation

Expand Down
Binary file modified examples/duplicate_points.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/random_points_brandenburg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion geovoronoi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@


__title__ = 'geovoronoi'
__version__ = '0.3.0dev'
__version__ = '0.3.0'
__author__ = 'Markus Konrad'
__license__ = 'Apache License 2.0'
8 changes: 6 additions & 2 deletions geovoronoi/_voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ def voronoi_regions_from_coords(coords, geo_shape, per_geom=True, return_unassig
# iterate through sub-geometries in `geo_shape`
for i_geom, geom in enumerate(geoms):
# get point indices of points that lie within `geom`
pts_in_geom = [i for i in pts_indices if geom.contains(pts[i])]
if len(geoms) == 1 or i_geom == len(geoms) - 1:
pts_in_geom = list(pts_indices) # no need to check if we only have one geom or only one geom left
else:
pts_in_geom = [i for i in pts_indices if geom.contains(pts[i])]

# start with empty data for this sub-geometry
geom_region_polys[i_geom] = {}
Expand All @@ -179,7 +182,8 @@ def voronoi_regions_from_coords(coords, geo_shape, per_geom=True, return_unassig
continue

# remove the points that we're about to use (point - geometry assignment is bijective)
pts_indices.difference_update(pts_in_geom)
if i_geom < len(geoms) - 1: # no need to do this on last iteration
pts_indices.difference_update(pts_in_geom)

# generate the Voronoi regions using the SciPy Voronoi class
logger.info('generating Voronoi regions')
Expand Down
9 changes: 5 additions & 4 deletions geovoronoi/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def plot_voronoi_polys_with_points_in_area(ax, area_shape, region_polys, points,
as `plot_area_opts`, `plot_voronoi_opts` or `plot_points_opts` respectively.
:param ax: matplotlib Axes object to plot on
:param area_shape: geographic shape surrounding the Voronoi regions
:param area_shape: geographic shape surrounding the Voronoi regions; can be None to disable plotting of geogr. shape
:param region_polys: dict mapping region IDs to Voronoi region geometries
:param points: NumPy array or list of Shapely Point objects
:param region_pts: dict mapping Voronoi region IDs to point indices of `points`
Expand Down Expand Up @@ -281,10 +281,11 @@ def plot_voronoi_polys_with_points_in_area(ax, area_shape, region_polys, points,
plot_voronoi_opts = plot_voronoi_opts or {'alpha': 0.5}
plot_points_opts = plot_points_opts or {}

plot_polygon_collection_with_color(ax, [area_shape], color=area_color, edgecolor=area_edgecolor, **plot_area_opts)
if area_shape is not None:
plot_polygon_collection_with_color(ax, [area_shape], color=area_color, edgecolor=area_edgecolor,
**plot_area_opts)

if voronoi_and_points_cmap and region_pts and \
not all(map(bool, (voronoi_color, voronoi_edgecolor, points_color))):
if voronoi_and_points_cmap and region_pts and (voronoi_color is None or points_color is None):
voronoi_color, points_color = colors_for_voronoi_polys_and_points(region_polys, region_pts,
point_indices=list(range(len(points))),
cmap_name=voronoi_and_points_cmap)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
GITHUB_URL = 'https://github.com/WZBSocialScienceCenter/geovoronoi'

__title__ = 'geovoronoi'
__version__ = '0.3.0dev'
__version__ = '0.3.0'
__author__ = 'Markus Konrad'
__license__ = 'Apache License 2.0'

Expand Down

0 comments on commit 0646bed

Please sign in to comment.