vector files

reading

an shapefile comes with raster package

library(raster)
## Warning: package 'raster' was built under R version 3.6.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.6.3
filename<-system.file("external/lux.shp",package="raster")
filename 
## [1] "D:/Application/R-3.6.2/library/raster/external/lux.shp"
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.6.3
## rgdal: version: 1.5-16, (SVN revision 1050)
## 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: D:/Application/R-3.6.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: D:/Application/R-3.6.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.
s<-shapefile(filename)
s<-readOGR(filename)
## OGR data source with driver: ESRI Shapefile 
## Source: "D:\Application\R-3.6.2\library\raster\external\lux.shp", layer: "lux"
## with 12 features
## It has 5 fields
s
## class       : SpatialPolygonsDataFrame 
## features    : 12 
## extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
## crs         : +proj=longlat +datum=WGS84 +no_defs 
## variables   : 5
## names       : ID_1,     NAME_1, ID_2,   NAME_2, AREA 
## min values  :    1,   Diekirch,    1, Capellen,   76 
## max values  :    3, Luxembourg,   12,    Wiltz,  312

writing

outfile<-"test1.shp"
# shapefile(s,outfile,overwrite=TRUE)
writeOGR(s, getwd(), "outfile3", driver="ESRI Shapefile")

raster files

f<-system.file("external/rlogo.grd",package="raster")
f
## [1] "D:/Application/R-3.6.2/library/raster/external/rlogo.grd"
r1<-raster(f)
r1
## class      : RasterLayer 
## band       : 1  (of  3  bands)
## dimensions : 77, 101, 7777  (nrow, ncol, ncell)
## resolution : 1, 1  (x, y)
## extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
## crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
## source     : D:/Application/R-3.6.2/library/raster/external/rlogo.grd 
## names      : red 
## values     : 0, 255  (min, max)
r2<-raster(f,band=2)
r2
## class      : RasterLayer 
## band       : 2  (of  3  bands)
## dimensions : 77, 101, 7777  (nrow, ncol, ncell)
## resolution : 1, 1  (x, y)
## extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
## crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
## source     : D:/Application/R-3.6.2/library/raster/external/rlogo.grd 
## names      : green 
## values     : 0, 255  (min, max)
b<-brick(f)
b
## class      : RasterBrick 
## dimensions : 77, 101, 7777, 3  (nrow, ncol, ncell, nlayers)
## resolution : 1, 1  (x, y)
## extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
## crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
## source     : D:/Application/R-3.6.2/library/raster/external/rlogo.grd 
## names      : red, green, blue 
## min values :   0,     0,    0 
## max values : 255,   255,  255
s<-stack(f)
s
## class      : RasterStack 
## dimensions : 77, 101, 7777, 3  (nrow, ncol, ncell, nlayers)
## resolution : 1, 1  (x, y)
## extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
## crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
## names      : red, green, blue 
## min values :   0,     0,    0 
## max values : 255,   255,  255

stack获取所有波段的效率没有brick高 其他的栅格数据也可以使用以上方法:netcdf,imagine,ESRI grid,GeoTIFF

files=list.files(pattern = ".nc") #获取本目录下的所有格式为.nc的文件
f<-raster(files[1])
## Loading required namespace: ncdf4
## Warning in .varName(nc, varname, warn = warn): varname used is: SoilMoi0_10cm_inst
## If that is not correct, you can set it to one of: SoilMoi0_10cm_inst, SoilMoi10_40cm_inst, SoilMoi40_100cm_inst, SoilMoi100_200cm_inst, SoilTMP0_10cm_inst, SoilTMP10_40cm_inst, SoilTMP40_100cm_inst, SoilTMP100_200cm_inst
f
## class      : RasterLayer 
## dimensions : 90, 137, 12330  (nrow, ncol, ncell)
## resolution : 0.25, 0.25  (x, y)
## extent     : 70.75, 105, 23.25, 45.75  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : E:/Study/Rstudio/netfcopen/netfile/GLDAS_NOAH025_3H.A20060101.0000.021.nc4.SUB.nc4 
## names      : Soil.moisture 
## z-value    : 3156300 
## zvar       : SoilMoi0_10cm_inst
b<-brick(f)
b
## class      : RasterBrick 
## dimensions : 90, 137, 12330, 1  (nrow, ncol, ncell, nlayers)
## resolution : 0.25, 0.25  (x, y)
## extent     : 70.75, 105, 23.25, 45.75  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : Soil.moisture 
## min values :         3.999 
## max values :        88.138

writing

x<-writeRaster(s,"output.img",overwrite=TRUE)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded datum Unknown based on WGS84 ellipsoid in CRS definition,
##  but +towgs84= values preserved