Extract cell numbers from raster based on condition
Brian, Andrew's approach works, but you really should not do it that way. It is inefficient due to the use of extract. Rather than extract(x, 1:ncell(x) you could do the much more direct values(x). The code could also lead to an error due to memory limitations whereas the other two approaches (Which or rasterToPoints) might work for the same data (if there are relatively many NA values). Andrew improved the original ras[ras[] > 50] <- NA to the much better ras[ras > 50] <- NA ; again because that will work with large files, but reclassify(ras, cbind(50, Inf, NA)) would be my preference. I understand that you may only use relatively small files, but as the archives of this mailing list are a resource for many, I am compelled to explain this such that others won't get into trouble. (The next questions is whether you actually need to extract cell numbers for what you want to accomplish (this is often done but rarely needed) ---- but that is not the topic of your post) Robert
On Mon, Nov 25, 2013 at 7:49 AM, Brian Buma <brian.buma at uas.alaska.edu> wrote:
Thanks Andrew, this works. I appreciate it. -brian On Sat, Nov 23, 2013 at 10:27 AM, Andrew Vitale <vitale232 at gmail.com> wrote:
Brian,
This works, but it will use memory. I guess it depends just how big that
DEM is...
-Andrew
library(raster)
ras <- raster(ncols=10,nrows=10)
ras[] <- 1:100
#now I want to get the cell numbers for cells > 50. I don't see a good way
#to do it.
#An attempt with spatial points doesn't work:
ras[ras<50] <- NA
temp <- extract(ras, 1:ncell(ras), df = TRUE)
cells <- temp$ID[which(!is.na(temp$layer))]
On 11/23/2013 10:48 AM, Brian Buma wrote:
Thanks, but that does not do what I need. As in my example, that just
reclasses the raster, it doesn't return the cell numbers for the non-NA
portions of the raster. Calling SpatialPoints on the raster with NA
likewise does not return the non-NA cells only.
On Fri, Nov 22, 2013 at 11:51 PM, Roman Lu?trik <roman.lustrik at gmail.com> <roman.lustrik at gmail.com>wrote:
Try this.
library(raster)
ras <- raster(ncols=10,nrows=10)
ras[] <- 1:100
par(mfrow = c(1, 2))
plot(ras)
ras[ras[] > 50] <- NA
plot(ras)
Cheers,
Roman
On Sat, Nov 23, 2013 at 7:58 AM, Brian Buma <brian.buma at uas.alaska.edu> <brian.buma at uas.alaska.edu>wrote:
Hello all,
I'm trying to extract the cell numbers from a raster in R based on a
logical. For example:
ras <- raster(ncols=10,nrows=10)
ras[] <- 1:100
#now I want to get the cell numbers for cells > 50. I don't see a good
way
to do it.
#An attempt with spatial points doesn't work:
temp <- SpatialPoints(ras>50)
out <- cellFromXY(ras,temp)
#nor does reclassing the raster
ras[ras<50] <- NA
I hope this isn't an obvious question, but I'm just not seeing it. For
what it's worth, I'm analyzing a large DEM and want to skip elevations
that
equal zero, rather than process those as well as the rest. The current
code is quite efficient (no for loops, vectorized) but the dataset is so
large it takes a while. The process runs off of cell numbers (currently
running all the cells), so if I could not analyze those cell numbers at
all, so much the better.
Thanks. I imagine there's a simple, elegant solution I'm just not seeing
for some reason.
[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing listR-sig-Geo at r-project.orghttps://stat.ethz.ch/mailman/listinfo/r-sig-geo -- In God we trust, all others bring data. [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing listR-sig-Geo at r-project.orghttps://stat.ethz.ch/mailman/listinfo/r-sig-geo -- Andrew Vitale Master's Student Department of Geography University of Nevada, Renovitale232 at gmail.com
[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo