Re: [R-sig-Geo] summing rasters with a condition given by other rasters
Hi Martin, On May 10, 2015, at 4:42 PM, Martin Brandt wrote: > Hi Ben, > > thanks again, I see, but i'm not sure if i understand it correctly. The > thing is that each pixel in the rasters sos and eos has different values, > i.e. the start of season and end of season is not the same for each pixel. > Thus, I cannot really define eos and sos but need to use the given rasters > containing the values.. > Oo. I think I see what you are after. I'll bet there are better ways, but my brute force and ignorance approach would be to prepend your start-of-season raster (sos) and end-of-season raster (eos) to your raster data. I would still use calc but modify the sum_segment function. After the computation I would then drop the sos and eos layer form the data. Note that sos and eos still have to be numeric indices into the layers of the data, b. library(raster) # create a multiple layer brick b <- brick(system.file("external/rlogo.grd", package="raster")) # add layers b <- addLayer(b,b,b) b # class : RasterStack # dimensions : 77, 101, , 9 (nrow, ncol, ncell, nlayers) # resolution : 1, 1 (x, y) # extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) # coord. ref. : +proj=merc # names : red.1, green.1, blue.1, red.2, green.2, blue.2, red.3, green.3, blue.3 # min values : 0, 0, 0, 0, 0, 0, 0, 0, 0 # max values : 255, 255,255, 255, 255,255, 255, 255, 255 # define the sos and eos by indexed position # sos will range from 1 to 4 sos <- raster(matrix(sample(1:4, ncell(b), replace = TRUE), nrow = nrow(b), ncol = ncol(b)), template = b) #eos will range from 6 to 9 eos <- raster(matrix(sample(6:nlayers(b), ncell(b), replace = TRUE), nrow = nrow(b), ncol = ncol(b)), template = b) # prepend sos and eos to your data b <- addLayer(sos, eos, b) # define the summing function - add 2 to account for sos and eos at the start sum_segment <- function(x, ...) { sum(x[(x[1]+2):(x[2] + 2)],...) } # run the computation s <- calc(b, sum_segment) #restore b b <- dropLayer(b, c(1,2)) s #class : RasterLayer #dimensions : 77, 101, (nrow, ncol, ncell) #resolution : 1, 1 (x, y) #extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) #coord. ref. : +proj=merc #data source : in memory #names : layer #values : 0, 2295 (min, max) b #class : RasterStack #dimensions : 77, 101, , 9 (nrow, ncol, ncell, nlayers) #resolution : 1, 1 (x, y) #extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) #coord. ref. : +proj=merc #names : red.1, green.1, blue.1, red.2, green.2, blue.2, red.3, green.3, blue.3 #min values : 0, 0, 0, 0, 0, 0, 0, 0, 0 #max values : 255, 255,255, 255, 255,255, 255, 255, 255 You might be able to do something similar with the overlay(). If that doesn't do it then we'll both have to wait for someone to come to our rescue! Cheers, Ben > cheers, > Martin > > > > > -- > View this message in context: > http://r-sig-geo.2731867.n2.nabble.com/summing-rasters-with-a-condition-given-by-other-rasters-tp7588222p7588233.html > Sent from the R-sig-geo mailing list archive at Nabble.com. > > ___ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] slope for rarefaction curve
Apologies for this e-mail, I accidentally sent it to wrong R list! I am subscribed to multiple lists. Cheers, Simone On Sun, May 10, 2015 at 11:56 PM, Simone Ruzza wrote: > Dear all, > > apologies for the total beginner's question. I was wondering if anyone > can give some advice on how to calculate the slope for the last 10% of > the records of a rarefaction curve computed with rarefy from vegan. > Here is a graphic representation of what I would like to do: > > https://dl.dropboxusercontent.com/u/33966347/figure.JPG > > I have seen that this has been done in a recent paper and I was > wondering if anyone may have any code snippet to do that. Sorry, maybe > this is something really obvious but I have not quite understood how > to do it. > > thanks! > > Simone ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[R-sig-Geo] slope for rarefaction curve
Dear all, apologies for the total beginner's question. I was wondering if anyone can give some advice on how to calculate the slope for the last 10% of the records of a rarefaction curve computed with rarefy from vegan. Here is a graphic representation of what I would like to do: https://dl.dropboxusercontent.com/u/33966347/figure.JPG I have seen that this has been done in a recent paper and I was wondering if anyone may have any code snippet to do that. Sorry, maybe this is something really obvious but I have not quite understood how to do it. thanks! Simone ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] summing rasters with a condition given by other rasters
Hi Ben, thanks again, I see, but i'm not sure if i understand it correctly. The thing is that each pixel in the rasters sos and eos has different values, i.e. the start of season and end of season is not the same for each pixel. Thus, I cannot really define eos and sos but need to use the given rasters containing the values.. cheers, Martin -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/summing-rasters-with-a-condition-given-by-other-rasters-tp7588222p7588233.html Sent from the R-sig-geo mailing list archive at Nabble.com. ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Issue with ogrInfo
On Sun, 10 May 2015, Barry Rowlingson wrote: On Sun, May 10, 2015 at 4:31 PM, Daniel Marcelino wrote: Dear all, I'm having the following issue when trying to read a topographical json file: That's a **topological** (not topographical) geojson file. Instead of recording each polygon separately, and thus duplicating common boundaries, a topojson file stores all the line segments once, and then defines polygons as which sequence of line segments draws the polygon. I'm guessing your ogr library doesn't have topojson support. Does the output of ogrDrivers() agree with me? Solution is usually "use gdal/ogr tools to convert to plain old geojson (ogr2ogr)" Right, but while the geojson driver does read topojson from 1.11, ogrinfo says no-go. On inspecting the file, it appears only to contain lost of links to other stuff and no geometries that I (or OGR) recognise. The geometries must be hidden in one of the links, but are not exposed. Roger Barry ___ 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, Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 91 00 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] Issue with ogrInfo
On Sun, May 10, 2015 at 6:12 PM, Barry Rowlingson wrote: > Solution is usually "use gdal/ogr tools to convert to plain old > geojson (ogr2ogr)" Actually that's a really stupid solution. If ogr2ogr can do it, you need the support in the first place! Doh! You need a separate tool. Something here might help: http://recology.info/2015/01/geojson-topojson-io/ > > Barry ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Issue with ogrInfo
On Sun, May 10, 2015 at 4:31 PM, Daniel Marcelino wrote: > Dear all, I'm having the following issue when trying to read a > topographical json file: That's a **topological** (not topographical) geojson file. Instead of recording each polygon separately, and thus duplicating common boundaries, a topojson file stores all the line segments once, and then defines polygons as which sequence of line segments draws the polygon. I'm guessing your ogr library doesn't have topojson support. Does the output of ogrDrivers() agree with me? Solution is usually "use gdal/ogr tools to convert to plain old geojson (ogr2ogr)" Barry ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] summing rasters with a condition given by other rasters
Hi, On May 10, 2015, at 7:37 AM, Martin Brandt wrote: > Hi Ben, > > many thanks for the detailed answer. The code works fine, but when I use > rasters for eos and sos instead of numbers, i get an error: > > > > b <- brick(system.file("external/rlogo.grd", package="raster")) > > b <- addLayer(b,b,b) > b > > sos <- raster(system.file("external/rlogo.grd", package="raster")) > eos <- raster(system.file("external/rlogo.grd", package="raster")) > > > sum_segment <- function(x, from = sos, to = eos, ...) { > sum(x[from:to],...) > } > > s <- calc(b, sum_segment) > > Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : > cannot use this function > > am I doing something wrong? Yes, as tempting as it is to do otherwise you really do have to use numeric position indices. Inside the function sum_segment the value of x is simple a numeric vector. Within the scope of the function the context of the pixels embedded in the raster object is temporarily "lost" - they are just a vector of numbers. To work with the calc() function you must first compute the position indices for sos and eos using which(). sos <- which(names(b) == "blue.1") # you would substitue the name of your layer for blue.1 eos <- which(names(b) == "green.3") # and again for green.3 Cheers, Ben > > > > > -- > View this message in context: > http://r-sig-geo.2731867.n2.nabble.com/summing-rasters-with-a-condition-given-by-other-rasters-tp7588222p7588226.html > Sent from the R-sig-geo mailing list archive at Nabble.com. > > ___ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Issue with ogrInfo
On Sun, 10 May 2015, Daniel Marcelino wrote: Dear all, I'm having the following issue when trying to read a topographical json file: The file is not recognised by OGR either, so is probably not conforming to geojson expectations. It declares itself as HTML, but within github puts boundaries on a map background, so I think the file is trying to do much more than simply represent Welsh parliamentary constituencies. Roger The file is here https://github.com/kjhealy/uk-elections/blob/master/maps/topo_wpc.json uk.map <- readOGR(dsn = "maps/topo_wpc.json", layer = "wpc") Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open file traceback() 5: .Call("ogrInfo", as.character(dsn), as.character(layer), PACKAGE = "rgdal") 4: ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, swapAxisOrder = swapAxisOrder, require_geomType = require_geomType) 3: withCallingHandlers(expr, message = function(c) invokeRestart("muffleMessage")) 2: suppressMessages(ogr_info <- ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, swapAxisOrder = swapAxisOrder, require_geomType = require_geomType)) 1: readOGR(dsn = "maps/topo_wpc.json", layer = "wpc") sessionInfo() R version 3.2.0 Patched (2015-04-19 r68207) Platform: x86_64-apple-darwin10.8.0 (64-bit) Running under: OS X 10.8.5 (Mountain Lion) 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] rgdal_0.9-2 sp_1.1-0 loaded via a namespace (and not attached): [1] MASS_7.3-40 colorspace_1.2-6 scales_0.2.4 plyr_1.8.1 [5] tools_3.2.0 gtable_0.1.2 reshape2_1.4.1 Rcpp_0.11.5 [9] ggplot2_1.0.1grid_3.2.0 stringr_0.6.2digest_0.6.8 [13] proto_0.3-10 munsell_0.4.2lattice_0.20-31 ___ 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, Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 91 00 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
[R-sig-Geo] Issue with ogrInfo
Dear all, I'm having the following issue when trying to read a topographical json file: The file is here https://github.com/kjhealy/uk-elections/blob/master/maps/topo_wpc.json > uk.map <- readOGR(dsn = "maps/topo_wpc.json", layer = "wpc") Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open file > traceback() 5: .Call("ogrInfo", as.character(dsn), as.character(layer), PACKAGE = "rgdal") 4: ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, swapAxisOrder = swapAxisOrder, require_geomType = require_geomType) 3: withCallingHandlers(expr, message = function(c) invokeRestart("muffleMessage")) 2: suppressMessages(ogr_info <- ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, swapAxisOrder = swapAxisOrder, require_geomType = require_geomType)) 1: readOGR(dsn = "maps/topo_wpc.json", layer = "wpc") > sessionInfo() R version 3.2.0 Patched (2015-04-19 r68207) Platform: x86_64-apple-darwin10.8.0 (64-bit) Running under: OS X 10.8.5 (Mountain Lion) 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] rgdal_0.9-2 sp_1.1-0 loaded via a namespace (and not attached): [1] MASS_7.3-40 colorspace_1.2-6 scales_0.2.4 plyr_1.8.1 [5] tools_3.2.0 gtable_0.1.2 reshape2_1.4.1 Rcpp_0.11.5 [9] ggplot2_1.0.1grid_3.2.0 stringr_0.6.2digest_0.6.8 [13] proto_0.3-10 munsell_0.4.2lattice_0.20-31 ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] summing rasters with a condition given by other rasters
Hi Ben, many thanks for the detailed answer. The code works fine, but when I use rasters for eos and sos instead of numbers, i get an error: b <- brick(system.file("external/rlogo.grd", package="raster")) b <- addLayer(b,b,b) b sos <- raster(system.file("external/rlogo.grd", package="raster")) eos <- raster(system.file("external/rlogo.grd", package="raster")) sum_segment <- function(x, from = sos, to = eos, ...) { sum(x[from:to],...) } s <- calc(b, sum_segment) Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : cannot use this function am I doing something wrong? -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/summing-rasters-with-a-condition-given-by-other-rasters-tp7588222p7588226.html Sent from the R-sig-geo mailing list archive at Nabble.com. ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo