-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sits training data poc
- Loading branch information
Showing
2 changed files
with
53 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,33 @@ | ||
library(sitsdata) | ||
library(sits) | ||
library(jsonlite) | ||
library(geojsonsf) | ||
library(dplyr) | ||
library(purrr) | ||
|
||
sits_data <- sitsdata::samples_prodes_4classes | ||
sits_data | ||
|
||
sits_data$time_series[[1]] | ||
|
||
sf_samples <- sits::sits_as_sf(sits_data) | ||
## get sits data | ||
out <- sitsdata::samples_prodes_4classes |> tidyr::unnest(cols = c(time_series)) | ||
|
||
sf_samples | ||
## convert to json on client side | ||
out_to_json <- jsonlite::toJSON(out) | ||
|
||
## convert to from json to sits tibble on sever side | ||
data <- jsonlite::fromJSON(out_to_json) | ||
|
||
json_samples <- jsonlite::fromJSON(jsonlite::toJSON(sf_samples), simplifyDataFrame = F, simplifyMatrix = F) | ||
json_samples | ||
orig <- data |> tidyr::nest( | ||
.by = c(longitude, latitude, start_date, end_date, label, cube), | ||
.key = "time_series") | ||
|
||
sf_to_json <- geojsonsf::sf_geojson(sf_samples) | ||
sf_to_json | ||
### function to convert 'Index' column to Date | ||
convert_index_to_date <- function(data) { | ||
data |> mutate(Index = as.Date(Index)) | ||
} | ||
|
||
json_to_sf <- geojsonsf::geojson_sf(json_samples) | ||
json_to_sf | ||
|
||
orig <- orig |> | ||
mutate(time_series = map(time_series, convert_index_to_date), | ||
start_date = as.Date(start_date), | ||
end_date = as.Date(end_date)) | ||
|
||
orig = structure(orig, class = c("sits", class(orig))) | ||
|
||
all.equal(orig, samples_prodes_4classes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
library(sits) | ||
library(sitsdata) | ||
library(stars) | ||
samples_prodes_4classes | ||
# get dimensions | ||
(time = samples_prodes_4classes$time_series[[1]]$Index) | ||
samples_prodes_4classes |> | ||
st_as_sf(coords = c("longitude", "latitude"), crs = "OGC:CRS84") |> | ||
st_geometry()-> points | ||
points | ||
bands = colnames(samples_prodes_4classes$time_series[[1]])[-1] | ||
bands | ||
# merge all time series: | ||
cube = do.call(rbind, samples_prodes_4classes$time_series) | ||
cube$Index = NULL | ||
cube = as.matrix(cube) | ||
dim(cube) | ||
dim(cube) = c(time = length(time), geom = length(points), band = length(bands)) | ||
dim(cube) | ||
# create stars object: | ||
st_as_stars(cube, raster = list(dimensions = c(NA_character_,NA_character_))) |> | ||
st_set_dimensions(1, values = as.Date(time)) |> | ||
st_set_dimensions(2, values = points) |> | ||
st_set_dimensions(3, values = bands) -> st | ||
st | ||
# print slices: | ||
image(st[[1]][1,,]) | ||
image(st[[1]][,1,]) | ||
image(st[[1]][,,1]) |