Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoAI_2021_unit_04_EX_Create Masks #10

Open
Baldl opened this issue Nov 30, 2021 · 28 comments
Open

GeoAI_2021_unit_04_EX_Create Masks #10

Baldl opened this issue Nov 30, 2021 · 28 comments

Comments

@Baldl
Copy link

Baldl commented Nov 30, 2021

No description provided.

Copy link

R is unable the find the "AllEqual" function/command in the above created "remove_files" function. I looked on the internet if this is a potential typo or I forgot to load one of the packages but I could not find the "AllEqual" function and the "all.equal" argument by base R doesnot work.

Copy link
Contributor

gisma commented Dec 4, 2021

@seegerkonstantin ALLEqual is a function of the package greenbrown . So pls try to reinstall the package and try greenbrown::AllEqual

Copy link

FloFranz commented Dec 6, 2021

I have the same error in applying the remove_files function. Then I updated my R version, but it doesn`t helped. Error message: Error in loadNamespace(x) : es gibt kein Paket namens ‘greenbrown’ . I tried to install the package greenbrown from source with install.packages("greenbrown", repos="http://R-Forge.R-project.org"), but the same error occurs. I also tried to download a *.tar file with an older version of the greenbrown package as described here...not helped. Any other idea how it can be fixed?

Copy link

@FloFranz I solved this problem by installing greenbrown with the following command: "install.packages("greenbrown", repos="http://R-Forge.R-project.org")" as described on this side https://greenbrown.r-forge.r-project.org/install.php .

Copy link

FloFranz commented Dec 6, 2021

@seegerkonstantin Confusing! I first had to install bfast, phenopix and Kendall. Then it also worked to install greenbrown as mentioned above.

@gisma
Copy link
Contributor

gisma commented Dec 6, 2021

@FloFranz sorry for confusing. It is often helpful to have a look in the Description file of the package.There you find the dependencies in this case Imports: Kendall, bfast, phenopix, quantreg, parallel, plyr. In common this should work with dependencies = TRUE, however it sometimes fails than you can install this list manually.

@gisma
Copy link
Contributor

gisma commented Dec 8, 2021

As far as I see it you need to install the requested packages as you tried.

  1. SDMTools'
  2. phenopix
    However you were not able to install SDMTools for your R version. So what you can try next is:
  • install it from source
    install.packages("SDMTools",,"http://rforge.net/",type="source")
  • download the package and install it manually local via the MEnu in Rstudio

@gisma
Copy link
Contributor

gisma commented Dec 9, 2021

@SedaBekar

  1. start the System Control Panel applet (Start - Settings - Control Panel - System)
  2. select the Advanced tab
  3. choose Environment Variables
  4. at System Variables, select Path, then click Edit and add the path to Rtools e.g. D:\RTools

@gisma
Copy link
Contributor

gisma commented Dec 9, 2021

@SedaBekar this is fine - pretty good solution ! It would be the best du put it in your setup script or in the .Renviron file of the project.

Copy link

@gisma
I cannot install greenbrown. It says there were non-zero exit status. Is there a further way to install this package apart from install.packages and tar?

Copy link

@gisma I have tried to follow the way of Florian Franz, but I had still to install stucchange manually to be able to install greenbrown.

@gisma
Copy link
Contributor

gisma commented Mar 10, 2022

pleasse follow the above instructions. I have no other hints.

Copy link

@gisma Now another problem occurred when I tried to set the extent of the raster image. It says is.image_collection is not true. Why? And how can I change this?

@gisma
Copy link
Contributor

gisma commented Mar 10, 2022

Again pls Upload your complete and actual Scripts to a repo . Otherwise can not answer your questions.

Copy link

@gisma

#ex create masks
require(envimaR)
source(file.path(envimaR::alternativeEnvi(root_folder = "C:/Users/jomue/edu/geoAI",

  •                                       alt_env_id = "COMPUTERNAME",
    
  •                                       alt_env_value = "PCRZP",
    
  •                                       alt_env_root_folder = "F:/BEN/edu/geoAI"),
    
  •              "src/000-rspatial-setup.R"))
    

variable alt_env_id is NOT defined
'COMPUTERNAME' is set by defaultvariable alt_env_value is NOT defined
'PCRZP' is set by defaultvariable alt_env_root_folder is NOT defined
'F:/BEN/edu' is set by defaultvariable root_folder is NOT defined by calling script... 'root_folder' is set as rootDIR
variable path_prefix is NOT defined by calling script...
'path_' is set as default

variable fcts_folder is NOT defined by calling script...
'src/functions/' is set as default

read data

ras <- raster::stack(file.path(envrmt$path_data, "/marburg_dop.tif"))

subset to three channels

ras <- subset(ras, c("red", "green", "blue"))

download and crop the OSM building data to the extent of the raster data

buildings = opq(bbox = "marburg de") %>%

  • add_osm_feature(key = "building") %>%
  • osmdata_sf()

buildings = buildings$osm_polygons
buildings = sf::st_transform(buildings, crs(ras))
ras_extent <- extent(ras)
Error in extent(ras) : is.image_collection(x) ist nicht TRUE

Copy link

Here my setup script:
#------------------------------------------------------------------------------

basic rspatial course setup script

Type: script

Name: 000-rspatial-setup.R

Author: Chris Reudenbach, creuden@gmail.com

Description: create/read project folder structure and returns pathes as list

load all necessary packages

sources all functions in a defined function folder

Dependencies: devtools::install_github("envima/envimaR")

Output: list containing the folder strings as shortcuts

Copyright: Reudenbach/Nauss 2015-2021, GPL (>= 3)

Details: Please note that the queries on standard arguments have been set for

pragmatic reasons for a typical course setting. Of course, it is

intended to assign the arguments directly in the createEnvi function

Date: 2021-12-12

#------------------------------------------------------------------------------
require(envimaR)

1 - source files

#-----------------

source(file.path(envimaR::alternativeEnvi(root_folder = "C:/Users/jomue/edu/geoAI",
alt_env_id = "COMPUTERNAME",
alt_env_value = "PCRZP",
alt_env_root_folder = "F:/BEN/edu/geoAI"),
"src/geoAI_setup.R"))

rspatial packages

packagesToLoad = c("rgdal","png","tensorflow","keras","reticulate","osmdata","greenbrown","rsample","tfdatasets","purrr","magick","rprojroot","sen2r","patchwork","ggplot2","rpart","rpart.plot","rasterVis", "forcats","e1071","mapview", "mapedit", "tmap", "raster", "terra", "stars", "gdalcubes", "sf","dplyr","tidyverse","RStoolbox",
"randomForest", "ranger", "e1071", "caret", "link2GI","gdalUtils","raster","exactextractr", "doParallel", "CAST","ranger")

mandantory folder structure

projectDirList = c("data/",
"data/modelling/",
"data/modelling/model_training_data/",
"data/modelling/model_training_data/dop/",
"data/modelling/model_training_data/bui/",
"data/modelling/models/",
"data/modelling/prediction/",
"data/modelling/validation/",
"data/modelling/model_testing_data/",
"data/modelling/model_testing_data/dop/",
"data/modelling/model_testing_data/bui/",
"docs/",
"run/",
"tmp",
"src/",
"src/functions/"
)

append additional packages if defined by calling script

if (exists("appendProjectDirList") && appendProjectDirList[[1]] != "")
{
projectDirList = append(projectDirList,appendProjectDirList)
}

if (exists("appendpackagesToLoad") && appendpackagesToLoad[[1]] != "")
{
packagesToLoad = append(packagesToLoad,appendpackagesToLoad)
}

Now create/read root direcory, folder structure and load packages

NOTE root_folder MUST be defined in calling script

if (!exists("root_folder")) {
message("variable root_folder is NOT defined by calling script...\ 'root_folder' is set as rootDIR")
root_folder=rootDIR
}
if (!exists("alt_env_id")) {
message("variable alt_env_id is NOT defined by calling script...\n 'COMPUTERNAME' is set as default\n")
alt_env_id = "COMPUTERNAME"
}
if (!exists("alt_env_value")) {
message("variable alt_env_value is NOT defined by calling script...\n 'PCRZP' is set as default\n")
alt_env_value = "PCRZP"
}
if (!exists("alt_env_root_folder")) {
message("variable alt_env_root_folder is NOT defined by calling script...\n 'F:/BEN/edu' is set as default\n")
alt_env_root_folder = "F:/BEN/edu"
}

if (!exists("path_prefix")) {
message("variable path_prefix is NOT defined by calling script...\n 'path_' is set as default\n")
path_prefix = "path_"
}

root_folder = envimaR::alternativeEnvi(root_folder = root_folder,
alt_env_id = alt_env_id,
alt_env_value = alt_env_value,
alt_env_root_folder = alt_env_root_folder)

if (!exists("fcts_folder")) {
message("variable fcts_folder is NOT defined by calling script...\n 'src/functions/' is set as default\n")
fcts_folder = paste0(root_folder,"/src/functions/")
}

append additional folders if defined by calling script

if (exists("appendProjectDirList") && appendProjectDirList[[1]] != "")
{
projectDirList = append(projectDirList,appendProjectDirList)
}

call central function

envrmt = envimaR::createEnvi(root_folder = root_folder,
folders = projectDirList,
fcts_folder = fcts_folder,
path_prefix = path_prefix,
libs = packagesToLoad,
setup_script = "000-rspatial-setup.R",
source_functions = TRUE,
alt_env_id = alt_env_id,
alt_env_value = alt_env_value,
alt_env_root_folder = alt_env_root_folder)

set temp path to speed up raster package operations

raster::rasterOptions(tmpdir = envrmt$path_tmp)

suppres gdal warnings

rgdal::set_thin_PROJ6_warnings(TRUE)

define some color palettes

mvTop = mapview::mapviewPalette("mapviewTopoColors")
mvSpec = mapview::mapviewPalette("mapviewSpectralColors")
mvVec = mapview::mapviewPalette("mapviewVectorColors")
mvRas = mapview::mapviewPalette("mapviewRasterColors")

@gisma
Copy link
Contributor

gisma commented Mar 10, 2022

Die to possible dependencies in your functions and also due to a convenience etc. I rather orefer a complete GitHub repository.

@Muenchj4
Copy link

gh repo clone Muenchj4/geoAI

Copy link

In this repository you can find the scripts. I am still trying to follow up the dependencies.

Copy link

I have now tried to print the extent of the raster file mannualy like this:
"ras_extent <- c(xmin=480000,xmax=484000,ymin=5626000,ymax=5628000)"
But than I wanted to crop the extent like this:
" e_test <- c(xmin=483000,xmax=484000, ymin=5626000, ymax=5628000)
e_train <- c(xmin=480000, xmax=483000, ymin=5626000, ymax=5628000)
marburg_mask_train <- crop(rasterized_vector, e_train)"
The message occured:
"Error in crop(rasterized_vector, e_train) : is.cube(cube) ist nicht TRUE"
The same error occurred printing the cube of rasterized vector manually like this:
"marburg_mask_train <- crop(cube=c(xmin=480000,xmax=484000,ymin=5626000,ymax=5628000), extent=e_train)"
Error in crop(cube = c(xmin = 480000, xmax = 484000, ymin = 5626000, ymax = 5628000), :
is.cube(cube) ist nicht TRUE
Is there another way to crop the rasterized vector? Or is there a way to tackle this problem from the beginning?

@gisma
Copy link
Contributor

gisma commented Mar 12, 2022

I will Check IT asap

@gisma
Copy link
Contributor

gisma commented Mar 12, 2022

Have an Look at the Setup File .probably you will find some predefined extents . In Addition Check the crs are they identical?

Copy link

In the setup file I cannot find any predefined extent. I have tried to use proj4string instead of crs, but I still gets the message is.cube is not TRUE. What could be the reason?

@gisma
Copy link
Contributor

gisma commented Mar 14, 2022

@Muenchj4 the above script starting with #ex create masks is not included in this repository.

Copy link

@gisma now I have included the script in the it is runned until the error message occurs.

Copy link

@gisma Have you allready chequed my scripts? Can you imagine why the extent function does not work in my script?

Copy link

@gisma Do you know why rasterized_vector has no content:

mapview(rasterized_vector)
Error in res[i] <- readBin(x@file@con, what = dtype, n = 1, size = dsize, :
replacement has length zero
In addition: Warning message:
In rasterCheckSize(x, maxpixels = maxpixels) :
maximum number of pixels for Raster* viewing is 5e+05 ;
the supplied Raster* has 2e+08
... decreasing Raster* resolution to 5e+05 pixels
to view full resolution set 'maxpixels = 2e+08 '

Copy link

png::writePNG(
        terra::as.array(subs),
        target = file.path(path, paste0(targetname, i, ".png"))
      )

Die Funktion in dem lapply- Loop am Ende der subset_ds Funktion erstellt Falschfarbenkomposite der DOP´s. Ich hab nicht wirklich heraus gefunden warum, aber einen funktionierenden Workaround gefunden.

if(nlyr(subs) > 1){
      png(file.path(path, paste0(targetname, i, ".png")), height=nrow(subs), width=ncol(subs)) 
      plotRGB(subs)
      dev.off()
    }else{
      png::writePNG(
        terra::as.array(subs),
        target = file.path(path, paste0(targetname, i, ".png"))
      )
    }

Wenn das Raster subs mehr als einen Kanal besitzt, werden die PNG´s mit dev.off() gespeichert, wenn nicht werden die Masken wie gehabt gespeichert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants