Hello @yiboliu ,
welcome in the community.
I hope that you already have an answer to your question.
If so please answer your own question and indicate it as solved.
If not we can only redo your analysis if you provide us a reproducible example .
By providing us a small tif file (e.g. a data file from one of the packages) that leads to the same problem, you make it easier for us to help you.
I downloaded the tif-file and I can view its contents (a rotated rectangle) in a browser.
However, when I use your code (see the reprex output below) I get an error saying the raster function can not create a RasterLayer object.
This is different from your situation so maybe the file got corrupted during transport (??) .
I assume you had no messages as a result of using the raster function (? )
What is the result of summary(r1)?
Not sure how to continue.
library(raster)
#> Warning: package 'raster' was built under R version 4.1.1
#> Loading required package: sp
library(rgdal)
#> rgdal: version: 1.5-23, (SVN revision 1121)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
#> Path to GDAL shared files: D:/tools/R/Packages/rgdal/gdal
#> GDAL binary built with GEOS: TRUE
#> Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
#> Path to PROJ shared files: D:/tools/R/Packages/rgdal/proj
#> PROJ CDN enabled: FALSE
#> Linking to sp version:1.4-5
#> To mute warnings of possible GDAL/OSR exportToProj4() degradation,
#> use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
#> Overwritten PROJ_LIB was D:/tools/R/Packages/rgdal/proj
setwd("D:/data/R/RStudio_Community")
list.files(path=".",pattern = "*.tif")
#> [1] "20160207.tif"
r1<-raster("./20160207.tif")
#> Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.
e<-raster::extent(236400,468600,3553200,3789900)
r1_crop<-raster::crop(r1,e)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'crop': object 'r1' not found
r_r1_crop<-raster_to_matrix(r1_crop)
#> Error in raster_to_matrix(r1_crop): could not find function "raster_to_matrix"
r11<-raster2matrix(r1_crop)
#> Error in raster2matrix(r1_crop): could not find function "raster2matrix"
r111<-as.matrix(r1_crop)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'as.matrix': object 'r1_crop' not found
Created on 2021-08-31 by the reprex package (v2.0.0)
It was possible for me to read the file with the package tiff :
library(tiff)
setwd("D:/data/R/RStudio_Community")
list.files(path=".",pattern = "*.tif")
#> [1] "20160207.tif"
r1<-readTIFF("./20160207.tif")
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 33550 (0x830e) encountered
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 33922 (0x8482) encountered
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 34735 (0x87af) encountered
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 34737 (0x87b1) encountered
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 42113 (0xa481) encountered
class(r1)
#> [1] "matrix" "array"
dim(r1)
#> [1] 7891 7751
Created on 2021-08-31 by the reprex package (v2.0.0)
Dear @HanOostdijk
Thank you again for your enthusiastic help. I tried again and found that the matrix converted after reinstalling the R package became the correct matrix. I am very happy to communicate with you on the R language. I am a beginner of the R language. I hope I can consult you in the future