diff --git a/Model scripts/plotter.R b/Model scripts/plotter.R index 64ad621e507bef80f629dfb23ef214994c8de310..af9962c0319c7ccb50f1dc39e5b021c964d34f18 100644 --- a/Model scripts/plotter.R +++ b/Model scripts/plotter.R @@ -2,18 +2,37 @@ library(rgdal) library(sp) library(khroma) -plotter.plot.map <- function(grid,out_file,col, breaks,width,height,boundaries){ +#' Plot pathogen map +#' +#' @param grid +#' @param out_file +#' @param col +#' @param breaks +#' @param width +#' @param height +#' @param boundaries +#' +#' @return +#' @export +#' +#' @examples +plotter.plot.map <- function(grid,out_file,col, breaks,width,height,boundaries,extent=NULL){ + # determine a plot title. Either pathogen name, pathogen type or nothing pathogen_text <- if(!is.null(PATHOGEN$name) || !PATHOGEN$name == '') PATHOGEN$name else if(!is.null(PATHOGEN$pathogenType || !PATHOGEN$pathogenType))SCENARIO$pathogenType else "" + # construct output path for plot if out_file is not givevn. if(missing(out_file)){ fname <- sprintf("humanemissions_%s_%s.png",pathogen_text,SCENARIO$run) out_file <- file.path(SCENARIO$model_output,fname) } + # set default colors if array of colors is missing if(missing(col)){ col < -c("slateblue4","slateblue4","blue","skyblue","limegreen", "yellow", "darkorange","red2","red4","red4") } + # calculate class breaks for plot based on grid if the breaks are not given to function. if(missing(breaks)){ breaks <- plotter.calc.breaks(grid) } + # set default width and height if not specified. if(missing(width)){ width <- 750 } @@ -24,8 +43,8 @@ plotter.plot.map <- function(grid,out_file,col, breaks,width,height,boundaries){ png(filename = out_file, width = width, height = height, units = "px") # we set bg to gray, because it the color of missing data par(lwd=1,mar=c(6,1,1,1),ps=18,bty="n",bg="gray") - plot(grid,col=col,breaks=breaks,legend=FALSE,axes=FALSE) - + plot(grid,col=col,breaks=breaks,legend=FALSE,axes=FALSE,ext=extent) + # plot administrative borders if given to function on top of the grid. if(!missing(boundaries)){ plot(boundaries, add=T) } @@ -46,8 +65,10 @@ plotter.plot.map <- function(grid,out_file,col, breaks,width,height,boundaries){ if(breaks[1] < 0){ labels[1] <- "NA" } + # if last class break is equal to infinity set label > previous last class break. if(breaks[length(breaks)]==Inf){ labels[length(labels)] <- sprintf(">%s",breaks[length(breaks)-1]) + # modify the last class break value in case of infinity for the legend by using the previous last class break and difference between breaks. breaks[length(breaks)] <- breaks[length(breaks)-1] + (breaks[length(breaks)-1] - breaks[length(breaks)-2]) } plot(grid, legend.only=TRUE, col=col,breaks=breaks,horizontal=TRUE, diff --git a/api.R b/api.R index c36e01953946be65b2d7fa4f99160d92dfaad102..3527bfe0d5faf84731e6f76538558c44ba85eec7 100644 --- a/api.R +++ b/api.R @@ -93,7 +93,8 @@ function(human_data,isoraster,popurban,poprural,wwtp,level,wkt_extent,pathogen_t if(level==0){ boundaries_plot <- rnaturalearth::ne_countries(scale=50,returnclass = "sp") } - plot_path <- plotter.plot.map(glowpa_output$grid$pathogen_water,col=cols,breaks=brks,boundaries=boundaries_plot) + plot_extent <- as_Spatial(st_as_sfc(wkt_extent)) + plot_path <- plotter.plot.map(glowpa_output$grid$pathogen_water,col=cols,breaks=brks,boundaries=boundaries_plot, extent=plot_extent) response <- list( grid=list( file=glowpa_output$files$pathogen_water_grid, diff --git a/examples/api_req_run.txt b/examples/api_req_run.txt new file mode 100644 index 0000000000000000000000000000000000000000..ba573891189863cb34638b0d93584413317b7ec0 --- /dev/null +++ b/examples/api_req_run.txt @@ -0,0 +1 @@ +curl -X POST -d 'human_data=_id%2Ciso%2Cgid%2Ciso3%2Csubarea%2Chdi%2Cpopulation%2Cfraction_urban_pop%2Cfraction_pop_under5%2CsheddingRate%2Cshedding_duration%2Cincidence_urban_under5%2Cincidence_urban_5plus%2Cincidence_rural_under5%2Cincidence_rural_5plus%2CflushSewer_urb%2CflushSeptic_urb%2CflushPit_urb%2CflushOpen_urb%2CflushUnknown_urb%2CpitSlab_urb%2CpitNoSlab_urb%2CcompostingToilet_urb%2CbucketLatrine_urb%2CcontainerBased_urb%2ChangingToilet_urb%2CopenDefecation_urb%2Cother_urb%2CcoverBury_urb%2CsewageTreated_urb%2CfecalSludgeTreated_urb%2CisWatertight_urb%2ChasLeach_urb%2CemptyFrequency_urb%2CpitAdditive_urb%2Curine_urb%2CtwinPits_urb%2ConsiteDumpedland_urb%2CflushSewer_rur%2CflushSeptic_rur%2CflushPit_rur%2CflushOpen_rur%2CflushUnknown_rur%2CpitSlab_rur%2CpitNoSlab_rur%2CcompostingToilet_rur%2CbucketLatrine_rur%2CcontainerBased_rur%2ChangingToilet_rur%2CopenDefecation_rur%2Cother_rur%2CcoverBury_rur%2CsewageTreated_rur%2CfecalSludgeTreated_rur%2CisWatertight_rur%2ChasLeach_rur%2CemptyFrequency_rur%2CpitAdditive_rur%2Curine_rur%2CtwinPits_rur%2ConsiteDumpedland_rur%2CFractionPrimarytreatment%2CFractionSecondarytreatment%2CFractionTertiarytreatment%2CFractionPonds%2CfRemoval_treatment%2CfEmitted_inEffluent_after_treatment%2Cnotes1%2Cnotes2%2Cnotes3%0D%0A44%2C44%2C%22CHN%22%2C%22CHN%22%2C%22China%22%2C%220.758%22%2C1415045928%2C0.59151585%2C0.058313799%2C10000000000%2C7%2C0.24%2C0.01%2C0.24%2C0.01%2C0.859%2C0.033%2C0.001%2C0%2C0%2C0.069%2C0%2C0.006%2C0.009%2C0%2C0%2C0.002%2C0.02%2C0.0996447%2C0.6768327%2C0.0604821%2C0%2C0%2C3%2C0%2C0%2C0%2C0.1%2C0.111%2C0.055%2C0.014%2C0%2C0%2C0.715%2C0%2C0.034%2C0.014%2C0%2C0%2C0.02%2C0.035%2C0.23322%2C0.3274863%2C0%2C0%2C0%2C3%2C0%2C0%2C0%2C0.1%2C0.807692308%2C0.192307692%2C0%2C0%2C0.788461538%2C0.200673077%2C%22%22%2C%22%22%2C%22%22%0D%0A116%2C116%2C%22JPN%22%2C%22JPN%22%2C%22Japan%22%2C%220.915%22%2C127185332%2C0.916155371%2C0.037781585%2C10000000000%2C7%2C0.08%2C0.01%2C0.08%2C0.01%2C0.74%2C0.203%2C0%2C0%2C0%2C0.057%2C0%2C0%2C0%2C0%2C0%2C0%2C0%2C0%2C0.973256611%2C0.9%2C0%2C0%2C3%2C0%2C0%2C0%2C0.1%2C0.74%2C0.203%2C0%2C0%2C0%2C0.057%2C0%2C0%2C0%2C0%2C0%2C0%2C0%2C0%2C0.973256611%2C0.9%2C0%2C0%2C3%2C0%2C0%2C0%2C0.1%2C0%2C0.743243243%2C0.256756757%2C0%2C0.96027027%2C0.019608108%2C%22%22%2C%22Urban%20sanitation%20technologies%20data%20was%20not%20available%2C%20so%20national%20data%20was%20used.%22%2C%22Rural%20sanitation%20technologies%20data%20was%20not%20available%2C%20so%20national%20data%20was%20used.%22%0D%0A123%2C123%2C%22KOR%22%2C%22KOR%22%2C%22South%20Korea%22%2C%220.906%22%2C51164435%2C0.814593692%2C0.037000999%2C10000000000%2C7%2C0.08%2C0.01%2C0.08%2C0.01%2C0.929%2C0.043%2C0%2C0%2C0%2C0.028%2C0%2C0%2C0%2C0%2C0%2C0%2C0%2C0%2C0.899718808%2C0.9%2C0%2C0%2C3%2C0%2C0%2C0%2C0.1%2C0.929%2C0.043%2C0%2C0%2C0%2C0.028%2C0%2C0%2C0%2C0%2C0%2C0%2C0%2C0%2C0.899718808%2C0.9%2C0%2C0%2C3%2C0%2C0%2C0%2C0.1%2C0%2C0.415730337%2C0.584269663%2C0%2C0.973370787%2C0.012730337%2C%22%22%2C%22Urban%20sanitation%20technologies%20data%20was%20not%20available%2C%20so%20national%20data%20was%20used.%22%2C%22Rural%20sanitation%20technologies%20data%20was%20not%20available%2C%20so%20national%20data%20was%20used.%22&isoraster=http://data.waterpathogens.org/dataset/6a1395cf-aa8d-4f9c-9cc6-99bb7f1a9dfa/resource/e6b36595-d9c2-4647-a424-893ddfdb6c32/download/isoraster_level0_05.tif&popurban=http://data.waterpathogens.org/dataset/6a1395cf-aa8d-4f9c-9cc6-99bb7f1a9dfa/resource/7c42faa2-848a-44c4-b8e7-923792bf28d2/download/urban_pop.asc&poprural=http://data.waterpathogens.org/dataset/6a1395cf-aa8d-4f9c-9cc6-99bb7f1a9dfa/resource/caaa4458-8dea-40c6-a71f-f61995a2669b/download/rural_pop.asc&wwtp=&level=0&wkt_extent=POLYGON((73.675379 18.197701,73.675379 53.458804,145.543137 53.458804,145.543137 18.197701,73.675379 18.197701))&id=0&wwtp_available=2&pathogen_type=Virus' "http://127.0.0.1:7870/scenario" -H "accept: application/json" \ No newline at end of file