Skip to content

Commit

Permalink
adapted remaining examples
Browse files Browse the repository at this point in the history
  • Loading branch information
internaut committed Jan 28, 2021
1 parent cfe9e94 commit 56c5379
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 235 deletions.
2 changes: 1 addition & 1 deletion examples/duplicate_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Duplicate points, i.e. points with exactly the same coordinates will belong to the same Voronoi region.
Author: Markus Konrad <markus.konrad@wzb.eu>
January 2020
January 2021
"""


Expand Down
2 changes: 1 addition & 1 deletion examples/random_points_across_italy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
and their points will be plotted using the `plotting` sub-module of `geovoronoi`.
Author: Markus Konrad <markus.konrad@wzb.eu>
March 2018
January 2021
"""


Expand Down
Binary file modified examples/random_points_and_area.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 15 additions & 7 deletions examples/random_points_and_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
Note that it is important to use an *equal area* projection before calculating the areas of the Voronoi regions!
Author: Markus Konrad <markus.konrad@wzb.eu>
March 2018
January 2021
"""

import logging
from pprint import pprint

import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -24,6 +25,8 @@
geovoronoi_log.setLevel(logging.INFO)
geovoronoi_log.propagate = True

#%%

N_POINTS = 20
COUNTRY = 'Spain'

Expand All @@ -48,39 +51,44 @@

# use only the points inside the geographic area
pts = [p for p in coords_to_points(coords) if p.within(area_shape)] # converts to shapely Point
n_pts = len(pts)

print('will use %d of %d randomly generated points that are inside geographic area' % (len(pts), N_POINTS))
print('will use %d of %d randomly generated points that are inside geographic area' % (n_pts, N_POINTS))
coords = points_to_coords(pts) # convert back to simple NumPy coordinate array

del pts

#%%

#
# calculate the Voronoi regions, cut them with the geographic area shape and assign the points to them
#

poly_shapes, pts, poly_to_pt_assignments = voronoi_regions_from_coords(coords, area_shape)
poly_shapes, poly_to_pt_assignments = voronoi_regions_from_coords(coords, area_shape)

# calculate area in km², too
poly_areas = calculate_polygon_areas(poly_shapes, m2_to_km2=True) # converts m² to km²

print('areas in km²:')
print(poly_areas)
pprint(poly_areas)

print('sum:')
print(sum(poly_areas))
print(sum(poly_areas.values()))

#%%

#
# plotting
#

fig, ax = subplot_for_map(show_x_axis=True, show_y_axis=True)

voronoi_labels = ['%d km²' % round(a) for a in poly_areas]
voronoi_labels = {poly_i: '%d km²' % round(a) for poly_i, a in poly_areas.items()}
plot_voronoi_polys_with_points_in_area(ax, area_shape, poly_shapes, coords, poly_to_pt_assignments,
voronoi_labels=voronoi_labels, voronoi_label_fontsize=7,
voronoi_label_color='gray')

ax.set_title('%d random points and their Voronoi regions in %s' % (len(pts), COUNTRY))
ax.set_title('%d random points and their Voronoi regions in %s' % (n_pts, COUNTRY))

plt.tight_layout()
plt.savefig('random_points_and_area.png')
Expand Down
13 changes: 11 additions & 2 deletions examples/toyexample.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
"""
Artificial mini-example useful for debugging.
Author: Markus Konrad <markus.konrad@wzb.eu>
January 2021
"""


import logging

import matplotlib.pyplot as plt
Expand All @@ -21,16 +29,17 @@

points = np.array([[1, 1], [1.1, 0.9], [1.15, 0.8], [2.5, 0]])

# surrounding shape
shape = Polygon([[-1, -1], [3, -1], [3, 3], [-1, 3]])

#%%
#%% Voronoi region generation

region_polys, region_pts = voronoi_regions_from_coords(points, shape)

print('Voronoi region to point assignments:')
print(region_pts)

#%%
#%% plotting

fig, ax = subplot_for_map(show_x_axis=True, show_y_axis=True)

Expand Down
Binary file modified examples/using_geopandas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions examples/using_geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Example script to show how to incorporate GeoPandas with geovoronoi.
Author: Markus Konrad <markus.konrad@wzb.eu>
March 2018
January 2021
"""

import logging
Expand All @@ -20,6 +20,8 @@
geovoronoi_log.setLevel(logging.INFO)
geovoronoi_log.propagate = True

#%%

#
# load geo data
#
Expand All @@ -35,6 +37,7 @@
south_am_shape = cascaded_union(south_am.geometry)
south_am_cities = cities[cities.geometry.within(south_am_shape)] # reduce to cities in South America

#%%

#
# calculate the Voronoi regions, cut them with the geographic area shape and assign the points to them
Expand All @@ -44,16 +47,19 @@
coords = points_to_coords(south_am_cities.geometry)

# calculate the regions
poly_shapes, pts, poly_to_pt_assignments = voronoi_regions_from_coords(coords, south_am_shape)
poly_shapes, poly_to_pt_assignments = voronoi_regions_from_coords(coords, south_am_shape)


#%%

#
# Plotting
#

fig, ax = subplot_for_map()

plot_voronoi_polys_with_points_in_area(ax, south_am_shape, poly_shapes, pts, poly_to_pt_assignments)
plot_voronoi_polys_with_points_in_area(ax, south_am_shape, poly_shapes, coords, poly_to_pt_assignments,
point_labels=south_am_cities.name.tolist())

ax.set_title('Cities data for South America from GeoPandas\nand Voronoi regions around them')

Expand Down
212 changes: 0 additions & 212 deletions examples/vregion_assignment.py

This file was deleted.

8 changes: 3 additions & 5 deletions geovoronoi/_geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def calculate_polygon_areas(poly_shapes, m2_to_km2=False):
Return the area of the respective polygons in `poly_shapes`. Returns a NumPy array of areas in m² (if `m2_to_km2` is
False) or km² (otherwise).
"""
areas = np.array([p.area for p in poly_shapes])
if m2_to_km2:
return areas / 1000000 # = 1000²
else:
return areas

unit_convert = 1000000 if m2_to_km2 else 1
return {i_poly: p.area / unit_convert for i_poly, p in poly_shapes.items()}
Loading

0 comments on commit 56c5379

Please sign in to comment.