Skip to content

Commit

Permalink
SAP
Browse files Browse the repository at this point in the history
  • Loading branch information
zoometh committed Feb 21, 2024
1 parent 491354b commit d8cd900
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
43 changes: 43 additions & 0 deletions R/geojson_imagery.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# THIS IS THE MERGE BETWEEN geom_stat and geom_boxplot

#' Basic statistics on GeoJSON file like lists or charts
#'
#' @name geojson_stat
#'
#' @description Basic descriptive statistics on GeoJSON file. This function is used by `geojson_format_path()` and is the front counterpart of `ref_hps()` for a Postgres query.
#'
#' @param stat.name name of output file. Default "stat".
#' @param geojson.path path of GeoJSON file. Default 'caravanserail.geojson'
#' @param concept.name name of field used to store IDs. Default `hp.id`.
#' @param stat type of statistic that will be computed. Default `list_fields` (list the fields). Other options are: `list_ids` list EAMENA IDs. Use `stat` to diplay charts like pie chart or histograms, etc., see option `chart.type`
#' @param chart.type either "`pie`" for pie chart, or "`hist`" for histogram, "`radar`" for radar diagrams. Only useful if the option `stat` is set to `stats` (`stat = "stats"`).
#' @param field.names field name on which statistic will be performed. Only useful if the option `stat` is set to `stats` (`stat = "stats"`).
#' @param name of field on which paths will be grouped. For example "route". Will create as many plots as there are different categories. Default NA.
#' @param color.set the RBrewer color set. Default "Set1".
#' @param fig.width,fig.height size of output chart.
#' @param fig.dev format of image: "png" (default), "jpg", "svg", etc.
#' @param export.stat if TRUE return stats to be stored in new variable
#' @param export.plot if TRUE, export tats in new file, if FALSE will only display it
#' @param dirOut folder where outputs will be saved. Default: '/results'. If it doesn't exist, will be created. Only useful is export.plot is TRUE.
#' @param verbose if TRUE (by default), print messages.
#'
#' @return Show or export basic statistics on GeoJSON file
#'
#' @examples
#'
#' # list HP names
#' geojson_stat(stat.name = "geojson_fields", stat = "list_ids")
#'
#'
#' @export
geojson_imagery <- function(geojson.path = paste0(system.file(package = "eamenaR"),
"/extdata/caravanserail.geojson"),
export.plot = FALSE,
dirOut = paste0(system.file(package = "eamenaR"),
"/results/"),
verbose = TRUE){
pass
}

