1

I need to extract Risk Data Hub from the site: https://drmkc.jrc.ec.europa.eu/risk-data-hub/#/risk for a project.

If you go on the site, you can select the country (I need Romania), region, type of asset, type of hazzard, the TimeFrame and download the report for each. I already download everything the first time, but I need an automatization because the site implement new data.

I found on their site the the Risk Data Hub (RDH) API portal: https://drmkc.jrc.ec.europa.eu/risk-data-hub-api/docs/

I used it to download the polygons, but I can't download the Risk Data. I didn't found how to use the API to download the rest of information that I need.

If you try the following - visit https://drmkc.jrc.ec.europa.eu/risk-data-hub/#/risk - select an asset (say "Residential") and a hazard (say "River Flood"), then go to Romania, select a macro region, select a county, than you can download an excel containing the data (however, you need to go through all the dimension in the setting gear (years 1,2,5,10,15,25) in order to get all the possible data). The data that's available to be downloaded as per this example is not mentioned (or I am missing it) within the API documentation.

I used this code in R to download the polygons and it's work, but I didn't find how to modify the code for the rest of the information.

library(httr)
library(jsonlite)
library(geojsonsf)
library(leaflet)

jrc_codes <- readxl::read_excel("C:/Users/CG14328/Desktop/adm_unit_codes3.xlsx")

get_new_token <- function() {
token_url <- "https://drmkc.jrc.ec.europa.eu/risk-data-hub-api/ "
response <- GET(
url = token_url,
query = list(key = "demo_key"),
add_headers(apikey = "demo_api_key")
  )

if (status_code(response) != 200) {
stop("Failed to get token: ", content(response, "text"))
  }

message("Successfully obtained new token.")
content(response, "text")
}

safe_geojson_sf <- function(geojson) {
tryCatch(
geojsonsf::geojson_sf(geojson),
error = function(e) {
message("Error converting GeoJSON to sf: ", e$message)
NULL
    }
  )
}
 
division_url <- "https://drmkc.jrc.ec.europa.eu/risk-data-hub-api/collections/admin/division/items"

results <- list()

batch_size <- 5

num_batches <- ceiling(length(jrc_codes$Code) / batch_size)

for (batch_num in 1:num_batches) {

repeat {
token <- tryCatch(
get_new_token(),
error = function(e) {
message("Failed to obtain token: ", e$message, ". Retrying in 60 seconds...")
Sys.sleep(60)
NULL
      }
    )

if (!is.null(token)) break
  }


start_index <- (batch_num - 1) * batch_size + 1
end_index <- min(batch_num * batch_size, length(jrc_codes$Code))


admin_codes <- gsub("\"", "", jrc_codes$Code[start_index:end_index])


for (code in admin_codes) {
division_query_params <- list(
lang = "en-US",
admin_unit_code = code
    )

response <- GET(
url = division_url,
add_headers(Authorization = paste("Bearer", token)),
query = division_query_params
    )

if (status_code(response) == 200) {
data <- tryCatch(
content(response, "parsed"),
error = function(e) {
message("Failed to parse response for code ", code, ": ", e$message)
NULL
        }
      )

if (!is.null(data) && is.list(data) && !is.null(data$features) && length(data$features) > 0) {
results[[code]] <- data
else {
message("No valid features returned for code: ", code)
      }
else {
message("Failed to get data for code: ", code, " with status: ", status_code(response))
message("Response content: ", content(response, "text", encoding = "UTF-8"))
    }
  }

Sys.sleep(60)
}

combined_data <- lapply(results, function(res) safe_geojson_sf(toJSON(res, auto_unbox = TRUE)))

final_data <- do.call(rbind, combined_data[!sapply(combined_data, is.null)])

leaflet(final_data) %>%
addTiles() %>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.