Skip to content

Commit

Permalink
Merge pull request #127 from technologiestiftung/staging
Browse files Browse the repository at this point in the history
feat: flag trees_watered if the tree is in map layer (#126)
  • Loading branch information
Jaszkowic authored May 28, 2024
2 parents aa3256f + f1cc83f commit f66efb1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
39 changes: 37 additions & 2 deletions harvester/src/mapbox_tree_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def generate_trees_csv(temp_dir, db_conn):
"""
)
trees = cur.fetchall()
trees = []

# Get all waterings that are included in the amount of waterings for the last 30 days
cur.execute(
"""
SELECT * FROM trees_watered w WHERE w.timestamp >= CURRENT_DATE - INTERVAL '30 days' AND DATE_TRUNC('day', w.timestamp) < CURRENT_DATE;
"""
)
trees_watered = cur.fetchall()

logging.info(f"Creating trees.csv file for {len(trees)} trees...")

# Build CSV file with all trees in it
Expand Down Expand Up @@ -109,7 +119,7 @@ def generate_trees_csv(temp_dir, db_conn):
with open(trees_csv_full_path, "w") as out:
out.write(trees_csv)

return trees_csv_full_path
return (trees_csv_full_path, trees_watered)


def update_mapbox_tree_layer(
Expand All @@ -125,7 +135,7 @@ def update_mapbox_tree_layer(

with tempfile.TemporaryDirectory() as temp_dir:
# Generate trees.csv from trees in database
trees_csv_full_path = generate_trees_csv(temp_dir, db_conn)
(trees_csv_full_path, trees_watered) = generate_trees_csv(temp_dir, db_conn)

# Preprocess trees.csv with tippecanoe
trees_preprocessed_full_path = preprocess_trees_csv(
Expand Down Expand Up @@ -164,3 +174,28 @@ def update_mapbox_tree_layer(

if tileset_creation_error is not None:
logging.error("Could not create Mapbox tileset")

return trees_watered


def update_tree_waterings(trees_watered, db_conn):
print(f"Set included_in_map_layer = TRUE for {len(trees_watered)} watered trees...")
tree_ids = [tree_watered[4] for tree_watered in trees_watered]
with db_conn.cursor() as cur:
# Set included_in_map_layer = FALSE for all waterings
cur.execute(
"""
UPDATE trees_watered SET included_in_map_layer = FALSE WHERE TRUE;
""",
(tree_ids,),
)
db_conn.commit()

# Set included_in_map_layer = FALSE for the waterings that are included in this round of the harvester
cur.execute(
"""
UPDATE trees_watered SET included_in_map_layer = TRUE WHERE id = ANY (%s);
""",
(tree_ids,),
)
db_conn.commit()
7 changes: 5 additions & 2 deletions harvester/src/run_harvester.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from radolan_db_utils import (
get_start_end_harvest_dates,
)
from mapbox_tree_update import update_mapbox_tree_layer
from mapbox_tree_update import update_mapbox_tree_layer, update_tree_waterings

# Set up logging
logging.basicConfig()
Expand Down Expand Up @@ -79,7 +79,7 @@

# Update Mapbox layer
if not SKIP_MAPBOX:
update_mapbox_tree_layer(
trees_watered = update_mapbox_tree_layer(
MAPBOX_USERNAME,
MAPBOX_TOKEN,
MAPBOX_TILESET,
Expand All @@ -89,3 +89,6 @@
SUPABASE_SERVICE_ROLE_KEY,
database_connection,
)

# Update the tree waterings and flag the waterings which are now included in the mapbox layer with included_in_map_layer = TRUE
update_tree_waterings(trees_watered, database_connection)

0 comments on commit f66efb1

Please sign in to comment.