Closed malmorrow closed 3 years ago
The mapping works. I think you mixed how the mapping in voronoi_points
works. It's a dict that maps a region ID to (possibly multiple) point IDs. The point IDs are indices into local_cities.geometry
. You should also be aware that this uses array indices 0 to N and not the pandas indices (which are, after filtering, 57, 58, 171, 189 in your case). So to find out the region ID for a given city it would be:
cities_list = local_cities.name.to_list()
city = 0
for reg_id, reg_pts in voronoi_points.items():
if city in reg_pts:
print(f'city #{city} {cities_list[city]} is in region #{reg_id}')
# returns city #0 Bloemfontein is in region #3
The whole hassle is because you could theoretically have multiple points in one region (if they're duplicates points or extremely close together). Btw. there's only a function points_to_region
to inverse the mapping which makes the whole thing easier:
from geovoronoi import points_to_region
pts_region = points_to_region(voronoi_points)
pts_region[city]
# returns 3
Thank you. I can go from here. I appreciate the help!
I'm trying to generate
geovoronoi
regions using a GeoDataFrame. The input points come from the dataframe'sgeometry
. Once I have the resulting polygons, I need a mapping of the rows in the dataframe to the regions generated by the library. I don't find any way to do this using the outputs fromvoronoi_regions_from_coords
. Here follows an example program using cities in South Africa. The idea would be to get the right city name back for each Voronoi region. It's not correct at the moment. What am I missing?The region polygon in
voronoi_regions[voronoi_points][0]]
is Cape Town, not Bloemfontein.