read informations from Web Feature Service (WFS) with R - r

I like to use a Web Feature Service (WFS) [1] to get soil informations. But I am very unsure how to do it right, maybe you can give me a hint on how to get all available informations.
I can query bmf:Musterstueck and convert it to a Simple Feature. But if I do the same with so:SoilHorizon or so:ObservedSoilProfile I get the error message like
Error: Cannot open "https://via.bund.de/bmf/inspire/so/wfs?service=wfs&version=2.0.0&request=GetFeature&typename=so%3ASoilHorizon"; The file doesn't seem to exist.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 1: HTTP error code : 403
Thank you
Christof
[1] https://via.bund.de/bmf/inspire/so/wfs?service=wfs&request=getcapabilities
name title
bmf:Musterstueck Musterstücke der Bodenschätzung
so:SoilHorizon INSPIRE SoilHorizon
so:ObservedSoilProfile INSPIRE ObservedSoilProfile
library(tidyverse)
library(sf)
library(ows4R)
library(httr)
wfs = WFSClient$new("https://via.bund.de/bmf/inspire/so/wfs", "2.0.0", logger = "INFO")
wfs$getFeatureTypes(pretty = TRUE)
url <- parse_url("https://via.bund.de/bmf/inspire/so/wfs")
url$query <- list(service = "wfs",
version = "2.0.0",
request = "GetFeature",
typename = "bmf:Musterstueck")
request <- build_url(url)
request
bmf_musterstueck <- read_sf(request)
bmf_musterstueck

Related

rscopus package on R in MacBook - Invalid API key error

I am trying to use the Scopus API for the first time. I have the API key and the institution token. However, I am still getting an error, when I try to use it in R on my Mac. Here is my code:
library(rscopus)
set_api_key(MY_KEY)
hdr=inst_token_header(MY_TOKEN)
key=get_api_key()
print(rscopus::get_api_key(), reveal=TRUE)
have_api_key()
auth_info = process_author_name(last_name="Muschelli", first_name="John", verbose=FALSE)
The error message is:
> library(rscopus)
>
> set_api_key(MY_KEY)
> hdr=inst_token_header(MY_TOKEN)
> key=get_api_key()
> print(rscopus::get_api_key(), reveal=TRUE)
[1] "MY_KEY"
> have_api_key()
[1] TRUE
>
> if (have_api_key()) {
+ auth = elsevier_authenticate(api_key=key)
+ }
HTTP specified is: https://api.elsevier.com/authenticate
Warning message:
In elsevier_authenticate(api_key = key) : Forbidden (HTTP 403).
> auth_info = process_author_name(last_name="Muschelli", first_name="John", verbose=FALSE)
$`service-error`
$`service-error`$status
$`service-error`$status$statusCode
[1] "AUTHENTICATION_ERROR"
$`service-error`$status$statusText
[1] "Invalid API Key: valid apikey credentials required."
Error in get_complete_author_info(...) : Service Error
I tried
if (have_api_key()) {
auth = elsevier_authenticate(api_key=key)
}
but I get the error:
HTTP specified is: https://api.elsevier.com/authenticate
Warning message:
In elsevier_authenticate(api_key = key) : Forbidden (HTTP 403).
I have tried using auth_token_header(MY_TOKEN) instead of inst_token_header(MY_TOKEN) but the code is still not working.
I have also taken the following step in my terminal:
export Elsevier_API=MY_KEY > ~/.bash_profile
source ~/.bash_profile
I am still getting the error. However, the combination of key and institution token work here: https://dev.elsevier.com/scopus.html
Can anyone please help me debug this issue?
Thank You!
I figured out the issue. So, the correct way to query would be to pass the headers argument as well:
auth_info = process_author_name(last_name="Muschelli", first_name="John", verbose=FALSE, headers=hdr)
And now the code will work! :)

Error in downloading the WorldClim data in R using geoData package

