From 510a97814597b3641057042715f7be1189c9c4ce Mon Sep 17 00:00:00 2001
From: Riccardo Boero <ribo@nilu.no>
Date: Wed, 3 Apr 2024 10:36:23 +0200
Subject: [PATCH] Added treatment of errors in elevation data and of missing
 job info in jobs

---
 src/FACT_elevation.jl | 55 ++++++++++++++++++++++++++++++++-----------
 src/FACT_jobs.jl      |  6 +++++
 2 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/src/FACT_elevation.jl b/src/FACT_elevation.jl
index bf2ac92..231a70c 100644
--- a/src/FACT_elevation.jl
+++ b/src/FACT_elevation.jl
@@ -30,28 +30,55 @@ function elevation_data_full(ws_host_port::String, selection::Dict)
     mean_elevations = Float64[]
     std_elevations = Float64[]
     median_elevations = Float64[]
-    entropy_elevations = Float64[] 
+    entropy_elevations = Float64[]
+    
     for (id, shape) in zip(geo_id, shape_obj)
+        # setting starting values to missing
+        min_elevation = missing
+        max_elevation = missing
+        mean_elevation = missing
+        std_elevation = missing
+        median_elevation = missing
+        entropy_elevation = missing
+
+
         # Construct the JSON payload
         json_payload = JSON.json(Dict("polygon_wkt" => shape))
         # Construct the URL
         url = "$ws_host_port/band_statistics"
         # Set headers for JSON content type
         headers = Dict("Content-Type" => "application/json")
-        # Make the POST request
-        response = HTTP.post(url, headers, json_payload)
-        # Parse the response
-        data = JSON.parse(String(response.body))
-        # Access the nested dictionary for band_0
-        band_data = data["band_0"]
-        # extract relevant info
+
+        try
+            # Make the POST request
+            response = HTTP.post(url, headers, json_payload)
+            # Ensure the response status code is 200 (OK)
+            if response.status != 200
+                throw(Exception("Request failed with status code $(response.status)"))
+            end
+            # Parse the response
+            data = JSON.parse(String(response.body))
+            # Access the nested dictionary for band_0
+            band_data = data["band_0"]
+            # extract relevant info
+            min_elevation = band_data["min"][1]
+            max_elevation = band_data["max"][1]
+            mean_elevation = band_data["mean"][1]
+            std_elevation = band_data["std"][1]
+            median_elevation = band_data["median"][1]
+            entropy_elevation = band_data["entropy"][1]
+        catch e
+            println("Error during HTTP request or response parsing: ", e)
+        end
+
+        # update info
         push!(geo_ids, id)
-        push!(min_elevations, band_data["min"][1])
-        push!(max_elevations, band_data["max"][1])
-        push!(mean_elevations, band_data["mean"][1])
-        push!(std_elevations, band_data["std"][1])
-        push!(median_elevations, band_data["median"][1])
-        push!(entropy_elevations, band_data["entropy"][1])
+        push!(min_elevations, min_elevation)
+        push!(max_elevations, max_elevation)
+        push!(mean_elevations, mean_elevation)
+        push!(std_elevations, std_elevation)
+        push!(median_elevations, median_elevation)
+        push!(entropy_elevations, entropy_elevation)
     end
     # object to be returned
     dftemp = DataFrame(
diff --git a/src/FACT_jobs.jl b/src/FACT_jobs.jl
index da921e5..cbb44f4 100644
--- a/src/FACT_jobs.jl
+++ b/src/FACT_jobs.jl
@@ -99,6 +99,12 @@ function us_qcew(conn::MySQL.Connection, selection::Dict)
         query = "SELECT Naics as industry, (Jan_jobs+Feb_jobs+Mar_jobs+Apr_jobs+May_jobs+Jun_jobs+Jul_jobs+Aug_jobs+Sep_jobs+Oct_jobs+Nov_jobs+Dec_jobs)/12 as jobs FROM QCEW WHERE Year = "*string(year)*" AND GeoID = '"*full_id*"' AND Agglvl_code = "*string(agglvl_code)*";"
         # execute
         result = query_connection(conn, query)
+        # Check if the 'industry' column exists
+        if !(:industry in names(result))
+            println("No data returned for query with GeoID = $full_id and Year = $year and AND Agglvl_code = $agglvl_code.")
+            # Initialize `result` with the expected columns but no rows
+            result = DataFrame(industry = String[], jobs = Float64[], geo_id = String[], agg_level = Int[])
+        end
         # add geo_id col
         result[!, :geo_id] = fill(id, nrow(result))
         # add agg level
-- 
GitLab