kunifujiwara commited on
Commit
6e3a184
·
1 Parent(s): f57fc72

modify pages

Browse files
Files changed (1) hide show
  1. pages/1_🌲_Japan_Vegetation_Cover.py +37 -202
pages/1_🌲_Japan_Vegetation_Cover.py CHANGED
@@ -220,7 +220,6 @@ def get_saturday(in_date):
220
 
221
 
222
  def app():
223
-
224
  st.title("Japan Vegetation Cover Fraction")
225
  st.markdown(
226
  """**Introduction:** This interactive dashboard is designed for visualizing Japan Fractional Vegetation Cover at town block levels.
@@ -231,100 +230,14 @@ def app():
231
  """
232
  )
233
 
234
- with st.expander("See a demo"):
235
- st.image("https://i.imgur.com/Z3dk6Tr.gif")
 
 
236
 
237
- row1_col1, row1_col2, row1_col3, row1_col4, row1_col5 = st.columns(
238
- [0.6, 0.8, 0.6, 1.4, 2]
239
- )
240
- with row1_col1:
241
- frequency = st.selectbox("Monthly/weekly data", ["Monthly", "Weekly"])
242
- with row1_col2:
243
- types = ["Current month data", "Historical data"]
244
- if frequency == "Weekly":
245
- types.remove("Current month data")
246
- cur_hist = st.selectbox(
247
- "Current/historical data",
248
- types,
249
- )
250
- with row1_col3:
251
- if frequency == "Monthly":
252
- scale = st.selectbox(
253
- "Scale", ["National", "State", "Metro", "County"], index=3
254
- )
255
- else:
256
- scale = st.selectbox("Scale", ["National", "Metro"], index=1)
257
-
258
- gdf = get_geom_data(scale.lower())
259
-
260
- if frequency == "Weekly":
261
- inventory_df = get_inventory_data(data_links["weekly"][scale.lower()])
262
- weeks = get_weeks(inventory_df)
263
- with row1_col1:
264
- selected_date = st.date_input("Select a date", value=weeks[-1])
265
- saturday = get_saturday(selected_date)
266
- selected_period = saturday.strftime("%-m/%-d/%Y")
267
- if saturday not in weeks:
268
- st.error(
269
- "The selected date is not available in the data. Please select a date between {} and {}".format(
270
- weeks[0], weeks[-1]
271
- )
272
- )
273
- selected_period = weeks[-1].strftime("%-m/%-d/%Y")
274
- inventory_df = get_inventory_data(data_links["weekly"][scale.lower()])
275
- inventory_df = filter_weekly_inventory(inventory_df, selected_period)
276
-
277
- if frequency == "Monthly":
278
- if cur_hist == "Current month data":
279
- inventory_df = get_inventory_data(
280
- data_links["monthly_current"][scale.lower()]
281
- )
282
- selected_period = get_periods(inventory_df)[0]
283
- else:
284
- with row1_col2:
285
- inventory_df = get_inventory_data(
286
- data_links["monthly_historical"][scale.lower()]
287
- )
288
- start_year, end_year = get_start_end_year(inventory_df)
289
- periods = get_periods(inventory_df)
290
- with st.expander("Select year and month", True):
291
- selected_year = st.slider(
292
- "Year",
293
- start_year,
294
- end_year,
295
- value=start_year,
296
- step=1,
297
- )
298
- selected_month = st.slider(
299
- "Month",
300
- min_value=1,
301
- max_value=12,
302
- value=int(periods[0][-2:]),
303
- step=1,
304
- )
305
- selected_period = str(selected_year) + str(selected_month).zfill(2)
306
- if selected_period not in periods:
307
- st.error("Data not available for selected year and month")
308
- selected_period = periods[0]
309
- inventory_df = inventory_df[
310
- inventory_df["month_date_yyyymm"] == int(selected_period)
311
- ]
312
-
313
- data_cols = get_data_columns(inventory_df, scale.lower(), frequency.lower())
314
-
315
- with row1_col4:
316
- selected_col = st.selectbox("Attribute", data_cols)
317
- with row1_col5:
318
- show_desc = st.checkbox("Show attribute description")
319
- if show_desc:
320
- try:
321
- label, desc = get_data_dict(selected_col.strip())
322
- markdown = f"""
323
- **{label}**: {desc}
324
- """
325
- st.markdown(markdown)
326
- except:
327
- st.warning("No description available for selected attribute")
328
 
