pgzmnk commited on
Commit
29ff8b7
1 Parent(s): 8c07762
Files changed (3) hide show
  1. app.py +1 -1
  2. utils/duckdb_queries.py +6 -2
  3. utils/indicators.py +13 -11
app.py CHANGED
@@ -5,7 +5,7 @@ from utils.gradio import get_window_url_params
5
  from utils.indicators import IndexGenerator
6
 
7
  # Instantiate outside gradio app to avoid re-initializing GEE, which is slow
8
- indexgenerator = IndexGenerator(indices=['NDWI','Water','Protected','Air'])
9
 
10
  with gr.Blocks() as demo:
11
  with gr.Column():
 
5
  from utils.indicators import IndexGenerator
6
 
7
  # Instantiate outside gradio app to avoid re-initializing GEE, which is slow
8
+ indexgenerator = IndexGenerator(indices=["NDWI", "Water", "Protected", "Air"])
9
 
10
  with gr.Blocks() as demo:
11
  with gr.Column():
utils/duckdb_queries.py CHANGED
@@ -28,12 +28,16 @@ def get_project_geometry(project_name):
28
  "SELECT geometry FROM project WHERE name = ? LIMIT 1", [project_name]
29
  ).fetchall()
30
 
 
31
  def get_project_centroid(project_name):
32
  # Workaround to get centroid of project
33
  # To-do: refactor to only use DuckDB spatial extension
34
  _geom = get_project_geometry(project_name)
35
- _polygon = json.dumps(json.loads(_geom[0][0])['features'][0]['geometry'])
36
- return con.sql(f"SELECT ST_X(ST_Centroid(ST_GeomFromGeoJSON('{_polygon}'))) AS longitude, ST_Y(ST_Centroid(ST_GeomFromGeoJSON('{_polygon}'))) AS latitude;").fetchall()[0]
 
 
 
37
 
38
  def get_project_scores(project_name, start_year, end_year):
39
  return con.execute(
 
28
  "SELECT geometry FROM project WHERE name = ? LIMIT 1", [project_name]
29
  ).fetchall()
30
 
31
+
32
  def get_project_centroid(project_name):
33
  # Workaround to get centroid of project
34
  # To-do: refactor to only use DuckDB spatial extension
35
  _geom = get_project_geometry(project_name)
36
+ _polygon = json.dumps(json.loads(_geom[0][0])["features"][0]["geometry"])
37
+ return con.sql(
38
+ f"SELECT ST_X(ST_Centroid(ST_GeomFromGeoJSON('{_polygon}'))) AS longitude, ST_Y(ST_Centroid(ST_GeomFromGeoJSON('{_polygon}'))) AS latitude;"
39
+ ).fetchall()[0]
40
+
41
 
42
  def get_project_scores(project_name, start_year, end_year):
43
  return con.execute(
utils/indicators.py CHANGED
@@ -13,7 +13,9 @@ from utils import duckdb_queries as dq
13
 
14
  from . import logging
15
 
16
- GEE_SERVICE_ACCOUNT = "climatebase-july-2023@ee-geospatialml-aquarry.iam.gserviceaccount.com"
 
 
17
  INDICES_FILE = "indices.yaml"
18
 
19
 
@@ -36,7 +38,6 @@ class IndexGenerator:
36
  self.indices = self._load_indices(INDICES_FILE)
37
  breakpoint()
38
 
39
-
40
  def _cloudfree(self, gee_path, daterange):
41
  """
42
  Internal method to generate a cloud-free composite.
@@ -141,7 +142,6 @@ class IndexGenerator:
141
  return out
142
 
143
  def generate_composite_index_df(self, year, project_geometry, indices=[]):
144
-
145
  data = {
146
  "metric": indices,
147
  "year": year,
@@ -175,22 +175,26 @@ class IndexGenerator:
175
  project_geometry = dq.get_project_geometry(project_name)
176
  project_centroid = dq.get_project_centroid(project_name)
177
  # to-do: refactor to involve less transformations
178
- _polygon = json.dumps(json.loads(project_geometry[0][0])['features'][0]['geometry'])
 
 
179
  # to-do: don't use self.roi and instead pass patameter strategically
180
- self.roi = ee.Geometry.Polygon(json.loads(_polygon)['coordinates'])
181
 
182
  # to-do: pararelize?
183
  for year in years:
184
  logging.info(year)
185
  self.project_name = project_name
186
- df = self.generate_composite_index_df(year, project_geometry, list(self.indices.keys()))
 
 
187
  dfs.append(df)
188
 
189
  # Concatenate all dataframes
190
  df_concat = pd.concat(dfs)
191
- df_concat['centroid'] = project_centroid
192
- df_concat['project_name'] = project_name
193
- df_concat['geojson'] = project_geometry
194
  breakpoint()
195
  return df_concat
196
 
@@ -299,5 +303,3 @@ class IndexGenerator:
299
  logging.info("upserted records into motherduck")
300
  scores = dq.get_project_scores(project_name, start_year, end_year)
301
  return scores
302
-
303
-
 
13
 
14
  from . import logging
15
 
16
+ GEE_SERVICE_ACCOUNT = (
17
+ "climatebase-july-2023@ee-geospatialml-aquarry.iam.gserviceaccount.com"
18
+ )
19
  INDICES_FILE = "indices.yaml"
20
 
21
 
 
38
  self.indices = self._load_indices(INDICES_FILE)
39
  breakpoint()
40
 
 
41
  def _cloudfree(self, gee_path, daterange):
42
  """
43
  Internal method to generate a cloud-free composite.
 
142
  return out
143
 
144
  def generate_composite_index_df(self, year, project_geometry, indices=[]):
 
145
  data = {
146
  "metric": indices,
147
  "year": year,
 
175
  project_geometry = dq.get_project_geometry(project_name)
176
  project_centroid = dq.get_project_centroid(project_name)
177
  # to-do: refactor to involve less transformations
178
+ _polygon = json.dumps(
179
+ json.loads(project_geometry[0][0])["features"][0]["geometry"]
180
+ )
181
  # to-do: don't use self.roi and instead pass patameter strategically
182
+ self.roi = ee.Geometry.Polygon(json.loads(_polygon)["coordinates"])
183
 
184
  # to-do: pararelize?
185
  for year in years:
186
  logging.info(year)
187
  self.project_name = project_name
188
+ df = self.generate_composite_index_df(
189
+ year, project_geometry, list(self.indices.keys())
190
+ )
191
  dfs.append(df)
192
 
193
  # Concatenate all dataframes
194
  df_concat = pd.concat(dfs)
195
+ df_concat["centroid"] = project_centroid
196
+ df_concat["project_name"] = project_name
197
+ df_concat["geojson"] = project_geometry
198
  breakpoint()
199
  return df_concat
200
 
 
303
  logging.info("upserted records into motherduck")
304
  scores = dq.get_project_scores(project_name, start_year, end_year)
305
  return scores