[R-sig-Geo] Is the raster package still receiving updates?

2017-10-23 Thread Robert J. Hijmans
For a variety of reasons, I have not been able to do much work on raster for the past year. And most of my thinking about it, and some work, has been around a next generation replacement that is simpler in use and much faster. But for now, over the coming months, I will make sure that it gets

Re: [R-sig-Geo] difficulties on applying a function trough calc on a rasterstack

2016-08-08 Thread Robert J. Hijmans
Works for me like this library(raster) r1 <- raster (matrix(ncol=3, data = 1,nrow = 3)) r2 <- raster (matrix(ncol=3, data = 21,nrow = 3)) r3 <- raster (matrix(ncol=3, data = 72,nrow = 3)) rst <- stack(r1, r2, r3) calc (rst, mfri.test) Here is an improved version (I think) of mfri.test

Re: [R-sig-Geo] Problems with rbind(list(), makeUniqueIDs=T)

2016-07-23 Thread Robert J. Hijmans
The raster way would be: library(raster) m <- lapply(c("TZA", "UGA", "GHA"), function(x) getData("GADM", country=x, level=1)) m <- do.call(bind, m) Robert On Sat, Jul 23, 2016 at 4:10 AM, Bacou, Melanie wrote: > Edzer, Rolf, > Many thanks for the clarification! > Just to

Re: [R-sig-Geo] raster: extract with different methods (simple, bilinear) among layers in brick?

2016-07-20 Thread Robert J. Hijmans
Tim, here is an an approach: library(raster) b <- brick(system.file("external/rlogo.grd", package="raster")) p <- SpatialPoints(cbind(sample(77,10), sample(77,10))) methods <- c("bilinear","simple","bilinear") k <- methods == "simple" x1 <- extract(b[[which(k), drop=F]], p) x2 <-

Re: [R-sig-Geo] Announcing the R Shapefile Contest

2016-07-14 Thread Robert J. Hijmans
That still misses the point completely. Geopackage is just another format out of many. For a map you make in R, it normally should not matter how the data used may have been stored. Perhaps it would be more appropriate to call it the SpatialPolygons contest; but I think that what you have in mind

Re: [R-sig-Geo] Minimum bounding circle from cluster of points (Tina Cormier)

2016-07-10 Thread Robert J. Hijmans
Here is a solution using optimization library(raster) library(rgeos) set.seed(7) n <- 4 xy <- cbind(runif(n), runif(n)) f <- function(p) { max(pointDistance(rbind(p), xy, lonlat=FALSE)) } p <- optim(colMeans(xy), f) cc <- buffer(SpatialPoints(rbind(p$par)), width=p$value, quadsegs=45) plot(cc)

Re: [R-sig-Geo] Incorrect area coming from raster

