Skip to content

Commit

Permalink
added brandenburg example, removed unnecessary is_simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
internaut committed Jan 29, 2021
1 parent d8c29ca commit 96347c9
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 67 deletions.
7 changes: 7 additions & 0 deletions examples/brandenburg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "FeatureCollection",
"name": "Germany_AL4",
"features": [
{ "type": "Feature", "properties": { "srid": "4326", "id": "62504", "name": "Brandenburg", "localname": "Brandenburg", "official_name": "", "boundary": "adminstrative", "admin_level": "4", "note": "", "wikidata": "Q1208", "wikipedia": "de:Brandenburg", "timestamp": "2018\/07\/19 01:55:20", "rpath": "62504,51477,0", "alltags": { "ref": "BB", "name": "Brandenburg", "name:am": "ብራንደንቡርግ", "name:an": "Brandemburgo", "name:ar": "براندنبورغ", "name:ay": "Brandenburg suyu", "name:az": "Brandenburq", "name:ba": "Бранденбург", "name:be": "Брандэнбург", "name:bg": "Бранденбург", "name:bn": "ব্রান্ডেনবুর্গ", "name:ca": "Brandenburg", "name:ce": "Бранденбург", "name:cs": "Braniborsko", "name:cv": "Бранденбург", "name:de": "Brandenburg", "name:el": "Βρανδεμβούργο", "name:eo": "Brandenburgio", "name:es": "Brandemburgo", "name:eu": "Brandenburgo", "name:fa": "براندنبورگ", "name:fr": "Brandebourg", "name:fy": "Brandenburch", "name:gl": "Brandeburgo", "name:gn": "Brandeburgo", "name:he": "ברנדנבורג", "name:hi": "ब्रैंडेनबर्ग", "name:hy": "Բրանդենբուրգ", "name:ia": "Brandeburgo", "name:ie": "Brandenburgia", "name:is": "Brandenborg", "name:it": "Brandeburgo", "name:ja": "ブランデンブルク州", "name:ka": "ბრანდენბურგი", "name:kk": "Бранденбург", "name:ko": "브란덴부르크 주", "name:la": "Brandenburgum", "name:lb": "Brandenburg", "name:li": "Brandeburg", "name:lt": "Brandenburgas", "name:lv": "Brandenburga", "name:mk": "Бранденбург", "name:mn": "Бранденбург", "name:mr": "ब्रांडेनबुर्ग", "name:ne": "ब्रान्डेनबर्ग", "name:nl": "Brandenburg", "name:oc": "Brandeborg", "name:os": "Бранденбург", "name:pa": "ਬ੍ਰਾਂਡਨਬੁਰਕ", "name:pl": "Brandenburgia", "name:ps": "براندنبورگ", "name:pt": "Brandemburgo", "name:ru": "Бранденбург", "name:sk": "Brandenbursko", "name:sq": "Brandenburgu", "name:sr": "Бранденбург", "name:th": "รัฐบรันเดินบวร์ค", "name:tl": "Brandeburgo", "name:uk": "Бранденбург", "name:ur": "برندنبرگ", "name:vo": "Brandänburgän", "name:yi": "בראנדנבורג", "name:zh": "勃兰登堡", "boundary": "administrative", "name:hsb": "Braniborska", "wikidata": "Q1208", "ISO3166-2": "DE-BB", "wikipedia": "de:Brandenburg", "admin_level": "4", "alt_name:cs": "Země Braniborsko", "border_type": "state", "name:prefix": "Bundesland", "de:regionalschluessel": "12", "TMC:cid_58:tabcd_1:Class": "Area", "TMC:cid_58:tabcd_1:LCLversion": "12.0", "TMC:cid_58:tabcd_1:LocationCode": "267", "de:amtlicher_gemeindeschluessel": "12" } }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 11.267612, 53.121972 ], [ 11.393711, 53.10984 ], [ 11.395417, 53.140179 ], [ 11.42153, 53.141106 ], [ 11.53464, 53.125218 ], [ 11.569435, 53.165502 ], [ 11.550948, 53.20823 ], [ 11.629009, 53.241997 ], [ 11.699607, 53.244091 ], [ 11.728497, 53.21695 ], [ 11.827851, 53.227466 ], [ 11.795949, 53.253312 ], [ 11.861657, 53.248446 ], [ 11.892566, 53.278955 ], [ 11.948175, 53.271419 ], [ 11.973662, 53.297528 ], [ 12.018622, 53.299422 ], [ 12.014026, 53.33423 ], [ 12.055753, 53.349153 ], [ 12.045029, 53.370756 ], [ 12.061792, 53.371256 ], [ 12.079668, 53.366857 ], [ 12.081924, 53.346194 ], [ 12.106835, 53.34353 ], [ 12.139637, 53.361395 ], [ 12.168702, 53.339156 ], [ 12.229785, 53.358135 ], [ 12.260042, 53.323897 ], [ 12.310277, 53.327242 ], [ 12.393245, 53.302036 ], [ 12.392042, 53.284889 ], [ 12.428612, 53.274005 ], [ 12.445349, 53.249652 ], [ 12.529791, 53.264621 ], [ 12.605645, 53.244335 ], [ 12.660175, 53.254649 ], [ 12.674905, 53.248638 ], [ 12.67066, 53.229694 ], [ 12.757725, 53.224094 ], [ 12.739304, 53.199295 ], [ 12.764253, 53.188972 ], [ 12.848072, 53.200874 ], [ 12.879718, 53.179359 ], [ 12.965443, 53.200246 ], [ 12.979795, 53.19081 ], [ 12.942102, 53.174467 ], [ 12.980444, 53.162734 ], [ 13.078718, 53.200462 ], [ 13.10653, 53.214958 ], [ 13.104125, 53.236278 ], [ 13.149808, 53.25066 ], [ 13.180433, 53.251228 ], [ 13.203488, 53.22126 ], [ 13.229785, 53.216611 ], [ 13.252443, 53.262103 ], [ 13.298454, 53.28097 ], [ 13.360916, 53.277759 ], [ 13.386071, 53.244784 ], [ 13.405712, 53.246824 ], [ 13.44252, 53.286519 ], [ 13.43781, 53.299583 ], [ 13.48359, 53.291228 ], [ 13.502451, 53.327389 ], [ 13.523603, 53.319917 ], [ 13.516355, 53.352135 ], [ 13.556168, 53.376039 ], [ 13.550068, 53.399166 ], [ 13.624767, 53.408976 ], [ 13.64059, 53.447112 ], [ 13.711784, 53.48137 ], [ 13.812113, 53.484135 ], [ 13.826096, 53.498866 ], [ 13.778005, 53.513742 ], [ 13.791149, 53.558362 ], [ 13.82185, 53.518072 ], [ 13.87974, 53.504012 ], [ 13.875533, 53.473292 ], [ 13.918138, 53.45541 ], [ 13.902906, 53.43181 ], [ 13.918155, 53.421914 ], [ 14.001263, 53.434914 ], [ 14.081912, 53.410847 ], [ 14.121929, 53.441704 ], [ 14.179376, 53.422526 ], [ 14.224816, 53.434292 ], [ 14.242334, 53.421925 ], [ 14.230912, 53.367779 ], [ 14.099028, 53.26147 ], [ 14.264627, 53.259103 ], [ 14.261981, 53.276502 ], [ 14.302061, 53.286208 ], [ 14.316346, 53.312279 ], [ 14.402259, 53.330064 ], [ 14.415453, 53.324377 ], [ 14.406803, 53.308729 ], [ 14.421205, 53.276137 ], [ 14.449841, 53.260722 ], [ 14.404364, 53.209488 ], [ 14.376247, 53.199131 ], [ 14.366777, 53.168451 ], [ 14.38642, 53.140675 ], [ 14.349985, 53.057392 ], [ 14.257073, 53.001979 ], [ 14.143936, 52.961109 ], [ 14.160814, 52.886771 ], [ 14.123005, 52.837765 ], [ 14.216389, 52.816773 ], [ 14.28029, 52.774268 ], [ 14.350431, 52.751291 ], [ 14.46523, 52.661411 ], [ 14.596211, 52.610643 ], [ 14.639014, 52.573317 ], [ 14.603838, 52.531031 ], [ 14.633926, 52.491484 ], [ 14.548115, 52.432337 ], [ 14.534376, 52.395 ], [ 14.584829, 52.306347 ], [ 14.575288, 52.289048 ], [ 14.689475, 52.256758 ], [ 14.71555, 52.236128 ], [ 14.685814, 52.193906 ], [ 14.705567, 52.168914 ], [ 14.680016, 52.143383 ], [ 14.681812, 52.11564 ], [ 14.759094, 52.065383 ], [ 14.748422, 52.031928 ], [ 14.714029, 52.003683 ], [ 14.721659, 51.994475 ], [ 14.704855, 51.976008 ], [ 14.721287, 51.951315 ], [ 14.694184, 51.901895 ], [ 14.611105, 51.857131 ], [ 14.590107, 51.821362 ], [ 14.645475, 51.795528 ], [ 14.668163, 51.725857 ], [ 14.7474, 51.675669 ], [ 14.76512, 51.60747 ], [ 14.729824, 51.581589 ], [ 14.696636, 51.596762 ], [ 14.70554, 51.576932 ], [ 14.674128, 51.550852 ], [ 14.609559, 51.550107 ], [ 14.599006, 51.571187 ], [ 14.567686, 51.581087 ], [ 14.516481, 51.55428 ], [ 14.387123, 51.541567 ], [ 14.332329, 51.504899 ], [ 14.273278, 51.532376 ], [ 14.136602, 51.543333 ], [ 14.142298, 51.522966 ], [ 14.108512, 51.521879 ], [ 14.073806, 51.492216 ], [ 14.08894, 51.477779 ], [ 14.032744, 51.475003 ], [ 14.062587, 51.445421 ], [ 14.036877, 51.434537 ], [ 14.044861, 51.419073 ], [ 14.015904, 51.404489 ], [ 14.028122, 51.396264 ], [ 14.003784, 51.395536 ], [ 14.000596, 51.372537 ], [ 13.970463, 51.375699 ], [ 13.972375, 51.393434 ], [ 13.955527, 51.397329 ], [ 13.882903, 51.374429 ], [ 13.83426, 51.383752 ], [ 13.763328, 51.359064 ], [ 13.767724, 51.371696 ], [ 13.599149, 51.368673 ], [ 13.570333, 51.385659 ], [ 13.542538, 51.369405 ], [ 13.523314, 51.380931 ], [ 13.520726, 51.403668 ], [ 13.462982, 51.412214 ], [ 13.475845, 51.418519 ], [ 13.446034, 51.430853 ], [ 13.421893, 51.421568 ], [ 13.428913, 51.431774 ], [ 13.400668, 51.454248 ], [ 13.374004, 51.438229 ], [ 13.378692, 51.425301 ], [ 13.320841, 51.437384 ], [ 13.328584, 51.426473 ], [ 13.285623, 51.410778 ], [ 13.286452, 51.399297 ], [ 13.260211, 51.401434 ], [ 13.268686, 51.384585 ], [ 13.216339, 51.395353 ], [ 13.201536, 51.431875 ], [ 13.174032, 51.428282 ], [ 13.20328, 51.451605 ], [ 13.182092, 51.49191 ], [ 13.201199, 51.491789 ], [ 13.208042, 51.524272 ], [ 13.185566, 51.557986 ], [ 13.142584, 51.567988 ], [ 13.154377, 51.600253 ], [ 13.120197, 51.619986 ], [ 13.08593, 51.608095 ], [ 13.050548, 51.647505 ], [ 13.154312, 51.686403 ], [ 13.154189, 51.710474 ], [ 13.186882, 51.715693 ], [ 13.152096, 51.744237 ], [ 13.163733, 51.75484 ], [ 13.14744, 51.766448 ], [ 13.17055, 51.787018 ], [ 13.123927, 51.85429 ], [ 13.150508, 51.859678 ], [ 13.149145, 51.872145 ], [ 13.118628, 51.883107 ], [ 13.040988, 51.870573 ], [ 13.026893, 51.880785 ], [ 13.045574, 51.900435 ], [ 12.973468, 51.900767 ], [ 12.976643, 51.920968 ], [ 12.955935, 51.922641 ], [ 12.960558, 51.934642 ], [ 12.852415, 51.935165 ], [ 12.844325, 51.967582 ], [ 12.776118, 51.964935 ], [ 12.756244, 51.986499 ], [ 12.668292, 52.01306 ], [ 12.597264, 51.98152 ], [ 12.539432, 51.984922 ], [ 12.535818, 52.002928 ], [ 12.494043, 52.011615 ], [ 12.480728, 52.033175 ], [ 12.429478, 52.018333 ], [ 12.276527, 52.103891 ], [ 12.216113, 52.170543 ], [ 12.247333, 52.183879 ], [ 12.248787, 52.211782 ], [ 12.282094, 52.216715 ], [ 12.296648, 52.22846 ], [ 12.24541, 52.249863 ], [ 12.262923, 52.295045 ], [ 12.30809, 52.344632 ], [ 12.284122, 52.364199 ], [ 12.306555, 52.377714 ], [ 12.291711, 52.386111 ], [ 12.302269, 52.405309 ], [ 12.27461, 52.416183 ], [ 12.297198, 52.423694 ], [ 12.289413, 52.430579 ], [ 12.330787, 52.478274 ], [ 12.308522, 52.479194 ], [ 12.329612, 52.496642 ], [ 12.271845, 52.487501 ], [ 12.257958, 52.518197 ], [ 12.23607, 52.523873 ], [ 12.221768, 52.500231 ], [ 12.184673, 52.495722 ], [ 12.167122, 52.514696 ], [ 12.18874, 52.532226 ], [ 12.142172, 52.528285 ], [ 12.18152, 52.575218 ], [ 12.166939, 52.627464 ], [ 12.23646, 52.628728 ], [ 12.23259, 52.688766 ], [ 12.197363, 52.718902 ], [ 12.222162, 52.739826 ], [ 12.205848, 52.76049 ], [ 12.220406, 52.78989 ], [ 12.244407, 52.786606 ], [ 12.25762, 52.80487 ], [ 12.233428, 52.859922 ], [ 12.197054, 52.877878 ], [ 12.123719, 52.853635 ], [ 12.125539, 52.89439 ], [ 12.112775, 52.875695 ], [ 12.022564, 52.89038 ], [ 11.979908, 52.876112 ], [ 11.83449, 52.910145 ], [ 11.823288, 52.92125 ], [ 11.846216, 52.95132 ], [ 11.788134, 52.960208 ], [ 11.743077, 52.987857 ], [ 11.692056, 52.979161 ], [ 11.676296, 53.008221 ], [ 11.627038, 53.011963 ], [ 11.637715, 53.039368 ], [ 11.510955, 53.047305 ], [ 11.446347, 53.078476 ], [ 11.340537, 53.054726 ], [ 11.273544, 53.098469 ], [ 11.267612, 53.121972 ] ], [ [ 13.08835, 52.41964 ], [ 13.138103, 52.397856 ], [ 13.12722, 52.391666 ], [ 13.13108, 52.387236 ], [ 13.245947, 52.421126 ], [ 13.249725, 52.404957 ], [ 13.296755, 52.41625 ], [ 13.312082, 52.399139 ], [ 13.34328, 52.411273 ], [ 13.388437, 52.377865 ], [ 13.420834, 52.376127 ], [ 13.41878, 52.409995 ], [ 13.463563, 52.421065 ], [ 13.479767, 52.395947 ], [ 13.53843, 52.400603 ], [ 13.5355, 52.38899 ], [ 13.59275, 52.39383 ], [ 13.605798, 52.37361 ], [ 13.642682, 52.377509 ], [ 13.63632, 52.346819 ], [ 13.64843, 52.338276 ], [ 13.699975, 52.375599 ], [ 13.686815, 52.385306 ], [ 13.739034, 52.407335 ], [ 13.740057, 52.432333 ], [ 13.729895, 52.433924 ], [ 13.726043, 52.435298 ], [ 13.723017, 52.436721 ], [ 13.723066, 52.437057 ], [ 13.76116, 52.43771 ], [ 13.701267, 52.468224 ], [ 13.704616, 52.454756 ], [ 13.648568, 52.478744 ], [ 13.611369, 52.470653 ], [ 13.65695, 52.529883 ], [ 13.625691, 52.530182 ], [ 13.63739, 52.54225 ], [ 13.586412, 52.549782 ], [ 13.581446, 52.571081 ], [ 13.508126, 52.592159 ], [ 13.497131, 52.606708 ], [ 13.52287, 52.644812 ], [ 13.489883, 52.655517 ], [ 13.47951, 52.67551 ], [ 13.450794, 52.662706 ], [ 13.469909, 52.651915 ], [ 13.424448, 52.635545 ], [ 13.394554, 52.647554 ], [ 13.357644, 52.622977 ], [ 13.302609, 52.62719 ], [ 13.310177, 52.65737 ], [ 13.282752, 52.660761 ], [ 13.283772, 52.641113 ], [ 13.262141, 52.64072 ], [ 13.264234, 52.626865 ], [ 13.22057, 52.628242 ], [ 13.201643, 52.606479 ], [ 13.217397, 52.587477 ], [ 13.164528, 52.598793 ], [ 13.128966, 52.587302 ], [ 13.153538, 52.57324 ], [ 13.145686, 52.552791 ], [ 13.130503, 52.556023 ], [ 13.117393, 52.517033 ], [ 13.168632, 52.509253 ], [ 13.117693, 52.47732 ], [ 13.109278, 52.450632 ], [ 13.123153, 52.438709 ], [ 13.08835, 52.41964 ] ], [ [ 13.503438, 52.618993 ], [ 13.505442, 52.619687 ], [ 13.505449, 52.619884 ], [ 13.503814, 52.619223 ], [ 13.503438, 52.618993 ] ] ], [ [ [ 12.265677, 52.231337 ], [ 12.272055, 52.231249 ], [ 12.275011, 52.230527 ], [ 12.272864, 52.228934 ], [ 12.265677, 52.231337 ] ] ], [ [ [ 12.282741, 52.227261 ], [ 12.283196, 52.228181 ], [ 12.286261, 52.227816 ], [ 12.285645, 52.227026 ], [ 12.282741, 52.227261 ] ] ], [ [ [ 12.276428, 52.227809 ], [ 12.278846, 52.22944 ], [ 12.282289, 52.228014 ], [ 12.280859, 52.227598 ], [ 12.276428, 52.227809 ] ] ], [ [ [ 12.286549, 52.225852 ], [ 12.286796, 52.226286 ], [ 12.290414, 52.225202 ], [ 12.290055, 52.22474 ], [ 12.286549, 52.225852 ] ] ], [ [ [ 11.267355, 53.12452 ], [ 11.269086, 53.12301 ], [ 11.268632, 53.12211 ], [ 11.267954, 53.122929 ], [ 11.267355, 53.12452 ] ] ], [ [ [ 12.218076, 52.86133 ], [ 12.221282, 52.863301 ], [ 12.222102, 52.862805 ], [ 12.219041, 52.861117 ], [ 12.218076, 52.86133 ] ] ], [ [ [ 12.260021, 52.229894 ], [ 12.260818, 52.230771 ], [ 12.26174, 52.230569 ], [ 12.26097, 52.229695 ], [ 12.260021, 52.229894 ] ] ] ] } }
]
}
12 changes: 0 additions & 12 deletions examples/random_points_across_italy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@
from geovoronoi import coords_to_points, voronoi_regions_from_coords
from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area

