# Print on the screen some message
print(paste0("iteration: ", counter))
}
# get the raster for the species if file is present
if(file.exists(i)){
species_raster=raster(i)
#plot(species_raster)
#plot(world,add=T)
# define cells in which species is present
cells_species_presence=which(species_raster[1:length(species_raster)]==1)
# create an empty vector in which presence absence for this species wwill be stored
c_c_vector = c(species)
# 1a. present in north america?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_north_america,cells_ambiguous,c_c_vector)
# 1b. present in south america?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_south_america,cells_ambiguous,c_c_vector)
# 2. present in eurasia?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_eurasia,cells_ambiguous,c_c_vector)
# 3. present in africa?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_africa,cells_ambiguous,c_c_vector)
# 4. present in australia?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_australia,cells_ambiguous,c_c_vector)
# 5. present in carribean?
c_c_vector = cell_count_vector(cells_species_presence,cells_carribean,c_c_vector)
# 6.present in madagascar?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_madagascar,cells_ambiguous,c_c_vector)
# 7. present in oceanic?
c_c_vector = cell_count_vector(cells_species_presence,cells_oceanic,c_c_vector)
# 8. contains ambiguous cells?
c_c_vector = cell_count_vector(cells_species_presence,cells_ambiguous,c_c_vector)
# add the presence absence vector to dataframe
cc_df[nrow(cc_df) + 1,] = c_c_vector
}else{
c_c_vector = c(0,0,0,0,0,0,0,0,0)
cc_df[nrow(cc_df) + 1,] = c_c_vector
}
}
write.table(cc_df,'../../../processed/spatial_data/pa_file_cleaning_steps/cell_count_ambiguous_filter_raw.txt',sep="\t",quote = FALSE,row.names=FALSE)
# __________________________________________________________________
# ________get a list of all target species________
species_list_df = read.csv('/Users/tobias/GitHub/mammal_extinction/data/processed/extinction_dates/global_pyrate_species_list.txt',sep = '\t')
species_list = species_list_df$taxon
species_ranges=list.files('../Present_Natural/')
# _____________________________________________________
# ______________cell count ambiguous filter --> only counts occurrences on continents if they are in non-ambiguous cells_______________________________
cc_df <- data.frame(matrix(ncol = 10, nrow = 0))
col_labels <- c("species","north_america","south_america", "eurasia", "africa", "australia", "carribean", "madagascar", "oceanic","contains_ambiguous")
colnames(cc_df) <- col_labels
counter = 0
#i = 'Aethomys_kaiseri.tif'
for (species in species_list){
i = paste0('/Users/tobias/GitHub/mammal_extinction/data/raw/spatial_data/Present_Natural/',sub(' ','_',species),'.tif')
counter = counter+1
if(counter %% 100==0) {
# Print on the screen some message
print(paste0("iteration: ", counter))
}
# get the raster for the species if file is present
if(file.exists(i)){
species_raster=raster(i)
#plot(species_raster)
#plot(world,add=T)
# define cells in which species is present
cells_species_presence=which(species_raster[1:length(species_raster)]==1)
# create an empty vector in which presence absence for this species wwill be stored
c_c_vector = c(species)
# 1a. present in north america?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_north_america,cells_ambiguous,c_c_vector)
# 1b. present in south america?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_south_america,cells_ambiguous,c_c_vector)
# 2. present in eurasia?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_eurasia,cells_ambiguous,c_c_vector)
# 3. present in africa?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_africa,cells_ambiguous,c_c_vector)
# 4. present in australia?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_australia,cells_ambiguous,c_c_vector)
# 5. present in carribean?
c_c_vector = cell_count_vector(cells_species_presence,cells_carribean,c_c_vector)
# 6.present in madagascar?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_madagascar,cells_ambiguous,c_c_vector)
# 7. present in oceanic?
c_c_vector = cell_count_vector(cells_species_presence,cells_oceanic,c_c_vector)
# 8. contains ambiguous cells?
c_c_vector = cell_count_vector(cells_species_presence,cells_ambiguous,c_c_vector)
# add the presence absence vector to dataframe
cc_df[nrow(cc_df) + 1,] = c_c_vector
}else{
c_c_vector = c(species,0,0,0,0,0,0,0,0,0)
cc_df[nrow(cc_df) + 1,] = c_c_vector
}
}
write.table(cc_df,'../../../processed/spatial_data/pa_file_cleaning_steps/cell_count_ambiguous_filter_raw.txt',sep="\t",quote = FALSE,row.names=FALSE)
# __________________________________________________________________
library(raster)
library(maptools)
library(maps)
library(rgeos)
#library(postGIStools)
#PostGIS 2.0, ST_IsValid() and ST_MakeValid()
setwd('/Users/tobias/GitHub/mammal_extinction/data/raw/spatial_data/')
# _____________________FUNCTIONS_____________________
# define function that reads a bunch of raster files based on a list provided as csv file
get_spatial_data_from_csv <- function(csv_path,world_polygon_file) {
content = read.csv(file=csv_path,header=TRUE,sep="\t")
indeces = content$ISLANDS
spatial_unit = world_polygon_file[indeces[1],]
for (index in indeces[2:length(indeces)]){
island_raster = world_polygon_file[index,]
spatial_unit = island_raster + spatial_unit
}
return(spatial_unit)
}
# funciton to return a vector with the gintersect test results for all continents/areas
get_pa_vector_intersect <- function(continent,species_range,pa_vector) {
# North America
if (continent == 'north_america'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(north_america,species_range,byid = TRUE)==TRUE)){
pa_vector[2]=1
}
}
# South America
if (continent == 'south_america'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(south_america,species_range,byid = TRUE)==TRUE)){
pa_vector[3]=1
}
}
# Eurasia
if (continent == 'eurasia'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(eurasia,species_range,byid = TRUE)==TRUE)){
pa_vector[4]=1
}
}
# Africa
if (continent == 'africa'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(africa,species_range,byid = TRUE)==TRUE)){
pa_vector[5]=1
}
}
# Australia
if (continent == 'australia'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(australia,species_range,byid = TRUE)==TRUE)){
pa_vector[6]=1
}
}
# Carribean
if (continent == 'carribean'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(carribean,species_range,byid = TRUE)==TRUE)){
pa_vector[7]=1
}
}
# Madagascar
if (continent == 'madagascar'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(madagascar,species_range,byid = TRUE)==TRUE)){
pa_vector[8]=1
}
}
# Oceanic
if (continent == 'oceanic'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(oceanic,species_range,byid = TRUE)==TRUE)){
pa_vector[9]=1
}
}
return(pa_vector)
}
#____________________________________________________
#_____________load the geographic regions polygons_________________
world = readShapePoly('Shore_Lat_long/GSHHS_i_L1')
# if you want to plot a specific id:
#plot(world[world@data$id == 1,])
# if you want to plot a specific row number:
#plot(world[1,])
#_______________________________________________
#____________load the species range polygons___________________
iucn_ranges = readShapePoly('IUCN_2019_3_TERRESTRIAL_MAMMALS/TERRESTRIAL_MAMMALS')
#______________________________________________________________
#___________define region polygons______________
# *****Eurasia*****
eurasia1=world[1,]
eurasia2=world[2,]
eurasia3=get_spatial_data_from_csv("island_shape_indeces/asia_islands.csv",world)
eurasia4=get_spatial_data_from_csv("island_shape_indeces/europe_islands.csv",world)
eurasia = eurasia1 + eurasia2 + eurasia3 + eurasia4
# *****Africa*****
africa=world[3,]
# *****Americas*****
north_america_main=world[4,]
south_america_main=world[5,]
north_america_islands = get_spatial_data_from_csv("island_shape_indeces/north_america_islands.csv",world)
south_america_islands = get_spatial_data_from_csv("island_shape_indeces/south_america_islands.csv",world)
north_america = north_america_main+north_america_islands
south_america = south_america_main+south_america_islands
#americas
# *****Australia*****
australia_main = world[8,]
australia_islands = get_spatial_data_from_csv("island_shape_indeces/australia_islands.csv",world)
australia = australia_main+australia_islands
# *****Madagascar*****
madagascar=world[12,]
# *****Carribean*****
carribean = get_spatial_data_from_csv("island_shape_indeces/carribean_islands.csv",world)
# *****Oceanic islands*****
oceanic = get_spatial_data_from_csv("island_shape_indeces/oceanic_islands.csv",world)
# *****Everything*****
all_combined = africa+north_america+south_america+australia+eurasia+madagascar+carribean+oceanic
# _____________________________________________________
#_____________________________TESTING__________________________
# how to read the polygon of a specific species
example_species_range = iucn_ranges[iucn_ranges@data$binomial=='Elephas maximus',]#'Papio papio'
example_species_range2 = iucn_ranges[iucn_ranges@data$binomial=='Loxodonta africana',]
# example plot
pdf(file = "../../processed/spatial_data/plots/Elephas_maximus_range_map_modern_elephas.pdf", width = 12.5, height = 5, family = "Helvetica") # defaults to 7 x 7 inches
plot(eurasia1)
#plot(example_species_range,add=T,col='blue')
plot(example_species_range,add=T,col='blue')
dev.off()
# test if the species range overlaps with a defined region, e.g. eurasia
any(gIntersects(africa,example_species_range,byid = TRUE)==TRUE)
#_____________________________TESTING__________________________
# how to read the polygon of a specific species
example_species_range = iucn_ranges[iucn_ranges@data$binomial=='Elephas maximus',]#'Papio papio'
example_species_range2 = iucn_ranges[iucn_ranges@data$binomial=='Loxodonta africana',]
# example plot
pdf(file = "../../processed/spatial_data/plots/Elephas_maximus_range_map_modern_elephas.pdf", width = 12.5, height = 5, family = "Helvetica") # defaults to 7 x 7 inches
plot(eurasia1)
#plot(example_species_range,add=T,col='blue')
plot(example_species_range,add=T,col='blue')
dev.off()
# test if the species range overlaps with a defined region, e.g. eurasia
any(gIntersects(africa,example_species_range,byid = TRUE)==TRUE)
# ___________________get presence absence data___________________________
# read cell count tables
count_table_no_amb = read.csv('../../processed/spatial_data/pa_file_cleaning_steps/cell_count_ambiguous_filter_raw.txt',sep='\t')
#count_table_original = read.csv('../../processed/spatial_data/pa_file_cleaning_steps/cell_count_raw.txt',sep='\t')
#count_table = count_table_original
# convert into binary
count_table_no_amb[count_table_no_amb>0]=1
count_table_no_amb
iucn_ranges
iucn_ranges$binomial
iucn_ranges$binomial == 'Tadaria teniotis'
iucn_ranges$binomial[iucn_ranges$binomial == 'Tadaria teniotis']
'Tadaria teniotis' $in$ iucn_ranges$binomial
'Tadaria teniotis' %in% iucn_ranges$binomial
'Tadarida teniotis' %in% iucn_ranges$binomial
'Ursus arctos' %in% iucn_ranges$binomial
'Ursus maritimus' %in% iucn_ranges$binomial
cc_df
# correct a few mismatches by hand
problematic_taxa = c('Geocapromys SP_A','HexolobodontinaeGen_NOW Sp_NOW','Megaoryzomys Sp_Now','Nesophontes SP_A','Nesoryzomys Sp_B','Nesoryzomys Sp_C','Nesoryzomys Sp_D','Peroryctes SP_NOW','Peroryctinae GEN_NOW')
# correct a few mismatches by hand
problematic_taxa = c('Geocapromys SP_A','HexolobodontinaeGen_NOW Sp_NOW','Megaoryzomys Sp_Now','Nesophontes SP_A','Nesoryzomys Sp_B','Nesoryzomys Sp_C','Nesoryzomys Sp_D','Peroryctes SP_NOW','Peroryctinae GEN_NOW')
filenames = c('Geocapromys_SpA.tif','HexolobodontinaeGen_SpA.tif','Megaoryzomys_SpA.tif','Nesophontes_SpA.tif','Nesoryzomys_SpB.tif','Nesoryzomys_SpC.tif','Nesoryzomys_SpD.tif','Peroryctes_SpA.tif','PeroryctinaeGen_SpA.tif')
counter = 0
counter = counter+1
filenames = c('Geocapromys_SpA.tif','HexolobodontinaeGen_SpA.tif','Megaoryzomys_SpA.tif','Nesophontes_SpA.tif','Nesoryzomys_SpB.tif','Nesoryzomys_SpC.tif','Nesoryzomys_SpD.tif','Peroryctes_SpA.tif','PeroryctinaeGen_SpA.tif')
filename = filenames[counter]
filename
problematic_taxa = c('Geocapromys SP_A','HexolobodontinaeGen_NOW Sp_NOW','Megaoryzomys Sp_Now','Nesophontes SP_A','Nesoryzomys Sp_B','Nesoryzomys Sp_C','Nesoryzomys Sp_D','Peroryctes SP_NOW','Peroryctinae GEN_NOW')
filenames = c('Geocapromys_SpA.tif','HexolobodontinaeGen_SpA.tif','Megaoryzomys_SpA.tif','Nesophontes_SpA.tif','Nesoryzomys_SpB.tif','Nesoryzomys_SpC.tif','Nesoryzomys_SpD.tif','Peroryctes_SpA.tif','PeroryctinaeGen_SpA.tif')
counter = 0
for (species in problematic_taxa){
}
species
counter = 0
counter = counter+1
filename = filenames[counter]
i = paste0('/Users/tobias/GitHub/mammal_extinction/data/raw/spatial_data/Present_Natural/',filename)
species_raster=raster(i)
#plot(species_raster)
#plot(world,add=T)
# define cells in which species is present
cells_species_presence=which(species_raster[1:length(species_raster)]==1)
# create an empty vector in which presence absence for this species wwill be stored
c_c_vector = c(species)
# 1a. present in north america?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_north_america,cells_ambiguous,c_c_vector)
# 1b. present in south america?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_south_america,cells_ambiguous,c_c_vector)
# 2. present in eurasia?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_eurasia,cells_ambiguous,c_c_vector)
# 3. present in africa?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_africa,cells_ambiguous,c_c_vector)
# 4. present in australia?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_australia,cells_ambiguous,c_c_vector)
# 5. present in carribean?
c_c_vector = cell_count_vector(cells_species_presence,cells_carribean,c_c_vector)
# 6.present in madagascar?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_madagascar,cells_ambiguous,c_c_vector)
# 7. present in oceanic?
c_c_vector = cell_count_vector(cells_species_presence,cells_oceanic,c_c_vector)
# 8. contains ambiguous cells?
c_c_vector = cell_count_vector(cells_species_presence,cells_ambiguous,c_c_vector)
c_c_vector
cc_df
cc_df$species == species
cc_df[cc_df$species == species]
cc_df[cc_df$species == species,]
cc_df[cc_df$species == species,] = c_c_vector
cc_df[cc_df$species == species,]
# correct a few mismatches by hand
problematic_taxa = c('Geocapromys SP_A','HexolobodontinaeGen_NOW Sp_NOW','Megaoryzomys Sp_Now','Nesophontes SP_A','Nesoryzomys Sp_B','Nesoryzomys Sp_C','Nesoryzomys Sp_D','Peroryctes SP_NOW','Peroryctinae GEN_NOW')
filenames = c('Geocapromys_SpA.tif','HexolobodontinaeGen_SpA.tif','Megaoryzomys_SpA.tif','Nesophontes_SpA.tif','Nesoryzomys_SpB.tif','Nesoryzomys_SpC.tif','Nesoryzomys_SpD.tif','Peroryctes_SpA.tif','PeroryctinaeGen_SpA.tif')
counter = 0
for (species in problematic_taxa){
counter = counter+1
filename = filenames[counter]
i = paste0('/Users/tobias/GitHub/mammal_extinction/data/raw/spatial_data/Present_Natural/',filename)
species_raster=raster(i)
#plot(species_raster)
#plot(world,add=T)
# define cells in which species is present
cells_species_presence=which(species_raster[1:length(species_raster)]==1)
# create an empty vector in which presence absence for this species wwill be stored
c_c_vector = c(species)
# 1a. present in north america?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_north_america,cells_ambiguous,c_c_vector)
# 1b. present in south america?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_south_america,cells_ambiguous,c_c_vector)
# 2. present in eurasia?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_eurasia,cells_ambiguous,c_c_vector)
# 3. present in africa?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_africa,cells_ambiguous,c_c_vector)
# 4. present in australia?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_australia,cells_ambiguous,c_c_vector)
# 5. present in carribean?
c_c_vector = cell_count_vector(cells_species_presence,cells_carribean,c_c_vector)
# 6.present in madagascar?
c_c_vector = cell_count_vector_ambiguous(cells_species_presence,cells_madagascar,cells_ambiguous,c_c_vector)
# 7. present in oceanic?
c_c_vector = cell_count_vector(cells_species_presence,cells_oceanic,c_c_vector)
# 8. contains ambiguous cells?
c_c_vector = cell_count_vector(cells_species_presence,cells_ambiguous,c_c_vector)
cc_df[cc_df$species == species,] = c_c_vector
# update the df with the new value
}
cc_df
setwd('/Users/tobias/GitHub/mammal_extinction/data/raw/spatial_data/ISLAND_RASTERS')
write.table(cc_df,'../../../processed/spatial_data/pa_file_cleaning_steps/cell_count_ambiguous_filter_raw.txt',sep="\t",quote = FALSE,row.names=FALSE)
library(raster)
library(maptools)
library(maps)
library(rgeos)
#library(postGIStools)
#PostGIS 2.0, ST_IsValid() and ST_MakeValid()
setwd('/Users/tobias/GitHub/mammal_extinction/data/raw/spatial_data/')
# _____________________FUNCTIONS_____________________
# define function that reads a bunch of raster files based on a list provided as csv file
get_spatial_data_from_csv <- function(csv_path,world_polygon_file) {
content = read.csv(file=csv_path,header=TRUE,sep="\t")
indeces = content$ISLANDS
spatial_unit = world_polygon_file[indeces[1],]
for (index in indeces[2:length(indeces)]){
island_raster = world_polygon_file[index,]
spatial_unit = island_raster + spatial_unit
}
return(spatial_unit)
}
# funciton to return a vector with the gintersect test results for all continents/areas
get_pa_vector_intersect <- function(continent,species_range,pa_vector) {
# North America
if (continent == 'north_america'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(north_america,species_range,byid = TRUE)==TRUE)){
pa_vector[2]=1
}
}
# South America
if (continent == 'south_america'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(south_america,species_range,byid = TRUE)==TRUE)){
pa_vector[3]=1
}
}
# Eurasia
if (continent == 'eurasia'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(eurasia,species_range,byid = TRUE)==TRUE)){
pa_vector[4]=1
}
}
# Africa
if (continent == 'africa'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(africa,species_range,byid = TRUE)==TRUE)){
pa_vector[5]=1
}
}
# Australia
if (continent == 'australia'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(australia,species_range,byid = TRUE)==TRUE)){
pa_vector[6]=1
}
}
# Carribean
if (continent == 'carribean'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(carribean,species_range,byid = TRUE)==TRUE)){
pa_vector[7]=1
}
}
# Madagascar
if (continent == 'madagascar'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(madagascar,species_range,byid = TRUE)==TRUE)){
pa_vector[8]=1
}
}
# Oceanic
if (continent == 'oceanic'){
# if the intersect test returns 'true' store it as a '1' in the new row for the dataframe
if (any(gIntersects(oceanic,species_range,byid = TRUE)==TRUE)){
pa_vector[9]=1
}
}
return(pa_vector)
}
#____________________________________________________
#_____________load the geographic regions polygons_________________
world = readShapePoly('Shore_Lat_long/GSHHS_i_L1')
# if you want to plot a specific id:
#plot(world[world@data$id == 1,])
# if you want to plot a specific row number:
#plot(world[1,])
#_______________________________________________
#____________load the species range polygons___________________
iucn_ranges = readShapePoly('IUCN_2019_3_TERRESTRIAL_MAMMALS/TERRESTRIAL_MAMMALS')
#______________________________________________________________
#___________define region polygons______________
# *****Eurasia*****
eurasia1=world[1,]
eurasia2=world[2,]
eurasia3=get_spatial_data_from_csv("island_shape_indeces/asia_islands.csv",world)
eurasia4=get_spatial_data_from_csv("island_shape_indeces/europe_islands.csv",world)
eurasia = eurasia1 + eurasia2 + eurasia3 + eurasia4
# *****Africa*****
africa=world[3,]
# *****Americas*****
north_america_main=world[4,]
south_america_main=world[5,]
north_america_islands = get_spatial_data_from_csv("island_shape_indeces/north_america_islands.csv",world)
south_america_islands = get_spatial_data_from_csv("island_shape_indeces/south_america_islands.csv",world)
north_america = north_america_main+north_america_islands
south_america = south_america_main+south_america_islands
#americas
# *****Australia*****
australia_main = world[8,]
australia_islands = get_spatial_data_from_csv("island_shape_indeces/australia_islands.csv",world)
australia = australia_main+australia_islands
# *****Madagascar*****
madagascar=world[12,]
# *****Carribean*****
carribean = get_spatial_data_from_csv("island_shape_indeces/carribean_islands.csv",world)
# *****Oceanic islands*****
oceanic = get_spatial_data_from_csv("island_shape_indeces/oceanic_islands.csv",world)
# *****Everything*****
all_combined = africa+north_america+south_america+australia+eurasia+madagascar+carribean+oceanic
# _____________________________________________________
#_____________________________TESTING__________________________
# how to read the polygon of a specific species
example_species_range = iucn_ranges[iucn_ranges@data$binomial=='Elephas maximus',]#'Papio papio'
example_species_range2 = iucn_ranges[iucn_ranges@data$binomial=='Loxodonta africana',]
# example plot
pdf(file = "../../processed/spatial_data/plots/Elephas_maximus_range_map_modern_elephas.pdf", width = 12.5, height = 5, family = "Helvetica") # defaults to 7 x 7 inches
plot(eurasia1)
#plot(example_species_range,add=T,col='blue')
plot(example_species_range,add=T,col='blue')
dev.off()
# test if the species range overlaps with a defined region, e.g. eurasia
any(gIntersects(africa,example_species_range,byid = TRUE)==TRUE)
# ___________________get presence absence data___________________________
# read cell count tables
count_table_no_amb = read.csv('../../processed/spatial_data/pa_file_cleaning_steps/cell_count_ambiguous_filter_raw.txt',sep='\t')
#count_table_original = read.csv('../../processed/spatial_data/pa_file_cleaning_steps/cell_count_raw.txt',sep='\t')
#count_table = count_table_original
# convert into binary
count_table_no_amb[count_table_no_amb>0]=1
# ___________________get presence absence data___________________________
# read cell count tables
count_table_no_amb = read.csv('../../processed/spatial_data/pa_file_cleaning_steps/cell_count_ambiguous_filter_raw.txt',sep='\t')
#count_table_original = read.csv('../../processed/spatial_data/pa_file_cleaning_steps/cell_count_raw.txt',sep='\t')
#count_table = count_table_original
# convert into binary
count_table_no_amb[count_table_no_amb>0]=1
count_table_no_amb
#______________create new df for clean presence absence data_________________
# create a new dataframe for presence absence data
new_pa_df <- data.frame(matrix(ncol = 9, nrow = 0))
col_labels <- c("species","north_america", "south_america", "eurasia", "africa", "australia", "carribean", "madagascar", "oceanic")
colnames(new_pa_df) <- col_labels
count_table
count_table_no_amb
count_table$contains_ambiguous == 0
count_table_no_amb$north_america+count_table_no_amb$south_america
count_table_no_amb$north_america+count_table_no_amb$south_america==0
count_table_no_amb$north_america+count_table_no_amb$south_america+count_table_no_amb$eurasia+count_table_no_amb$africa+count_table_no_amb$australia+count_table_no_amb$carribean+count_table_no_amb$madagascar==0+count_table_no_amb$oceanic==0
count_table_no_amb$north_america+count_table_no_amb$south_america+count_table_no_amb$eurasia+count_table_no_amb$africa+count_table_no_amb$australia+count_table_no_amb$carribean+count_table_no_amb$madagascar+count_table_no_amb$oceanic==0
count_table_no_amb
count_table_no_amb[count_table_no_amb$north_america+count_table_no_amb$south_america+count_table_no_amb$eurasia+count_table_no_amb$africa+count_table_no_amb$australia+count_table_no_amb$carribean+count_table_no_amb$madagascar+count_table_no_amb$oceanic==0]
count_table_no_amb[count_table_no_amb$north_america+count_table_no_amb$south_america+count_table_no_amb$eurasia+count_table_no_amb$africa+count_table_no_amb$australia+count_table_no_amb$carribean+count_table_no_amb$madagascar+count_table_no_amb$oceanic==0,]
no_data_secies = count_table_no_amb[count_table_no_amb$north_america+count_table_no_amb$south_america+count_table_no_amb$eurasia+count_table_no_amb$africa+count_table_no_amb$australia+count_table_no_amb$carribean+count_table_no_amb$madagascar+count_table_no_amb$oceanic==0,]
no_data_species = count_table_no_amb[count_table_no_amb$north_america+count_table_no_amb$south_america+count_table_no_amb$eurasia+count_table_no_amb$africa+count_table_no_amb$australia+count_table_no_amb$carribean+count_table_no_amb$madagascar+count_table_no_amb$oceanic==0,]
no_data_species
count_table_no_amb
