Very early attempts at combining Voronoi diagrams, data analysis and interactive visualization. The(still imprecise) goal is using Voronoi cells to cluster GeoDataframes and display informations about the resulting clusters. Ultimately, the method should be applicable with any set of polygons partitionning any type of GeoDataframe, even if the Dataframe contains complex shapes.
import pandas as pd
import qgrid as qg
import ipyleaflet as ipyl
import ipywidgets as ipyw
qg.set_defaults(show_toolbar=None, remote_js=True, precision=None, grid_options=None, export_mode=None)
df = pd.read_csv('../data/geo_centroids.csv')
cdf = df[['LAT','LONG']]
qg.show_grid(cdf)
# Map and label widgets
m= ipyl.Map(scroll_wheel_zoom=True, layout=ipyw.Layout(width='100%', height='500px'), center=[-0.3515602939922709, 22.5], zoom=2)
label = ipyw.Label(layout=ipyw.Layout(width='100%'))
display = ipyw.Button(
description='Voronoi cells',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Display proximity cells',
icon='check',
layout=ipyw.Layout(width='50%')
)
clean = ipyw.Button(
description='Clean map',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Remove proximity cells',
icon='check',
layout=ipyw.Layout(width='50%')
)
ipyw.VBox([m, ipyw.HBox([display, clean])])
def base_geojson():
return {"type": "Feature",'properties': {'style': {'clickable': True,
'color': '#f06eaa',
'dashArray': None,
'fill': True,
'fillColor': None,
'fillOpacity': 0.2,
'lineCap': None,
'lineJoin': None,
'noClip': True,
'opacity': 0.5,
'smoothFactor': 1,
'stroke': True,
'weight': 2}}}
from voronoi import *
list_polygons = list_voronoi_cells(cdf.values)
vor_poly = [{**base_geojson(), 'geometry':{'coordinates': geo.mapping(poly)["coordinates"], 'type': 'Polygon'}} for poly in list_polygons]
cell_layers = [ipyl.GeoJSON(data=geo_poly, hover_style={'fillColor': 'black', 'fillOpacity': .5}) for geo_poly in vor_poly]
seed_layers = [ipyl.Marker(location=pos) for pos in cdf.values.tolist()]
def vorono_lay(b):
for seed_layer in seed_layers:
m.add_layer(seed_layer)
for cell_layer in cell_layers:
m.add_layer(cell_layer)
def vorono_rem(b):
while len(m.layers) > 1:
m.remove_layer(m.layers[-1])
display.on_click(vorono_lay)
clean.on_click(vorono_rem)