mbuuck commited on
Commit
b1ee3a4
·
1 Parent(s): 12ca36b

Got map to load

Browse files
Files changed (1) hide show
  1. app.py +47 -106
app.py CHANGED
@@ -1,17 +1,5 @@
1
  import gradio as gr
2
  import plotly.graph_objects as go
3
- # import ee
4
- # # import geemap
5
-
6
- # # GEE
7
- # service_account = 'climatebase-july-2023@ee-geospatialml-aquarry.iam.gserviceaccount.com'
8
- # credentials = ee.ServiceAccountCredentials(service_account, 'service_account.json')
9
- # ee.Initialize(credentials)
10
-
11
- # # Gradio dataset
12
- # dataset = load_dataset("gradio/NYC-Airbnb-Open-Data", split="train")
13
- # df = dataset.to_pandas()
14
-
15
  import os
16
  import duckdb
17
  import pandas as pd
@@ -19,6 +7,9 @@ import datetime
19
  import ee
20
  # import geemap
21
  import yaml
 
 
 
22
 
23
  # Define constants
24
  MD_SERVICE_TOKEN = 'md_service_token.txt'
@@ -162,8 +153,8 @@ class IndexGenerator:
162
  "centroid": str(self.centroid),
163
  "project_name": self.project_name,
164
  "value": list(map(self.zonal_mean_index, indices)),
165
- "area": roi.area().getInfo(), # m^2
166
- "geojson": str(roi.getInfo()),
167
  }
168
 
169
  print('data', data)
@@ -214,47 +205,48 @@ def create_dataframe(years, project_name):
214
  dfs.append(df)
215
  return pd.concat(dfs)
216
 
217
- # def preview_table():
218
- # con.sql("FROM bioindicator;").show()
219
-
220
- # if __name__ == '__main__':
221
-
222
-
223
- # Map = geemap.Map()
224
-
225
-
226
- # # Create a cloud-free composite with custom parameters for cloud score threshold and percentile.
227
- # composite_cloudfree = ee.Algorithms.Landsat.simpleComposite(**{
228
- # 'collection': collection,
229
- # 'percentile': 75,
230
- # 'cloudScoreRange': 5
231
- # })
232
-
233
- # Map.addLayer(composite_cloudfree, {'bands': ['B4', 'B3', 'B2'], 'max': 128}, 'Custom TOA composite')
234
- # Map.centerObject(roi, 14)
235
-
236
-
237
- # ig = IndexGenerator(centroid=LOCATION, year=2015, indices_file=INDICES_FILE, project_name='Test Project', map=Map)
238
- # dataset = ig.generate_index(indices['Air'])
239
 
240
- # minMax = dataset.clip(roi).reduceRegion(
241
- # geometry = roi,
242
- # reducer = ee.Reducer.minMax(),
243
- # scale= 3000,
244
- # maxPixels= 10e3,
245
- # )
 
 
 
 
 
 
 
 
 
 
246
 
247
- # minMax.getInfo()
248
  def calculate_biodiversity_score(start_year, end_year, project_name):
249
  years = []
250
  for year in range(start_year, end_year):
251
- row_exists = con.sql(f"SELECT COUNT(1) FROM bioindicator WHERE (year = {year} AND project_name = '{project_name}')").fetchall()[0][0]
 
 
252
  if not row_exists:
253
  years.append(year)
254
 
255
  if len(years)>0:
256
  df = create_dataframe(years, project_name)
257
- # con.sql('FROM df LIMIT 5').show()
258
 
259
  # Write score table to `_temptable`
260
  con.sql('CREATE OR REPLACE TABLE _temptable AS SELECT *, (value * area) AS score FROM (SELECT year, project_name, AVG(value) AS value, area FROM df GROUP BY year, project_name, area ORDER BY project_name)')
@@ -265,12 +257,14 @@ def calculate_biodiversity_score(start_year, end_year, project_name):
265
  USE climatebase;
266
  CREATE TABLE IF NOT EXISTS bioindicator (year BIGINT, project_name VARCHAR(255), value DOUBLE, area DOUBLE, score DOUBLE, CONSTRAINT unique_year_project_name UNIQUE (year, project_name));
267
  """)
268
-
269
- return con.sql(f"SELECT * FROM bioindicator WHERE (year > {start_year} AND year <= {end_year} AND project_name = '{project_name}')").df()
 
 
270
 
271
  def view_all():
272
  print('view_all')
273
- return con.sql(f"SELECT * FROM bioindicator").df()
274
 
275
  def push_to_md():
276
  # UPSERT project record
@@ -280,68 +274,16 @@ def push_to_md():
280
  """)
