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

2017-10-22 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 updat

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 mfri.te

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 confirm that Rolf's `do

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 <- extract(b[[which

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 i

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', lev

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 wrote: > Hi, > > I have a

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: > Thanks again Loïc and

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) c2

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 generating polygons based on

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, includ

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 wrote: > Hi, > > > I use

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
.) : > '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, October 17,

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, cel

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 functionality to easily > c

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 <- cellFro

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 i

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* object > y: if x is a Ra

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 <- merge(sta

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 t

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
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 > wrote: >> >> I wo

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 wrote: > This is a nice idea with gBuffer, but testing on a simple case shows you > need explicit curvature to get any extra verts. > > librar

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 wrote: > On Mon, 10 Aug 2015 at 17:52 nevil amos wrote: > >> I am attempting to combine overlapping polygons in a >> single spatialPolygonsDataFrame so that the output is a set of p

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, repre

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 ns

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 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 which plots fi

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 wrote: > Hi all, > >

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] Coordinates of the closest point

2015-06-23 Thread Robert J. Hijmans
And for longitude/latitude data you can use geosphere::dist2Line Robert On Tue, Jun 23, 2015 at 8:13 PM, GeoTux Tuxman wrote: > Hi Juta, you can do this (only with planar coordinates): > > require("maptools") > coordsLine = cbind(c(1,2,3),c(3,2,2)) > coordsPoint = c(1.2,1.5) > nearestPointOnLine(

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 wrote: > Just another short comment below > > On Sat, 6 Jun 2015 at 09

Re: [R-sig-Geo] Setting raster x- and y-resolutions equal

2015-06-01 Thread Robert J. Hijmans
The problem is with dynatopmodel::upslope.area that uses xres(x)==yres(x) where all.equal(xres(rast), yres(rast)) would be appropriate. One should not compare decimal numbers for equality using '=='. rast <- raster(nrows=4072, ncols=3111, xmn=656419.81022503704298, xmx=730403.8816690669627

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 bu

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 an

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

2015-05-05 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
stall from local zip file" in Rgui). Just a tiny bit of additional effort. Robert On Tue, May 5, 2015 at 2:20 PM, Michael Sumner wrote: > > > On Wed, 6 May 2015 at 03:28 Robert J. Hijmans wrote: >> >> Even better (renaming not necessary): >&

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 wrote: > > Not to bad hint :p > > So From this one, I modified the function to get it work correctly: > > ###

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 wrote: > In whi

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 wrote: > Not a

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] 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 wrote: > Thank you "Robert Hijmans", > > it works with this function ; > > shapefile(pol3, "polygon3.shp") > > > but I want to have the correspond

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
" it has not recognized as a polygon!!! I don't know why? " That is not true: > class(pol3) [1] "SpatialPolygons" attr(,"package") [1] "sp" It is not a 'SpatialPolygonsDataFrame' You can write it with: shapefile(pol3, "polygon3.shp") or use the SpatialPolygons object to create a SpatialPolygo

Re: [R-sig-Geo] Count Pixel with Specific of Values Appearing Sequentially in Rasterstack

2015-04-25 Thread Robert J. Hijmans
Arnold, In cases like this you can make a function that computes this for a vector, test that function, including for a vector of NA values, and, if it works, use calc. # counting runs of 255 run <- function(x) { x[x != 255] <- NA r <- rle(x) w <- which(!is.na(r$values)) ifelse(length(w)

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 <- crop

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

2015-04-22 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, 26

Re: [R-sig-Geo] over() in sp package

2015-04-13 Thread Robert J. Hijmans
Kenny, Here is an alternative route library(raster) states <- shapefile("cb_2013_us_state_20m.shp") tMaxGrid <- raster(unzippedFiles[1]) x <- extract(tMaxGrid, states) This returns a list of the length of states. Each list element has a vector with the grid cell values. For a mean do xs <- ext

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 <- shapefile(system.file("external/lux.s

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 wrote: > I'm guessing based on t

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 ind

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] 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 des

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

2015-03-17 Thread Robert J. Hijmans
_AGWBC + 1)^(1/0.2626263)-1 > quantile(AGWBC[], c(0.025, 0.5, 0.975), na.rm=TRUE) > } > > > Check function using calc > CROP_AGWBC_2 <- calc(testbrick, fun2.CROP_AGWBC) > plot(CROP_AGWBC_2) > CROP_AGWBC_2 > > > Run function using parallel process

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] 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 the

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, fu

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 wrote: > I faced the same question recently: how to tranform a multipart polygon layer > into single polygons? Here is a little bit more general function to do t

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 package

Re: [R-sig-Geo] Raster package - Focal sum in circles

2015-01-26 Thread Robert J. Hijmans
Your focal weights sum to 1, such that you get the mean value when using 'fun=sum' if you want to sum, you can do fw <- focalWeight(x, 50, type='circle') fw[fw>0] <- 1 p_focal<-focal(x, w=fw,fun=sum , na.rm=TRUE) Robert On Mon, Jan 26, 2015 at 3:17 AM, Vanessa Machault wrote: > Hi, > I have a

Re: [R-sig-Geo] Unexpected behaviour of "area" slot in sp Polygons

