nataliegref commited on
Commit
b87b193
·
1 Parent(s): af925e5

modified marker size to be dynamic

Browse files
Files changed (2) hide show
  1. map-spike.py +0 -162
  2. plot_utils.py +18 -6
map-spike.py DELETED
@@ -1,162 +0,0 @@
1
- # Example taken from here - https://shinylive.io/py/examples/#map
2
-
3
- import os
4
- from shiny import reactive
5
- from shiny.express import input, render, ui
6
- from shinywidgets import render_widget
7
- import ipyleaflet as ipyl
8
-
9
- import pandas as pd
10
- import geopandas as gpd
11
- from ipyleaflet import Map, Rectangle, basemaps, basemap_to_tiles, LayersControl, ColorMap, FeatureGroup
12
- from branca.colormap import linear
13
-
14
- from shapely.geometry import box
15
-
16
- def point_to_square(row, size=0.0009):
17
- x, y = row.geometry.x, row.geometry.y
18
- half = size / 2
19
- return box(x - half, y - half, x + half, y + half)
20
-
21
-
22
-
23
- df = pd.read_csv("example.csv")
24
-
25
- # Step 2: Create GeoDataFrame with UTM Zone 15N
26
- gdf = gpd.GeoDataFrame(
27
- df,
28
- geometry=gpd.points_from_xy(df['x'], df['y']),
29
- crs="EPSG:32615" # <- Your current system
30
- )
31
-
32
-
33
-
34
- # Step 3: Reproject to WGS84 (lat/lon)
35
- gdf = gdf.to_crs("EPSG:4326")
36
-
37
- # Convert points to small square polygons
38
- gdf["geometry_box"] = gdf.apply(point_to_square, axis=1)
39
-
40
- print(gdf.head())
41
-
42
- # Step 4: Extract lat/lon columns for ipyleaflet
43
- gdf["lat"] = gdf.geometry.y
44
- gdf["lon"] = gdf.geometry.x
45
-
46
- temp_colormap = linear.YlOrRd_09.scale(df['LST'].min(), df['LST'].max())
47
-
48
- # def generate_temp_layer():
49
- # rectangles = []
50
- # for _, row in gdf.iterrows():
51
- # lat = row['lat']
52
- # lon = row['lon']
53
- # size_deg = 0.0009 # Approx ~100m (latitude degrees)
54
-
55
- # bounds = [
56
- # (lat - size_deg / 2, lon - size_deg / 2),
57
- # (lat + size_deg / 2, lon + size_deg / 2)
58
- # ]
59
-
60
- # color = temp_colormap(row['LST'])
61
-
62
- # rect = Rectangle(
63
- # bounds=bounds,
64
- # color=color,
65
- # fill_color=color,
66
- # fill_opacity=0.5,
67
- # weight=0
68
- # )
69
- # rectangles.append(rect)
70
- # return rectangles
71
-
72
- city_centers = {
73
- "London": (51.5074, 0.1278),
74
- "Paris": (48.8566, 2.3522),
75
- "New York": (40.7128, -74.0060),
76
- "Guatemala city": (14.6349149, -90.5068824),
77
- "Zone 15": (14.61925, -90.49386)
78
- }
79
-
80
- ui.input_text("Text", "Some vector", "Zone 15")
81
-
82
-
83
-
84
-
85
- @render_widget
86
- def map():
87
- city = use_in_model(input.Text())
88
- center = city_centers.get(city, (0, 0))
89
- m = Map(center=center, zoom=13, basemap=basemaps.OpenStreetMap.Mapnik)
90
- # Add rectangle overlay
91
-
92
- geo_data = ipyl.GeoData(
93
- geo_dataframe=gdf,
94
- style={
95
- "color": "black",
96
- "fillColor": "#3366cc",
97
- "opacity": 0.05,
98
- "weight": 1.9,
99
- "dashArray": "2",
100
- "fillOpacity": 0.6,
101
- },
102
- hover_style={"fillColor": "red", "fillOpacity": 0.2},
103
- name="Temperature",
104
- )
105
- m.add(geo_data)
106
- m.add(ipyl.LayersControl())
107
-
108
- return m
109
-
110
- # @render_widget
111
- # def map():
112
- # city = use_in_model(input.Text())
113
- # center = city_centers.get(city, (0, 0))
114
-
115
- # # m = Map(center=center, zoom=13, basemap=basemaps.OpenStreetMap.Mapnik)
116
- # m = Map(center=city_centers["Guatemala city"], zoom=13, basemap=basemaps.OpenStreetMap.Mapnik)
117
-
118
- # print(f"Centering on city: {city}")
119
-
120
- # # Add rectangle overlay
121
- # for rect in generate_temp_layer():
122
- # m.add_layer(rect)
123
-
124
- # # Optional: Add color legend
125
- # # m.add_control(temp_colormap.legend(title= "Temp"))
126
-
127
- # # Add controls
128
- # m.add_control(LayersControl(position="topright"))
129
-
130
- # return m
131
-
132
-
133
- def use_in_model(x):
134
- # TODO: Call some model loaded in huggingface_hub.
135
- # Define the client once then just call the
136
- # client from here.
137
-
138
- # from huggingface_hub import InferenceClient
139
- # client = InferenceClient(
140
- # api_key=os.environ["HF_TOKEN"],
141
- # provider="auto",
142
- # )
143
- #
144
- # completion = client.chat.completions.create(
145
- # model="deepseek-ai/DeepSeek-V3-0324",
146
- # messages=[{"role": "user", "content": "A story about hiking in the mountains"}]
147
- # )
148
-
149
- return x
150
-
151
-
152
- @render.text
153
- def text():
154
- return ("You entered: " + use_in_model(input.Text()))
155
-
156
-
157
- @reactive.effect
158
- def _():
159
- try:
160
- map.widget.center = city_centers[use_in_model(input.Text())]
161
- except:
162
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
plot_utils.py CHANGED
@@ -5,6 +5,13 @@ import matplotlib.gridspec as gridspec
5
 