329
  row2_col1, row2_col2, row2_col3, row2_col4, row2_col5, row2_col6 = st.columns(
330
  [0.6, 0.68, 0.7, 0.7, 1.5, 0.8]
@@ -349,134 +262,56 @@ def app():
349
  else:
350
  elev_scale = 1
351
 
352
- gdf = join_attributes(gdf, inventory_df, scale.lower())
353
- gdf_null = select_null(gdf, selected_col)
354
- gdf = select_non_null(gdf, selected_col)
355
- gdf = gdf.sort_values(by=selected_col, ascending=True)
356
-
357
- colors = cm.get_palette(palette, n_colors)
358
- colors = [hex_to_rgb(c) for c in colors]
359
-
360
- for i, ind in enumerate(gdf.index):
361
- index = int(i / (len(gdf) / len(colors)))
362
- if index >= len(colors):
363
- index = len(colors) - 1
364
- gdf.loc[ind, "R"] = colors[index][0]
365
- gdf.loc[ind, "G"] = colors[index][1]
366
- gdf.loc[ind, "B"] = colors[index][2]
367
-
368
- initial_view_state = pdk.ViewState(
369
- latitude=40,
370
- longitude=-100,
371
- zoom=3,
372
- max_zoom=16,
373
- pitch=0,
374
- bearing=0,
375
- height=900,
376
- width=None,
377
- )
378
 
379
- min_value = gdf[selected_col].min()
380
- max_value = gdf[selected_col].max()
381
- color = "color"
382
- # color_exp = f"[({selected_col}-{min_value})/({max_value}-{min_value})*255, 0, 0]"
383
- color_exp = f"[R, G, B]"
384
 
385
- geojson = pdk.Layer(
 
386
  "GeoJsonLayer",
387
  gdf,
388
  pickable=True,
389
- opacity=0.5,
390
  stroked=True,
391
  filled=True,
392
  extruded=show_3d,
393
  wireframe=True,
394
- get_elevation=f"{selected_col}",
395
  elevation_scale=elev_scale,
396
- # get_fill_color="color",
397
- get_fill_color=color_exp,
398
  get_line_color=[0, 0, 0],
399
  get_line_width=2,
400
  line_width_min_pixels=1,
401
  )
402
 
403
- geojson_null = pdk.Layer(
404
- "GeoJsonLayer",
405
- gdf_null,
406
- pickable=True,
407
- opacity=0.2,
408
- stroked=True,
409
- filled=True,
410
- extruded=False,
411
- wireframe=True,
412
- # get_elevation="properties.ALAND/100000",
413
- # get_fill_color="color",
414
- get_fill_color=[200, 200, 200],
415
- get_line_color=[0, 0, 0],
416
- get_line_width=2,
417
- line_width_min_pixels=1,
418
  )
419
 
420
- # tooltip = {"text": "Name: {NAME}"}
421
-
422
- # tooltip_value = f"<b>Value:</b> {median_listing_price}""
423
- tooltip = {
424
- "html": "<b>Name:</b> {NAME}<br><b>Value:</b> {"
425
- + selected_col
426
- + "}<br><b>Date:</b> "
427
- + selected_period
428
- + "",
429
- "style": {"backgroundColor": "steelblue", "color": "white"},
430
- }
431
-
432
- layers = [geojson]
433
- if show_nodata:
434
- layers.append(geojson_null)
435
-
436
  r = pdk.Deck(
437
- layers=layers,
438
- initial_view_state=initial_view_state,
439
- map_style="light",
440
- tooltip=tooltip,
441
  )
442
 
443
- row3_col1, row3_col2 = st.columns([6, 1])
444
-
445
- with row3_col1:
446
- st.pydeck_chart(r)
447
- with row3_col2:
448
- st.write(
449
- cm.create_colormap(
450
- palette,
451
- label=selected_col.replace("_", " ").title(),
452
- width=0.2,
453
- height=3,
454
- orientation="vertical",
455
- vmin=min_value,
456
- vmax=max_value,
457
- font_size=10,
458
- )
459
- )
460
- row4_col1, row4_col2, row4_col3 = st.columns([1, 2, 3])
461
- with row4_col1:
462
- show_data = st.checkbox("Show raw data")
463
- with row4_col2:
464
- show_cols = st.multiselect("Select columns", data_cols)
465
- with row4_col3:
466
- show_colormaps = st.checkbox("Preview all color palettes")
467
- if show_colormaps:
468
- st.write(cm.plot_colormaps(return_fig=True))
469
- if show_data:
470
- if scale == "National":
471
- st.dataframe(gdf[["NAME", "GEOID"] + show_cols])
472
- elif scale == "State":
473
- st.dataframe(gdf[["NAME", "STUSPS"] + show_cols])
474
- elif scale == "County":
475
- st.dataframe(gdf[["NAME", "STATEFP", "COUNTYFP"] + show_cols])
476
- elif scale == "Metro":
477
- st.dataframe(gdf[["NAME", "CBSAFP"] + show_cols])
478
- elif scale == "Zip":
479
- st.dataframe(gdf[["GEOID10"] + show_cols])
480
 
481
 
482
  app()
 
220
 
221
 
222
  def app():
 
223
  st.title("Japan Vegetation Cover Fraction")
224
  st.markdown(
225
  """**Introduction:** This interactive dashboard is designed for visualizing Japan Fractional Vegetation Cover at town block levels.
 
230
  """
231
  )
232
 
233
+ prefecture = st.selectbox("Prefecture", ["Tokyo", "Kanagawa", "Chiba", "Saitama"])
234
+
235
+ # Load GeoJSON data
236
+ gdf = gpd.read_file(f'https://github.com/kunifujiwara/data/blob/master/frac_veg/FRAC_VEG_{prefecture}.geojson')
237
 
238
+ # Select attribute to visualize
239
+ attributes = gdf.select_dtypes(include=[float, int]).columns.tolist()
240
+ selected_attribute = st.selectbox("Select attribute to visualize", attributes)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
  row2_col1, row2_col2, row2_col3, row2_col4, row2_col5, row2_col6 = st.columns(
243
  [0.6, 0.68, 0.7, 0.7, 1.5, 0.8]
 
262
  else:
263
  elev_scale = 1
264
 
265
+ # Create color map
266
+ color_scale = cm.LinearColormap(colors=cm.get_palette(palette, n_colors), vmin=gdf[selected_attribute].min(), vmax=gdf[selected_attribute].max())
267
+ gdf['color'] = gdf[selected_attribute].apply(lambda x: color_scale(x))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
+ # Convert hex colors to RGB
270
+ gdf['color'] = gdf['color'].apply(lambda x: [int(x[1:3], 16), int(x[3:5], 16), int(x[5:7], 16)])
 
 
 
271
 
272
+ # Create PyDeck layer
273
+ layer = pdk.Layer(
274
  "GeoJsonLayer",
275
  gdf,
276
  pickable=True,
277
+ opacity=0.8,
278
  stroked=True,
279
  filled=True,
280
  extruded=show_3d,
281
  wireframe=True,
282
+ get_elevation=selected_attribute if show_3d else None,
283
  elevation_scale=elev_scale,
284
+ get_fill_color="color",
 
285
  get_line_color=[0, 0, 0],
286
  get_line_width=2,
287
  line_width_min_pixels=1,
288
  )
289
 
290
+ # Set initial view state
291
+ view_state = pdk.ViewState(
292
+ latitude=gdf.geometry.centroid.y.mean(),
293
+ longitude=gdf.geometry.centroid.x.mean(),
294
+ zoom=9,
295
+ pitch=45 if show_3d else 0,
 
 
 
 
 
 
 
 
 
296
  )
297
 
298
+ # Create PyDeck chart
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  r = pdk.Deck(
300
+ layers=[layer],
301
+ initial_view_state=view_state,
302
+ map_style="mapbox://styles/mapbox/light-v9",
303
+ tooltip={"text": "{NAME}\n{" + selected_attribute + "}"}
304
  )
305
 
306
+ # Display the map
307
+ st.pydeck_chart(r)
308
+
309
+ # Display color scale
310
+ st.write(color_scale)
311
+
312
+ # Option to show raw data
313
+ if st.checkbox("Show raw data"):
314
+ st.write(gdf[[selected_attribute, 'NAME']])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
 
316
 
317
  app()