.dat to raster conversion - r

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.

Related

Map xml data from arc GIS

Is it possible to plot/map xml data from arc GIS in R? I have an XML file with a basemap I would like to map in R.
I dont know how to add my XML file to my question but if someone could show me how to plot using this type of file that would be awesome. I usually make my maps in r using ggplot and geom_sf.
#How I loaded my XML file into r
# Load the package required to read XML files.
library("XML")
# Also load the other required package.
library("methods")
# Give the input file name to the function.
result <- xmlParse(file = "Grey background.xml")
# Print the result.
print(result)

plot VIIRS raster data (.h5) in R

Im looking into how to handle .h5 (hdf) VIIRS DNB raster data in R. I would like to plot it or export it as a geotiff.
So far I am able to read in the file with
hdf<- h5file("mypath/GDNBO-SVDNB_npp_d20171101_t0110370_e0116174_b31151_c20180208224859630066_nobc_ops.h5", mode = "r")
But I cannot figure out how to access the "radiance" band, let alone plot it. I have read the "h5" package documentation without any progress..
As another option I have tried to export the h5 file to geotiff with
sds <- get_subdatasets("subset_1_d20171101_t0110370.h5")
gdal_translate(sds[17], dst_dataset = "radiance.tif", overwrite=T) #subset 17 is the radiance band i need
however the file has lost its geolocation during the transformation, when the geotiff is opened in e.g. QGIS, it doesnt have any location or projection.
Anyone knows how to deal with these type of files in R?

reading .csv into EBImage

I'm rather new to R and image processing. I have a series of images stored as csv files that I would like to read into EBImage. I have been using ImageJ in the past. I've been using the imageData(y) command but keep getting the error that my "object must be an array" when I try to read in the file.
I think the issue I'm having is that I've imported the csv as a spatial indexed matrix (x,y) and I need to convert to an array (x,y,value).
test=read.csv('Fe_Kα1.csv', sep=",", header=FALSE)
test1 <- as.matrix(test)
writeImage('test1')
Error in validImage(x) : object must be an array
Thank you

How to write raster files to ENVI format as BIP

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.

Creating Shapefiles in R

I'm trying to create a shapefile in R that I will later import to either Fusion Table or some other GIS application.
To start,I imported a blank shapefile containing all the census tracts in Canada. I have attached other data (in tabular format) to the shapefile based on the unique ID of the CTs, and I have mapped my results. At the moment, I only need the ones in Vancouver and I would like to export a shapefile that contains only the Vancouver CTs as well as my newly attached attribute data.
Here is my code (some parts omitted due to privacy reasons):
shape <- readShapePoly('C:/TEST/blank_ct.shp') #Load blank shapefile
shape#data = data.frame(shape#data, data2[match(shape#data$CTUID, data2$CTUID),]) #data2 is my created attributes that I'm attaching to blank file
shape1 <-shape[shape$CMAUID == 933,] #selecting the Vancouver CTs
I've seen other examples using this: writePolyShape to create the shapefile. I tried it, and it worked to an extent. It created the .shp, .dbf, and .shx files. I'm missing the .prj file and I'm not sure how to go about creating it. Are there better methods out there for creating shapefiles?
Any help on this matter would be greatly appreciated.
Use rgdal and writeOGR. rgdal will preserve the projection information
something like
library(rdgal)
shape <- readOGR(dsn = 'C:/TEST', layer = 'blank_ct')
# do your processing
shape#data = data.frame(shape#data, data2[match(shape#data$CTUID, data2$CTUID),]) #data2 is my created attributes that I'm attaching to blank file
shape1 <-shape[shape$CMAUID == 933,]
writeOGR(shape1, dsn = 'C:/TEST', layer ='newstuff', driver = 'ESRI Shapefile')
Note that the dsn is the folder containing the .shp file, and the layer is the name of the shapefile without the .shp extension. It will read (readOGR) and write (writeOGR) all the component files (.dbf, .shp, .prj etc)
Problem solved! Thank you again for those who help!
Here is what I ended up doing:
As Mnel wrote, this line will create the shapefile.
writeOGR(shape1, dsn = 'C:/TEST', layer ='newstuff', driver = 'ESRI Shapefile')
However, when I ran this line, it came back with this error:
Can't convert columns of class: AsIs; column names: ct2,mprop,mlot,mliv
This is because my attribute data was not numeric, but were characters. Luckily, my attribute data is all numbers so I ran transform() to fix this problem.
shape2 <-shape1
shape2#data <- transform(shape1#data, ct2 = as.numeric(ct2),
mprop = as.numeric(mprop),
mlot = as.numeric(mlot),
mliv = as.numeric(mliv))
I tried the writeOGR() command again, but I still didn't get the .prj file that I was looking for. The problem was I didn't specified the coordinate systems for the shapefile when I was importing the file. Since I already know what the coordinate system is, all I had to do was define it when importing.
readShapePoly('C:/TEST/blank_ct.shp',proj4string=CRS("+proj=longlat +datum=WGS84")
After that, I re-ran all the things I wanted to do with the shapefile, and the writeOGR line for exporting. And that's it!

Resources