281
  print('Saved records')
282
 
283
- # preview_table()
284
-
285
- def filter_map(min_price, max_price, boroughs):
286
-
287
- filtered_df = df[(df['neighbourhood_group'].isin(boroughs)) &
288
- (df['price'] > min_price) & (df['price'] < max_price)]
289
- names = filtered_df["name"].tolist()
290
- prices = filtered_df["price"].tolist()
291
- text_list = [(names[i], prices[i]) for i in range(0, len(names))]
292
- fig = go.Figure(go.Scattermapbox(
293
- customdata=text_list,
294
- lat=filtered_df['latitude'].tolist(),
295
- lon=filtered_df['longitude'].tolist(),
296
- mode='markers',
297
- marker=go.scattermapbox.Marker(
298
- size=6
299
- ),
300
- hoverinfo="text",
301
- hovertemplate='<b>Name</b>: %{customdata[0]}<br><b>Price</b>: $%{customdata[1]}'
302
- ))
303
-
304
- fig.update_layout(
305
- mapbox_style="open-street-map",
306
- hovermode='closest',
307
- mapbox=dict(
308
- bearing=0,
309
- center=go.layout.mapbox.Center(
310
- lat=40.67,
311
- lon=-73.90
312
- ),
313
- pitch=0,
314
- zoom=9
315
- ),
316
- )
317
-
318
- return fig
319
-
320
  with gr.Blocks() as demo:
321
  con = set_up_duckdb(MD_SERVICE_TOKEN)
322
  authenticate_gee(GEE_SERVICE_ACCOUNT, GEE_SERVICE_ACCOUNT_CREDENTIALS_FILE)
323
- # Create circle buffer over point
324
- # roi = ee.Geometry.Point(*LOCATION).buffer(ROI_RADIUS)
325
-
326
- # # Load a raw Landsat ImageCollection for a single year.
327
- # start_date = str(datetime.date(YEAR, 1, 1))
328
- # end_date = str(datetime.date(YEAR, 12, 31))
329
- # collection = (
330
- # ee.ImageCollection('LANDSAT/LC08/C02/T1')
331
- # .filterDate(start_date, end_date)
332
- # .filterBounds(roi)
333
- # )
334
-
335
- # indices = load_indices(INDICES_FILE)
336
- # push_to_md(START_YEAR, END_YEAR, 'Test Project')
337
  with gr.Column():
338
- # map = gr.Plot().style()
339
  with gr.Row():
340
  start_year = gr.Number(value=2017, label="Start Year", precision=0)
341
  end_year = gr.Number(value=2022, label="End Year", precision=0)
342
  project_name = gr.Textbox(label='Project Name')
343
- # boroughs = gr.CheckboxGroup(choices=["Queens", "Brooklyn", "Manhattan", "Bronx", "Staten Island"], value=["Queens", "Brooklyn"], label="Select Methodology:")
344
- # btn = gr.Button(value="Update Filter")
345
  with gr.Row():
346
  calc_btn = gr.Button(value="Calculate!")
347
  view_btn = gr.Button(value="View all")
@@ -351,10 +293,9 @@ with gr.Blocks() as demo:
351
  datatype=["number", "str", "number"],
352
  label="Biodiversity scores by year",
353
  )
354
- # demo.load(filter_map, [min_price, max_price, boroughs], map)
355
- # btn.click(filter_map, [min_price, max_price, boroughs], map)
356
- calc_btn.click(calculate_biodiversity_score, inputs=[start_year, end_year, project_name], outputs=results_df)
357
- view_btn.click(view_all, outputs=results_df)
358
  save_btn.click(push_to_md)
359
 
360
  demo.launch()
 
1
  import gradio as gr
2
  import plotly.graph_objects as go
 
 
 
 
 
 
 
 
 
 
 
 
3
  import os
4
  import duckdb
5
  import pandas as pd
 
7
  import ee
8
  # import geemap
9
  import yaml
10
+ import numpy as np
11
+ import json
12
+ import geojson
13
 
14
  # Define constants
15
  MD_SERVICE_TOKEN = 'md_service_token.txt'
 
153
  "centroid": str(self.centroid),
154
  "project_name": self.project_name,
155
  "value": list(map(self.zonal_mean_index, indices)),
