[R-sig-Geo] Creating presence/absence from grid cells & polygons
Robert J. Hijmans
r.hijmans at gmail.com
Mon Jan 27 19:00:59 CET 2014
Barnabas,
In my example the IDs are the same as the rows on the data.frame (row
1 is cell 1, etc). Cells are numbered from left to right and then from
top to bottom. See
http://cran.r-project.org/web/packages/raster/vignettes/Raster.pdf
for more info
If you have merged your species polygons you can subset them within
the loop (i.e. iterate over the length of the polygons = iterating
over species)
It seems that your 'grid' consists of polygons that you are
intersecting with your species ranges. It is probably better to define
and use a raster as I showed. If you insist on the polygons-grid
route, you can use "intersect"; but that may fail; there was a reason
why ArcGIS crashed.... Here is a simple example:
library(raster)
# some example data
p1 <- Polygons(list(Polygon(rbind(c(-180,-20), c(-140,55), c(10, 0),
c(-140,-60), c(-180,-20)))), 1)
p2 <- Polygons(list(Polygon(rbind(c(-10,0), c(140,60), c(160,0),
c(140,-55), c(-10,0)))), 2)
p3 <- Polygons(list(Polygon(rbind(c(-125,0), c(0,60), c(40,5),
c(15,-45), c(-125,0)))), 3)
sp <- SpatialPolygons( list( p1 , p2, p3), proj4string=CRS('+proj=utm
+zone=1') )
sp <- SpatialPolygonsDataFrame(sp, data.frame(species=1:3))
r <- raster(ncol=90, nrow=45, crs='+proj=utm +zone=1')
p <- rasterToPolygons(r)
p$cell <- 1:ncell(r)
p$layer <- NULL
v <- intersect(sp, p)
m <- as.matrix(table(v$cell, v$species))
m[1:10, ]
Robert
Robert
On Thu, Jan 23, 2014 at 12:05 PM, Barnabas Daru <darunabas at gmail.com> wrote:
> Hi Robert
>
> Thanks very much for your reply and the code you provide. When I run the
> code, I could not get the rows to give me the IDs of the grid cells.
>
> In my case, I have two sets of data.
>
> The first set are polygons of several species distributions merged together
> with the attribute of the merged polygon representing the species names.
>
> The second set are the grid cells prepared in ArcMap and they are of fixed
> size, 50 by 50 km. Each grid has an ID and longitude/latitude locality. My
> aim is to know which species is present in each grid, thereby creating a
> presence/absence matrix where the columns are the names of the species in
> the merged file, and rows are the IDs of the grid cells.
>
> If it is OK by you, I can send you a subset of the data as shapefiles to see
> what I mean.
>
> Thanks and kind regards
>
> Barnabas Daru
>
>
>
> On Thu, Jan 23, 2014 at 8:51 PM, Robert J. Hijmans <r.hijmans at gmail.com>
> wrote:
>>
>> 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
>
>
>
>
> --
>
> \-/
> /\
> /--|
> /---/ Daru, Barnabas Haruna
> |--/ PhD Candidate,
> \-/ African Centre for DNA Barcoding,
> /\ University of Johannesburg,
> /--\ PO BOX 524 Auckland Park, 2006,
> |---\ South Africa
> \---\ Lab: +27 11 559 3477
> \--| Mobile: +27 7381 89 583
> \-/
> /\ My homepage: http://barnabasdaru.com
> /--\
>
>
More information about the R-sig-Geo
mailing list