[R-sig-Geo] Some issues when ploting rasters...
Dear all, I have been playing with rasters for a while but when it comes to plotting I always encounter some difficulties that I have not resolved yet... * I have a raster r2 e class : Extent xmin: -2356170 xmax: -1624740 ymin: 1218210 ymax: 2538240 r2 = raster (e,ncol=10,nrow=5) r2[]= runif (50,0,100) r2 class : RasterLayer dimensions : 5, 10, 50 (nrow, ncol, ncell) resolution : 73143, 264006 (x, y) extent : -2356170, -1624740, 1218210, 2538240 (xmin, xmax, ymin, ymax) coord. ref. : NA data source : in memory names : layer values : 3.23514, 98.28136 (min, max) * Since I am going to plot several rasters (of the same extent but different values) I want my colors and legend to go from 0 to 100 plot (r2, breaks=seq(0,100,1), col=pal_temp(length(my.at)) ,ext=extent(r) , axis.args=list(at=seq(0,100,50), labels=seq(0, 100,50), cex.axis=0.6), legend.args=list(text='MYUNITSHERE', side=4, font=2, line=2.5, cex=0.8)) ### PROBLEM? 1. Why does the extent on the x-axis does not restrict to the xmin and xmax of the extent? How can I change that ? I have gone thorugh serveral posts but could not find any solution within plot::raster 2. Worse case scenario, I would remove the bounding box of the map. I tried with axes=F and frame.plot=F put it just removes the objectshow can I do that? THANKS A LOT, Pep ### SESSION INFO sessionInfo() R version 2.15.2 (2012-10-26) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] fields_6.7 spam_0.29-2RColorBrewer_1.0-5 rgdal_0.7-22 raster_2.0-31 sp_0.9-99 ggplot2_0.9.2.1 loaded via a namespace (and not attached): [1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 grid_2.15.2 gtable_0.1.1 labeling_0.1 lattice_0.20-10 MASS_7.3-22 memoise_0.1 [10] munsell_0.3 plyr_1.7.1 proto_0.3-9.2reshape2_1.2.1 scales_0.2.2 stringr_0.6 tools_2.15.2 [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[R-sig-Geo] Modify lagsarlm function in spdep package
Dear subscribers and Prof. Bivand, I am using lagsarlm function in spdep package to study my research problem. I am trying to use restaurants' price, rating, and some other variables to explain their customer visit. So customer visit is my dependent variable, and I have 6 independent variables. I want to use the spatial mixed model so that I can find out whether there are spatial correlated issue in my independent variables. I understand that for the spatial mixed model, the lagsarlm function calculates the spatial lag coefficients for all the 6 independent variable, but what I really want is just for two of them. So I checked the course code for lagsarlm function to see where I should modify based on my need. I figured I should change the part that construct the WX matrix. My modification is as follows: if (m 1) { WX - matrix(nrow = n, ncol = 2) wx1 - lag.listw(listw, x[, 7], zero.policy = zero.policy) wx2 - lag.listw(listw, x[, 8], zero.policy = zero.policy) if (any(is.na(wx))) stop(NAs in lagged independent variable) WX - cbind(wx1, wx2) } and all the other parts remain same. However, when I run this self-defined function, R keeps giving me the following error message: Error in get(verbose, envir = .spdepOptions) : object '.spdepOptions' not found I have made sure that I loaded the spdep package before I run this function. Could you please let me know why I get this error and how should I fix it? I really appreciate any help or suggestion you could give to me. Best, Michelle [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[R-sig-Geo] Raster memory problem
Dear list, I am using large raster bricks for my analyses and I am running out of memory. I get the following message: Error: cannot allocate vector of size 231.0 Mb and this whether I do all my brick manipulations by loading the brick in R's memory or not (i.e., whether I use the option filename=name of my file). The strangest characteristic of the problem I have is that I get an error message when I handle smaller brick and not when handle larger brick. Here is a simple example: library(raster) b0 - brick(nrows=79, ncols=90, nl=1000) b0[] - rnorm(ncell(b0)*nlayers(b0)) writeRaster(b0, filename=b0,overwrite=TRUE) s1 - stack(b0,b0,b0,b0) # inMemory(s1) s2 - stack(b0,b0,b0,b0,b0) b1 - brick(s1, filename=b1, overwrite=TRUE) # b1 gives an error of the type: # Error: cannot allocate vector of size 217.0 Mb b2 - brick(s2, filename=b2, overwrite=TRUE) # b2 does not give me an error # And this even if b1 is smaller than b2 Platform info: R version 2.15.2 (2012-10-26) -- Trick or Treat Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i386-w64-mingw32/i386 (32-bit) On windows 32-bit I have clean up my computer, defragmented it, increase my virtual memory, etc, and I still get an error. Any help will be greatly appreciated, Marie [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Modify lagsarlm function in spdep package
On Wed, 12 Dec 2012, Lei Wang wrote: Dear subscribers and Prof. Bivand, I am using lagsarlm function in spdep package to study my research problem. I am trying to use restaurants' price, rating, and some other variables to explain their customer visit. So customer visit is my dependent variable, and I have 6 independent variables. I want to use the spatial mixed model so that I can find out whether there are spatial correlated issue in my independent variables. I understand that for the spatial mixed model, the lagsarlm function calculates the spatial lag coefficients for all the 6 independent variable, but what I really want is just for two of them. So I checked the course code for lagsarlm function to see where I should modify based on my need. I figured I should change the part that construct the WX matrix. My modification is as follows: You do not want to do this in this way - simply create two new variables with lag.listw() and use them in a regular lag model. You will not be able to use impact measures in the usual way - if n is small enough to invert (I - \rho W), use dense matrix methods to generate the S(W)_r for the variable pair. Roger PS: the reasons functions and variables are not found in your case is that they are not exported in the NAMESPACE, so need to be prepended with spdep:::. if (m 1) { WX - matrix(nrow = n, ncol = 2) wx1 - lag.listw(listw, x[, 7], zero.policy = zero.policy) wx2 - lag.listw(listw, x[, 8], zero.policy = zero.policy) if (any(is.na(wx))) stop(NAs in lagged independent variable) WX - cbind(wx1, wx2) } and all the other parts remain same. However, when I run this self-defined function, R keeps giving me the following error message: Error in get(verbose, envir = .spdepOptions) : object '.spdepOptions' not found I have made sure that I loaded the spdep package before I run this function. Could you please let me know why I get this error and how should I fix it? I really appreciate any help or suggestion you could give to me. Best, Michelle [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo -- Roger Bivand Department of Economics, NHH Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: roger.biv...@nhh.no ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Raster memory problem
2012/12/12 Marie Auger-Methe marie.augerme...@gmail.com Dear list, I am using large raster bricks for my analyses and I am running out of memory. I get the following message: Error: cannot allocate vector of size 231.0 Mb and this whether I do all my brick manipulations by loading the brick in R's memory or not (i.e., whether I use the option filename=name of my file). The strangest characteristic of the problem I have is that I get an error message when I handle smaller brick and not when handle larger brick. I have clean up my computer, defragmented it, increase my virtual memory, etc, and I still get an error. Any help will be greatly appreciated, Marie Welcome to the band called Three of us (can't handle such big rasters) ;-) -raffaele [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Raster memory problem
Take a look at setOptions to force disk to disk. raster will guess if it can do the operation in memory but occasionally underestimates the memory available or required. Sent from my iPhone On 12/12/2012, at 15:20, Raffaele Morelli raffaele.more...@gmail.com wrote: 2012/12/12 Marie Auger-Methe marie.augerme...@gmail.com Dear list, I am using large raster bricks for my analyses and I am running out of memory. I get the following message: Error: cannot allocate vector of size 231.0 Mb and this whether I do all my brick manipulations by loading the brick in R's memory or not (i.e., whether I use the option filename=name of my file). The strangest characteristic of the problem I have is that I get an error message when I handle smaller brick and not when handle larger brick. I have clean up my computer, defragmented it, increase my virtual memory, etc, and I still get an error. Any help will be greatly appreciated, Marie Welcome to the band called Three of us (can't handle such big rasters) ;-) -raffaele [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Raster memory problem
Marie, It seems you are manipulating large amounts of data in RAM with 32-bit R --- use 64 bit if you can. As Jacob indicated, you can use options like rasterOptions(maxmemory=1e+07, chunksize=1e+06). However, you might also have been able to avoid memory problems by using the more direct b1 - writeRaster(s1, filename=b1, overwrite=TRUE) instead of: b1 - brick(s1, filename=b1, overwrite=TRUE) It would probably also be better to do: b0 - writeRaster(b0, filename=b0,overwrite=TRUE) (values of b0 now on disk) instead of writeRaster(b0, filename=b0,overwrite=TRUE) (R object b0 did not change; all values still in RAM) Best, Robert On Wed, Dec 12, 2012 at 1:14 PM, Marie Auger-Methe marie.augerme...@gmail.com wrote: Thanks for the replies. changing the raster options as suggested by Jacob fixed the problem. I lowered the maxmemory: rasterOptions(maxmemory=**9000) rather than changing the todisk option. It worked like a charm. Thanks! Marie On 12/12/2012 8:24 PM, Jacob van Etten wrote: Take a look at setOptions to force disk to disk. raster will guess if it can do the operation in memory but occasionally underestimates the memory available or required. Sent from my iPhone On 12/12/2012, at 15:20, Raffaele Morelli raffaele.more...@gmail.com wrote: 2012/12/12 Marie Auger-Methe marie.augerme...@gmail.com Dear list, I am using large raster bricks for my analyses and I am running out of memory. I get the following message: Error: cannot allocate vector of size 231.0 Mb and this whether I do all my brick manipulations by loading the brick in R's memory or not (i.e., whether I use the option filename=name of my file). The strangest characteristic of the problem I have is that I get an error message when I handle smaller brick and not when handle larger brick. I have clean up my computer, defragmented it, increase my virtual memory, etc, and I still get an error. Any help will be greatly appreciated, Marie Welcome to the band called Three of us (can't handle such big rasters) ;-) -raffaele [[alternative HTML version deleted]] __**_ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/**listinfo/r-sig-geohttps://stat.ethz.ch/mailman/listinfo/r-sig-geo __**_ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/**listinfo/r-sig-geohttps://stat.ethz.ch/mailman/listinfo/r-sig-geo __**_ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/**listinfo/r-sig-geohttps://stat.ethz.ch/mailman/listinfo/r-sig-geo [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[R-sig-Geo] rasterize func: polygon values to raster cells - error msg
Hi, I have used the rasterize function to get values from a spatialPolyDataFrame variable (@data$variable) to raster: rasterize(SPDFobj, rasterObj, field=variable) For a specific case a get a, to me, strange error message . Error in writeValues(out, fun(n), tr$row[i]) : error in evaluating the argument 'v' in selecting a method for function 'writeValues': Error in fun(n) : unused argument(s) (n) It works with one SpatialPolyDataFrame that Im working with, but not with another one and I cannot figure out why. Any ideas of what is going on? Unfortunately, I have not been able to reproduce the error with simulated data (and my data files are huge) but this is essentially what I have done (but the code below works - actually the raster layer in this code works with my SPDFobj): p1 - rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20)) p2 - rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0)) p3 - rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0)) pols - SpatialPolygons( list( Polygons(list(Polygon(p1), Polygon(hole)), 1), Polygons(list(Polygon(p2)), 2), Polygons(list(Polygon(p3)), 3))) pols -SpatialPolygonsDataFrame(pols, data.frame(W=1:3)) r - raster(ncol=80, nrow=90) test-rasterize(pols, r, field=W) Here is an overview of the objects I am using. summary(SPDFobj) Object of class SpatialPolygonsDataFrame Coordinates: min max x -11.08333 31.91753 y 33.91667 71.91743 Is projected: FALSE proj4string : [+proj=longlat +datum=WGS84] Data attributes: variable ... ... rasterObj_orig class : RasterLayer dimensions : 12768, 11050, 141086400 (nrow, ncol, ncell) resolution : 250, 250 (x, y) extent : 2782250, 5544750, 2220250, 5412250 (xmin, xmax, ymin, ymax) coord. ref. : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=321 +ellps=GRS80 +units=m +no_defs data source : C:\Users\veGustaf\Desktop\phd\Where is Ndep neg\bogs.grd names : layer values : 36, 36 (min, max) rasterObj #projection fixed etc through an extent object, raster -- extent -- raster class : RasterLayer dimensions : 12768, 11050, 141086400 (nrow, ncol, ncell) resolution : 250, 250 (x, y) extent : 2782250, 5544750, 2220250, 5412250 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 Cheers, Gustaf -- Gustaf Granath (PhD) Dept of Plant Ecology and Evolution Uppsala University ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo