Artelys / geonetworkx

Python tools for geographic graphs
MIT License
34 stars 4 forks source link

PyVoronoiHelper: easily make the link between cells' polygon and cells' info #1

Closed RomainLaporte closed 3 years ago

RomainLaporte commented 4 years ago

Hi everybody,

First, I've just begun to use this library which I find simple and useful. So thanks.

When using PyVoronoiHelper.get_cells_as_gdf() it is hard to have the correspondence of the cell's polygon and the cell's info (like site, contains point etc...). Currently, the only info we have is the cell's id, so we need to call PyVoronoiHelper.get_cells_as_gdf() and PyVoronoiHelper.pv.GetCells() to manually retrieve cell's info.

My goal is to have cell's polygon associated to cell's site.

Is there a way to do it easier ?

RomainLaporte commented 4 years ago

Proposed patch :

From 081423e97541b916b663301399a5f9cbf916ca1b Mon Sep 17 00:00:00 2001
From: Romain Laporte <romain.laporte@delair.aero>
Date: Mon, 12 Oct 2020 14:05:13 +0200
Subject: [PATCH 1/1] voronoi: get more info with get_cells_as_gdf()

Cells as geodataframe can now have more info (see pyvoronoi project for
meaning) :
    - site
    - contains_point
    - contains_segment
    - is_open
    - is_degenerate

Other info given as list like "vertices" and "edges" are discarded for
now since writing those info into a file with "geopandas.to_file()" can
be troublesome.

Related to #1
---
 geonetworkx/utils/voronoi_utils.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/geonetworkx/utils/voronoi_utils.py b/geonetworkx/utils/voronoi_utils.py
index 251f172..7262555 100644
--- a/geonetworkx/utils/voronoi_utils.py
+++ b/geonetworkx/utils/voronoi_utils.py
@@ -27,13 +27,24 @@ class PyVoronoiHelper:
         self.discretization_tolerance = 10000 / scaling_factor
         self.bounding_box_coords = bounding_box_coords

-    def get_cells_as_gdf(self) -> gpd.GeoDataFrame:
+    def get_cells_as_gdf(self, with_more_attributes: bool = False) -> gpd.GeoDataFrame:
         """Returns the voronoi cells in `geodataframe` with a column named `id` referencing the index of the associated
          input geometry."""
         gdf = gpd.GeoDataFrame(columns=["id", "geometry"])
         cells_geometries = self.get_cells_as_polygons()
         gdf["geometry"] = list(cells_geometries.values())
         gdf["id"] = list(cells_geometries.keys())
+        if with_more_attributes:
+            cell_ids = gdf["id"].values
+            gdf["site"] = [self.pv.GetCell(cell_id).site for cell_id in cell_ids]
+            gdf["contains_point"] = [
+                self.pv.GetCell(cell_id).contains_point for cell_id in cell_ids
+            ]
+            gdf["contains_segment"] = [
+                self.pv.GetCell(cell_id).contains_segment for cell_id in cell_ids
+            ]
+            gdf["is_open"] = [self.pv.GetCell(cell_id).is_open for cell_id in cell_ids]
+            gdf["is_degenerate"] = [self.pv.GetCell(cell_id).is_degenerate for cell_id in cell_ids]
         return gdf

     def get_cells_as_polygons(self) -> dict:
-- 
2.25.1
Hyrae commented 4 years ago

Hi Romain,

Thanks for the feedback, your patch is really relevant. I merged it in the master, this will follow in a minor release.

Hugo

RomainLaporte commented 3 years ago

Hi @Hyrae , When will you make the minor release to include this patch ?

RomainLaporte commented 3 years ago

Hi @Hyrae, Any news on this subject ?

Hyrae commented 3 years ago

Hi Romain,

I just released GeoNetworkX 0.5.3 on pypi. Sorry for the delay.

RomainLaporte commented 3 years ago

Nice. Thank you.