From 58aeb3f52f0337b22097e23b7d00b95c330e6f09 Mon Sep 17 00:00:00 2001
From: lcalisto <luis.calisto@hotmail.com>
Date: Wed, 30 Aug 2023 15:10:54 +0200
Subject: [PATCH] Updated R scripts

---
 README.md | 235 +++---------------------------------------------------
 1 file changed, 13 insertions(+), 222 deletions(-)

diff --git a/README.md b/README.md
index d631613..c7c1043 100644
--- a/README.md
+++ b/README.md
@@ -263,7 +263,6 @@ query MyQuery {
     profileCode
     layers(first: 10) {
       layerId
-      licence
       layerNumber
       lowerDepth
       upperDepth
@@ -289,7 +288,7 @@ query MyQuery {
     profileCode
     layers(first: 10) {
       layerId
-      licence
+      date
       layerNumber
       lowerDepth
       upperDepth
@@ -297,7 +296,6 @@ query MyQuery {
       siltValues(first: 10) {
         valueAvg
         value
-        date
       }
     }
   }
@@ -321,7 +319,7 @@ query MyQuery {
     profileCode
     layers(first: 10) {
       layerId
-      licence
+      date
       layerNumber
       lowerDepth
       upperDepth
@@ -329,12 +327,10 @@ query MyQuery {
       siltValues(first: 10) {
         valueAvg
         value
-        date
       }
       orgcValues(first: 10) {
         valueAvg
         value
-        date
       }
     }
   }
@@ -994,204 +990,11 @@ parsed <- fromJSON(content(response, "text"), flatten = TRUE)
 df <- as.data.frame(parsed$data$wosisLatestProfiles)
 
 head(df)
-```
-
-The result will be:
-
-![r_q1_result](./images/r_q1_result.png "r_q1_result")
-
-Using variables in our script: 
-
-- Get the __first 3 profiles__ that are __inside Gelderland region__ and add it to a Pandas dataframe:
-
-```r
-library(httr)
-library(jsonlite)
-
-geomGelderland <- list(
-    type = "MultiPolygon",
-    coordinates = list(
-        list(
-            list(
-                c(5.177260142422514, 51.74291774914947), c(5.126747881386732, 51.737828850498403), c(5.137580867932065, 51.772905259431077), c(5.014540023249575, 51.808984680959583), c(5.031415073146523, 51.841084802107702), c(4.993967909252922, 51.861222725420994), c(5.062358224116345, 51.859362053527242), c(5.180226727863164, 51.96744832651509), c(5.236867149255078, 51.978757478459428), c(5.321611332014112, 51.954919171164796), c(5.486214078473083, 51.98382644510454), c(5.627223829712356, 51.952386168324438), c(5.550342661060417, 52.10541954546126), c(5.459242995490565, 52.080225755481266), c(5.514079463312799, 52.135923065932062), c(5.439875615559026, 52.171197458274222), c(5.44103943147957, 52.205693438951691), c(5.393219147822698, 52.220626892173925), c(5.404643399611359, 52.249630480909225), c(5.533281176545358, 52.27274084169683), c(5.587707385036856, 52.361454261431376), c(5.787257137970521, 52.422573287061603), c(5.876205471530124, 52.522025026941051), c(5.925559518063968, 52.474057592745915), c(6.027857569808684, 52.509606205409327), c(6.099483437203417, 52.469970896552461), c(6.130552948323514, 52.399978162269164), c(6.078506385563601, 52.369523051161245), c(6.066224466859907, 52.318839289847247), c(6.163909067147507, 52.21749619292715), c(6.38185154627214, 52.246112812566473), c(6.492401220236633, 52.177371870181403), c(6.671338986248984, 52.165683203635673), c(6.662399005672591, 52.130167439615931), c(6.760572413121598, 52.118779940206082), c(6.687853003658449, 52.039856158091141), c(6.832754328999235, 51.972938087693585), c(6.721969582522561, 51.89606334135938), c(6.683993990179909, 51.91757645733221), c(6.472507886098918, 51.853823023864017), c(6.390566170881016, 51.87396806966867), c(6.401818441765064, 51.827262656663407), c(6.117889496603739, 51.901659142837225), c(6.166559884993931, 51.840721643435401), c(6.063485632339608, 51.86545122678897), c(5.962978284523374, 51.836913960582471), c(5.946569966406273, 51.813479919592751), c(5.992067051189349, 51.770245909123908), c(5.943962150919553, 51.741816814422592), c(5.893409336802974, 51.777852926426895), c(5.765188291802036, 51.752789880063702), c(5.638112608999517, 51.819025176083443), c(5.493105254357093, 51.830750957327069), c(5.403157084105017, 51.821611677731141), c(5.357568231054432, 51.757890339715857), c(5.300338754648935, 51.737287437014395), c(5.177260142422514, 51.74291774914947)
-            )
-        )
-    )
-)
-
-
-# GraphQL query
-query <- "
-query MyQuery($geomGelderland: GeoJSON!) {
-  wosisLatestProfiles(
-    first: 3
-    filter: {layersExist: true, geom: {intersects: $geomGelderland}}
-  ) {
-    continent
-    region
-    profileId
-    datasetCode
-    latitude
-    longitude
-    geom {
-      geojson
-      x
-      y
-    }
-  }
-}
-"
-
-# GraphQL endpoint
-url <- "https://graphql.isric.org/wosis/graphql"
-
-# Send POST request
-response <- POST(url, body = list(query = query, variables = list(geomGelderland = geomGelderland)), encode = "json")
-
-# Print status_code
-print(status_code(response))
-
-# Parse JSON
-parsed <- fromJSON(content(response, "text"), flatten = TRUE)
-
-# Convert to data frame
-df <- as.data.frame(parsed$data$wosisLatestProfiles)
-
-# Print data frame
-print(df)
 
 ```
 
 The result will be:
 
-![r_q2_result](./images/r_q2_result.png "r_q2_result")
-
-- Get __all WoSIS profiles with layers that exist in Gelderland__ and also __export it to CSV__.
-
-
-```r
-library(httr)
-library(jsonlite)
-library(dplyr)
-
-# GeoJSON geometry
-geomGelderland <- list(
-    type = "MultiPolygon",
-    coordinates = list(
-        list(
-            list(
-                c(5.177260142422514, 51.74291774914947), c(5.126747881386732, 51.737828850498403), c(5.137580867932065, 51.772905259431077), c(5.014540023249575, 51.808984680959583), c(5.031415073146523, 51.841084802107702), c(4.993967909252922, 51.861222725420994), c(5.062358224116345, 51.859362053527242), c(5.180226727863164, 51.96744832651509), c(5.236867149255078, 51.978757478459428), c(5.321611332014112, 51.954919171164796), c(5.486214078473083, 51.98382644510454), c(5.627223829712356, 51.952386168324438), c(5.550342661060417, 52.10541954546126), c(5.459242995490565, 52.080225755481266), c(5.514079463312799, 52.135923065932062), c(5.439875615559026, 52.171197458274222), c(5.44103943147957, 52.205693438951691), c(5.393219147822698, 52.220626892173925), c(5.404643399611359, 52.249630480909225), c(5.533281176545358, 52.27274084169683), c(5.587707385036856, 52.361454261431376), c(5.787257137970521, 52.422573287061603), c(5.876205471530124, 52.522025026941051), c(5.925559518063968, 52.474057592745915), c(6.027857569808684, 52.509606205409327), c(6.099483437203417, 52.469970896552461), c(6.130552948323514, 52.399978162269164), c(6.078506385563601, 52.369523051161245), c(6.066224466859907, 52.318839289847247), c(6.163909067147507, 52.21749619292715), c(6.38185154627214, 52.246112812566473), c(6.492401220236633, 52.177371870181403), c(6.671338986248984, 52.165683203635673), c(6.662399005672591, 52.130167439615931), c(6.760572413121598, 52.118779940206082), c(6.687853003658449, 52.039856158091141), c(6.832754328999235, 51.972938087693585), c(6.721969582522561, 51.89606334135938), c(6.683993990179909, 51.91757645733221), c(6.472507886098918, 51.853823023864017), c(6.390566170881016, 51.87396806966867), c(6.401818441765064, 51.827262656663407), c(6.117889496603739, 51.901659142837225), c(6.166559884993931, 51.840721643435401), c(6.063485632339608, 51.86545122678897), c(5.962978284523374, 51.836913960582471), c(5.946569966406273, 51.813479919592751), c(5.992067051189349, 51.770245909123908), c(5.943962150919553, 51.741816814422592), c(5.893409336802974, 51.777852926426895), c(5.765188291802036, 51.752789880063702), c(5.638112608999517, 51.819025176083443), c(5.493105254357093, 51.830750957327069), c(5.403157084105017, 51.821611677731141), c(5.357568231054432, 51.757890339715857), c(5.300338754648935, 51.737287437014395), c(5.177260142422514, 51.74291774914947)
-            )
-        )
-    )
-)
-
-# GraphQL query
-query <- "
-query MyQuery($first: Int, $offset: Int, $geomGelderland: GeoJSON!) {
-  wosisLatestProfiles(
-    first: $first,
-    offset: $offset,
-    filter: {layersExist: true, geom: {intersects: $geomGelderland}}
-  ) {
-    continent
-    region
-    profileId
-    datasetCode
-    latitude
-    longitude
-  }
-}
-"
-
-# GraphQL endpoint
-url <- "https://graphql.isric.org/wosis/graphql"
-
-new_results <- TRUE
-first <- 100
-offset <- 0
-all_results <- list()
-
-while (new_results) {
-    # Send POST request
-    response <- POST(url, body = list(query = query, variables = list(
-        first = first,
-        offset = offset, geomGelderland = geomGelderland
-    )), encode = "json")
-    # Parse JSON
-    parsed <- fromJSON(content(response, "text"), flatten = TRUE)
-    # Add results to all_results list
-    all_results <- append(all_results, list(parsed$data$wosisLatestProfiles))
-
-    if (!"wosisLatestProfiles" %in% names(parsed$data) || length(parsed$data$wosisLatestProfiles) == 0) {
-        print("No more results")
-        # update new_results
-        new_results <- FALSE
-    } else {
-        print("We have more results")
-        # update offset
-        offset <- offset + first
-    }
-}
-
-df <- bind_rows(all_results) %>% as_tibble()
-# print dataframe
-cat("There are", nrow(df), "WoSIS profiles with layers inside Gelderland region\n")
-# Export dataframe to CSV
-write.csv(df, "wosis_gelderland.csv", row.names = FALSE, quote = FALSE)
-
-
-```
-The result will be:
-
-`There are 136 WoSIS profiles with layers inside Gelderland region`
-
-CSV result file can be found [here](./scripts/r/wosis_gelderland.csv)
-
-The simplest way to perform a graphQL request in r is to use {httr}.
-
-- Get the fist 5 profiles and add it to a Pandas dataframe:
-
-```r
-library(httr)
-library(jsonlite)
-
-# GraphQL query
-query <- '
-query MyQuery {
-  wosisLatestProfiles(first: 5) {
-    continent
-    region
-    countryName
-    datasetCode
-    latitude
-    longitude
-    geomAccuracy
-    profileCode
-  }
-}
-'
-
-# GraphQL endpoint
-url <- 'https://graphql.isric.org/wosis/graphql'
-
-# Send POST request
-response <- POST(url, body = list(query = query), encode = "json")
-
-# Print status_code
-print(status_code(response))
-
-# Parse JSON
-parsed <- fromJSON(content(response, "text"), flatten = TRUE)
-
-## convert the from json to dataframe object
-df <- as.data.frame(parsed$data$wosisLatestProfiles)
-
-head(df)
-```
-
-The result will be:
-
 ![r_q1_result](./images/r_q1_result.png "r_q1_result")
 
 Using variables in our script: 
@@ -1202,17 +1005,12 @@ Using variables in our script:
 library(httr)
 library(jsonlite)
 
-geomGelderland <- list(
-    type = "MultiPolygon",
-    coordinates = list(
-        list(
-            list(
-                c(5.177260142422514, 51.74291774914947), c(5.126747881386732, 51.737828850498403), c(5.137580867932065, 51.772905259431077), c(5.014540023249575, 51.808984680959583), c(5.031415073146523, 51.841084802107702), c(4.993967909252922, 51.861222725420994), c(5.062358224116345, 51.859362053527242), c(5.180226727863164, 51.96744832651509), c(5.236867149255078, 51.978757478459428), c(5.321611332014112, 51.954919171164796), c(5.486214078473083, 51.98382644510454), c(5.627223829712356, 51.952386168324438), c(5.550342661060417, 52.10541954546126), c(5.459242995490565, 52.080225755481266), c(5.514079463312799, 52.135923065932062), c(5.439875615559026, 52.171197458274222), c(5.44103943147957, 52.205693438951691), c(5.393219147822698, 52.220626892173925), c(5.404643399611359, 52.249630480909225), c(5.533281176545358, 52.27274084169683), c(5.587707385036856, 52.361454261431376), c(5.787257137970521, 52.422573287061603), c(5.876205471530124, 52.522025026941051), c(5.925559518063968, 52.474057592745915), c(6.027857569808684, 52.509606205409327), c(6.099483437203417, 52.469970896552461), c(6.130552948323514, 52.399978162269164), c(6.078506385563601, 52.369523051161245), c(6.066224466859907, 52.318839289847247), c(6.163909067147507, 52.21749619292715), c(6.38185154627214, 52.246112812566473), c(6.492401220236633, 52.177371870181403), c(6.671338986248984, 52.165683203635673), c(6.662399005672591, 52.130167439615931), c(6.760572413121598, 52.118779940206082), c(6.687853003658449, 52.039856158091141), c(6.832754328999235, 51.972938087693585), c(6.721969582522561, 51.89606334135938), c(6.683993990179909, 51.91757645733221), c(6.472507886098918, 51.853823023864017), c(6.390566170881016, 51.87396806966867), c(6.401818441765064, 51.827262656663407), c(6.117889496603739, 51.901659142837225), c(6.166559884993931, 51.840721643435401), c(6.063485632339608, 51.86545122678897), c(5.962978284523374, 51.836913960582471), c(5.946569966406273, 51.813479919592751), c(5.992067051189349, 51.770245909123908), c(5.943962150919553, 51.741816814422592), c(5.893409336802974, 51.777852926426895), c(5.765188291802036, 51.752789880063702), c(5.638112608999517, 51.819025176083443), c(5.493105254357093, 51.830750957327069), c(5.403157084105017, 51.821611677731141), c(5.357568231054432, 51.757890339715857), c(5.300338754648935, 51.737287437014395), c(5.177260142422514, 51.74291774914947)
-            )
-        )
-    )
-)
 
+geomGelderland <- fromJSON('{
+    "type": "MultiPolygon",
+    "coordinates": [ [ [ [ 5.177260142422514, 51.74291774914947 ], [ 5.126747881386732, 51.737828850498403 ], [ 5.137580867932065, 51.772905259431077 ], [ 5.014540023249575, 51.808984680959583 ], [ 5.031415073146523, 51.841084802107702 ], [ 4.993967909252922, 51.861222725420994 ], [ 5.062358224116345, 51.859362053527242 ], [ 5.180226727863164, 51.96744832651509 ], [ 5.236867149255078, 51.978757478459428 ], [ 5.321611332014112, 51.954919171164796 ], [ 5.486214078473083, 51.98382644510454 ], [ 5.627223829712356, 51.952386168324438 ], [ 5.550342661060417, 52.10541954546126 ], [ 5.459242995490565, 52.080225755481266 ], [ 5.514079463312799, 52.135923065932062 ], [ 5.439875615559026, 52.171197458274222 ], [ 5.44103943147957, 52.205693438951691 ], [ 5.393219147822698, 52.220626892173925 ], [ 5.404643399611359, 52.249630480909225 ], [ 5.533281176545358, 52.27274084169683 ], [ 5.587707385036856, 52.361454261431376 ], [ 5.787257137970521, 52.422573287061603 ], [ 5.876205471530124, 52.522025026941051 ], [ 5.925559518063968, 52.474057592745915 ], [ 6.027857569808684, 52.509606205409327 ], [ 6.099483437203417, 52.469970896552461 ], [ 6.130552948323514, 52.399978162269164 ], [ 6.078506385563601, 52.369523051161245 ], [ 6.066224466859907, 52.318839289847247 ], [ 6.163909067147507, 52.21749619292715 ], [ 6.38185154627214, 52.246112812566473 ], [ 6.492401220236633, 52.177371870181403 ], [ 6.671338986248984, 52.165683203635673 ], [ 6.662399005672591, 52.130167439615931 ], [ 6.760572413121598, 52.118779940206082 ], [ 6.687853003658449, 52.039856158091141 ], [ 6.832754328999235, 51.972938087693585 ], [ 6.721969582522561, 51.89606334135938 ], [ 6.683993990179909, 51.91757645733221 ], [ 6.472507886098918, 51.853823023864017 ], [ 6.390566170881016, 51.87396806966867 ], [ 6.401818441765064, 51.827262656663407 ], [ 6.117889496603739, 51.901659142837225 ], [ 6.166559884993931, 51.840721643435401 ], [ 6.063485632339608, 51.86545122678897 ], [ 5.962978284523374, 51.836913960582471 ], [ 5.946569966406273, 51.813479919592751 ], [ 5.992067051189349, 51.770245909123908 ], [ 5.943962150919553, 51.741816814422592 ], [ 5.893409336802974, 51.777852926426895 ], [ 5.765188291802036, 51.752789880063702 ], [ 5.638112608999517, 51.819025176083443 ], [ 5.493105254357093, 51.830750957327069 ], [ 5.403157084105017, 51.821611677731141 ], [ 5.357568231054432, 51.757890339715857 ], [ 5.300338754648935, 51.737287437014395 ], [ 5.177260142422514, 51.74291774914947 ] ] ] ]
+    }
+')
 
 # GraphQL query
 query <- "
@@ -1252,7 +1050,7 @@ parsed <- fromJSON(content(response, "text"), flatten = TRUE)
 df <- as.data.frame(parsed$data$wosisLatestProfiles)
 
 # Print data frame
-print(df)
+head(df)
 
 ```
 
@@ -1269,16 +1067,11 @@ library(jsonlite)
 library(dplyr)
 
 # GeoJSON geometry
-geomGelderland <- list(
-    type = "MultiPolygon",
-    coordinates = list(
-        list(
-            list(
-                c(5.177260142422514, 51.74291774914947), c(5.126747881386732, 51.737828850498403), c(5.137580867932065, 51.772905259431077), c(5.014540023249575, 51.808984680959583), c(5.031415073146523, 51.841084802107702), c(4.993967909252922, 51.861222725420994), c(5.062358224116345, 51.859362053527242), c(5.180226727863164, 51.96744832651509), c(5.236867149255078, 51.978757478459428), c(5.321611332014112, 51.954919171164796), c(5.486214078473083, 51.98382644510454), c(5.627223829712356, 51.952386168324438), c(5.550342661060417, 52.10541954546126), c(5.459242995490565, 52.080225755481266), c(5.514079463312799, 52.135923065932062), c(5.439875615559026, 52.171197458274222), c(5.44103943147957, 52.205693438951691), c(5.393219147822698, 52.220626892173925), c(5.404643399611359, 52.249630480909225), c(5.533281176545358, 52.27274084169683), c(5.587707385036856, 52.361454261431376), c(5.787257137970521, 52.422573287061603), c(5.876205471530124, 52.522025026941051), c(5.925559518063968, 52.474057592745915), c(6.027857569808684, 52.509606205409327), c(6.099483437203417, 52.469970896552461), c(6.130552948323514, 52.399978162269164), c(6.078506385563601, 52.369523051161245), c(6.066224466859907, 52.318839289847247), c(6.163909067147507, 52.21749619292715), c(6.38185154627214, 52.246112812566473), c(6.492401220236633, 52.177371870181403), c(6.671338986248984, 52.165683203635673), c(6.662399005672591, 52.130167439615931), c(6.760572413121598, 52.118779940206082), c(6.687853003658449, 52.039856158091141), c(6.832754328999235, 51.972938087693585), c(6.721969582522561, 51.89606334135938), c(6.683993990179909, 51.91757645733221), c(6.472507886098918, 51.853823023864017), c(6.390566170881016, 51.87396806966867), c(6.401818441765064, 51.827262656663407), c(6.117889496603739, 51.901659142837225), c(6.166559884993931, 51.840721643435401), c(6.063485632339608, 51.86545122678897), c(5.962978284523374, 51.836913960582471), c(5.946569966406273, 51.813479919592751), c(5.992067051189349, 51.770245909123908), c(5.943962150919553, 51.741816814422592), c(5.893409336802974, 51.777852926426895), c(5.765188291802036, 51.752789880063702), c(5.638112608999517, 51.819025176083443), c(5.493105254357093, 51.830750957327069), c(5.403157084105017, 51.821611677731141), c(5.357568231054432, 51.757890339715857), c(5.300338754648935, 51.737287437014395), c(5.177260142422514, 51.74291774914947)
-            )
-        )
-    )
-)
+geomGelderland <- fromJSON('{
+    "type": "MultiPolygon",
+    "coordinates": [ [ [ [ 5.177260142422514, 51.74291774914947 ], [ 5.126747881386732, 51.737828850498403 ], [ 5.137580867932065, 51.772905259431077 ], [ 5.014540023249575, 51.808984680959583 ], [ 5.031415073146523, 51.841084802107702 ], [ 4.993967909252922, 51.861222725420994 ], [ 5.062358224116345, 51.859362053527242 ], [ 5.180226727863164, 51.96744832651509 ], [ 5.236867149255078, 51.978757478459428 ], [ 5.321611332014112, 51.954919171164796 ], [ 5.486214078473083, 51.98382644510454 ], [ 5.627223829712356, 51.952386168324438 ], [ 5.550342661060417, 52.10541954546126 ], [ 5.459242995490565, 52.080225755481266 ], [ 5.514079463312799, 52.135923065932062 ], [ 5.439875615559026, 52.171197458274222 ], [ 5.44103943147957, 52.205693438951691 ], [ 5.393219147822698, 52.220626892173925 ], [ 5.404643399611359, 52.249630480909225 ], [ 5.533281176545358, 52.27274084169683 ], [ 5.587707385036856, 52.361454261431376 ], [ 5.787257137970521, 52.422573287061603 ], [ 5.876205471530124, 52.522025026941051 ], [ 5.925559518063968, 52.474057592745915 ], [ 6.027857569808684, 52.509606205409327 ], [ 6.099483437203417, 52.469970896552461 ], [ 6.130552948323514, 52.399978162269164 ], [ 6.078506385563601, 52.369523051161245 ], [ 6.066224466859907, 52.318839289847247 ], [ 6.163909067147507, 52.21749619292715 ], [ 6.38185154627214, 52.246112812566473 ], [ 6.492401220236633, 52.177371870181403 ], [ 6.671338986248984, 52.165683203635673 ], [ 6.662399005672591, 52.130167439615931 ], [ 6.760572413121598, 52.118779940206082 ], [ 6.687853003658449, 52.039856158091141 ], [ 6.832754328999235, 51.972938087693585 ], [ 6.721969582522561, 51.89606334135938 ], [ 6.683993990179909, 51.91757645733221 ], [ 6.472507886098918, 51.853823023864017 ], [ 6.390566170881016, 51.87396806966867 ], [ 6.401818441765064, 51.827262656663407 ], [ 6.117889496603739, 51.901659142837225 ], [ 6.166559884993931, 51.840721643435401 ], [ 6.063485632339608, 51.86545122678897 ], [ 5.962978284523374, 51.836913960582471 ], [ 5.946569966406273, 51.813479919592751 ], [ 5.992067051189349, 51.770245909123908 ], [ 5.943962150919553, 51.741816814422592 ], [ 5.893409336802974, 51.777852926426895 ], [ 5.765188291802036, 51.752789880063702 ], [ 5.638112608999517, 51.819025176083443 ], [ 5.493105254357093, 51.830750957327069 ], [ 5.403157084105017, 51.821611677731141 ], [ 5.357568231054432, 51.757890339715857 ], [ 5.300338754648935, 51.737287437014395 ], [ 5.177260142422514, 51.74291774914947 ] ] ] ]
+    }
+')
 
 # GraphQL query
 query <- "
@@ -1334,7 +1127,6 @@ cat("There are", nrow(df), "WoSIS profiles with layers inside Gelderland region\
 # Export dataframe to CSV
 write.csv(df, "wosis_gelderland.csv", row.names = FALSE, quote = FALSE)
 
-
 ```
 The result will be:
 
@@ -1342,5 +1134,4 @@ The result will be:
 
 CSV result file can be found [here](./scripts/r/wosis_gelderland.csv)
 
-
 ----------
-- 
GitLab