Hi, I have a road network data, and the segment has an attribute called OBJ which I want to display on the map. I am able to create an initial map with 5 color classes; however, I would like to create some user-defined inputs so that those color class breaks can be changed later and the map will be re-rendered accordingly
As shown below, I create a function that assign varying color and weight to segment (5 color classes with 4 class breaks)
from dash_extensions.javascript import arrow_function,assign
breaks = [200,500,1000,2000]
colorramp = ["#488f31","#a8bb59","#ffe792","#f2975a","#de425b"]
color_schema_5c = assign("""function(geoJsonFeature){
const aobj = geoJsonFeature.properties.OBJ;
if (aobj <= %d) {
return {"color": "%s", "weight": "0.53"};
}
else if (aobj > %d && aobj <= %d) {
return {"color": "%s", "weight": "1.07"};
}
else if (aobj > %d && aobj <= %d) {
return {"color": "%s", "weight": "2.67"};
}
else if (aobj > %d && aobj <= %d) {
return {"color": "%s", "weight": "3.3"};
}
else {
return {"color": "%s", "weight": "4"};
}
}"""% (breaks[0],colorramp[0],breaks[0],breaks[1],colorramp[1],breaks[1],breaks[2],colorramp[2],breaks[2],breaks[3],colorramp[3],colorramp[4])
)
The created function is used in the style parameter of GeoJSON to add the initial layer
dl.GeoJSON(data=roaddata, id="road",style=color_schema_5c )
Since I intend to allow user to change the class breaks, I also define the class inputs
dcc.Input(value=f"{breaks[0]}",type= 'number',min=0,max=8252,step=1,id="class_break_1"),
dcc.Input(value=f"{breaks[1]}",type= 'number',min=0,max=8252,step=1,id="class_break_2"),
dcc.Input(value=f"{breaks[2]}",type= 'number',min=0,max=8252,step=1,id="class_break_3"),
dcc.Input(value=f"{breaks[3]}",type= 'number',min=0,max=8252,step=1,id="class_break_4")
I suppose the next step is to define a callback function. I attempt to write the callback function but map just does not get re-rendered as I change the input box value (error such as “No match for [dashExtensions.default.function1] in the global window object.” is thrown)
Any suggestion on how such a callback function should be wrote (like what output should be modified/returned by the function; whether style can be modified in this case directly or maybe the map has to be re-created?) or perhaps is there any other potential solution? I know ipyleaflet has a substitute method, but does not see similar method in dash-leaflet