2015-01-26 Thread Robert J. Hijmans
This is probably because you are not subtracting areas of holes? To get areas for SpatialPolygons, use: library(rgeos) gArea(vdum) and in this case for the RasterLayer, I would use freq(rdum) Robert On Mon, Jan 26, 2015 at 7:19 AM, Gregory Duveiller wrote: > Dear All, > > I noticed something

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 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 then > over

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 wrote: > Dave, > > The example works if you do > > rf.mod <- randomForest(color.frame[, c('r

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

2015-01-13 Thread Robert J. Hijmans
Dave, The example works if you do rf.mod <- randomForest(color.frame[, c('red','green','blue')],response) instead of rf.mod <- randomForest(color.frame,response) color.frame has 4 variables, one with column name "NA". Given that you use names(color.frame) <- c('red','green','blue') it seems t

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 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 wrot

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 by

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. Se

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 <- gstat(fo

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 wrote: > Hi,

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

2014-11-19 Thread Robert J. Hijmans
> Since I have your attention Mr. Hijmanns I ask a quick extra question - in > the cellStats you mention that it will fail "gracefully" for very large > rasters (not my case here since it gracefully succeeded), do you know what > is the limit of cells for that failure? &

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

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 moti

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

Re: [R-sig-Geo] distance between centroids

2014-10-23 Thread Robert J. Hijmans
Alessandra, This is pretty straightforward but you need to do your part (show us what you have done) using a simple reproducible example. For example, you can use the North Carolina data that comes with spdep. That package also allows you to identify neighbors. library(spdep) library(raster) nc

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

2014-10-23 Thread Robert J. Hijmans
that... Robert On Thu, Oct 23, 2014 at 9:46 AM, Robert J. Hijmans wrote: > 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))

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] 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 wrote: > Dear list, > > I have a multiple dimension array (9 x 9 x 7) and I would lik

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 to

Re: [R-sig-Geo] Changing coordinates in shapefile

2014-10-07 Thread Robert J. Hijmans
Leigh, I am at loss by what you mean with "shapefile" in this context (SpatalPolygons object? or raster data given your reference to cells?), but raster::shift is probably the function you are looking for. Robert On Sun, Oct 5, 2014 at 8:18 AM, Leigh Kroeger wrote: > Hello all, > > I have realise

Re: [R-sig-Geo] Calculating distance to nest with spDistsN1

2014-09-30 Thread Robert J. Hijmans
Anna-Marie, If I (and Frede) understand the problem, then you can use the raster::pointDistance function or similar functions in the geosphere package. dat <- read.table(text = "lon lat Date Time Speed Direction col_lat col_long dist_col trip_no bird_id trip_id DT date_time 7.87248 54.1866 '04.07

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 objects

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] Spatial maximisation problem using polygons

2014-09-19 Thread Robert J. Hijmans
Rui, You could create a matrix (=raster) and a function that creates a valid set of groups of 4 cells that are not adjacent. The function should return the solution (output matrix values: TRUE or FALSE) and the sum of the selected values. Then use the optim function to optimize based on the sum. Wh

Re: [R-sig-Geo] Intended usage of gIntersection ?

2014-09-13 Thread Robert J. Hijmans
The raster package has a few functions that extend rgeos by also attempting to handle attribute data as well. In this case, see raster::intersect And the list of functions here: ?"raster-package" (section XIV) Robert On Sat, Sep 13, 2014 at 10:04 AM, Bernd Vogelgesang wrote: > Dear list, > > I'm

Re: [R-sig-Geo] Distances from points to coast do not make sense.

2014-09-11 Thread Robert J. Hijmans
The below seems to work correctly (albeit slowly). library(maptools) library(raster) library(geosphere) data(wrld_simpl) w <- aggregate(wrld_simpl) af = cbind(16, 13) ng <- cbind(139, -4) p <- rbind(af, ng) dist2Line(p, w) On Thu, Sep 11, 2014 at 4:13 PM, Agus Camacho wrote: > I have two spec

Re: [R-sig-Geo] Create raster layer that counts SpatialPoint occurrence within cells

2014-09-11 Thread Robert J. Hijmans
Hi Andrew, There is no need to loop over rasterize. This should do it: library(raster) n = 500 set.seed(232) x = rnorm(n, -96, 5) y = rnorm(n, 32, 5) xy = cbind(x, y) r = raster(nrow=90, ncol=180) rr = rasterize(xy, r, fun='count') Best, Robert On Thu, Sep 11, 2014 at 4:01 PM, Andrew Vitale w

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

2014-09-11 Thread Robert J. Hijmans
559 3477 > \--| Mobile: +277 3818 9583 > \-/ Website: www.barnabasdaru.com >/\ > /--\ > > #…if you can think it, you can do it. > > > > > > On Sep 7, 2014, at 5:23 AM, Robert J. Hijmans wrote: > > Barnabas, , > You can try something lik

Re: [R-sig-Geo] Collinearity test domain

2014-09-11 Thread Robert J. Hijmans
Maurizio, One typically cares about collinearity because it can lead to a sub optimal model. The model is only affected by the data it sees. Therefore only the values at your presence/absence points matter. Robert On Thu, Sep 4, 2014 at 7:40 AM, Maurizio Marchi wrote: > Hallo everybody, > I have

Re: [R-sig-Geo] Extract points from raster brick by both location and a matching attributes.

2014-09-11 Thread Robert J. Hijmans
I would extract all values and select the ones I need after that. The alternative is to extract values layer by layer, that is probably less efficient. Robert On Mon, Sep 8, 2014 at 1:55 AM, Kristin wrote: > Hi > > I have a raster brick of 464 layers, each layer representing time defined as > num

Re: [R-sig-Geo] Construct prediction grid that has a value for each corresponding predictor raster grid cell

2014-09-11 Thread Robert J. Hijmans
Justin, See ?raster::predict Robert On Wed, Sep 10, 2014 at 7:42 AM, Justin Michell wrote: > I have aligned raster layers as follows: > >> NDVI1 > class : RasterLayer > dimensions : 1287, 1321, 1700127 (nrow, ncol, ncell) > resolution : 0.00833, 0.00833 (x, y) > extent :

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

2014-09-11 Thread Robert J. Hijmans
(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] raster_2.3-0 sp_1.0-15 > > loaded via a nam

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")

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 =

  1   2   3   4   5   6   7   >