import traceback
import warnings
import sys

def warn_with_traceback(message, category, filename, lineno, file=None, line=None):

log = file if hasattr(file,'write') else sys.stderr
traceback.print_stack(file=log)
log.write(warnings.formatwarning(message, category, filename, lineno, line))

warnings.showwarning = warn_with_traceback

logging.basicConfig(level=logging.INFO)
geovoronoi_log = logging.getLogger('geovoronoi')
geovoronoi_log.setLevel(logging.INFO)
Expand Down
Binary file added 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.
85 changes: 85 additions & 0 deletions examples/random_points_brandenburg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Example script that scatters random points across Brandenburg and generates the Voronoi regions for them.
The boundary shape of Brandenburg contains a hole (Berlin) and when loaded is regarded as "invalid" shape.
Author: Markus Konrad <markus.konrad@wzb.eu>
January 2021
"""


import logging
from pprint import pprint

import matplotlib.pyplot as plt
import numpy as np
import fiona
import pyproj
from shapely.geometry import shape
from shapely.ops import transform

from geovoronoi import coords_to_points, voronoi_regions_from_coords
from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area

logging.basicConfig(level=logging.INFO)
geovoronoi_log = logging.getLogger('geovoronoi')
geovoronoi_log.setLevel(logging.INFO)
geovoronoi_log.propagate = True

#%%

INPUT_FILE = 'brandenburg.json'
N_POINTS = 20

np.random.seed(20210129)

print('loading %s' % INPUT_FILE)

with fiona.open(INPUT_FILE) as f:
src_crs = pyproj.CRS.from_dict(f.meta['crs'])
target_crs = pyproj.CRS.from_epsg(3395) # World Mercator CRS
print('source CRS:', src_crs)
print('target CRS:', target_crs)
crs_transformer = pyproj.Transformer.from_crs(src_crs, target_crs)
brandenburg = shape(f[0]['geometry'])
# note that we also apply ".buffer(0)", otherwise the shape is not valid
# see https://shapely.readthedocs.io/en/stable/manual.html#object.buffer on the "buffer(0)" trick
brandenburg = transform(crs_transformer.transform, brandenburg).buffer(0)

#%%

# generate some random points within the bounds
minx, miny, maxx, maxy = brandenburg.bounds

randx = np.random.uniform(minx, maxx, N_POINTS)
randy = np.random.uniform(miny, maxy, N_POINTS)
coords = np.vstack((randx, randy)).T

# use only the points inside the geographic area
pts = [p for p in coords_to_points(coords) if p.within(brandenburg)] # converts to shapely Point
del coords # not used any more

print('will use %d of %d randomly generated points that are inside geographic area' % (len(pts), N_POINTS))

#%%

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

poly_shapes, poly_to_pt_assignments = voronoi_regions_from_coords(pts, brandenburg)

print('Voronoi region to point assignments:')
pprint(poly_to_pt_assignments)

#%% plotting

fig, ax = subplot_for_map()

plot_voronoi_polys_with_points_in_area(ax, brandenburg, poly_shapes, pts, poly_to_pt_assignments,
point_labels=list(map(str, range(len(pts)))))

ax.set_title('%d random points and their Voronoi regions in Brandenburg' % len(pts))

plt.tight_layout()
plt.savefig('random_points_brandenburg.png')
plt.show()
22 changes: 16 additions & 6 deletions geovoronoi/_voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def voronoi_regions_from_coords(coords, geo_shape, per_geom=True, return_unassig
if not isinstance(geo_shape, (Polygon, MultiPolygon)):
raise ValueError('`geo_shape` must be a Polygon or MultiPolygon')

if not geo_shape.is_valid:
raise ValueError('`geo_shape` is not a valid shape; try applying `.buffer(b)` where `b` is zero '
'or a very small number')

if not per_geom or isinstance(geo_shape, Polygon):
geoms = [geo_shape]
else: # Multipolygon
Expand Down Expand Up @@ -196,14 +200,14 @@ def region_polygons_from_voronoi(vor, geom, return_point_assignments=False):
if not isinstance(p, Polygon):
raise RuntimeError('generated convex hull is not a polygon')

if not p.is_valid or not p.is_simple or p.is_empty:
raise RuntimeError('generated polygon is not valid, not simple or empty')
if not p.is_valid or p.is_empty:
raise RuntimeError('generated polygon is not valid or is empty')

if not geom.contains(p):
p = p.intersection(geom)

if not p.is_valid or not p.is_simple or p.is_empty:
raise RuntimeError('generated polygon is not valid, not simple or empty after intersection with the'
if not p.is_valid or p.is_empty:
raise RuntimeError('generated polygon is not valid or is empty after intersection with the'
' surrounding geometry `geom`')

covered_area += p.area
Expand Down Expand Up @@ -253,13 +257,19 @@ def region_polygons_from_voronoi(vor, geom, return_point_assignments=False):

def get_points_to_poly_assignments(poly_to_pt_assignments):
"""
Reverse of poly to points assignments: Returns a list of size N, which is the number of unique points in
`poly_to_pt_assignments`. Each list element is an index into the list of Voronoi regions.
Reverse Voronoi region polygon IDs to point ID assignments by returning a dict that maps each point ID to its
Voronoi region polygon ID. All IDs should be integers.
:param poly_to_pt_assignments: dict mapping Voronoi region polygon IDs to list of point IDs
:return: dict mapping point ID to Voronoi region polygon ID
"""

pt_to_poly = {}
for poly, pts in poly_to_pt_assignments.items():
for pt in pts:
if pt in pt_to_poly.keys():
raise ValueError('invalid assignments in `poly_to_pt_assignments`: '
'point %d is assigned to several polygons' % pt)
pt_to_poly[pt] = poly

return pt_to_poly
Loading

0 comments on commit 96347c9

Please sign in to comment.