Cannot Run Raster Calculation Using GDAL - raster

I am having some trouble running a raster calculation through GDAL. I am attempting to convert a raster to dB through the equation below.
A = my raster file
"10*log10(power(A,2))-83"
Using gdal_calc it looks like the following.
C:\>gdal_calc.py -A "C:\Users\moses\Desktop\Calc_Test\test444.img"
--outfile="C:\Users\moses\Desktop\Calc_Test\test555.img"
--calc="10*log10(power(A,2))-83" --debug --overwrite
I have attempted to do this in separate pieces. So, raise the raster to the second power, then get log10 of that result. But each time that I do this i recieve the following error...
:1: RuntimeWarning: divide by zero encountered in log10
Any idea on how I might go about resolving this?
Thanks in advance for any potential help.

Apparently some of your pixel values contain zeros.
So if you take log10(0), it returns -Inf which causes the error.
Try this:
C:\>gdal_calc.py -A "C:\Users\moses\Desktop\Calc_Test\test444.img" --outfile="C:\Users\moses\Desktop\Calc_Test\test555.img" --calc="10*log10(power(A+0.0001,2))-83" --debug --overwrite
Or set your 0 values to NA.

Related

rafalib - as.fumeric error " 'x' must be a character"

Generating a plot with rafalib open.
Have a dataset with a column labeled "Tissue". The entire table is in object "b". "hc" contains a hclust of the distribution of the numeric values of "b".
When I run:
myplclust(hc, xlab="distance",main="Hierarchical Clustering Dendrogram",labels=b$Tissue,lab.col=as.fumeric(b$Tissue),cex=0.5)
RStudio responds with:
Error in as.fumeric(b$Tissue) : 'x' must be a character
What's going on here? I've reset R multiple times. I have rafalib installed and active.
SOLVED:
The names I was entering under this function were being recognized as data rather than values for some reason. By converting my list to a vector with as.vector inside of the as.fumeric function, the problem was solved.
The correct code now looks like;
myplclust(hc, xlab="distance",main="Hierarchical Clustering Dendrogram",labels=b$Tissue,lab.col=as.fumeric(as.vector(b$Tissue)),cex=0.5)

Large data.tree causes plot() to error

I'm trying to build an org chart from a data.frame in r using the data.tree package.
As far as i can tell i have constructed the tree correctly, but when I try to plot() the data.tree object (which print()s fine) I get an error:
abort(0) at jsStackTrace#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:5:22110
stackTrace#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:5:22258
abort#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:28:10656
nullFunc_iii#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:5:662065
a8#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:21:31634
iC#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:9:83383
aD#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:9:102098
uF#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:9:173805
pG#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:9:204484
xc#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:11:740
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:28:403
ccallFunc#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:5:15982
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/viz-0.3/viz.js:47:42
renderValue#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/grViz-binding-0.8.4/grViz.js:38:27
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:625:30
forEach#[native code]
forEach#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:55:21
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:551:14
forEach#[native code]
forEach#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:55:21
staticRender#http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:549:12
http://localhost:30899/session/viewhtml2fdc215a4edd/lib/htmlwidgets-0.7/htmlwidgets.js:638:38
Any ideas?
I have just started using the data.frame package a couple of days ago, but came across the same problem with some of my trees - print(tree) worked but plot(tree) gave a similar error message to yours. So far, I could always resolve the issue by removing special characters like single and double quotation marks from the input data. Thus, the plot function appears to be sensitive to certain symbols or special characters ... maybe a starting point for your search for a solution?

Can't project data to robinson map with ggplot "no system list"

I am trying to plot some points on a map in R following instructions found here
http://rpsychologist.com/working-with-shapefiles-projections-and-world-maps-in-ggplot
When I get to the line where I use the rgdal:project function though I get an error message
places_robin_df <- project(cbind(places_df$LONGITUDE, places_df$LATITUDE), proj="+init=ESRI:54030")
Error in project(cbind(places_df$LONGITUDE, places_df$LATITUDE), proj = "+init=ESRI:54030") :
no system list, errno: 2
I find that changing the ESRI:54030 to random gibberish gives me the same error message which suggests to me that it can't find the ESRI:54030 information. What do I need to do to get things working?
Note I am running R in Ubuntu 14.04.
I ended up finding the following link that suggested that ESRI is case sensitive in Linux and should be lower-case.
http://lists.osgeo.org/pipermail/mapserver-users/2011-October/070334.html
changing the code to
places_robin_df <- project(cbind(places_df$LONGITUDE, places_df$LATITUDE), proj="+init=esri:54030")
fixes things

R: Error thrown while using RGDAL and RASTER packages

To whom this may concern:
Here is the source code:
GRA_D1<- raster(files[[1]])
//Sets up an empty output raster:
GRA_D1<- writeStart(GRA_D1,filename='GRA_D1.tif', format='GTiff', overwrite=TRUE)
//Write to the raster, for loop:
for(i in 1:dim(GRA_D1)[1]){
//Extract raster values at rows
d.Frame<- matrix(NA,ncol=2,nrow=dim(GRA_D1)[2])
d.Frame[,1]<- getValues(r1[[1]],i)
d.Frame[,2]<- getValues(r1[[2]],i)
w.Frame<- as.data.frame(d.Frame)
names(w.Frame)<- c("D1_pred_disAg","D1_pred_RK")
//Apply the predictive model:
m.pred<-predict(mod.1, w.Frame)
//Write the predictions to the empty TIFF raster
GRA_D1<-writeValues(GRA_D1,m.pred,i)
print(i)}
//Finish writing to the raster
GRA_D1<- writeStop(GRA_D1)
I am attempting to write output to an empty TIFF raster, but I keep receiving the following error message:
#Error in .local(.Object, ...) :
`general_file_path\GRA_D1.tif' does not exist in the file system,
and is not recognised as a supported dataset name.
I wonder if this is related to misusing functions in either the RGDAL or RASTER package.
Could someone please assist me?
Thanks in advance for your generosity.
Cheers,
AD
Super simple fix. Cannot believe that it is this simple and that it took me this long, but here is the answer:
"rgdal" and/or "GTiff" files don't like the use of underscores in their dataset names.
When running the code with "GRAD1.tif" (instead of "GRA_D1.tif"), all works well.
You really should not be doing any of this, I think, as you could do:
p <- predict(r1, mod.1, filename='GRA_D1.tif')
(and that filename works just fine)

Output FALSE instead of error in R

What I would like to do is test whether a matrix is positive definite using the command chol() in R. However, if the chol() command produces an error I would like it to output false rather than producing an error and stopping the code from running. Is there an easy way to do this? I have been messing around with the command try() but it still gives me an error.
For example, if I have the matrix:
mat=matrix(c(1,2,3,1,2,3,5,5,5),nrow=3,ncol=3,byrow=F)
try(chol(mat))
this still produces an error "Error in chol.default(mat) :
the leading minor of order 3 is not positive definite."
How can I get this to produce FALSE instead of this error?
Use tryCatch, and define a handler to use in the event of an error:
tryCatch(chol(mat), error=function(e) FALSE)

Resources