So, after an entire day of work, I can’t find a proper way to do it, I depicted the map, but my grid doesn’t show up, I try with the coordinates stored in a .txt file, then in a .json and finally I came back to the .txt. nothing is working. I will let you see my code, maybe it could help. (I hope)
#!/usr/bin/env python3
import pandas as pd
import dash_leaflet as dl
from dash import Dash, Output, Input, State
from dash_extensions.javascript import assign
# Color selected state(s) red.
style_handle = assign('''function(feature, context){
const {selected} = context.hideout;
if(selected.includes(feature.properties.name)){
return {fillColor: 'red', color: 'grey'}
}
return {fillColor: 'grey', color: 'grey'}
}''')
# Create small example app.
app = Dash()
dl.FullScreenControl()
file_path = '/home/saltybear/Bureau/Geopy/wgs84GridCells.txt' # Change this to the path of your coordinates text file
# Read coordinates from a txt file and convert to float
def read_coordinates(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
coordinates = []
for line in lines:
coords = [float(coord) for coord in line.strip().split(',')]
coordinates.append(coords)
print("Coordinates:", coordinates)
return coordinates
# Function to create Rectangle components based on coordinates with unique IDs
def create_rectangles(coordinates):
rectangles = []
for i, bounds in enumerate(coordinates):
rectangle_id = f"rectangle_{i}"
rectangle = dl.Rectangle(bounds=bounds, id=rectangle_id)
rectangles.append(rectangle)
return rectangles
# Read coordinates from the file
coordinates = read_coordinates(file_path)
print("coordinates: ", coordinates)
# Create rectangles based on the coordinates with unique IDs
rectangles = create_rectangles(coordinates)
print("rectangles: ", rectangles)
app.layout = dl.Map([
dl.TileLayer(),
dl.GeoJSON(url='/path/to/your/worldMap.geo.json', zoomToBounds=True, id='geojson',
hideout=dict(selected=[]), style=style_handle)
], style={'height': '100vh'}, center=[50.85045, 4.34878], zoom=2)
@app.callback(
Output('geojson', 'hideout'),
Input('geojson', 'n_clicks'),
State('geojson', 'clickData'),
State('geojson', 'hideout'),
prevent_initial_call=True)
def toggle_select(_, feature, hideout):
selected = hideout['selected']
name = feature['properties']['name']
if name in selected:
selected.remove(name)
else:
selected.append(name)
return hideout
if __name__ == '__main__':
app.run_server()
my .txt is composed by 72 lines of coordinates organized like so:
90,180,60,150
60,180,30,150
30,180,00,150
00,180,-30,150
two lines equal one cell of the grid.