raster

Error in predicting raster with randomForest, Caret, and factor variables

空扰寡人 提交于 2019-12-07 13:13:20
问题 I am trying to predict a raster layer with randomForest and the caret package, but fail when I introduce factor variables. Without factors, everything works fine, but as soon as I bring a factor in, I get the error: Error in predict.randomForest(modelFit, newdata) : Type of predictors in new data do not match that of the training data. I have created some sample code below that walks through he process. I present it in a few steps for transparency and to provide a working example. (To skip

time and geographical subset of netcdf raster stack or raster brick using R

风流意气都作罢 提交于 2019-12-07 08:32:51
问题 For the following netcdf file with daily global sea surface temperatures for 2016, I'm trying to (i) subset temporally, (ii) subset geographically, (iii) then take long-term means for each pixel and create a basic plot. Link to file: here library(raster) library(ncdf4) open the netcdf after setting my working directory nc_data <- nc_open('sst.day.mean.2016.v2.nc') change the time variable so it's easy to interpret time <- ncdf4::ncvar_get(nc_data, varid="time") head(time) change to dates that

R - Finding least cost path through raster image (maze)?

随声附和 提交于 2019-12-07 04:44:25
问题 How can I find a non-linear path through raster image data? e.g., least cost algorithm? Starting and ending points are known and given as: Start point = (0,0) End point = (12,-5) For example, extract the approximate path of a winding river through a (greyscale) raster image. # fake up some noisy, but reproducible, "winding river" data set.seed(123) df <- data.frame(x=seq(0,12,by=.01), y=sapply(seq(0,12,by=.01), FUN = function(i) 10*sin(i)+rnorm(1))) # convert to "pixels" of raster data #

Intersection of bands in R raster package

巧了我就是萌 提交于 2019-12-06 21:28:27
My input raster consists of multiple layers, each with an image area surrounded by no data values. These layers don't overlap completely, and I am trying to output a file which consists of only the intersection of all bands (the zone which has no NoData values on any layer). The following works for a few layers, but not for the 50+ that I have in my real files (at least 3000x3000 pixels): library(raster) fin = "D:\\temp\\all_modes.pix" fout = "D:\\temp\\test.pix" inbands = stack(fin, bands = c(3:20)) NAvalue(inbands) = 0 # Not great: #out = all(is.na(inbands) == FALSE) * inbands #writeRaster

How does the function “extract” deal with different projections?

∥☆過路亽.° 提交于 2019-12-06 15:47:03
I need to use the function extract() to do a weighted average extraction from a raster using a grid cell of equal sized squares. My polygon grid is in UTM21n and the raster is in GCS WGS84 datum D. Do I have to reproject the raster before using it into extract()? Or will the function handle it properly? You can find the source code of function extract for SpatialPolygons here . The code starts with the following snippet: setMethod('extract', signature(x='Raster', y='SpatialPolygons'), function(x, y, fun=NULL, na.rm=FALSE, weights=FALSE, cellnumbers=FALSE, small=FALSE, df=FALSE, layer, nl,

add result of image.plot layer on geographic map (bing map)

ぐ巨炮叔叔 提交于 2019-12-06 14:42:02
I have a 2D footprint result for an eddy-covariance system on a 1100*1100 square meter, with the domain of domain = c(-100,1000,-100, 1000) the cell size of the domain (each grid) is 2 meters with the origin (0,0) accordingly on this location: Lon = -97.191391 #longtitude Lat = 36.055935 #latitude An example data (9.01 Mb) is attached here. FFP.rds then I can plot a Bing map on my site as this: library(OpenStreetMap) library(rgdal) map <- openmap(c(36.05778,-97.19250), c(36.05444,-97.18861),type='bing') plot(map) and I use image.plot() from package "fields" to plot the footprint with these

How to change directory for temporary files - problems with huge temporary raster files

ぐ巨炮叔叔 提交于 2019-12-06 14:15:46
问题 I'm desperately trying to writeRaster() but, since the raster is quite large I need a lot of temp memory. The space on my C:/ drive is limited and therefore I want to change the temporary dir to D:/TEMP/ . I tried different approaches I found on the Internet like: Change temporary directory http://r.789695.n4.nabble.com/How-do-I-set-the-Windows-temporary-directory-in-R-td876483.html etc. rasterOptions(tmpdir = "D:/RTEMP/") didn't solve the problem. Even if it worked while the calculation of

get cell number within a distance of one point (raster)

旧街凉风 提交于 2019-12-06 13:38:29
I need to get the cell numbers within a distance (e.g., 10 km) of one point using R, but I didn't figure out how to handle it for the raster data. library(raster) r <- raster(ncols=10, nrows=10) cellFromXY(r, c(2,2)) # get the cell number of one point How to get the cell numbers within a distance of one point? Use extract with the buffer option and cellnumbers=TRUE r <- raster(ncols=100, nrows=100) r[]<-runif(ncell(r)) xy <- cbind(-50, seq(-80, 80, by=20)) extract(r, xy[1:3,], buffer=1000000,cellnumbers=TRUE) 来源: https://stackoverflow.com/questions/13996256/get-cell-number-within-a-distance-of

ggplot2: dealing with extremes values by setting a continuous color scale

假装没事ソ 提交于 2019-12-06 12:46:03
I am trying to plot some global maps (raster files) and I have some problems in setting up a good color scale for my data. What I would like to do is to plot my data using a divergent palette (e.g. cm.colors ), and I would like to center the color "white" of such scale with the value zero, but without having to set symmetric values in the scale (i.e. the same value both negative and positive, i.e. limits=c(-1,1) ). Additionally, I would like to plot all values above and/or below a certain value all with the same color. In other words, if we suppose that my map has a range of -100 to 150, I

NetCDF to Raster Brick “Unable to find inherited method for function 'brick' for 'ncdf4'”

浪尽此生 提交于 2019-12-06 11:29:44
问题 Really simple problem with the raster package, also using ncdf4 to load in an ECMWF Era-Interim Netcdf file. Simply doing this: a <- nc_open("SSTs.nc") B <- brick(a, varname="sst") Returns this: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘brick’ for signature ‘"ncdf4"’ The file is just SST data over the whole globe, for 1 month (Jan2016). When I convert it into an array (i.e. extract dimensions/variable, and convert time to UTC, shove it into