2016-06-09 Thread Robert J. Hijmans
Alex, You should provide a full reproducible example. You can get the boundaries via raster::getData, and you can create a raster in memory. Your script should also indicate the packages you use. To get the area of the polygons, I would do library(raster) nga <- getData('GADM', country='NGA',

Re: [R-sig-Geo] Determining the area of grid cells from a raster in R

2016-06-08 Thread Robert J. Hijmans
> library(raster) > library(spatstat) ( ...) The following objects are masked from ‘package:raster’: area, rotate, shift So either do not load spatstat or call the raster function explicitly raster::area(x) Best, Robert On Wed, Jun 8, 2016 at 12:49 PM, Alex Fitz

Re: [R-sig-Geo] How to calculate climatology in rasterbricks

2016-06-08 Thread Robert J. Hijmans
This (zapply passing ellipses arguments to stackApply) was fixed, I think, in the development version. You can try it if you want: install.packages("raster", repos="http://R-Forge.R-project.org;) On Sat, Jun 4, 2016 at 1:59 PM, Thiago V. dos Santos via R-sig-Geo wrote:

Re: [R-sig-Geo] Create circular polygon of certain radius in R

2016-06-08 Thread Robert J. Hijmans
For lon/lat coordinates, you can use the circles function in "dismo": library(dismo) r <- raster(system.file("external/rlogo.grd", package="raster")) pts <- data.frame(x=c(17, 42, 85, 70, 20, 53, 26, 84), y=c(28, 73, 38, 56, 0, 29, 63, 22)) c1 <- circles(pts, lonlat=FALSE, d=9, dissolve=FALSE)

Re: [R-sig-Geo] generating object with multiple polygons

2016-04-04 Thread Robert J. Hijmans
Karla, You can do library(raster) slices <- bind(slice1, slice2) Robert On Sun, Apr 3, 2016 at 6:53 AM, Karla Shikev wrote: > Dear all, > > This might be a newbie question, so I'd appreciate your patience with this. > > I'm obtaining climatic data from worldclim and

Re: [R-sig-Geo] Count frequency in raster stacks

2016-02-17 Thread Robert J. Hijmans
Here is a more direct (and safer) way: library(raster) set.seed(0) r <- raster(nrows=22, ncols=20, xmn=-58, xmx=-48, ymn=-33, ymx=-22) s <- stack(lapply(1:5, function(x) {setValues(r, round(runif(22 * 20, min=0, max=600), digits=0))})) intervals <- seq(0, 600, 100) x <- cut(s, intervals,

Re: [R-sig-Geo] Needing to speed up a process involving calc() and cover() raster functions

2015-12-24 Thread Robert J. Hijmans
How about this: b <- brick(system.file("external/rlogo.grd", package="raster")) classified <- b / 255 threshs <- c(.1, .3, .5) x <- classified < threshs y <- max(x, na.rm=TRUE) z <- reclassify(y, cbind(0, NA)) Robert On Tue, Dec 22, 2015 at 1:57 AM, Mathieu Rajerison

Re: [R-sig-Geo] raster: stackApply problems..

2015-12-12 Thread Robert J. Hijmans
Mark, Thanks for that clear report. This unexpected order of the layers returned by stackApply is due to a bug which only reveals itself if "unique(indices)" is not sorted from 1 to n. This has been fixed in version 2.5-1 (you can now also use a factor variable, but that should not matter). This

Re: [R-sig-Geo] Convert rasters to data frame with time stamp

2015-10-18 Thread Robert J. Hijmans
(x, fobj, ...) : > 'ncol(x)' and 'nrow(fobj$par$modq)' should be eaqual > > > Any way to circumvent this? > > Greetings, > -- Thiago V. dos Santos > > PhD student > Land and Atmospheric Science > University of Minnesota > > > > On Saturday, Octobe

Re: [R-sig-Geo] Convert rasters to data frame with time stamp

2015-10-17 Thread Robert J. Hijmans
Thiago, > Therefore, I need to load data as rasters and iterate through all individual > gridcells to create a data frame containing: > date1, cell1, cell2, cell3, ..., cell3600 > date2, cell1, cell2, cell3, ..., cell3600 > date3, cell1, cell2, cell3, ..., cell3600... > date408, cell1, cell2,

Re: [R-sig-Geo] Calculate HIS/HSV values from RGB raster image

2015-10-16 Thread Robert J. Hijmans
Kevin, I think you can do that like this: library(raster) b <- brick(system.file("external/rlogo.grd", package="raster")) hsv <- overlay(b, fun=rgb2hsv) Best, Robert On Fri, Oct 16, 2015 at 3:52 PM, Kevin Wolz wrote: > Does anyone know if there is any pre-existing

Re: [R-sig-Geo] ncdf 4-dim file to raster brick - level error??

2015-10-13 Thread Robert J. Hijmans
John Thank you for reporting this. This has now been fixed in raster-devel install.packages("raster", repos="http://R-Forge.R-project.org;) and soon on CRAN. The error (always using the first level on extraction of cell values) occurred in ncdf files with "levels" as fourth dimension (rather than

Re: [R-sig-Geo] How to filter a xyz vector file

2015-10-13 Thread Robert J. Hijmans
Ashraf, Here is an approach: # some random points x <- runif(1) y <- runif(1) xy <- cbind(x, y) # a raster to specficy the extent and resolution library(raster) r <- raster(round(extent(xy)), res=0.1) # find the distance of each point to the center of the cell it falls in cell <-

Re: [R-sig-Geo] problems for reading ASCII file

2015-10-12 Thread Robert J. Hijmans
Ana Carolina, You say you are using ascii because: > i am trying to process a raster image (the one i transformed to ASCII) > but it is too big to work with it as a raster in R (i am having memory > problems)...thats why i am trying to see if its simplier to process it > in ASCII and then bring

Re: [R-sig-Geo] raser::boxplot : cannot add grouping layer

2015-10-08 Thread Robert J. Hijmans
Hi Agus, Your example works for me without error. Can you update.packages() and try this in a clean workspace? (perhaps another package is interfering?) Robert On Wed, Oct 7, 2015 at 2:44 AM, Agustin Lobo wrote: > According to the help page of raster::boxplot > x: Raster*

Re: [R-sig-Geo] Conditional operations and rasters

2015-09-19 Thread Robert J. Hijmans
You can also try this: f <- function(lai) { emiss_0 <- 0.95 + (0.01 * lai) emiss_nb <- 0.97 + (0.0033 * lai) i <- lai >= 1000 emiss_0[i] <- 0.95 emiss_nb[i] <- 0.98 cbind(emiss_0, emiss_nb) } library(raster) rlai <- raster(ncols=360, nrows=180) rlai[] <- 1:ncell(rlai) x

Re: [R-sig-Geo] [Help] Error in spChFIDs(SP, x) : lengths differ

2015-09-05 Thread Robert J. Hijmans
There is no need to 'make a copy of the attribute table' of the sp object. Instead, you should be able to do: library(sp) # From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html states <- readOGR(dsn = "./cb_2014_us_state_5m.shp", layer = "cb_2014_us_state_5m") states <-

Re: [R-sig-Geo] Union/Overlay single spatialPolygonsDataFrame containing overlapping polygonsto create polygons with all unique combinations of attributes

2015-08-12 Thread Robert J. Hijmans
(3,3,4,4,3)),hole = F)), 2_1), Polygons(list(Polygon(cbind(c(4,4,5,5,3,3,4),c(4,3,3,5,5,4,4)),hole = F)), 2_))) for (i in 1:4){ plot(p4[i,],col=cols[i],axes=T,xlim=c(0,5),ylim=c(0,5)) } print(Areas p4:) print(gArea(p4, byid=TRUE)) On Wed, Aug 12, 2015 at 11:02 AM, Robert J. Hijmans r.hijm

Re: [R-sig-Geo] is raster terrain accurate for WGS84 tiles?

2015-08-12 Thread Robert J. Hijmans
Thomas, I do not think it is wishful thinking. Au contraire, I think it is how it should be done. That is, if you start with a longitude/latitude raster with elevation values, and want slopes (or similar) also in that coordinate reference system, then this should be the way to go, as you skip two

Re: [R-sig-Geo] densify geometries

2015-08-11 Thread Robert J. Hijmans
geosphere functions makeLine and makePoly do this, but only for lon/lat, not for planar coordinates. Robert On Fri, Aug 7, 2015 at 6:30 AM, Michael Sumner mdsum...@gmail.com wrote: This is a nice idea with gBuffer, but testing on a simple case shows you need explicit curvature to get any extra

Re: [R-sig-Geo] Union/Overlay single spatialPolygonsDataFrame containing overlapping polygonsto create polygons with all unique combinations of attributes

2015-08-11 Thread Robert J. Hijmans
I would think raster::union(polygons) should get you there. Robert On Tue, Aug 11, 2015 at 7:58 AM, Michael Sumner mdsum...@gmail.com wrote: On Mon, 10 Aug 2015 at 17:52 nevil amos nevil.a...@gmail.com wrote: I am attempting to combine overlapping polygons in a single spatialPolygonsDataFrame

Re: [R-sig-Geo] from x,y plot to a raster with Raster package?

2015-07-19 Thread Robert J. Hijmans
Josep, I take it you have three variables, and want to create a raster for the third variable, based on the other two, that are also represented as rasters. If so, you can # make a three column data.frame d - data.frame( x=c(23,25,27), y=c(33,34,35) , z=c(0.1,0.2,0.3)) # your two rasters,

Re: [R-sig-Geo] rasterize in parrallel

2015-07-13 Thread Robert J. Hijmans
Jerome, rasterize is indeed a bit slow, it is #1 on the list of functions that need a rewrite for speed. However, I think your assumption is wrong. You can rasterize all fields in one step, and it is pretty quick for the example data: library(raster) polyg - shapefile(polyg.shp) grain - 50 nsc -

Re: [R-sig-Geo] Clip a SpatialLinesDataFrame

2015-07-10 Thread Robert J. Hijmans
Jim You can try library(raster) x - crop(roads, polygons) Robert On Fri, Jul 10, 2015 at 11:17 AM, Jim Burke javajimbu...@gmail.com wrote: Clipping a SpatialLinesDataFrame by an envelope SpatialPolygons results in a bare bones SpatialLines with no @data slot. The out file is a roads file

Re: [R-sig-Geo] Raster - rgdal Error: Failure during raster IO

2015-07-07 Thread Robert J. Hijmans
Hi Ben, I would not think this has to do with memory limitations. The error message suggest that you have a bad file, and, hence, that the problem is with the software that created the file, or with an interrupted download. Robert On Sat, Jul 4, 2015 at 6:24 PM, Ben Weinstein

Re: [R-sig-Geo] imprecise location extraction

2015-06-25 Thread Robert J. Hijmans
Dominik, The last part of your example does not run. If I understand you well, this is what you are after (but it all looks good to me): library(raster) # raster data with long and lat values r - raster(xmn=-112.25, xmx=-104.125, ymn=33, ymx=43.75, res=1/240) lat - init(r, v='y') lon - init(r,

Re: [R-sig-Geo] Problem with raster package and nc4 file

2015-06-05 Thread Robert J. Hijmans
Andrea, One small addition. To get all layers (one for each day, in stead of the single layer you get with 'raster'), use the brick function: b - brick(tmin_1980.nc4) Robert On Fri, Jun 5, 2015 at 4:52 PM, Michael Sumner mdsum...@gmail.com wrote: Just another short comment below On Sat, 6

Re: [R-sig-Geo] gIntersection versus intersect

2015-05-14 Thread Robert J. Hijmans
Jean-Luc. It is as you say, intersect returns Spatial*DataFrame objects whereas gIntersect does not the DataFrame bits. This pattern is also true for raster functions aggregate, union, erase, cover and crop (see section XIV in ?'raster-package') which all have their analogues in rgeos that they

Re: [R-sig-Geo] Multiply 2 raster without considering cells with NA

2015-05-13 Thread Robert J. Hijmans
These would seem to be plausible, and correct, values. I do not think there is anything wrong NA values. The extreme values are because you divide by a fraction. How would you expect the results to be non-negative if you divide by negative values? Antonio's answer is incorrect, as division with

Re: [R-sig-Geo] Problem reading a HDF5 file with readGDAL in Windows

2015-05-06 Thread Robert J. Hijmans
It works for me. I get: library(raster) Loading required package: sp library(ncdf4) raster(E:/downloads/201407291200, ncdf=T, varname='DSSF') class : RasterLayer dimensions : 651, 1701, 1107351 (nrow, ncol, ncell) resolution : 1, 1 (x, y) extent : 0.5, 1701.5, 0.5, 651.5

Re: [R-sig-Geo] Faster way to get raster average?

2015-05-05 Thread Robert J. Hijmans
This suggest that most of the time is spend on reading data from disk. It could be more efficient to do this in one step. m - .colMeans(getValues(cmip), nrow(cmip), ncol(cmip), na.rm=FALSE) I do not think there is much more you can do beyond that --- although a solid state hard disk could help.

Re: [R-sig-Geo] Problem reading a HDF5 file with readGDAL in Windows