6
 
7
  def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, label, split=False):
 
 
 
 
 
 
 
8
  print('split', split)
9
  if gdf.empty:
10
  raise ValueError("GeoDataFrame is empty")
@@ -26,7 +33,7 @@ def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, l
26
 
27
  if split == False:
28
  # First plot
29
- gdf.plot(column=name1, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=10)
30
 
31
  else:
32
  # Split data into three GeoDataFrames
@@ -36,13 +43,13 @@ def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, l
36
  above_colors = {"capacity":"chocolate", "100":"firebrick"}
37
 
38
  # Plot values ≤ 100 using colormap
39
- gdf.plot(column=name1, cmap=colorscheme, ax=axes[0], vmin=vmin, vmax=vmax, marker='s', markersize=10)
40
 
41
  # Plot values > capacity in orange
42
- above_capacity.plot(color=above_colors['capacity'], ax=axes[0], label='> capacidad debido a la construcción', marker='x', markersize=6)
43
 
44
  # Plot values > 100 in red
45
- above_100.plot(color=above_colors['100'], ax=axes[0], label='> 100% cobertura arbórea', marker='x', markersize=6)
46
 
47
  # Add legend manually for orange points
48
  orange_patch = plt.Line2D([0], [0], marker='o', color='w', label='> capacidad debido a la construcción',
@@ -60,7 +67,7 @@ def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, l
60
 
61
 
62
  # Second plot (original)
63
- gdf.plot(column=name2, cmap=cmap, norm=norm, ax=axes[1],marker='s', markersize=10)
64
  axes[1].set_title(title2, pad=-30)
65
  axes[1].set_axis_off()
66
 
@@ -74,6 +81,11 @@ def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, l
74
  return fig
75
 
76
  def show_one_plot(gdf, name, vmin, vmax, colorscheme, title, label):
 
 
 
 
 
77
  # Second set of plots
78
  cmap = plt.colormaps[colorscheme]
79
  norm = colors.Normalize(vmin, vmax)
@@ -85,7 +97,7 @@ def show_one_plot(gdf, name, vmin, vmax, colorscheme, title, label):
85
  axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1])]
86
 
87
 
88
- gdf.plot(column=name, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=10)
89
  axes[0].set_title(title, pad=-30)
90
  axes[0].set_axis_off()
91
 
 
5
 
6
 
7
  def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, label, split=False):
8
+ #dynamically change marker size
9
+ n_points = len(gdf)
10
+ marker_size = 8500 / n_points
11
+ marker_size = max(2, min(marker_size, 200))
12
+ filter_marker_size = marker_size*0.6
13
+ print(marker_size)
14
+
15
  print('split', split)
16
  if gdf.empty:
17
  raise ValueError("GeoDataFrame is empty")
 
33
 
34
  if split == False:
35
  # First plot
36
+ gdf.plot(column=name1, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=marker_size)
37
 
38
  else:
39
  # Split data into three GeoDataFrames
 
43
  above_colors = {"capacity":"chocolate", "100":"firebrick"}
44
 
45
  # Plot values ≤ 100 using colormap
46
+ gdf.plot(column=name1, cmap=colorscheme, ax=axes[0], vmin=vmin, vmax=vmax, marker='s', markersize=marker_size)
47
 
48
  # Plot values > capacity in orange
49
+ above_capacity.plot(color=above_colors['capacity'], ax=axes[0], label='> capacidad debido a la construcción', marker='x', markersize=filter_marker_size)
50
 
51
  # Plot values > 100 in red
52
+ above_100.plot(color=above_colors['100'], ax=axes[0], label='> 100% cobertura arbórea', marker='x', markersize=filter_marker_size)
53
 
54
  # Add legend manually for orange points
55
  orange_patch = plt.Line2D([0], [0], marker='o', color='w', label='> capacidad debido a la construcción',
 
67
 
68
 
69
  # Second plot (original)
70
+ gdf.plot(column=name2, cmap=cmap, norm=norm, ax=axes[1],marker='s', markersize=marker_size)
71
  axes[1].set_title(title2, pad=-30)
72
  axes[1].set_axis_off()
73
 
 
81
  return fig
82
 
83
  def show_one_plot(gdf, name, vmin, vmax, colorscheme, title, label):
84
+ #dynamically change marker size
85
+ n_points = len(gdf)
86
+ marker_size = 8500 / n_points
87
+ marker_size = max(2, min(marker_size, 200))
88
+
89
  # Second set of plots
90
  cmap = plt.colormaps[colorscheme]
91
  norm = colors.Normalize(vmin, vmax)
 
97
  axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1])]
98
 
99
 
100
+ gdf.plot(column=name, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=marker_size)
101
  axes[0].set_title(title, pad=-30)
102
  axes[0].set_axis_off()
103