我有从水中取出的旧捕蟹器的纬度/经度数据,以及它们被处理的 4 个相应地点中的 1 个。我想弄清楚的问题是,如果我有更多或更少的处置场所,是否会上交相同数量的捕蟹器。因此,在这种情况下,处置场所将是“物种”。
#### Load packages ####
library(adehabitatHR)
library(raster)
library(rgdal)
library(maptools)
rm(list = ls())
# load data, need ID column and coordinates
# ID column in this case is tag ID
# This sample data has 4 different tag TDs
library(readxl)
data <- as.data.frame(read_excel("coordinates2.xls"))
View(data)
###########################################################################################
# assign the correct columns as coordinates
coordinates(data)<-c("POINT_X","POINT_Y")
proj4string(data)<- CRS("+proj=longlat")
#convert the coordinates to utm format
trans.coor <-spTransform(data, CRS("+init=epsg:3857"))
trans.coor$PickupLocation <- factor(trans.coor$PickupLocation)
str(trans.coor$PickupLocation)
( colkud <- adehabitatHR::kernelUD(trans.coor[,1], h="href",
grid=500, same4all=FALSE) )
#############################################################################################
#### Data Formating ####
# Subset lat/lon from data
# Format lat/lon using coordinates()
# Use SpatialPoints to convert lat/lon data for CRS conversion
# Transform lat/lon to x/y (meters) using spTransform and epsg:3857 (pseudo-mercator)
# This is because the physical distance of 1 degree of lat =/= 1 degree of lon
# If not transfored, overlap will be calcuated incorrectly
# pull lat/lon
XY <- data[,c(3,4)]
xy <- coordinates(XY)
cord.dec = SpatialPoints(cbind(XY$x, XY$y), proj4string = CRS("+proj=longlat"))
# transform to X/Y
xyz <- spTransform(cord.dec, CRS("+init=epsg:3857"))
# pull transformed X and Y
xy <- coordinates(xyz)
colnames(xy) <- c("X", "Y")
# First need to format data
# Should be coordinates, metadata (in this case an ID)
dataXY <- SpatialPointsDataFrame(xy, data[,1:2])
# Calculate KDE
# The greater the grid value, the more percise the KDEs will be
# Think of the grid number as relating to the pixel number (ie., 500 x 500)
# Just be careful because setting the grid value too high can cause
# the model to take forever to run. Too low and it will look pixelated
kud <- kernelUD(dataxyz, h="href", grid = 500)
这是代码中出现错误的地方,之后其余部分将无法正常运行。该错误表明我至少需要 5 次搬迁。我不知道我做错了什么或我错过了什么。其余代码如下:
# Plot all data
image(kud)
# Calculate overlap, adjust percent and method to make most appropriate
# May need to read up a little on the different method types and what their
# outputs mean as they give slightly different results
# method "HR" is the default
# Grid number is the same as above, the higher the number, the more
# percise the measurment can be
kerneloverlap(dataXY[,1], meth="HR", percent = 95, grid = 500)
# To view individual home ranges
# To specify the ID change the number in kud[[]]
# Make sure to update the percent to match the overlap function
c1 <- getverticeshr(getvolumeUD(kud[[1]]), percent = 50, standardize=TRUE)
image(kud[[1]])
lines(c1)
# Calculate overlap, adjust percent and method to make most appropriate
# May need to read up a little on the different method types and what their
# outputs mean as they give slightly different results
# method "HR" is the default
kerneloverlap(dataXY[,1], meth="HR", percent = 50)
#### Exporting KDEs and HRs For Mapping ####
# Will need to run this code for each individual ID
# Converts each tarpon KUD into a raster with a defined CRS
r1 <- raster(as(kud[[1]], "SpatialPixelsDataFrame"))
crs(r1) <- "+init=epsg:3857"
writeRaster(r1, filename = "r1.tif", overwrite=TRUE)
# Exports home ranges as contour lines
# Home range for first ID
c1 <- getverticeshr(getvolumeUD(kud[[1]]), percent=95, standardize=TRUE)
proj4string(c1) <- CRS("+init=epsg:3857")
shapefile(c1, filename='c1.shp', overwrite=TRUE)