2015-05-05 Thread Robert J. Hijmans
local zip file in Rgui). Just a tiny bit of additional effort. Robert On Tue, May 5, 2015 at 2:20 PM, Michael Sumner mdsum...@gmail.com wrote: On Wed, 6 May 2015 at 03:28 Robert J. Hijmans r.hijm...@gmail.com wrote: Even better (renaming not necessary): x - raster(E:/downloads/OMI.L2

Re: [R-sig-Geo] Problem reading a HDF5 file with readGDAL in Windows

2015-05-05 Thread Robert J. Hijmans
Even better (renaming not necessary): x - raster(E:/downloads/OMI.L2.CloudOMCLDO2Strip200kmAlongCloudSat.2015.05.05.020752Z.v003.he5, var='Data Fields/ChiSquaredOfFit', ncdf=TRUE) Best, Robert On Tue, May 5, 2015 at 10:24 AM, Robert J. Hijmans r.hijm...@gmail.com wrote: In which case you can

Re: [R-sig-Geo] Predict gam in a loop on multiple raster stacks and keeping identical layer names

2015-05-05 Thread Robert J. Hijmans
Kristin, I think you can do something like this: lst - list() for(i in 1:96) { Predictors - stack(stackchl[[i]], stacksst[[i]], stackpar[[i]], lat, lon) names(Predictos) - c('chl', 'sst', 'par', 'lat', 'lon') lst[[i]] - predict(Predictors, gammodel, na.rm=TRUE, type=response) } s -

Re: [R-sig-Geo] Problem reading a HDF5 file with readGDAL in Windows

2015-05-05 Thread Robert J. Hijmans
In which case you can change the extension to '.nc' and do library(raster) library(ncdf4) x - raster(E:/downloads/OMI.L2.CloudOMCLDO2Strip200kmAlongCloudSat.2015.05.05.020752Z.v003.nc, var='Data Fields/ChiSquaredOfFit') Robert On Tue, May 5, 2015 at 5:28 AM, John Baumgartner

Re: [R-sig-Geo] adding a scale

2015-05-05 Thread Robert J. Hijmans
There is also a 'scalebar' function in the raster package (I have not compared the two) Robert On Mon, Apr 27, 2015 at 7:59 AM, Gilles Benjamin Leduc g...@hi.is wrote: Not to bad hint :p So From this one, I modified the function to get it work correctly:

Re: [R-sig-Geo] Delimit a polygon for the region which is, 1000 m from my raster altitude

2015-05-02 Thread Robert J. Hijmans
If you want to keep the raster cell values, you could do shapefile(pol2, polygon2.shp) and use that file. On Sat, May 2, 2015 at 2:10 PM, sadaoui sadaouimah...@outlook.com wrote: Thank you Robert Hijmans, it works with this function ; shapefile(pol3, polygon3.shp) but I want to have

Re: [R-sig-Geo] Problem clipping SpatialPolygonDataFrame

2015-04-23 Thread Robert J. Hijmans
Luciano, The extent you are using to crop is the same as the extent of the entire extent, so it does not surprise me that there is no change. Here is an example that works: library(raster) library(rgeos) g - getData('GADM', country='ARG', level=1) e - extent(-63.6, -55.1, -32.8, -27.6) x -

Re: [R-sig-Geo] Raster Prediction with factors in model

2015-04-23 Thread Robert J. Hijmans
Mark, I had not considered the case where the constant is a factor. I need to fix that. Here is a script that accomplishes what you want, I think # known presence and absence points p - matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 66, 42, 26, 4, 19, 17, 7, 14,

Re: [R-sig-Geo] Calculating the length of a line created from a spatial intersect.

2015-04-01 Thread Robert J. Hijmans
Walter, Next time, please try to give as a simple self contained example in R showing what you have done, and where you are stuck. Here is how you can intersect lines and polygons, keeping their attributes: library(raster) library(rgeos) # example data p -

Re: [R-sig-Geo] How to count unique TRUE's

2015-03-31 Thread Robert J. Hijmans
Forrest, it seems to me that you can simplify and do output$AC[i] - length(gIntersection(tmp, ac)) instead of tmp2 - as.vector(gIntersects(tmp, ac, byid=TRUE)) output$AC[i] - length(tmp[tmp2 == TRUE]) Robert On Tue, Mar 31, 2015 at 11:56 AM, Forrest Stevens forr...@ufl.edu wrote: I'm

Re: [R-sig-Geo] Calculating area of polygons created from a spatial intersect

2015-03-27 Thread Robert J. Hijmans
Walter, I think that instead of gIntersects(buffered_projects, census_blocks, byID=TRUE) you want library(raster) x - intersect(buffered_projects, census_blocks, byID=TRUE) and then Step 3 x$newarea - gArea(x, byid=TRUE) x$relarea - x$newarea / x$area Robert On Fri, Mar 27, 2015

Re: [R-sig-Geo] Help with latlong to UTM conversion when UTM zones are different

2015-03-27 Thread Robert J. Hijmans
Are these really reasonable reasons? I do not think so, given that the question had nothing to do with map navigation and the person asking appears to live in the UK. Moreover, other projections have, or can have, their units in meters as well (or feet or miles or whatever you might fancy). UTM

Re: [R-sig-Geo] inconsistent as.data.frame(SpatialPointsDF)

2015-03-20 Thread Robert J. Hijmans
The raster package 'enhances' as.data.frame for SpatialLines* and SpatialPolygons*. I am not sure why there is also a function for SpatialPoints* as it does not add anything, except for setting the names to 'x' and 'y'. Perhaps that is what I did it for, I cannot remember. But clearly it is not

Re: [R-sig-Geo] parallel raster processing with calc and mc2d monte carlo simulation

2015-03-17 Thread Robert J. Hijmans
Sean, fun.CROP_AGWBC refers to objects that are not defined inside the function (lm.final and lambda_DV). I assume that this is intentional and that these represent constants; and that they are available in your global environment. If so, you need to export these objects to the cluster nodes.

Re: [R-sig-Geo] parallel raster processing with calc and mc2d monte carlo simulation

2015-03-17 Thread Robert J. Hijmans
= testbrick, fun = fun2.CROP_AGWBC) [1] data should be numeric or logical attr(,class) [1] snow-try-error try-error Error in clusterR(x = testbrick, fun = fun2.CROP_AGWBC) : cluster error Any other thoughts? Many thanks, sean On Mar 17, 2015, at 8:14 AM, Robert J. Hijmans r.hijm...@gmail.com

