[R-sig-Geo] Creating presence/absence from grid cells & polygons

Robert J. Hijmans r.hijmans at gmail.com
Thu Jan 23 19:51:12 CET 2014


Here is an approach:

library(raster)
p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
sp1 <- SpatialPolygons(list(Polygons(list(Polygon(p1), Polygon(hole,
hole=TRUE)), 1)))
sp2 <- SpatialPolygons(list(Polygons(list(Polygon(rbind(c(-10,0),
c(140,60), c(160,0), c(140,-55), c(-10,0)))), 2)))
sp3 <- SpatialPolygons(list(Polygons(list(Polygon(rbind(c(-125,0),
c(0,60), c(40,5), c(15,-45), c(-125,0)))), 3)))

species <- list(sp1, sp2, sp3)
r <- raster(ncol=90, nrow=45)

rasters <- list()
for (i in 1:length(species)) {
   rasters[[i]] <- rasterize(species[[i]], r, field=1, background=0)
}
s <- stack(rasters)
x <- as.data.frame(s)

# alternatively, look at 'over' in sp.

################

# And this is how you might do it with shapefiles from a single directory
sps = list.files(pattern='.shp$')
# adjust raster to your taste (extent, resolution)
r <- raster(ncol=90, nrow=45)

rasters <- list()
for (i in 1:length(sps)) {
   sp <- shapefile(sps[i])
   f <- extension(sp, '.grd')
   rasters[[i]] <- rasterize(species[[i]], r, field=1, background=0,
filename=f, overwrite=TRUE)
}
s <- stack(rasters)
x <- as.data.frame(s)


On Thu, Jan 23, 2014 at 3:53 AM, Barnabas Daru <darunabas at gmail.com> wrote:
> Dear list members,
> I will like to create a presence/absence matrix using R where rows represent grid cells and columns species.
>
> I have a set of overlapping polygons each representing the distribution of different species and grid cells (also shapefiles generated using fishnet tool in ArcMap) to show presence or absence of species within the grid cells.
>
> In Arcmap, I was able to do this successfully for few species as follows:
> (1) I merged the polygons
> (2) I performed spatial intersect of the merged maps on the grid cells
> (3) From the result, I used the dataframe (dbf) of the intersect to create the presence absence matrix using the function sample2matrix in the R package picante. The 3 columns of the dataframe are:
> plots | abundance | species
>
> However, given the number of merged maps (over 200 overlapping polygons), the analysis in Arcmap got stalled and never proceed to completion.
> I will therefore like to do this in R.
> I will greatly appreciate any R code to help me do this.
>
> Thanks and kind regards
> Barnabas
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list