geojson.path <- "C:/Users/Thomas Huet/Desktop/Sistan/data/sistan_part1_hps.geojson"
geojson_imagery(geojson.path)
29 changes: 19 additions & 10 deletions R/ref_hps_1.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#' d = d,
#' stat = "hist",
#' stat.name = "Disturbance Cause Category Type",
#' stat.field = "Disturbance Cause Category Type",
#' max.num = 20,
#' export.data = TRUE,
#' dirOut = "C:/Rprojects/eamenaR/results/",
Expand All @@ -82,6 +83,7 @@
#' d = d,
#' stat = "pie",
#' stat.name = "Overall Condition State Type",
#' stat.field = "Overall Condition State Type",
#' export.data = TRUE,
#' dirOut = "C:/Rprojects/eamenaR/results/",
#' fig.width = 12,
Expand All @@ -92,6 +94,7 @@
#' d = d,
#' stat = "pie",
#' stat.name = "Overall Condition State Type",
#' stat.field = "Overall Condition State Type",
#' perc = TRUE,
#' rounded = 1,
#' export.data = TRUE,
Expand All @@ -104,6 +107,7 @@ ref_hps <- function(db.con = NA,
d = NA,
stat = c("spat"),
perc = FALSE,
rounded = 1,
stat.name = "eamena_hps",
stat.field = "Overall Condition State Type",
stat.format = ".geojson",
Expand Down Expand Up @@ -303,15 +307,15 @@ ref_hps <- function(db.con = NA,
}
if("hist" %in% stat){
# return, for example: # 34cfe9f5-c2c0-11ea-9026-02e7594ce0a0
field.name <- eamenaR::ref_ids(stat.field,
field.uuid <- eamenaR::ref_ids(stat.field,
choice = "db.concept.uuid")
# TODO: generalise the SQL for other categories. Currently it only deals with Disturbance Cause Category Type (UUID=34cfea68-c2c0-11ea-9026-02e7594ce0a0). Ex:
sqll <- stringr::str_interp(
"
SELECT v.value AS categ_type, COUNT(v.value) AS nb
FROM tiles t
JOIN values v ON t.tiledata ->> '${field.name}'::text IS NOT NULL
AND v.valueid = (t.tiledata ->> '${field.name}'::text)::uuid
JOIN values v ON t.tiledata ->> '${field.uuid}'::text IS NOT NULL
AND v.valueid = (t.tiledata ->> '${field.uuid}'::text)::uuid
GROUP BY v.valueid
ORDER BY nb DESC
"
Expand Down Expand Up @@ -342,8 +346,8 @@ ref_hps <- function(db.con = NA,
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, vjust = 1, hjust = 1, size = 10),
axis.title.y = ggplot2::element_text(angle = 90),
plot.margin = ggplot2::margin(0, 0, 1, 1, "cm"),
panel.spacing = unit(2, "lines"),
plot.title = ggplot2::element_text(margin = margin(b = 20))
panel.spacing = ggplot2::unit(2, "lines"),
plot.title = ggplot2::element_text(margin = ggplot2::margin(b = 20))
)
}
if(perc){
Expand All @@ -358,9 +362,13 @@ ref_hps <- function(db.con = NA,
) +
ggplot2::ylab(paste(stat.name, '%')) +
ggplot2::scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = warp.at)) +
ggplot2::scale_y_continuous(expand = c(0, 0, .075, 0)) + # expand the top to uncut large bar labels
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, vjust = 1, hjust = 1, size = 10),
axis.title.y = ggplot2::element_text(angle = 90),
plot.margin = ggplot2::margin(0, 0, 1, 1, "cm"))
plot.margin = ggplot2::margin(0, 0, 1, 1, "cm"),
panel.spacing = ggplot2::unit(2, "lines"),
plot.title = ggplot2::element_text(margin = ggplot2::margin(b = 20))
)
}
if (export.data) {
dir.create(dirOut, showWarnings = FALSE)
Expand All @@ -380,14 +388,14 @@ ref_hps <- function(db.con = NA,
}
if("pie" %in% stat){
# TODO: generalise the SQL for other categories. Currently it only deals with Overall Condition State Type (UUID=34cfe9f5-c2c0-11ea-9026-02e7594ce0a0)
field.name <- eamenaR::ref_ids(stat.field,
field.uuid <- eamenaR::ref_ids(stat.field,
choice = "db.concept.uuid")
sqll <- stringr::str_interp(
"
SELECT v.value AS categ_type, COUNT(v.value) AS nb
FROM tiles t
JOIN values v ON t.tiledata ->> '34cfe9f5-c2c0-11ea-9026-02e7594ce0a0'::text IS NOT NULL
AND v.valueid = (t.tiledata ->> '34cfe9f5-c2c0-11ea-9026-02e7594ce0a0'::text)::uuid
JOIN values v ON t.tiledata ->> '${field.uuid}'::text IS NOT NULL
AND v.valueid = (t.tiledata ->> '${field.uuid}'::text)::uuid
GROUP BY v.valueid
ORDER BY nb DESC
"
Expand Down Expand Up @@ -484,7 +492,8 @@ db.con <- RPostgres::dbConnect(drv = RPostgres::Postgres(),
d <- ref_hps(db.con = db.con,
d = d,
stat = "hist",
stat.name = "Disturbance Cause Category Type",
stat.name = "Threat Category",
stat.field = "Threat Category",
perc = T,
max.num = 20,
export.data = TRUE,
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Together with analysis functions, the package offers different methods to manage

```mermaid
flowchart LR
subgraph ide1 [<b>F</b>indable, <b>A</b>ccessible]
subgraph ide1 ["<b>F</b>indable, <b>A</b>ccessible"]
A[(EAMENA<br>DB)];
end
A[(EAMENA<br>DB)] <---> B{{"eamenaR"}}:::eamenaRpkg;
subgraph ide2 [<b>I</b>nteroperable, <b>R</b>eusable]
subgraph ide2 ["<b>I</b>nteroperable, <b>R</b>eusable"]
B;
end
classDef eamenaRpkg fill:#e3c071;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added results/Threat Category_hist_nb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added results/Threat Category_hist_perc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d8cd900

Please sign in to comment.