0

I am a beginning user of Rstudio, and I want to run Random Forest by using a couple of "raster layers" which were stacked in ENVI.

Before doing stacking I checked the cell size and the columns and rows numbers in GIS which they all had the same cell size and the columns and rows numbers. My data contains slope, aspect, landuse, geology, etc.

The main problem is that the following error happens whether I entered just one raster layer or whole layer as a stack layer I face with the error

library(rgdal)
Loading required package: sp
rgdal: version: 1.5-12, (SVN revision 1018)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.0.4, released 2020/01/28
Path to GDAL shared files: C:/Program Files/R/R-4.0.2/library/rgdal/gdal
GDAL binary built with GEOS: TRUE 
Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
Path to PROJ shared files: C:/Program Files/R/R-4.0.2/library/rgdal/proj
Linking to sp version:1.4-2
To mute warnings of possible GDAL/OSR exportToProj4() degradation,
use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
library(sp)
library(raster)
library(caret)
Loading required package: lattice
Loading required package: ggplot2
library(lattice)
library(ggplot2

library(readr)
img1 <- read_csv("E:/Topic_sinkholes/Final data/stack layers/img1.hdr")
Parsed with column specification:
cols(
  ENVI = col_character()
)
Warning: 8 parsing failures.
row col  expected     actual                                                  file
 12  -- 1 columns 11 columns 'E:/Topic_sinkholes/Final data/stack layers/img1.hdr'
 13  -- 1 columns 23 columns 'E:/Topic_sinkholes/Final data/stack layers/img1.hdr'
 16  -- 1 columns 3 columns  'E:/Topic_sinkholes/Final data/stack layers/img1.hdr'
 17  -- 1 columns 2 columns  'E:/Topic_sinkholes/Final data/stack layers/img1.hdr'
 18  -- 1 columns 3 columns  'E:/Topic_sinkholes/Final data/stack layers/img1.hdr'
... ... ......... .......... .....................................................
See problems(...) for more details.

This error happens even when I enter layers with TIFF format


I tried the suggested direction from the comments, but the problem still remains:

library(raster) library(readr) img1 <- read_csv("E:/Topic_sinkholes/Final data/stack layers/img1.img") Parsed with column specification: cols( EHFA_HEADER_TAG = col_character() ) Warning: 153556 parsing failures. row col expected actual file 1 EHFA_HEADER_TAG embedded null 'E:/Topic_sinkholes/Final data/stack layers/img1.img' 1 NA 1 columns 222 columns 'E:/Topic_sinkholes/Final data/stack layers/img1.img' 2 EHFA_HEADER_TAG embedded null 'E:/Topic_sinkholes/Final data/stack layers/img1.img' 3 EHFA_HEADER_TAG embedded null 'E:/Topic_sinkholes/Final data/stack layers/img1.img' 3 NA 1 columns 2 columns 'E:/Topic_sinkholes/Final data/stack layers/img1.img' ... ............... ......... ............. ..................................................... See problems(...) for more details.

View(img1) raster() class : RasterLayer dimensions : 180, 360, 64800 (nrow, ncol, ncell) resolution : 1, 1 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs

2
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works. – Ian Turton Jul 3 '20 at 19:36
  • Why are you using resd.csv to read an img or hdr file? The correct function(s) are raster for single band and stack or brick for multi band raster data. – Jeffrey Evans Aug 9 '20 at 3:23
3

You are trying to read raster data with read_csv:

library(readr)
img1 <- read_csv("E:/Topic_sinkholes/Final data/stack layers/img1.hdr")

You've not told us where you got this file from but if it is geospatial raster data it is unlikely to be a CSV file.

Try:

library(raster)
img1 = raster("E:/Topic_sinkholes/Final data/stack layers/img1.hdr")

Note that a .hdr file is sometimes only the "header" information for a raster, and the actual data values are in another file. Possibly raster will sort that out for you, or you have to point raster at that other file.

TIFF files should also read via raster("/path/to/a/raster.tiff").

Read any intro to raster processing in R for more on reading raster files.

2
  • Thank you for the comments I tried your suggested direction, but the problem is still remaind – user166551 Jul 3 '20 at 19:02
  • Read my answer carefully. First I show you using read_csv incorrectly, and then how to use raster correctly. You put the name of the file as an argument to raster. Like img1 = raster("E:/something/seomthing/img1.img). – Spacedman Jul 3 '20 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.