JamesDMSpeed / Mexican-mammal-phylogenetic-diversity

Spatial analysis of mammalian phylogenetic diversity in Mexico
MIT License
1 stars 0 forks source link

adding new sp #3

Closed ALEJANDRA-TOMASINI closed 4 years ago

ALEJANDRA-TOMASINI commented 4 years ago

I'll try here, may be is better. I manage to get the same projection on both the new distribution maps and the mammal stack that I've been using. The problem is that the extent, the resolution and the dimensions of the file are different so I cannot add the new distribution maps to the mammal stack. I can save in the same file, but when I ask to stack all the files, I get an error:

mexicomammalstack<-stack(filelist) Error in compareRaster(rasters) : different extent

So is there a way to convert the new distribution maps with not only the same projection as the mammals tack but also the rest of the info?

*ai_reproject= distribution map of Canis lupus

ai_reproject class : RasterLayer dimensions : 4567, 6925, 31626475 (nrow, ncol, ncell) resolution : 0.0167, 0.0167 (x, y) extent : -168.2077, -52.56024, 6.922938, 83.19184 (xmin, xmax, ymin, ymax) crs : +proj=aea +lat_1=14.5 +lat_2=32.5 +lat_0=24 +lon_0=-105 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs source : memory names : w001001 values : 0, 1 (min, max)

mexicomammalstack class : RasterStack dimensions : 2172, 3326, 7224072, 473 (nrow, ncol, ncell, nlayers) resolution : 1000, 1000 (x, y) extent : -1341038, 1984962, -1075341, 1096659 (xmin, xmax, ymin, ymax) crs : +proj=aea +lat_1=14.5 +lat_2=32.5 +lat_0=24 +lon_0=-105 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs names : Alouatta.palliata, Alouatta.pigra, Ammospermophilus.harrisii, Ammospermophilus.interpres, Ammospermophilus.leucurus, Anoura.geoffroyi, Antilocapra.americana, Antrozous.pallidus, Artibeus.hirsutus, Artibeus.jamaicensis, Artibeus.lituratus, Ateles.geoffroyi, Baeodon.alleni, Baeodon.gracilis, Baiomys.musculus, ... min values : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... max values : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...

JamesDMSpeed commented 4 years ago

It looks as though the ai_reproject layer has had the crs force-changed rather than reprojected. The units of the extent and resolution suggest that these were originally degrees lat and longs. Check the crs of the data after you first import it - then use projectRaster

ALEJANDRA-TOMASINI commented 4 years ago

this is the data after I first imported it:

ai class : RasterLayer dimensions : 4566, 6929, 31637814 (nrow, ncol, ncell) resolution : 0.016667, 0.016667 (x, y) extent : -168.1242, -52.63859, 7.006816, 83.10834 (xmin, xmax, ymin, ymax) crs : NA source : /home/shomeb/m/mariagui/Mexican-mammal-phylogenetic-diversity/canlup_f/w001001.adf names : w001001 values : 0, 1 (min, max)

I cannot use project raster since the cry is NA:

ai1<-projectRaster(ai,crs=crs(mexicomammalstack)) Error in projectRaster(ai, crs = crs(mexicomammalstack)) : input projection is NA

JamesDMSpeed commented 4 years ago

Check through the metadata associated with the raster layers - but looking at the extent values, and given that this is Canis lupus, it seems reasonable that these are geographic coordinates. You can set the crs to be '+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0', then use projectRaster to reproject to the projection of the other species data.

ALEJANDRA-TOMASINI commented 4 years ago

I already did that and it worked for the projection. I still cannot manage to stack the two of them because the extent is different. I tried "extent" to try set the extent of the mexicomammalstack, to the new distribution ranges, did not worked.

I searched in Jesus' scrip and it seemed he had kind of the same problem. I tried that part of his code but still did not work so I'm still stocked in adding the new distribution ranges.... is only 2 species anyways... i figured that the rest of the maps are not available anyways:

---Different Extent Issue fixed by stacking stacks-not classy but works---

filelist2<-list.files('differentextent',full.names = T) filelist2

Stack up

mexicomammalstack2<-stack(filelist2)

Tidy names

names(mexicomammalstack2)<-substring(names(mexicomammalstack2),1) names(mexicomammalstack2)

plot(mexicomammalstack2[[1]])

Project

mexicomammalstack2<-projectRaster(mexicomammalstack2,crs= "+proj=aea +lat_1=14.5 +lat_2=32.5 +lat_0=24 +lon_0=-105 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs") mexicomammalstack2

setExtent(mexicomammalstack2,mexicomammalstack)

Stack the two

mexicomammalstackbn<-stack(mexicomammalstack,crop(mexicomammalstack2,mexicomammalstack))

Stack the two

mexicomammalstackbn<-stack(mexicomammalstack,crop(mexicomammalstack2,mexicomammalstack)) Error in compareRaster(x) : different extent

extent(mexicomammalstack2,mexicomammalstack) Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘yFromRow’ for signature ‘"RasterBrick", "RasterStack"’

ALEJANDRA-TOMASINI commented 4 years ago

moving on... now I want to know how many species do I have inside and how many outside PA, I guess it would be informative to know which as well. Also, I want to know how many times a sp./branch tip is protected in the total set of PA.

I have no clear idea of how to do this. I was thinking separating the polygons of the PA:

SRiPA<-mask(SR_lcc,ALLPA_ToUse) cellStats(SRiPA, stat='mean', na.rm=TRUE, asSample=TRUE) cellStats(SRiPA, stat='max', na.rm=TRUE, asSample=TRUE) cellStats(SRiPA, stat='min', na.rm=TRUE, asSample=TRUE)

but I think that with that I only get the largest number of sp. contained in one cell inside the PA, not really the total number of species conserved by the total area of PA.

For the B diversity you suggested to make a new dataset where all of Mexico’s protected areas are merged into a single community. But could I do this for counting #of species as well? Also do I do this with:

communitydata2 <- getValues(mexicomammalstack_10km) #????

What I dont understand is which data to use for creating the new community, with for SR and PD

JamesDMSpeed commented 4 years ago

First - the extent: If you are certain that they have the same CRS and the CRS is correctly defined, you can then crop the larger one (C.lupus) to the smaller extent (Mexico). If you still cannot stack them it is then because the grids do not exactly overlap. Then you can use resample, setting the method='ngb' argument.

JamesDMSpeed commented 4 years ago

To identify which species are present or missing from protected areas, you can extract the species stack and the protected areas raster. Use indexing to identify which species are not present in any of the protected areas.

To combine all protected areas into one community (to look at turnover between all protected areas against individual unprotected cells) you can use the community dataset that you read into picante. Add a protected area column to that dataset, then sum the species presences across all protected cells in that dataset (i.e. how many protected area cells each species is found within). This is interesting from a conservation perspective. Before you do the B diversity turnover analysis, convert it back to presence-absence.