I wanted to download the current WorldClim data for the var='bio' for specific tiles. I used the following code to get this done.
bio_curr <- worldclim_tile(var= 'bio', res= 0.5, lon= 5.5, lat = 45.5,
path = "D:/acca files/extra fields/Statistical Analysis/R/SDM analyisis/Ring Ouzel data set")
I got the following error message
trying URL 'https://geodata.ucdavis.edu/climate/worldclim/2_1/tiles/tile/tile_19_wc2.1_30s_bio.tif'
Error in utils::download.file(url = url, destfile = filename, quiet = quiet, :
cannot open URL 'https://geodata.ucdavis.edu/climate/worldclim/2_1/tiles/tile/tile_19_wc2.1_30s_bio.tif'
In addition: Warning message:
In utils::download.file(url = url, destfile = filename, quiet = quiet, :
URL 'https://geodata.ucdavis.edu/climate/worldclim/2_1/tiles/tile/tile_19_wc2.1_30s_bio.tif': Timeout of 900 seconds was reached
Error in .downloadDirect(turl, outfname, ...) : download failed
And I'm a windows user and tried the measure by ticking the TLS 1.2 in internet options advanced settings. It didn't work either
Thanks in advance

Error when trying to prase a HTTP-Request in R

im using R package httr to get a HTTP-Response for a specific link.
When trying to parse the content of the response im getting the Error:
Fehler in parse(text = script_content) : <text>:1:10: Unerwartete(s) '['
1: {"lines":[
Translated to enlgish it says something like this (sorry for my error messages being in German):
Error in parsing(text = script_content) : <text>1:10: Unexpected '['
1: {"lines":[
It seems as there is a problem with the format/encoding of the text. Here is my code:
script <-
GET(
url = "https://my_url.which_origin_is_not_important/my_script.R",
authenticate(username, pass)
)
script_content <- content(script, as = "text", encoding = "ISO-8859-1")
parsed_condent <- parse(text = script_content )
The value of script_content looks like this:
"{\"lines\":[{\"text\":\"################## FUNCTION ##################\"},{\"text\":\"\"},{\"text\":\"library(log4r)\"}],\"start\":0,\"size\":32,\"isLastPage\":true,\"limit\":500,\"nextPageStart\":null}"
Some more background to this operation: Im trying to source a code, which is currently inside of a private repository. I wrote the code myself i'm trying to source. I made sure, that the issue is not coming from within th code.
I got the solution from: Sourcing R files in a private github folder
Thanks for any advice!!

R: cannot read API call - Error in open.connection(con, "rb")

I can see the contents of the API call in webbrowser, but getting this error with jsonlite package: read_json.
Error in open.connection(con, "rb") : connection cannot be opened
Añso: Warning message:
In open.connection(con, "rb") :
cannot open URL 'https://www.plazavea.com.pe/api/catalog_system/pub/products/search?fq=C:/678/687/&_from=21&_to=41&O=OrderByScoreDESC&': HTTP status was '206 Partial Content'
Code::
library(rvest)
library(tidyverse)
library(jsonlite)
api_request <- "https://www.plazavea.com.pe/api/catalog_system/pub/products/search?fq=C:/678/687/&_from=21&_to=41&O=OrderByScoreDESC&"
product_data <- jsonlite::read_json(api_request)
Use httr and then extract as = 'text' and pass over to parse_json(), or simply specify as = 'parsed' in the content() call on the response object.
library(httr)
api_request <- "https://www.plazavea.com.pe/api/catalog_system/pub/products/search?fq=C:/678/687/&_from=21&_to=41&O=OrderByScoreDESC&"
product_data <- content(httr::GET(api_request), as = 'parsed')

Connecting using RAdwords library and using doAuth gives error

I have been trying to connect to Adwords Account using RAdwords, but I get the following error on doAuth():
Error in
rjson::fromJSON(RCurl::postForm("https://accounts.google.com/o/oauth2/token",
: STRING_ELT() can only be applied to a 'character vector', not a
'raw'
I have the correct credentials and Developer's token, but I am still unable to resolve the problem. I am using windows 7. The traceback is as follows:
> traceback()
4: .Call("fromJSON", json_str, unexpected.escape, simplify, PACKAGE = "rjson")
3: rjson::fromJSON(RCurl::postForm("https://accounts.google.com/o/oauth2/token",
.opts = opts, code = credlist$c.token, client_id = credlist$c.id,
client_secret = credlist$c.secret, redirect_uri = "urn:ietf:wg:oauth:2.0:oob",
grant_type = "authorization_code", style = "POST"))
2: loadToken(credentials)
1: doAuth()
Looked and tried all options from other similar questions, e.g.: using suggestions), I have also installed this version of RAdwords.
install_github('jburkhardt/RAdwords', ref = "refresh_token_raw_data")
Install the RAdwords package from the following GitHub branch containing a bug fix for your issue:
require(devtools)
install_github('jburkhardt/RAdwords', ref = "bugfix_char_to_raw")

Resources