Commit d2a87c9a authored by Romulo Pereira Goncalves's avatar Romulo Pereira Goncalves
Browse files

Merge branch 'issue#3' into 'master'

Issue#3

See merge request !14
parents fa1238c4 f20db00b
Pipeline #24231 passed with stages
in 1 minute and 59 seconds
......@@ -11,9 +11,9 @@ Description: Calculates samples and related classifiers for mapping gradual prob
License: GPL-3
Imports:
BH (<= 1.69.0-1),
sf (<= 0.9-0),
sp (<= 1.4-4),
rgdal (<= 1.5-12),
sf,
sp,
rgdal,
raster,
geojsonio,
maptools,
......
......@@ -20,7 +20,7 @@
"source": [
"### **0.0** - Pin the version of certain dependencies\n",
"\n",
"The `velox` library is not anymore developed. Its latest version does not compile with the latest version of `Boost`. To avoid warnings and issues with the migration of `rgdal` to `gdal 3 and proj 6` we decided to pin the versions of `sf`, `sp` and `rgdal`."
"The `velox` library is not anymore developed. Its latest version does not compile with the latest version of `Boost`."
]
},
{
......@@ -40,10 +40,7 @@
"source": [
"if (install_dependencies == TRUE) {\n",
" install.packages(\"remotes\")\n",
" install.packages(\"https://cran.r-project.org/src/contrib/Archive/BH/BH_1.69.0-1.tar.gz\", repos=NULL, type=\"source\")\n",
" install.packages(\"https://cran.r-project.org/src/contrib/Archive/sf/sf_0.9-0.tar.gz\", repos=NULL, type=\"source\")\n",
" install.packages(\"https://cran.r-project.org/src/contrib/Archive/sp/sp_1.4-4.tar.gz\", repos=NULL, type=\"source\")\n",
" install.packages(\"https://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_1.5-12.tar.gz\", repos=NULL, type=\"source\") \n",
" install.packages(\"https://cran.r-project.org/src/contrib/Archive/BH/BH_1.69.0-1.tar.gz\", repos=NULL, type=\"source\") \n",
"}"
]
},
......
%% Cell type:markdown id: tags:
# HabitatSampler
%% Cell type:markdown id: tags:
## **0** - Setup
%% Cell type:markdown id: tags:
### **0.0** - Pin the version of certain dependencies
The `velox` library is not anymore developed. Its latest version does not compile with the latest version of `Boost`. To avoid warnings and issues with the migration of `rgdal` to `gdal 3 and proj 6` we decided to pin the versions of `sf`, `sp` and `rgdal`.
The `velox` library is not anymore developed. Its latest version does not compile with the latest version of `Boost`.
%% Cell type:code id: tags:
``` R
install_dependencies <- FALSE
```
%% Cell type:code id: tags:
``` R
if (install_dependencies == TRUE) {
install.packages("remotes")
install.packages("https://cran.r-project.org/src/contrib/Archive/BH/BH_1.69.0-1.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sf/sf_0.9-0.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sp/sp_1.4-4.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_1.5-12.tar.gz", repos=NULL, type="source")
}
```
%% Cell type:markdown id: tags:
### **0.1** - Install HaSa and its dependencies
If the you want to use the sources of `HaSa` R pacakge set the variable `from_sources` to `TRUE`.
%% Cell type:code id: tags:
``` R
from_sources <- TRUE
```
%% Cell type:code id: tags:
``` R
if (from_sources == TRUE) {
wd<-"./"
inPath<-"./../R-package/R/"
source(paste(inPath,"Class_Habitat.r",sep=""))
source(paste(inPath,"clip.r",sep=""))
source(paste(inPath,"inner_procedure.r",sep=""))
source(paste(inPath,"outer_procedure.r",sep=""))
source(paste(inPath,"model_opt.r",sep=""))
source(paste(inPath,"plot_results.r",sep=""))
source(paste(inPath,"plot_interactive.r",sep=""))
source(paste(inPath,"save_files.r",sep=""))
source(paste(inPath,"utils.r",sep=""))
} else {
library(remotes)
remotes::install_git(
"https://git.gfz-potsdam.de/habitat-sampler/HabitatSampler.git",
ref = "documentation",
subdir = "R-package",
dependencies = NA,
upgrade=FALSE,
build = TRUE,
build_manual = TRUE,
build_vignettes = TRUE
)
}
```
%% Cell type:markdown id: tags:
### **0.2** Load libraries
We load HaSa library and some of its dependencies
%% Cell type:code id: tags:
``` R
options("rgdal_show_exportToProj4_warnings"="none")
if (from_sources == TRUE) {
libraries <- c("rgdal","raster","maptools","spatialEco","randomForest","e1071","devtools","velox","rgeos","leaflet","htmlwidgets", "IRdisplay")
} else {
libraries <- c("rgdal","raster","maptools","spatialEco","randomForest","e1071","devtools","velox","rgeos","leaflet","htmlwidgets", "IRdisplay", "HaSa")
}
lapply(libraries, library, character.only = TRUE)
```
%%%% Output: stream
Loading required package: sp
rgdal: version: 1.4-8, (SVN revision 845)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.1.0, released 2020/05/03
Path to GDAL shared files:
GDAL binary built with GEOS: FALSE
Loaded PROJ.4 runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
Path to PROJ.4 shared files: /home/romulo/gitlab/python-setup-ubuntu/python/opt/share/proj
Linking to sp version: 1.4-1
Warning message:
“no function found corresponding to methods exports from ‘raster’ for: ‘wkt’”
Checking rgeos availability: TRUE
Warning message:
“'units::install_conversion_constant' is deprecated.
Use 'install_unit' instead.
See help("Deprecated")”
Warning message:
“'units::install_conversion_constant' is deprecated.
Use 'install_unit' instead.
See help("Deprecated")”
Warning message:
“'units::install_conversion_constant' is deprecated.
Use 'install_unit' instead.
See help("Deprecated")”
Warning message:
“'units::install_conversion_constant' is deprecated.
Use 'install_unit' instead.
See help("Deprecated")”
Warning message:
“'units::install_conversion_constant' is deprecated.
Use 'install_unit' instead.
See help("Deprecated")”
Attaching package: ‘spatialEco’
The following object is masked from ‘package:raster’:
shift
randomForest 4.6-14
Type rfNews() to see new features/changes/bug fixes.
Attaching package: ‘randomForest’
The following object is masked from ‘package:spatialEco’:
combine
Attaching package: ‘e1071’
The following object is masked from ‘package:raster’:
interpolate
Loading required package: usethis
Warning message in fun(libname, pkgname):
“rgeos: versions of GEOS runtime 3.9.0-CAPI-1.16.2
and GEOS at installation 3.7.0-CAPI-1.11.0differ”
rgeos version: 0.5-5, (SVN revision 640)
GEOS runtime version: 3.9.0-CAPI-1.16.2
Linking to sp version: 1.4-1
Polygon checking: TRUE
%%%% Output: display_data
1. 1. 'rgdal'
2. 'sp'
3. 'stats'
4. 'graphics'
5. 'grDevices'
6. 'utils'
7. 'datasets'
8. 'methods'
9. 'base'
2. 1. 'raster'
2. 'rgdal'
3. 'sp'
4. 'stats'
5. 'graphics'
6. 'grDevices'
7. 'utils'
8. 'datasets'
9. 'methods'
10. 'base'
3. 1. 'maptools'
2. 'raster'
3. 'rgdal'
4. 'sp'
5. 'stats'
6. 'graphics'
7. 'grDevices'
8. 'utils'
9. 'datasets'
10. 'methods'
11. 'base'
4. 1. 'spatialEco'
2. 'maptools'
3. 'raster'
4. 'rgdal'
5. 'sp'
6. 'stats'
7. 'graphics'
8. 'grDevices'
9. 'utils'
10. 'datasets'
11. 'methods'
12. 'base'
5. 1. 'randomForest'
2. 'spatialEco'
3. 'maptools'
4. 'raster'
5. 'rgdal'
6. 'sp'
7. 'stats'
8. 'graphics'
9. 'grDevices'
10. 'utils'
11. 'datasets'
12. 'methods'
13. 'base'
6. 1. 'e1071'
2. 'randomForest'
3. 'spatialEco'
4. 'maptools'
5. 'raster'
6. 'rgdal'
7. 'sp'
8. 'stats'
9. 'graphics'
10. 'grDevices'
11. 'utils'
12. 'datasets'
13. 'methods'
14. 'base'
7. 1. 'devtools'
2. 'usethis'
3. 'e1071'
4. 'randomForest'
5. 'spatialEco'
6. 'maptools'
7. 'raster'
8. 'rgdal'
9. 'sp'
10. 'stats'
11. 'graphics'
12. 'grDevices'
13. 'utils'
14. 'datasets'
15. 'methods'
16. 'base'
8. 1. 'velox'
2. 'devtools'
3. 'usethis'
4. 'e1071'
5. 'randomForest'
6. 'spatialEco'
7. 'maptools'
8. 'raster'
9. 'rgdal'
10. 'sp'
11. 'stats'
12. 'graphics'
13. 'grDevices'
14. 'utils'
15. 'datasets'
16. 'methods'
17. 'base'
9. 1. 'rgeos'
2. 'velox'
3. 'devtools'
4. 'usethis'
5. 'e1071'
6. 'randomForest'
7. 'spatialEco'
8. 'maptools'
9. 'raster'
10. 'rgdal'
11. 'sp'
12. 'stats'
13. 'graphics'
14. 'grDevices'
15. 'utils'
16. 'datasets'
17. 'methods'
18. 'base'
10. 1. 'leaflet'
2. 'rgeos'
3. 'velox'
4. 'devtools'
5. 'usethis'
6. 'e1071'
7. 'randomForest'
8. 'spatialEco'
9. 'maptools'
10. 'raster'
11. 'rgdal'
12. 'sp'
13. 'stats'
14. 'graphics'
15. 'grDevices'
16. 'utils'
17. 'datasets'
18. 'methods'
19. 'base'
11. 1. 'htmlwidgets'
2. 'leaflet'
3. 'rgeos'
4. 'velox'
5. 'devtools'
6. 'usethis'
7. 'e1071'
8. 'randomForest'
9. 'spatialEco'
10. 'maptools'
11. 'raster'
12. 'rgdal'
13. 'sp'
14. 'stats'
15. 'graphics'
16. 'grDevices'
17. 'utils'
18. 'datasets'
19. 'methods'
20. 'base'
12. 1. 'IRdisplay'
2. 'htmlwidgets'
3. 'leaflet'
4. 'rgeos'
5. 'velox'
6. 'devtools'
7. 'usethis'
8. 'e1071'
9. 'randomForest'
10. 'spatialEco'
11. 'maptools'
12. 'raster'
13. 'rgdal'
14. 'sp'
15. 'stats'
16. 'graphics'
17. 'grDevices'
18. 'utils'
19. 'datasets'
20. 'methods'
21. 'base'
\begin{enumerate}
\item \begin{enumerate*}
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'randomForest'
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'e1071'
\item 'randomForest'
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'devtools'
\item 'usethis'
\item 'e1071'
\item 'randomForest'
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'velox'
\item 'devtools'
\item 'usethis'
\item 'e1071'
\item 'randomForest'
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'rgeos'
\item 'velox'
\item 'devtools'
\item 'usethis'
\item 'e1071'
\item 'randomForest'
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'leaflet'
\item 'rgeos'
\item 'velox'
\item 'devtools'
\item 'usethis'
\item 'e1071'
\item 'randomForest'
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'htmlwidgets'
\item 'leaflet'
\item 'rgeos'
\item 'velox'
\item 'devtools'
\item 'usethis'
\item 'e1071'
\item 'randomForest'
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\item \begin{enumerate*}
\item 'IRdisplay'
\item 'htmlwidgets'
\item 'leaflet'
\item 'rgeos'
\item 'velox'
\item 'devtools'
\item 'usethis'
\item 'e1071'
\item 'randomForest'
\item 'spatialEco'
\item 'maptools'
\item 'raster'
\item 'rgdal'
\item 'sp'
\item 'stats'
\item 'graphics'
\item 'grDevices'
\item 'utils'
\item 'datasets'
\item 'methods'
\item 'base'
\end{enumerate*}
\end{enumerate}
%% Cell type:markdown id: tags:
### **0.3** - Configuration
%% Cell type:code id: tags:
``` R
wd<-"./"
dataPath<-"./Data/"
outPath<-paste(wd,"Data/Results/",sep="")
rasterOptions(tmpdir="./RasterTmp/")
```
%% Cell type:markdown id: tags:
# Demo
%% Cell type:markdown id: tags:
## **1. Prepare Input Data**
%% Cell type:markdown id: tags:
### **1.1** - Satellite time series
The Satellite time series is either passed as a **1.1.1.a)** stack of images already clipped or **1.1.1.b)** a stack of image to be clipped. In this Demo we only show option **1.1.1.a)**. In both cases, the input Satellite images need to either have a valid projection or it be passed as parameter `sat_crs_str = '+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs'`, otherwise, the function will report error.
%% Cell type:markdown id: tags:
#### 1.1.1.a - Satellite time series stack
Satellite time series stack (6 days stack using 9 bands) clipped to study area (specify file name)
%% Cell type:code id: tags:
``` R
satellite_series_path <- paste(dataPath,"SentinelStack_2018.tif",sep="")
timeseries_stack <- load_timeseries_stack(satellite_series_path)
```
%% Cell type:markdown id: tags:
### **1.2** - Reference data
The reference data is either passed as table **1.2.1.a)** or as shapefile **1.2.1.b)** with point locations on the extent of the input S. With option **1.2.1.b)** it is possible to visualize where the location of the reference data using a scene from the Satellite time series stack.
%% Cell type:markdown id: tags:
#### 1.2.1.a
Load the reference Spectra as table (rows=habitats, columns=spectral wavebands). The first row must contain the spectral waveband' names, the same names as the ones used in the input Satellite time series stack.
%% Cell type:code id: tags:
``` R
table_data_path <- paste(dataPath,"Example_Reference_table.txt", sep="")
ref <- load_reference_as_table(table_data_path)
```
%% Cell type:markdown id: tags:
#### 1.2.1.b
The user provides a shapefile with a point location per class to extract the reference data. The wavelengths for each point is extracted from the the Satellite time series stack using R routine `raster::extract`. In case the Shapefile does not have the same projection as the input Sentinel time series stack, HaSa will be automatically reseted it to the Sentinel time series stack's projection.
%% Cell type:code id: tags:
``` R
shp_path <- paste(dataPath,"Example_Reference_Points.shp", sep="")
satellite_series_path <- paste(dataPath,"SentinelStack_2018.tif", sep="")
ref <- load_reference_as_shape(shp_path, satellite_series_path, shp_crs_str='+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs')
```
%%%% Output: stream
OGR data source with driver: ESRI Shapefile
Source: "/home/romulo/gitlab/HabitatSampler/demo/Data/Example_Reference_Points.shp", layer: "Example_Reference_Points"
with 7 features
It has 2 fields
%% Cell type:markdown id: tags:
#### 1.2.2
The classe names should be passed as a vector in the same order as the reference spectra (rows = habitats).
%% Cell type:code id: tags:
``` R
classNames <- c("deciduous", "coniferous", "heather_young", "heather_old", "heather_shrub", "bare_ground", "xeric_grass")
```
%% Cell type:markdown id: tags:
### **1.3** Plot configuration
The interactive mode requires the user's expertise to define the *Threshold for Habitat Extraction* using an interactive Map. The background of the interative map is a plot of 3 bands (RGB) of one of the Satellite scenes. Each Sentinel scence has the following bands: \
![image.png](attachment:e5141071-e07a-44ee-a29d-fc5e147af790.png)
In this demo, each scence in the the stacked Sentinel 2 timeseries uses 9 bands: `Band 2 - Blue`, `Band 3 - Green`, `Band 4 - Red`, `Band 5 - Vegetation Red Edge`, `Band 6 - Vegetation Red Edge`, `Band 7 - Vegetation Red Edge`, `Band 8 - NIR`, and `Band 8a - Vegetation Red Edge`.
In the step **1.3.1**, using the clipped Sentinel 2 time series stack provided as input, the user can test which bands should be used in the plot.
%% Cell type:markdown id: tags:
#### 1.3.1
Define RGB channels for image plot.
%% Cell type:code id: tags:
``` R
shp_path <- paste(dataPath,"Example_Reference_Points.shp", sep="")
satellite_series_path <- paste(dataPath,"SentinelStack_2018.tif",sep="")
plot_rgb <- c("r" = 19, "g" = 20, "b" = 21)
plot_configuration(shp_path, satellite_series_path, plot_rgb, shp_crs_str = '+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs')
```
%%%% Output: stream
OGR data source with driver: ESRI Shapefile
Source: "/home/romulo/gitlab/HabitatSampler/demo/Data/Example_Reference_Points.shp", layer: "Example_Reference_Points"
with 7 features
It has 2 fields
%%%% Output: display_data
%% Cell type:markdown id: tags:
#### 1.3.2
Define color Palette for habitat type probability plot.
%% Cell type:code id: tags:
``` R
col<-colorRampPalette(c("lightgrey","orange","yellow","limegreen","forestgreen"))
```
%% Cell type:markdown id: tags:
## **2 Habitat Sampling**
%% Cell type:markdown id: tags:
```R
multi_Class_Sampling <-
function(
in.raster, #clipped satellite time series stack [raster brick]
init.samples, #starting number of spatial samples (suggest: 30)
sample_type # distribution of spatial samples ("random" or "regular"; suggest: "regular")
nb_models # number of models to collect (suggest: 40)
nb_it # number of iterations for model accuracy (suggest:10)
buffer # distance (in m) for new sample collection around initial samples (depends on pixel size)
reference # table of reference spectra [data.frame]
model # which machine learning algorithm to use ("rf" random forest or "svm" support vector machine; suggest: rf)
area # SpatialPolygonsDataFrame from satellite time series stack extent
mtry # number of predictor used at random forest splitting nodes (suggest: mtry << n predictors)
last # only true for one class classifier ("TRUE" or "FALSE"; suggest: "F")
seed # set seed for reproducible results (suggest: 3)
init.seed # "sample" for new or use Run@seeds to reproduce previous steps
outPath # output path for saving results
step # at which step should the procedure start (see 2.b.1) (suggest: 1 at the beginning)
classNames # vector with class names in the order of reference spectra
n_classes # total number of classes (habitat types) to be separated
multiTest # number of test runs to compare different probability output
RGB # pallet colors for the interactive plots
overwrite # overwrite the KML and raster files from previous runs (default TRUE)
save_runs # Habitat object is saved into disk for each run (default TRUE)
parallel_mode # run loops using all available cores (default False
max_num_cores # maximum number of cores for parallelism (default 5)
plot_on_browser # plot on the browser or inline in a notebook (default TRUE)
)
```
%% Cell type:code id: tags:
``` R
multi_Class_Sampling(
in.raster = timeseries_stack,
init.samples = 55,
sample_type = "regular",
nb_models = 70,
nb_it = 10,
buffer = 15,
reference = ref,
model = "rf",
mtry = 10,
last = FALSE,
seed = 3,
init.seed = "sample",
outPath = outPath,
step = 1,
classNames = classNames,
n_classes = 7,
multiTest = 1,
RGB = plot_rgb,
overwrite = TRUE,
save_runs = FALSE,
parallel_mode = TRUE,
max_num_cores = 7,
plot_on_browser = FALSE)
```
%%%% Output: stream
[1] "Habitat 0 Starting"
[1] "init.samples = 55 models = 70"
[1] "class=1 difference=0.56"
[1] "n_models = 2"
%%%% Output: display_data
%%%% Output: stream
Threshold for Habitat Extraction or Sample Again (0) [.. or 0]: 1
%%%% Output: stream
[1] "Habitat 1 Done"
[1] "Habitat 2 Starting"
%%%% Output: stream
Adjust init.samples/nb.models (actual 55/70), auto (0), or same (1) [../.. or 0 or 1]: 0
%%%% Output: stream
[1] "init.samples = 105 models = 85"
[1] "class=3 difference=0.68"
[1] "n_models = 10"
%%%% Output: display_data
%%%% Output: stream
Threshold for Habitat Extraction or Sample Again (0) [.. or 0]: 9
%%%% Output: stream
[1] "Habitat 2 Done"
[1] "Habitat 3 Starting"
%%%% Output: stream
Adjust init.samples/nb.models (actual 105/85), auto (0), or same (1) [../.. or 0 or 1]: 0
%%%% Output: stream
[1] "init.samples = 155 models = 100"
[1] "class=2 difference=0.8"
[1] "n_models = 8"
%%%% Output: display_data
%%%% Output: stream
Threshold for Habitat Extraction or Sample Again (0) [.. or 0]: 7
%%%% Output: stream
[1] "Habitat 3 Done"
[1] "Habitat 4 Starting"
%%%% Output: stream
Adjust init.samples/nb.models (actual 155/100), auto (0), or same (1) [../.. or 0 or 1]: 1
%%%% Output: stream
[1] "init.samples = 155 models = 100"
[1] "class=2 difference=0.92"
[1] "n_models = 27"
%%%% Output: display_data
%%%% Output: stream
Threshold for Habitat Extraction or Sample Again (0) [.. or 0]: 20
%%%% Output: stream
[1] "Habitat 4 Done"
[1] "Habitat 5 Starting"
%%%% Output: stream
Adjust init.samples/nb.models (actual 155/100), auto (0), or same (1) [../.. or 0 or 1]: 1
No optimal classifier - Adjust init.samples/nb.models (actual 155/100), abort (0) or auto (1) [../.. or 0 or 1]: 1
%%%% Output: stream
[1] "init.samples = 155 models = 100"
[1] "init.samples = 205 models = 115"
[1] "class=2 difference=0.92"
[1] "n_models = 20"
%%%% Output: display_data
%%%% Output: stream
Threshold for Habitat Extraction or Sample Again (0) [.. or 0]: 18
%%%% Output: stream
[1] "Habitat 5 Done"
[1] "Habitat 6 Starting"
%%%% Output: stream
Adjust init.samples/nb.models (actual 205/115), auto (0), or same (1) [../.. or 0 or 1]: 1
%%%% Output: stream
[1] "init.samples = 205 models = 115"
[1] "class=1 difference=0.96"
[1] "n_models = 57"
%%%% Output: display_data
%%%% Output: stream
Threshold for Habitat Extraction or Sample Again (0) [.. or 0]: 50
%%%% Output: stream
[1] "Habitat 6 Done"
[1] "Habitat 7 Starting"
%%%% Output: stream
Adjust init.samples/nb.models (actual 205/115), auto (0), or same (1) [../.. or 0 or 1]: 1
%%%% Output: stream
[1] "init.samples = 205 models = 115"
[1] "class=1 difference=0.68"
[1] "n_models = 10"
%%%% Output: display_data
%% Cell type:markdown id: tags:
## **3. Plot results**
%% Cell type:code id: tags:
``` R
plot_results(inPath=outPath)
```
%% Cell type:code id: tags:
``` R
```
......
......@@ -28,9 +28,6 @@
##0.0##
install.packages("remotes")
install.packages("https://cran.r-project.org/src/contrib/Archive/BH/BH_1.69.0-1.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sf/sf_0.9-0.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sp/sp_1.4-4.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_1.5-12.tar.gz", repos=NULL, type="source")
##0.1##
library(remotes)
......
......@@ -10,10 +10,10 @@ container_name="hasa"
input_data_folder="/tmp/hasa"
#Check if image exists
if [ $(sudo docker images | grep ${runner_iname} | wc -l) == 0 ]
if [ $(docker images | grep ${runner_iname} | wc -l) == 0 ]
then
# build docker image
sudo docker build --network=host -f ${context_dir}/${dockerfile} -m 20G -t ${runner_tag} ${context_dir}
docker build --network=host -f ${context_dir}/${dockerfile} -m 20G -t ${runner_tag} ${context_dir}
else
echo "It already exists a Docker image with the name ${runner_tag}!!!"
fi
......@@ -9,9 +9,6 @@ install.packages("rmarkdown")
install.packages("knitr")
install.packages("remotes")
install.packages("https://cran.r-project.org/src/contrib/Archive/BH/BH_1.69.0-1.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sf/sf_0.9-0.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sp/sp_1.4-4.tar.gz", repos=NULL, type="source", dependencies=TRUE)
install.packages("https://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_1.5-12.tar.gz", repos=NULL, type="source")
# Install HaSa
library(remotes)
......
......@@ -3,9 +3,6 @@ install.packages("rmarkdown")
install.packages("knitr")
install.packages("remotes")
install.packages("https://cran.r-project.org/src/contrib/Archive/BH/BH_1.69.0-1.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sf/sf_0.9-0.tar.gz", repos=NULL, type="source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sp/sp_1.4-4.tar.gz", repos=NULL, type="source", dependencies=TRUE)
install.packages("https://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_1.5-12.tar.gz", repos=NULL, type="source")
# Install HaSa
library(remotes)
......
......@@ -50,12 +50,12 @@ demo_data_foler="$(realpath ../demo/)"
jupyter_lab_port=8888
docker_jupyterlab_port=8888
sudo docker rm -f ${container_name}
docker rm -f ${container_name}
echo "Starting HaSa container, please add the input files to ${input_dir_} and read the results from the ${output_dir_}"
if [ "${jupyter_notebook,,}" = "true" ]
then
sudo docker run -it --name ${container_name} --privileged \
docker run -it --name ${container_name} --privileged \
--oom-kill-disable --memory=12g --memory-swap=14g --net=host \
-p $jupyter_lab_port:$docker_jupyterlab_port \
-v ${input_dir_}:${in_data_folder} \
......@@ -64,7 +64,7 @@ then
-v ${demo_dir}:${demo_data_folder} ${runner_tag} \
bash -i -c "cd /home/hasa; jupyter-lab --ip 0.0.0.0 --no-browser --allow-root demo/"
else
sudo docker run -it --name ${container_name} -u 1000:1000 --privileged \
docker run -it --name ${container_name} -u 1000:1000 --privileged \
--oom-kill-disable --memory=12g --memory-swap=14g --net=host \
-v ${input_dir_}:${in_data_folder} \
-v ${tmp_dir_}:${tmp_data_folder} \
......
......@@ -3,14 +3,6 @@ title: "An Introduction to Habitat Sampler"
author: "Carsten Neumann, Alison Beamish, Romulo Goncalves"
date: "01/06/2021"
output:
md_document:
pandoc_args: ["--output", "README.md"]
toc: true
toc_depth: 2
variant: gfm
pdf_document:
toc: true
toc_depth: 2
html_document:
theme: united
highlight: tango
......@@ -20,7 +12,14 @@ output:
collapsed: false
smooth_scroll: false
df_print: paged
always_allow_html: yes
md_document:
pandoc_args: ["--output", "README.md"]
toc: true
toc_depth: 2
variant: gfm
pdf_document:
toc: true
toc_depth: 2
header-includes:
- \usepackage{caption}
- \captionsetup[figure]{labelformat=empty}
......@@ -36,7 +35,7 @@ knitr::opts_chunk$set(tidy.opts = list(width.cutoff = 75), tidy = TRUE, fig.pos
# 1 Introduction
This manual introduces the Habitat Sampler (HaSa), an innovative tool that autonomously generates representative reference samples for predictive modelling of surface class probabilities. The tool can be applied to any image data that displays surface structures and dynamics of any kind at multiple spatial and temporal scales. HaSa was initially developed to classify habitat dynamics in semi-natural ecosystems but the procedure can theoretically be applied to any surface. The main innovation of the tool is that it reduces reliance on comprehensive in situ ground truth data or comprehensive training datasets which constrain accurate image classification particularly in complex scenes.
Though development of HaSa has prioritized ease of use, this documentation assume a familiarity with the R software. The document is built successively and is intended to lead you step-by-step through the HaSa procedure of generating probability and classification maps. HaSa is still in development and any suggestions or improvements are welcomed and encouraged in our [GitLab Community Version](https://git.gfz-potsdam.de/habitat-sampler/HabitatSampler.git). If questions remain please don't hesitate to contact the authors of the package. For a detailed description of the Habitat Sampler and its applications, see [Neumann et al., (2020)](https://doi.org/10.1111/ddi.13165).
Though development of HaSa has prioritized ease of use, this documentation assume a familiarity with the R software. The document is built successively and is intended to lead you step-by-step through the HaSa procedure of generating probability and classification maps. HaSa is still in development and any suggestions or improvements are welcomed and encouraged in our [GitHub Community Version](https://git.gfz-potsdam.de/habitat-sampler/HabitatSampler.git). If questions remain please don't hesitate to contact the authors of the package. For a detailed description of the Habitat Sampler and its applications, see [Neumann et al., (2020)](https://doi.org/10.1111/ddi.13165).
## 1.1 Usage
The tool is implemented in R and uses Leaflet [(Cheng et al., 2019)](https://rdrr.io/cran/leaflet/) to generate interactive maps in a web browser. There are no assumptions about the input image data and there are no constraints for the spectral-temporal-spatial domain in which the image is sampled. The tool requires the input of a priori expert user knowledge to generate reference data about expected surface classes which are delineated in the imagery or extracted from an external spectral library. The user has the choice between image classifiers [random forest](https://doi.org/10.1023/A:1010933404324) (RF) and [support vector](https://doi.org/10.1145/130385.130401) (SV).
......@@ -66,19 +65,13 @@ The point shapefile contains a point location per class and is used to extract t
The following procedure will lead you through the preliminary steps required to setup the HaSa tool.
## 2.1 HaSa dependencies
HaSa uses the latest version of the `velox` library (`v0.2.0`) which does not compile with the latest version the interface to the C++ Boost library `BH`. Hence, it is necessary to pin the `BH` version. HaSa does not yet support the latest developments in `rgdal` and `sp` related with projections. It is also necessary to pin the versions for `sf`, `sp` and `rgdal`.
HaSa uses the latest version of the `velox` library (`v0.2.0`) which does not compile with the latest version the interface to the C++ Boost library `BH`. Hence, it is necessary to pin the `BH` version.
The installation of `BH`, `sf`, `sp` and `rgdal` is possible with the following commands:
The installation of `BH` is possible with the following commands:
```{r install dependencies, eval = FALSE}
install.packages("remotes")
install.packages("https://cran.r-project.org/src/contrib/Archive/BH/BH_1.69.0-1.tar.
gz", repos = NULL, type = "source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sf/sf_0.9-0.tar.
gz", repos = NULL, type = "source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sp/sp_1.4-4.tar.
gz", repos = NULL, type = "source")
install.packages("https://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_1.5-12.
tar.gz", repos = NULL, type = "source")
```
## 2.2 Install HaSa
......
This diff is collapsed.
No preview for this file type
......@@ -2,21 +2,21 @@
- [1 Introduction](#introduction)
- [1.1 Usage](#usage)
- [1.2 Sample datasets](#sample-datasets)
- [2 HaSa installation](#hasa-installation)
- [2.1 HaSa dependencies](#hasa-dependencies)
- [2.2 Install HaSa](#install-hasa)
- [2.3 Load HaSa](#load-hasa)
- [3 Load demo data](#load-demo-data)
- [3.1 Data directories](#data-directories)
- [3.2 Satellite timeseries stack](#satellite-timeseries-stack)
- [3.3 Selecting reference samples](#selecting-reference-samples)
- [4 Generating outputs](#generating-outputs)
- [4.1 Calculating class
- [1 Introduction](#introduction)
- [1.1 Usage](#usage)
- [1.2 Sample datasets](#sample-datasets)
- [2 HaSa installation](#hasa-installation)
- [2.1 HaSa dependencies](#hasa-dependencies)
- [2.2 Install HaSa](#install-hasa)
- [2.3 Load HaSa](#load-hasa)
- [3 Load demo data](#load-demo-data)
- [3.1 Data directories](#data-directories)
- [3.2 Satellite timeseries stack](#satellite-timeseries-stack)
- [3.3 Selecting reference samples](#selecting-reference-samples)
- [4 Generating outputs](#generating-outputs)
- [4.1 Calculating class
probability](#calculating-class-probability)
- [4.2 Generating classification map and summary
- [4.2 Generating classification map and summary
statistics](#generating-classification-map-and-summary-statistics)
# 1 Introduction
......@@ -37,7 +37,7 @@ documentation assume a familiarity with the R software. The document is
built successively and is intended to lead you step-by-step through the
HaSa procedure of generating probability and classification maps. HaSa
is still in development and any suggestions or improvements are welcomed
and encouraged in our [GitLab Community
and encouraged in our [GitHub Community
Version](https://git.gfz-potsdam.de/habitat-sampler/HabitatSampler.git).
If questions remain please don’t hesitate to contact the authors of the
package. For a detailed description of the Habitat Sampler and its
......@@ -46,10 +46,10 @@ applications, see [Neumann et al.,
## 1.1 Usage
The tool is implemented in R and uses Leaflet [(Cheng et al.,
2019)](https://rdrr.io/cran/leaflet/) to generate interactive maps in a
web browser. There are no assumptions about the input image data and
there are no constraints for the spectral-temporal-spatial domain in
The tool is implemented in R and uses Leaflet [(Cheng et
al., 2019)](https://rdrr.io/cran/leaflet/) to generate interactive maps
in a web browser. There are no assumptions about the input image data
and there are no constraints for the spectral-temporal-spatial domain in
which the image is sampled. The tool requires the input of a priori
expert user knowledge to generate reference data about expected surface
classes which are delineated in the imagery or extracted from an
......@@ -78,7 +78,7 @@ data including the band ID in the timeseries stack are provided below
(Table 1).
| | Band 2 | Band 3 | Band 4 | Band 5 | Band 6 | Band 7 | Band 8 | Band 11 | Band 12 |
|------------|:-------|:-------|:-------|:-----------|:-----------|:-----------|:-------|:--------|:--------|
| ---------- | :----- | :----- | :----- | :--------- | :--------- | :--------- | :----- | :------ | :------ |
| Date | Blue | Green | Red | Red Edge 1 | Red Edge 2 | Red Edge 3 | NIR | SWIR 1 | SWIR 2 |
| 2018-03-03 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 2018-05-07 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
......@@ -112,27 +112,14 @@ required to setup the HaSa tool.
HaSa uses the latest version of the `velox` library (`v0.2.0`) which
does not compile with the latest version the interface to the C++ Boost
library `BH`. Hence, it is necessary to pin the `BH` version. HaSa does
not yet support the latest developments in `rgdal` and `sp` related with
projections. It is also necessary to pin the versions for `sf`, `sp` and
`rgdal`.
library `BH`. Hence, it is necessary to pin the `BH` version.
The installation of `BH`, `sf`, `sp` and `rgdal` is possible with the
following commands:
The installation of `BH` is possible with the following commands:
``` r
install.packages("remotes")
install.packages("https://cran.r-project.org/src/contrib/Archive/BH/BH_1.69.0-1.tar.
gz",
repos = NULL, type = "source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sf/sf_0.9-0.tar.
gz",
repos = NULL, type = "source")
install.packages("https://cran.r-project.org/src/contrib/Archive/sp/sp_1.4-4.tar.
gz",
repos = NULL, type = "source")
install.packages("https://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_1.5-12.
tar.gz",
gz",
repos = NULL, type = "source")
```
......@@ -146,8 +133,8 @@ install the `HaSa` R package, build its manual and its vignettes.
``` r
library(remotes)
remotes::install_git("https://git.gfz-potsdam.de/habitat-sampler/HabitatSampler.git",
ref = "master", subdir = "R-package", dependencies = NA, upgrade = FALSE,
remotes::install_git("https://git.gfz-potsdam.de/habitat-sampler/HabitatSampler.git",
ref = "master", subdir = "R-package", dependencies = NA, upgrade = FALSE,
build = TRUE, build_manual = TRUE, build_vignettes = TRUE)
```
......@@ -161,8 +148,8 @@ warning messages related with the latest changes in `gdal` and `PROJ6`.
``` r
options(rgdal_show_exportToProj4_warnings = "none")
libraries <- c("rgdal", "raster", "maptools", "spatialEco", "randomForest",
"e1071", "devtools", "velox", "rgeos", "leaflet", "htmlwidgets", "IRdisplay",
libraries <- c("rgdal", "raster", "maptools", "spatialEco", "randomForest",
"e1071", "devtools", "velox", "rgeos", "leaflet", "htmlwidgets", "IRdisplay",
"HaSa")
lapply(libraries, library, character.only = TRUE)
```
......@@ -201,10 +188,10 @@ raster::rasterOptions(tmpdir = "./RasterTmp/")
The satellite time series is either passed as a **3.2.1** stack of
images already clipped or **3.2.2** a stack of image to be clipped. In
both cases, the input Satellite images needs to either have a valid
projection or the projection be passed as parameter, i.e.,
`sat_crs_str = '+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs'`,
otherwise, the function will report error. Satellite time series data
are available in `dataPath`.
projection or the projection be passed as parameter, i.e., `sat_crs_str
= '+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs'`, otherwise, the
function will report error. Satellite time series data are available in
`dataPath`.
``` r
satellite_series_path <- paste(dataPath, "SentinelStack_2018.tif", sep = "")
......@@ -219,7 +206,7 @@ b = 21
raster::plotRGB(timeseries_stack, r = r, g = g, b = b, stretch = "lin", axes = T)
```
<img src="HabitatSampler_files/figure-markdown_github/raster preview clipped-1.png" />
<img src="HabitatSampler_files/figure-gfm/raster preview clipped-1.png" />
## 3.3 Selecting reference samples
......@@ -246,15 +233,15 @@ table_data_path <- paste(dataPath, "Example_Reference_table.txt", sep = "")
ref <- HaSa::load_reference_as_table(table_data_path)