I want to stack and write some Landsat bands/tiff files to BIP interleave in ENVI format. However, the results always come as BSQ, even though I change the bandorder to BIP.
Below is my code:
library(raster)
library(rgdal)
library(gdalUtils)
inbands <-list.files(pattern= "*.tif")
stk<-stack(inbands[2], inbands[3], inbands[4])
writeRaster(stk, "BIP_test", format="ENVI", bandorder='BIP')
This also did not work
writeRaster(stk, "BIP_test", format="ENVI", options="INTERLEAVE=PIXEL", overwrite=TRUE)
Any assistance is appreciated.
I think this should work:
writeRaster(stk, "BIP_test", format="ENVI", options="INTERLEAVE=BIP", overwrite=TRUE)
per the GDAL formats page on the ENVI format, "BIP" (not "PIXEL") is the argument to "INTERLEAVE". Based on my reading of the WriteRaster helpfile, bandorder='BIP' only works for the raster package's native file format.
Related
I have a map data whose format is rds. Now I want to use this data in another software which asks for shp format. How to convert rds format data into shp format in R?
If it is spatial object saved as a R-specific binary file of "Serialization Interface for Single Objects" type (see ?readRDS) probably created at some point by saveRDS(), read your file with
library(rgdal)
library(sp)
x <- readRDS("path/to/the/rds_file.rds")
and then write it with:
rgdal::writeOGR(x, "path/to/destination", "filename", driver = "ESRI Shapefile")
Be sure not to put ".shp" at the end of your output filename.
Also be sure not to put a / at the end of the destination folder. Otherwise you might face the error
Creation of output file failed
When the error
Error: inherits(obj, "Spatial") is not TRUE
you might have forgotten the x as the first argument in the writeOGR function.
I have downloaded a dataset that is supposed to be a global map of freezing and thawing indices. Unfortunately I cannot uploaded or view it as a raster in R. Does anyone know how to convert this type of file to a .bil file?
I can load the file using: dat<- read.delim('freez.dat', header = F)
But I cannot get this to plot as a traditional raster.
Any input is appreciated.
How can one make a Rasterbrick in R from several hdf5 files? Often, data are provided in hdf5 format and one has to convert it to a more friendly format for easy handling.
At the moment I know of the rhdf5 package but how to get a RasterBrick is that which I am unsure about.
source("http://bioconductor.org/biocLite.R")
biocLite("rhdf5")
library("rhdf5")
library("raster")
You can access several hdf5 files on this link http://mirador.gsfc.nasa.gov/cgi-bin/mirador/cart.pl?C1=GPM_3IMERGHH&CGISESSID=fb3b45e091f081aba8823f3e3f85a7d9&LBT_THRESHOLD=4000000.
You can use two files for illustration.
Thanks!
AT.
One option is using gdalUtils to convert the hdf5 files into GTiff. Once you did that you can read them in a stack. Here is an example code:
# list all the `hdf5` files
files <- list.files(path=".", pattern=paste(".*.h5",sep=""), all.files=FALSE, full.names=TRUE)
#choose the band that you want using the sds[] option and write GTiff files.
for (i in (files)) {
sds <- get_subdatasets(i)
r2 <- gdal_translate(sds[1], dst_dataset =paste(i,".tif",sep=""))}
I'm trying to use saveRDS() to save a large number of lists each containing a raster layer and a list with metadata. It worked fine when the raster layer was extracted from a ncdf file, but when the original file is an ascii file, saveRDS() only writes a pointer to the original file instead of writing the values to the end file.
Here's a condensed version of what's going on:
require(raster)
mf <- raster('myfile.asc')
meta <- list(mylonglistofmetadata)
res <- list(mf, meta)
saveRDS(res, 'myresult.Rdata')
myresult.Rdata is now simply a 33KB pointer to myfile.asc, when I really would like it to store the values so it will still work after I erase myfile.asc (so it should be about 15MB)
In contrast, for other files in ncdf format:
require(ncdf4)
require(raster)
ff <- 'myfile2.nc'
nc <- nc_open(ff)
meta <- list(mylonglistofmetadata)
res <- list(nc, meta)
saveRDS(res, 'myresult2.Rdata')
Here, myresult2.Rdata is storing everything just like I want it to, so my guess is that the issue arises with the raster package?
Anyone has any idea on how to fix this? I would prefer not to use writeRaster(), since I'm trying to keep the metadata together with the data, and use the same format as in my batch extracted from ncdf files to ease later processing.
The short answer is that you can do:
mf <- raster('myfile.asc')
mf <- readAll(mf)
mf
Now, the values are in memory and will be saved to the .RData file
Also note that:
You can save metadata with the data via writeRaster (see ?raster::metadata
you can access ncdf files (with geographic data) via raster('myfile2.nc')
your example for the ncdf file is not informative, as you do not actually use nc for anything. If you replaced mf with nc it would not work either after you removed 'myfile2.nc'
I have a script to create a randomly distributed square polygons in KML format which takes in shapefile with a single polygon as an input which works absolutely well. The problem arise when I tried to create the user defined function out of it. I used readShapePoly() function to read the shapefile and it works well when used out of the function. But when the function is created in which shapefile should be given as an input, it simply wont take. It shows this error message
Error in getinfo.shape(filen) : Error opening SHP file
I avoid writing extensions and I do have all the extension files to create the shapefile.
Part of the script to read the shapefile using it as the input file:
library(maptools)
library(sp)
library(ggplot2)
Polytokml <- function(shapefile_name){
###Input Files
file1 <- "shapefile_name"
Shape_file <- readShapePoly(file1) #requires maptools
return(Shape_file)
}
The function is created but it doesn't work if the function is called.
>Polytokml(HKH.shp)
Error in getinfo.shape(filen) : Error opening SHP file
This works well out of the function.
###Input Files
file1 <- "shapefile.shp"
Shape_file <- readShapePoly(file1) #requires maptools
This is just an example out of the whole script in which different arguments are taken as an input. So just to make things simple I have added script to read the shapefile which has been a problem now. Do let me know if it is not clear.
Thank you so much in advance :)