Giter Site home page Giter Site logo

Comments (5)

jeffreyhanson avatar jeffreyhanson commented on May 21, 2024

Hey, I'm sorry I don't know of any immediate fixes off the top of my head. The code for downloading the data uses a virtual web browser to download the code and sometimes it can fail to correctly shut down the web browser when it finishes, which causes problems when you try and download more data. Could you please try restarting your computer and trying the code again (this will terminate the virtual web browser if it's still running)? Also, could you please type in traceback() after running the code and copy in the output messages here so I can exactly exactly where it's going wrong?

from wdpar.

stanleesocca avatar stanleesocca commented on May 21, 2024

Thanks for the reply. I tried the first option of restarting my computer but it didn't work. The traceback() result:

15: stop(err)
14: checkError(res)
13: queryRD(qpath, "POST", qdata = serverOpts)
12: rd$open(silent = TRUE)
11: tryCatchList(expr, classes, parentenv, handlers)
10: tryCatch({
        url <- character(0)
        pjs <- wdman::phantomjs(verbose = FALSE)
        rd <- RSelenium::remoteDriver(port = 4567L, browserName = "phantomjs")
        rd$open(silent = TRUE)
        rd$maxWindowSize()
        rd$navigate(paste0("https://protectedplanet.net/country/", 
            x))
        Sys.sleep(2)
        elem <- rd$findElement(using = "css", ".link-with-icon--bold")
        elem$clickElement()
        Sys.sleep(2)
        elem <- rd$findElement(using = "css", ".link-with-icon+ .link-with-icon")
        elem$clickElement()
        Sys.sleep(2)
        src <- xml2::read_html(rd$getPageSource()[[1]][[1]], encoding = "UTF-8")
        divs <- xml2::xml_find_all(src, ".//div")
        divs <- divs[which(xml2::xml_attr(divs, "id") == "download-modal")]
        attrs <- xml2::xml_attr(xml2::xml_find_all(divs, ".//a"), 
            "href")
        url <- grep("shapefile", attrs, fixed = TRUE, value = TRUE)
    }, finally = {
        try(rd$close(), silent = TRUE)
        try(rd$close(), silent = TRUE)
        try(pjs$stop(), silent = TRUE)
        try(pjs$stop(), silent = TRUE)
    })
9: withCallingHandlers(expr, message = function(c) invokeRestart("muffleMessage"))
8: suppressMessages(tryCatch({
       url <- character(0)
       pjs <- wdman::phantomjs(verbose = FALSE)
       rd <- RSelenium::remoteDriver(port = 4567L, browserName = "phantomjs")
       rd$open(silent = TRUE)
       rd$maxWindowSize()
       rd$navigate(paste0("https://protectedplanet.net/country/", 
           x))
       Sys.sleep(2)
       elem <- rd$findElement(using = "css", ".link-with-icon--bold")
       elem$clickElement()
       Sys.sleep(2)
       elem <- rd$findElement(using = "css", ".link-with-icon+ .link-with-icon")
       elem$clickElement()
       Sys.sleep(2)
       src <- xml2::read_html(rd$getPageSource()[[1]][[1]], encoding = "UTF-8")
       divs <- xml2::xml_find_all(src, ".//div")
       divs <- divs[which(xml2::xml_attr(divs, "id") == "download-modal")]
       attrs <- xml2::xml_attr(xml2::xml_find_all(divs, ".//a"), 
           "href")
       url <- grep("shapefile", attrs, fixed = TRUE, value = TRUE)
   }, finally = {
       try(rd$close(), silent = TRUE)
       try(rd$close(), silent = TRUE)
       try(pjs$stop(), silent = TRUE)
       try(pjs$stop(), silent = TRUE)
   }))
7: try_and_find_url(x)
6: wdpa_url(x, wait = wait)
5: wdpa_fetch("France")
4: as_Spatial(wdpa_fetch("France"))
3: eval(lhs, parent, parent)
2: eval(lhs, parent, parent)
1: as_Spatial(wdpa_fetch("France")) %>% filter(WDPAID == wdpaid)

Hope this will help with the bug.

from wdpar.

jeffreyhanson avatar jeffreyhanson commented on May 21, 2024

Thanks! Yeah, it looks like something strange is happening with the virtual web browser (Phantom JS). This could be due firewall stuff - I don't know sorry. In terms of getting a working solution on your system, you could try something like the code below where we hard-code the download URL:

# load pacakges
library(sf)
library(wdpar)

# set parameters
wdpaid <- "555526224"
download_url <-  "https://www.protectedplanet.net/downloads/WDPA_Aug2019_FRA?type=shapefile"
download_dir <- rappdirs::user_data_dir("wdpar")

# download the file
file_name <- basename(httr::HEAD(download_url)$url)
file_path <- file.path(download_dir, file_name)
curl::curl_download(download_url, file_path, quiet = FALSE)

# read the data
france_pa <- wdpa_read(file_path)

# subset the data
mpa <- france_pa %>% filter(WDPAID == wdpaid)

Please let me know if you have any problems with that. I'm working on a mobile phone hotspot connection at the moment so I can't test it myself.

from wdpar.

stanleesocca avatar stanleesocca commented on May 21, 2024

Cool. It works fine. Thanks for the help. Hopefully you get to fit the virtual browser issue and wdpa_fetch() pipeline as I imagine with such way means less code.

Thanks once again. Hopefully, one day I will understand all these stuff well enough to solve some of them.

from wdpar.

jeffreyhanson avatar jeffreyhanson commented on May 21, 2024

No worries - glad I could help.

from wdpar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.