22
33import folium
44import jsonpath_ng
5+ from folium import plugins
56from folium .elements import JSCSSMixin
67from folium .map import Layer
78from jinja2 import Template
@@ -23,14 +24,14 @@ def arguments(parser):
2324 type = str ,
2425 nargs = "?" ,
2526 default = "" ,
26- help = "path to the geojson file to plot" ,
27+ help = "path to the GeoJSON file to plot" ,
2728 )
2829 parser .add_argument (
2930 "--jpath_geojson" ,
3031 type = str ,
3132 nargs = "?" ,
3233 default = "" ,
33- help = "JSON path to the geojson elements (XPATH like,"
34+ help = "JSON path to the GeoJSON elements (XPATH like,"
3435 + " see https://goessner.net/articles/JsonPath/,"
3536 + ' example: "state.routes[*].geojson")' ,
3637 )
@@ -149,27 +150,56 @@ def plot(
149150 bbox_sw [0 ], bbox_sw [1 ] = min (bbox_sw [0 ], sw [0 ]), min (bbox_sw [1 ], sw [1 ])
150151 bbox_ne [0 ], bbox_ne [1 ] = max (bbox_ne [0 ], ne [0 ]), max (bbox_ne [1 ], ne [1 ])
151152
152- # Make map plot of routes
153+ # Make map plot of geojson data
153154 map_file = output_map
154155 if not map_file :
155156 map_file = base_name + ".map.html"
156157 print (f"Plotting map to { map_file } " )
157- m = common .create_map (
158+ m , base_tree = common .create_map (
158159 (bbox_sw [1 ] + bbox_ne [1 ]) / 2.0 ,
159160 (bbox_sw [0 ] + bbox_ne [0 ]) / 2.0 ,
160161 custom_map_tile ,
161162 )
163+ plot_groups = {}
164+ group_names = {}
162165 for i , gj in enumerate (geojsons ):
163- group = folium .FeatureGroup (f"geojson { i } " )
166+ group_name = f"GeoJSON { i } "
167+ plot_groups [i ] = folium .FeatureGroup (name = group_name )
168+ group_names [plot_groups [i ]] = group_name
164169 if style :
165- StyledGeoJson (gj ).add_to (group )
170+ StyledGeoJson (gj ).add_to (plot_groups [ i ] )
166171 else :
167- folium .GeoJson (gj ).add_to (group )
168- group .add_to (m )
172+ folium .GeoJson (gj ).add_to (plot_groups [i ])
173+ plot_groups [i ].add_to (m )
174+
175+ # Add button to expand the map to fullscreen
176+ plugins .Fullscreen (
177+ position = "topright" ,
178+ title = "Expand me" ,
179+ title_cancel = "Exit me" ,
180+ ).add_to (m )
181+
182+ # Create overlay tree for advanced control of route/unassigned layers
183+ overlay_tree = {
184+ "label" : "Overlays" ,
185+ "select_all_checkbox" : "Un/select all" ,
186+ "children" : [
187+ {
188+ "label" : "GeoJSONs" ,
189+ "select_all_checkbox" : True ,
190+ "collapsed" : True ,
191+ "children" : [{"label" : group_names [v ], "layer" : v } for v in plot_groups .values ()],
192+ }
193+ ],
194+ }
169195
170196 # Add control for all layers and write file
171- folium .LayerControl ().add_to (m )
197+ plugins .TreeLayerControl (base_tree = base_tree , overlay_tree = overlay_tree ).add_to (m )
198+
199+ # Fit bounds
172200 m .fit_bounds ([sw , ne ])
201+
202+ # Save map
173203 m .save (map_file )
174204
175205
0 commit comments