156
+ "area": self.roi.area().getInfo(), # m^2
157
+ "geojson": str(self.roi.getInfo()),
158
  }
159
 
160
  print('data', data)
 
205
  dfs.append(df)
206
  return pd.concat(dfs)
207
 
208
+ def filter_map():
209
+ prepared_statement = \
210
+ con.execute("SELECT geometry FROM project WHERE name = ? LIMIT 1",
211
+ ["My project name"]).fetchall()
212
+ features = \
213
+ json.loads(prepared_statement[0][0].replace("\'", "\""))['features']
214
+ geometry = features[0]['geometry']
215
+ x_centroid = np.mean(np.array(geometry["coordinates"])[0, :, 0])
216
+ y_centroid = np.mean(np.array(geometry["coordinates"])[0, :, 1])
217
+ fig = go.Figure(go.Scattermapbox(
218
+ mode = "markers",
219
+ lon = [x_centroid], lat = [y_centroid],
220
+ marker = {'size': 20, 'color': ["cyan"]}))
 
 
 
 
 
 
 
 
 
221
 
222
+ fig.update_layout(
223
+ mapbox = {
224
+ 'style': "stamen-terrain",
225
+ 'center': { 'lon': x_centroid, 'lat': y_centroid},
226
+ 'zoom': 12, 'layers': [{
227
+ 'source': {
228
+ 'type': "FeatureCollection",
229
+ 'features': [{
230
+ 'type': "Feature",
231
+ 'geometry': geometry
232
+ }]
233
+ },
234
+ 'type': "fill", 'below': "traces", 'color': "royalblue"}]},
235
+ margin = {'l':0, 'r':0, 'b':0, 't':0})
236
+
237
+ return fig
238
 
 
239
  def calculate_biodiversity_score(start_year, end_year, project_name):
240
  years = []
241
  for year in range(start_year, end_year):
242
+ row_exists = \
243
+ con.execute("SELECT COUNT(1) FROM bioindicator WHERE (year = ? AND project_name = '?')",
244
+ [year, project_name]).fetchall()[0][0]
245
  if not row_exists:
246
  years.append(year)
247
 
248
  if len(years)>0:
249
  df = create_dataframe(years, project_name)
 
250
 
251
  # Write score table to `_temptable`
252
  con.sql('CREATE OR REPLACE TABLE _temptable AS SELECT *, (value * area) AS score FROM (SELECT year, project_name, AVG(value) AS value, area FROM df GROUP BY year, project_name, area ORDER BY project_name)')
 
257
  USE climatebase;
258
  CREATE TABLE IF NOT EXISTS bioindicator (year BIGINT, project_name VARCHAR(255), value DOUBLE, area DOUBLE, score DOUBLE, CONSTRAINT unique_year_project_name UNIQUE (year, project_name));
259
  """)
260
+ scores = \
261
+ con.execute("SELECT * FROM bioindicator WHERE (year > ? AND year <= ? AND project_name = '?')",
262
+ [start_year, end_year, project_name]).fetchall().df()
263
+ return scores
264
 
265
  def view_all():
266
  print('view_all')
267
+ return con.sql("SELECT * FROM bioindicator").df()
268
 
269
  def push_to_md():
270
  # UPSERT project record
 
274
  """)
275
  print('Saved records')
276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  with gr.Blocks() as demo:
278
  con = set_up_duckdb(MD_SERVICE_TOKEN)
279
  authenticate_gee(GEE_SERVICE_ACCOUNT, GEE_SERVICE_ACCOUNT_CREDENTIALS_FILE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
  with gr.Column():
281
+ m1 = gr.Plot()
282
  with gr.Row():
283
  start_year = gr.Number(value=2017, label="Start Year", precision=0)
284
  end_year = gr.Number(value=2022, label="End Year", precision=0)
285
  project_name = gr.Textbox(label='Project Name')
286
+
 
287
  with gr.Row():
288
  calc_btn = gr.Button(value="Calculate!")
289
  view_btn = gr.Button(value="View all")
 
293
  datatype=["number", "str", "number"],
294
  label="Biodiversity scores by year",
295
  )
296
+ demo.load(filter_map, outputs=[m1])
297
+ calc_btn.click(calculate_biodiversity_score, inputs=[start_year, end_year, project_name], outputs=[results_df])
298
+ view_btn.click(view_all, outputs=[results_df])
 
299
  save_btn.click(push_to_md)
300
 
301
  demo.launch()