SSA-Statistical-Team-Projects / GeoLink

GeoLink package
0 stars 1 forks source link

geolink_population() #28

Open dasalm20 opened 1 month ago

dasalm20 commented 1 month ago

I get this error: Error in py_module_import(module, convert = convert) : ModuleNotFoundError: No module named 'wpgpDownload' Run reticulate::py_last_error() for details.

code:

test_dt <- geolink_population(time_unit, start_year=2018, end_year = 2019, iso_code = "NGA", const_UNadj_2020 = Y, shp_dt = shp_dt[shp_dt$ADM1_EN == "Abia",], grid_size = 1000,

survey_dt = st_as_sf(hhgeo_dt[hhgeo_dt$ADM1_EN == "Abia",],extract_fun = "mean")

               )
DianaJaganjac commented 3 weeks ago

@dasalm20 we are no longer using python so this error will not occur.

@ifeanyi588 please help, this is the new code. It breaks at raster_list with this error:

raster_list <- lapply(raster_objs, raster) Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.

Do you have any insight?

geolink_population <- function(start_year = NULL,
                               end_year = NULL,
                               iso_code,
                               UN_adjst = NULL,
                               constrained = NULL,
                               bespoke = NULL,
                               version = NULL,
                               shp_dt = NULL,
                               shp_fn = NULL,
                               grid_size = 1000,
                               survey_dt,
                               survey_fn = NULL,
                               survey_lat = NULL,
                               survey_lon = NULL,
                               buffer_size = NULL,
                               extract_fun = "mean",
                               survey_crs = 4326) {

  if (!is.null(start_year) && !is.null(end_year)) {
    years <- seq(start_year, end_year)
    result_list <- paste0("ppp_", years)
  }

  if (!is.null(constrained) && constrained == "Y") {
    url1 <- paste0("https://data.worldpop.org/GIS/Population/Global_2000_2020_Constrained/2020/BSGM/", iso_code, "/")
    url2 <- paste0("https://data.worldpop.org/GIS/Population/Global_2000_2020_Constrained/2020/maxar_v1/", iso_code, "/")

    # Try url1 first, then url2 if url1 fails
    file_urls <- try_download(url1)
    if (is.null(file_urls)) {
      file_urls <- try_download(url2)
    }

    # If we have a list of file URLs, proceed to download
    if (!is.null(file_urls)) {
      download_files(file_urls, UN_adjst)
    } else {
      warning("No files found at both URLs.")
    }
  } else {
    if (!is.null(bespoke) && bespoke == "Y") {
      url <- paste0("https://data.worldpop.org/repo/wopr/", iso_code,
                    "/population/v", version, "/", iso_code, "_population_v",
                    gsub("\\.", "_", version), "_mastergrid.tif")
      download.file(url, file.path(tempdir(), basename(url)))
    } else {
      for (year in years) {
        url <- paste0("https://data.worldpop.org/GIS/Population/Global_2000_2020/", year, "/", iso_code, "/")

        file_urls <- try_download(url)

        # If we have a list of file URLs, proceed to download
        if (!is.null(file_urls)) {
          download_files(file_urls, UN_adjst)
        } else {
          warning(paste("No files found for year", year, "at URL", url))
        }
      }
    }
  }

  tif_files <- list.files(tempdir(), pattern = "\\.tif$", full.names = TRUE)

  raster_objs <- lapply(tif_files, terra::rast)

  raster_list <- lapply(raster_objs, raster)

  if (!is.null(start_year) && !is.null(end_year)) {
    year_sequence <- seq(lubridate::year(start_date), lubridate::year(end_date))
  } else {
    year_sequence <- year(start_date)
  }

  name_set <- paste0("population_", year_sequence)

  print("Population Raster Downloaded")

  dt <- postdownload_processor(shp_dt = shp_dt,
                               raster_objs = raster_objs,
                               shp_fn = shp_fn,
                               grid_size = grid_size,
                               survey_dt = survey_dt,
                               survey_fn = survey_fn,
                               survey_lat = survey_lat,
                               survey_lon = survey_lon,
                               extract_fun = extract_fun,
                               buffer_size = buffer_size,
                               survey_crs = survey_crs,
                               name_set = name_set)

  print("Process Complete!!!")

  return(dt)

}

Example usage


df <- geolink_population(iso_code = "NGA",
                         UN_adjst = "N",
                         constrained = "Y",
                         shp_dt = shp_dt[shp_dt$ADM1_EN == "Abia",],
                         grid_size = 1000,
                         extract_fun = "mean")
dasalm20 commented 1 week ago

Hi, Is there any updates on this ?

dasalm20 commented 5 days ago

@DianaJaganjac ,

When I run the following command, I get this: geolink_population(iso_code = "NGA", start_year = 2018, end_year = 2019, UN_adjst = "Y", constrained = "Y", shp_dt = shp_dt[shp_dt$ADM1_EN == "Abia",],

grid_size = 1000,

                           extract_fun = "mean")

Error in geolink_population(iso_code = "NGA", start_year = 2018, end_year = 2019, : object 'start_date' not found In addition: Warning messages: 1: In download.file(file_url, destfile, method = "auto") : URL https://data.worldpop.org/GIS/Population/Global_2000_2020_Constrained/2020/maxar_v1/NGA/nga_ppp_2020_UNadj_constrained.tif: cannot open destfile '/var/folders/gv/v64wc8fn2pq31hc53k09hg540000gn/T//Rtmp5MAjuR/nga_ppp_2020_UNadj_constrained.tif', reason 'No such file or directory' 2: In download.file(file_url, destfile, method = "auto") : download had nonzero exit status Error in file(con, "w") : cannot open the connection In addition: Warning message: In file(con, "w") : cannot open file '/var/folders/gv/v64wc8fn2pq31hc53k09hg540000gn/T//Rtmp5MAjuR/renv-hash-b8d17574cc2f': No such file or directory