Re: [R-sig-Geo] gdistance: costDistance with barriers

2015-03-12 Thread Robert J. Hijmans
Karl, You are using a default RasterLayer which has lon/lat coordinates. In that case the earth is considered spherical (or similar), not a plane. The maximum possible distance in longitude is 180 degrees, and the distance between -120 and 120 is not 240 degrees, but 60+60= 120 degrees, . To get

Re: [R-sig-Geo] dismo package : gbif function error

2015-03-09 Thread Robert J. Hijmans
The web-service that the function depends on has changed. It should work with the development version of dismo (version 1.0-8). You should be able to install it from R-Forge like this: install.packages(dismo, repos=http://R-Forge.R-project.org;) Robert On Sun, Mar 8, 2015 at 11:42 AM, Ahmed

Re: [R-sig-Geo] keep just one polygon for each element

2015-03-03 Thread Robert J. Hijmans
Leo, here is an approach that might work for you: library(raster) library(rgdal) library(rgeos) library(plyr) # create some example data. p - shapefile(system.file(external/lux.shp, package=raster)) p - p[, 'NAME_1'] p - aggregate(p, 'NAME_1', dissolve=FALSE) # now we have 3 regions, each

Re: [R-sig-Geo] Problem with using multicore resample in Raster package

2015-02-27 Thread Robert J. Hijmans
Bastien, This arises from a change in CRAN policy (the requirement to use requireNamespace instead of require) which somehow creates a problem for snow::recvOneData. This is all a bit mysterious to me. The development version uses require again for the snow package such that things work as they

Re: [R-sig-Geo] pixel-wsie correlation between raster stack and numeric vector?

2015-02-24 Thread Robert J. Hijmans
MIke, I think you can use the 'calc' function for that: library(raster) # example data set.seed(0) s - stack(system.file(external/rlogo.grd, package=raster)) s - stack(s, s[[1]]*runif(ncell(s)), s[[2]]*runif(ncell(s))/10, s[[3]]*runif(ncell(s))+10) # example vector v - 1:6 x - calc(s,

Re: [R-sig-Geo] transformin multipart shapefile to singlepart shapefile

2015-02-02 Thread Robert J. Hijmans
Jean-Luc I think you can use the disaggregate method (in package sp) for that. Robert On Sat, Jan 31, 2015 at 3:17 AM, Jean-Luc DUPOUEY dupo...@nancy.inra.fr wrote: I faced the same question recently: how to tranform a multipart polygon layer into single polygons? Here is a little bit more

Re: [R-sig-Geo] How to show the color bar legend in R?

2015-01-26 Thread Robert J. Hijmans
It seems there is a problem with my R ! It is for a good reason that you are requested to include your sessionInfo() information in messages to this list. But even more important is to first make sure that you are using the current version of R and the current versions of all relevant

Re: [R-sig-Geo] adding mask to spline raster

2015-01-15 Thread Robert J. Hijmans
Hi Pascal, On Wed, Jan 14, 2015 at 11:52 AM, Pascal Title pti...@umich.edu wrote: Hi, I am creating interpolations of global marine diversity via thin plate splines, using the interpolate() function in the raster package. The problem I am having is that if I plot the spline interpolation and

Re: [R-sig-Geo] Error when predicting a randomForest model to a raster

2015-01-13 Thread Robert J. Hijmans
P.S. you also need to do: rf.predict - predict(rgb.rast, rf.mod) Not rf.predict - predict(rf.mod, rgb.rast) Robert On Tue, Jan 13, 2015 at 6:09 PM, Robert J. Hijmans r.hijm...@gmail.com wrote: Dave, The example works if you do rf.mod - randomForest(color.frame[, c('red','green','blue

Re: [R-sig-Geo] merging polygons and IDs

2015-01-11 Thread Robert J. Hijmans
David, I think you could also do this: library(raster) a - disaggregate(aggregate(pols)) ids - over(pts, a) Now you have the aggregated polygons and for each point you know which polygon it belongs to. Perhaps continue with something like this: tab - table(ids, as.integer(names(ids))) tab -

Re: [R-sig-Geo] rgeos::gDifference

2015-01-05 Thread Robert J. Hijmans
You can also do: library(raster) x - sp1 - sp2 y - sp2 - sp1 (equivalent to the erase function) On Mon, Jan 5, 2015 at 5:43 AM, Roger Bivand roger.biv...@nhh.no wrote: On Mon, 5 Jan 2015, Edzer Pebesma wrote: On 01/05/2015 01:53 PM, Roger Bivand wrote: On Mon, 5 Jan 2015, Edzer Pebesma

Re: [R-sig-Geo] Raster projection alignment issues

2014-12-19 Thread Robert J. Hijmans
Wade, I do not think you are doing anything wrong. projectRaster gets confused because of the uncommon longitude coordinates used by climate models (0 to 360, rather than -180 to 180) You can use the 'rotate' function to fix that (ignore the warning), or by setting the extent, and then proceed.

Re: [R-sig-Geo] Extract spatial mean of subset of netCDF.

2014-12-19 Thread Robert J. Hijmans
Aseem, The problem is that you are trying to subset a raster using latitude/longitude coordinates, while the raster has another CRS. You can fix this, I think, by assigning the correct crs (you can find these at http://www.spatialreference.org/) and then, for example, adjusting your extent, or

Re: [R-sig-Geo] Extract with Large Rasters

2014-12-17 Thread Robert J. Hijmans
From the traceback 8: .readCellsGDAL(x, uniquecells, layers) it seems to me that the error occurs here: extract(elev, points) and that it has nothing to do with the size of the raster. I am not sure what causes it. Could you perhaps email me the points? Thanks, Robert On Wed, Dec 17, 2014

Re: [R-sig-Geo] rainfall interpolation using ANN

2014-12-02 Thread Robert J. Hijmans
Peter, This is a simplified version of your script untested, of course, given you send you no data): library(gstat) library(raster) dat71 - shapefile(d:/FireDanger/IDW/71stations.shp) sample.grid - raster(rawsd_71Stations.tif) #note that there are parameters you need to choose m -

Re: [R-sig-Geo] Calculating what proportion of polygons is covered by rivers

2014-12-01 Thread Robert J. Hijmans
Juta, I think you can do something along these lines: library(raster) library(rgeos) x - union(districts, rivers) a - gArea(x, byid=TRUE) head(x) head(a) or x - intersect(districts, rivers) and compare to 'districts' Robert On Sun, Nov 30, 2014 at 2:59 PM, Juta Kawalerowicz

Re: [R-sig-Geo] Question on histograms from Raster and RasterVIS packages

2014-11-19 Thread Robert J. Hijmans
Nuno, The hist function from the raster package uses a maximum of 100 000 values for generating the histogram That is the default, but you do not have to use that. Did you read the help file?? ?raster::hist Shows that you can set the maxpixels to any number you want. E.g. maxpixels=Inf if

Re: [R-sig-Geo] Question on histograms from Raster and RasterVIS packages

2014-11-19 Thread Robert J. Hijmans
large rasters (not my case here since it gracefully succeeded), do you know what is the limit of cells for that failure? Once again, thank you all for the help! Best of luck on everything for all! Nuno On 19 November 2014 15:14, Robert J. Hijmans r.hijm...@gmail.com wrote: Nuno, The hist

Re: [R-sig-Geo] Merge shapefiles

2014-11-14 Thread Robert J. Hijmans
Steven, Please do provide a self-contained example when you ask a question (see my example below) and try to use correct terms. You are dealing with SpatialPolygonDataFrame objects (shpA, shpB), these objects are perhaps derived from shapefiles, but it is not what they are. Also, providing a

Re: [R-sig-Geo] Generate a vector of names of intersecting polgons

2014-10-27 Thread Robert J. Hijmans
Steven, I believe you can use something like the below library(raster) # example data (p, d) p - shapefile(system.file(external/lux.shp, package=raster)) r - raster(p) values(r) - 1:ncell(r) d - as(r, 'SpatialPolygonsDataFrame') # approach 1 x - intersect(p, d) # approach 2 y - union(p,d) y -

Re: [R-sig-Geo] How to segment or split a spatial line in R

2014-10-23 Thread Robert J. Hijmans
I think the below is a solution: library(raster) # create some lines cds1 - rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60)) cds2 - rbind(c(-10,0), c(140,60), c(160,0), c(140,-55)) cds3 - rbind(c(-125,0), c(0,60), c(40,5), c(15,-45)) lns - SpatialLines(list(Lines(list(Line(cds1)), 1),

Re: [R-sig-Geo] Shortest distance between points within a polygon

2014-10-13 Thread Robert J. Hijmans
Ruari, You might be able to use gCrosses together with optim or another optimization function to find the best route. Below is a very simple example. For more complex polygons you may need to allow for several intermediate points (perhaps following the polygon boundary at times). Unless you want

Re: [R-sig-Geo] convert multiple dimension array to raster object/.png/.tif

2014-10-13 Thread Robert J. Hijmans
Chane, You can use b - brick(array) # for example library(raster) r - raster(ncol=3, nrow=3) r[] - 1:ncell(r) s - stack(r,r*2,r*3) a - as.array(s) brick(a) Robert On Mon, Oct 13, 2014 at 3:00 PM, Chane zoeeeregis...@gmail.com wrote: Dear list, I have a multiple dimension array (9 x 9 x 7)

Re: [R-sig-Geo] which.max() limit in raster package for RasterBrick

2014-09-23 Thread Robert J. Hijmans
Tyler, It is probably useful to study the vignette that comes with the raster package if you have not done so. Also please provide a reproducible example, if possible, and report the error message you get. Presumably it is: Error in which.min(harvest_area) : not yet implemented for large

Re: [R-sig-Geo] Mosaic Rasters

2014-09-22 Thread Robert J. Hijmans
Brian, What you can do in a situation like this (clearly a bug; difficult to reproduce) is with your list x: y - sapply(x, raster) # remove all valus save(y, file='mosaic.RData') and send the file (and explanation of how you get the error) to the package maintainer (i.e., me, in this case).

Re: [R-sig-Geo] Automate the extraction of climate variable at different time depths from netcdf in r

2014-09-06 Thread Robert J. Hijmans
Barnabas, , You can try something like this: b - brick(~sst.mnmean.nc, varname=sst) # loop over species, or combine all species into one data.frame? mydata - read.csv(~Species one.csv) extract.mydata - extract(b, mydata[,5:6]) write.csv(extract.mydata, file = Species_one_extracted.csv) Robert

Re: [R-sig-Geo] Raster focal Error with NAonly + na.rm=T, how to select NAonly focal cells?

2014-09-05 Thread Robert J. Hijmans
Joseph, The errors go away if you add ellipses to your functions, to let the na.rm pass through (even if you ignore it). r3 - focal (r,w=matrix.weights,fun = function(x, ...){sample(na.omit(x),size=1)},pad=T,padValue=NA,NAonly=T) The perhaps surprising error with r3 occurs because when NAonly

Re: [R-sig-Geo] raster - 4D Bricks

2014-09-03 Thread Robert J. Hijmans
Mark and Mike, Thanks for reporting and clarifying. Fixed, I think, in raster version 2.2-45. Robert On Wed, Jul 30, 2014 at 1:13 AM, Michael Sumner mdsum...@gmail.com wrote: Hello, Thanks for clarifying, I get that problem too. If I get a chance I'll have a closer look, but otherwise here's

Re: [R-sig-Geo] raster plotting question: figured it out

2014-09-03 Thread Robert J. Hijmans
Erin, This is not a good approach: min1 - min(a@data@values,b@data@values) max1 - max(a@data@values,b@data@values) Because many RasterLayer objects won't have any values there. It is another example of why you should not poke inside of S4 objects if there are functions to do that for you. This

Re: [R-sig-Geo] raster::extract fails on brick but works on individual layers of brick

2014-09-02 Thread Robert J. Hijmans
Frank, I hope this issue has been solved in the development version. install.packages(raster, repos=http://R-Forge.R-project.org;) I would appreciate feedback. Best, Robert On Fri, Jul 25, 2014 at 12:40 PM, Frank Davenport frank.davenp...@gmail.com wrote: Sorry for the mutliple postings but I

Re: [R-sig-Geo] raster::ratify and as.factor Error in 1:ncol(r) : argument of length with raster read from large tif

2014-08-31 Thread Robert J. Hijmans
Nevil, Thanks for reporting and including an example file. The error occurs when showing a RasterLayer with a RAT table that only has IDs (but no attributes for these IDs). Did it otherwise affect anything? Either way, this has been fixed for the next version. Robert On Mon, Jul 7, 2014 at 6:52

Re: [R-sig-Geo] Issue saving ascii with writeRaster function

2014-08-30 Thread Robert J. Hijmans
Damien, The default setting is to write Real numbers, hence the 1.000 (which is needed to trick GDAL and ESRI to not assume that the values are all integers when the first numbers have no decimals). If you want integers, you can do writeRaster(r, filename = test.asc,

Re: [R-sig-Geo] raster to dataframe with xy=TRUE, na.rm=TRUE

2014-08-30 Thread Robert J. Hijmans
Helen, This is a bug, thanks for reporting it. I have fixed it in (development) version 2.2-43 Robert On Fri, Aug 29, 2014 at 8:22 AM, Helen Sofaer he...@rams.colostate.edu wrote: Thanks Pascal, That's helpful. I am curious about what happened in the second example, if anyone else takes a

Re: [R-sig-Geo] distance between raster cell centroids and spatial points

2014-08-30 Thread Robert J. Hijmans
Gabriele, I think you can do: library(raster) x - distanceFromPoints(ras, villages) Robert On Mon, Jul 28, 2014 at 6:56 AM, Gabriele Cozzi gab.co...@gmail.com wrote: Dear list, I want to calculate the distance between the centroid of each cell in a raster ‘ras’ and the closest village

Re: [R-sig-Geo] Unexpected behavior of raster mask function

2014-08-30 Thread Robert J. Hijmans
Alex, Thanks for the clear example. I had not considered that case. I have added an argument 'updateNA' to the mask function (version 2.2-43) such that you can do: masked_image - mask(img, msk, updatevalue=2, updateNA=TRUE) That is, if updateNA is TRUE, NA cells outside the mask are also

Re: [R-sig-Geo] Verify units of distance between coordinates

2014-08-30 Thread Robert J. Hijmans
library(geosphere) d - distCosine(df[, 1:2], df[,3:4]) On Thu, Aug 28, 2014 at 8:02 AM, Roger Bivand roger.biv...@nhh.no wrote: On Thu, 28 Aug 2014, Roger Bivand wrote: On Thu, 28 Aug 2014, Sarah Goslee wrote: On Thu, Aug 28, 2014 at 9:32 AM, Michael Sumner mdsum...@gmail.com wrote: On

Re: [R-sig-Geo] Raster Layers same resolution but not the same coordinates over the same area

2014-08-30 Thread Robert J. Hijmans
Justin, The RasterStack approach is fine, but as you show, the layers do not match. The resolution of NDVIStack is larger than the resolution of rainStack; it seems that you did not resample the NDVI data correctly. Robert On Sat, Aug 30, 2014 at 4:31 AM, Justin Michell jwm...@gmail.com

Re: [R-sig-Geo] raster brick error cells are not equally spaced

2014-08-30 Thread Robert J. Hijmans
Dave, The code now works on both machines. Perhaps it works, but the results are probably incorrect. There always a check for non-regularly spaced values, but some cases slipped through. This has now been fixed. Robert On Tue, Aug 19, 2014 at 12:42 PM, Chagaris, Dave dave.chaga...@myfwc.com

Re: [R-sig-Geo] image(rs) returns error after reclassify

2014-08-30 Thread Robert J. Hijmans
Herry, This is caused by a call to as.integer(cell numbers) which leads to NAs as as.integer(ncell(rs)) returns NA I have removed the call to as.integer in version 2.2-43 and this works now (and it does not seem to mess up other things). Thanks for reporting this, Robert On Thu, Jul 17, 2014 at

Re: [R-sig-Geo] Randomly moving a locality (within set limits)

2014-08-30 Thread Robert J. Hijmans
I think this is implemented as the destPoint function in the geosphere package. Robert On Mon, Aug 25, 2014 at 1:26 AM, Frede Aakmann Tøgersen fr...@vestas.com wrote: Hi The following is based on http://www.movable-type.co.uk/scripts/latlong.html. foo - function(origin, bearing, distance){

Re: [R-sig-Geo] apply raster::aggregate to multi-layer raster stack, using function that processes values from multiple layers to create a single layer?

2014-06-21 Thread Robert J. Hijmans
Carlos, How about: x - mean(y) or x - calc(y, mean) Robert On Fri, Jun 20, 2014 at 2:50 PM, Carlos Carroll klamathconservat...@gmail.com wrote: When I use aggregate (package raster) on a multi-layer stack, with a function such as mean, it returns a second multi-layer stack at the

Re: [R-sig-Geo] new patch versus patch expansion

2014-06-11 Thread Robert J. Hijmans
Jonathan, I am not sure if this improves things, but it is an alternative route: a - boundaries(t1, type='outer', classes=TRUE, directions=4) f1 - freq(c.change, useNA='no') f2 - freq(mask(c.change, a, maskvalue=0), useNA='no') f - merge(f1, f2, by='value') newpatches - f[f[,2] == f[,3]] Robert

Re: [R-sig-Geo] fill layers of a RasterStack with values in rows of a matrix

2014-06-05 Thread Robert J. Hijmans
Vero, Here is a simpler solution: library(raster) m - matrix(data = runif(n = 9216*506, min = 0, max = 1000), nrow = 9216, ncol = 506) b - brick(nrows=96, ncols=96, xmn=-62, xmx=-58, ymn=-49, ymx=-45, crs=+proj=longlat +datum=WGS84) b - setValues(b, m) Robert On Tue, May 27, 2014 at 12:54 PM,

Re: [R-sig-Geo] decimals in NA value using raster::writeRaster with .ascii format

2014-04-10 Thread Robert J. Hijmans
Zack, You are using: dataType=INT2U but it should be: datatype=INT2U # (or any other INT data type) That fixes it. If the datatype is FLT* (the default), the first value gets decimals to assure that GDAL and others treat the values as floats, not integers. Robert On Thu, Apr 10, 2014

Re: [R-sig-Geo] Singlepart to multipart

2014-04-01 Thread Robert J. Hijmans
Jonathan, You can go back and forth with (raster/sp) functions aggregate and disaggregate Robert On Tue, Apr 1, 2014 at 7:50 AM, Jonathan Greenberg j...@illinois.edu wrote: This is a counter-question to the previous one: how would I go about taking a multipart SpatialPolygon* and flattening it

Re: [R-sig-Geo] re-projecting raster brick, output has no values

2014-03-22 Thread Robert J. Hijmans
Matt, The x (longitude) coordinates are not valid for lat/lon. It looks like they are using 0 to 360 instead of -180 to 180 which is what climate modelers do. For global datasets you can use 'rotate' to fix this, but that function does not (yet) work for smaller extents. In this case you can

Re: [R-sig-Geo] writeRaster error [Error in dim.create.ncdf(nc, d, verbose) : NA/NaN/Inf in foreign function call (arg 5)]

2014-03-03 Thread Robert J. Hijmans
Aseem provided me with some more info. writeRaster fails when the 'z-value' cannot be converted to an integer. In his case: head(getZ(ncdata.qtr)) [1] 1950 Q1 1950 Q2 1950 Q3 1950 Q4 1951 Q1 1951 Q2 The work around is to change these values using the setZ function. This has been fixed (with a

Re: [R-sig-Geo] Problem with raster

2014-02-21 Thread Robert J. Hijmans
Mark, You would not be tell from the error message, but the error occurs because you are using an invalid CRS. It should be: proj4string=CRS(+proj=longlat) instead of this: proj4string=CRS(longlat) Robert On Fri, Feb 21, 2014 at 6:59 AM, Mark Payne markpayneatw...@gmail.com wrote: Hi, I

  1   2   3   4   5   6   >