knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Step 1 Build some Example geometry
library(sdca) library(sf) library(dplyr)
# lines = sf::read_sf("../sdca-data-prep/multipart_example.gpkg") # points = sf::read_sf("../sdca-data-prep/point_example.gpkg") # # lines = lapply(lines$geometry, function(x){x}) # points = lapply(points$geometry, function(x){x}) # # #infra = sf::st_geometrycollection(c(lines, points)) # infra = sf::st_sfc(c(lines, points), crs = 4326) # infra = data.frame(infrastructure_type = c("transport","transport","onward_travel","transport","transport","transport"), # mode_class = c("Rail","Rail","onward_travel","Rail","Rail","Rail"), # intervention_class = c("New construction","New construction","onward_travel","New construction","New construction","New construction"), # mode = c("High speed rail","High speed rail","onward_travel","High speed rail","High speed rail","High speed rail"), # intervention = c("Viaduct","Viaduct","onward_travel","Station - at grade","Station - at grade","Station - at grade"), # geometry = infra) # # st_write(infra, "../inst/exdata/portishead.geojson")
Step 2: Simulate the API returning the necessary data
infra = read_sf("../data/map2.geojson") flow <- geojsonsf::geojson_sf("../../../ITSleeds/NTEM2OD/data/NTEM/NTEM_desire_lines.geojson") flow <- flow[flow$from != flow$to, ] buff <- sf::st_buffer(sf::st_combine(infra$geometry), 3000) desire <- flow[buff, op = sf::st_intersects] #desire <- desire[desire$from != desire$to,] interventions <- read.csv("../../sdca-data/data_tables/interventions.csv") assets <- read.csv("../../sdca-data/data_tables/assets.csv") assets_parameters <- read.csv("../../sdca-data/data_tables/assets_parameters.csv") components <- read.csv("../../sdca-data/data_tables/components.csv") carbon_factors <- read.csv("../../sdca-data/data_tables/carbon_factors.csv") dir.create("tmp") download.file("https://github.com/SDCA-tool/sdca-data/releases/download/map_data/materialsites.geojson.zip", "tmp/materialsites.geojson.zip") unzip("tmp/materialsites.geojson.zip",exdir = "tmp") material_sites <- sf::read_sf("tmp/materialsites.geojson") unlink("tmp", recursive = TRUE) cent <- sf::st_centroid(infra$geometry) distance <- nngeo::st_nn(material_sites, cent, returnDist = TRUE) distance <- distance$dist distance <- unlist(distance) material_sites$distance = distance material_sites = material_sites %>% st_drop_geometry %>% group_by(Material_Types) %>% summarise(distance_km = round(distance[distance == min(distance)] / 1000,1)) assets = assets[assets$intervention %in% infra$intervention,] assets_parameters = assets_parameters[assets_parameters$asset %in% assets$asset,] components = components[components$asset %in% assets$asset, ] carbon_factors = carbon_factors[carbon_factors$cf_name %in% components$cf_name, ] # Build example_json infra_json <- geojsonsf::sf_geojson(infra) desire_json <- geojsonsf::sf_geojson(desire) path_dem = "dem.tif" path_landcover = "landcover.tif" path_bedrock = "bedrock.tif" path_superficial = "superficial.tif" json = list(infra_json, assets, assets_parameters, components, carbon_factors, desire_json, path_dem, path_landcover, path_bedrock, path_superficial, material_sites) names(json) = c("user_input", "assets", "assets_parameters", "components", "carbon_factors", "desire_lines", "path_dem","path_landcover","path_bedrock", "path_superficial", "material_sites") jsonlite::write_json(json,"../tests/testthat/overbridge_example.json", pretty = TRUE, simplifyDataFrame = FALSE, simplifyVector = FALSE, na = "null")
Step 3: Parse the API's data
args = readLines("../tests/testthat/portishead_example_r_input.geojson") args = paste(args, collapse = "") fls = list.files("../R", full.names = TRUE) for(fl in fls){source(fl)}
Step 4: Make Calculations
dat = parse_json(args, FALSE)
# Test reading # dat = jsonlite::read_json("../inst/exdata/portishead_example_r_input.geojson", simplifyVector = TRUE) # dat = parse_json(jsonlite::toJSON(dat)) # # tmap_mode("plot") # tm_shape(desire) + # tm_lines(lwd = 0.1) + # tm_shape(infra) + # tm_lines(col = "red", lwd = 3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.