Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: flag trees_watered if the tree is in map layer (#126) #127

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)