[AniMov] Exporting kernel home range utilization distribution to an ESRI shapefile

Corrie Curtice cc157 at duke.edu
Wed Dec 2 02:05:07 CET 2009


Thanks, that worked well. I modified it slightly to use the rgdal
library for exporting the shapefile, which seems more robust and up to
date than the shapefile library (according to folks on the r-sig-geo
list).  Maybe the wiki can be updated to include the exportKDE
function for others to use? I have yet to update a wiki, but if it's
open and ok to do this, I'm happy to contribute.

I also updated the kver2spol function so that I can pass in the
proj4string, so that the exported shapefile has the projection
information included. I'm sure it can be improved, but maybe this type
of change can be included in the next adehabitat release?

I've put both updated functions below.

#######
# OVERRIDE the default kver2spol function so that we can include the
projection info
#######
kver2spol <- function(kv,projstr)
{
    x <- kv
    if (!inherits(x, "kver"))
        stop("x should be of class \"kver\"")
    if (!require(sp))
        stop("sp package needed")
    lipols <- lapply(1:length(x), function(i) {
        y <- x[[i]]
        class(y) <- c("data.frame", "list")
        res <- split(y[, 2:3], y[, 1])
        lipol <- lapply(res, function(z) {
            if (sum(abs(z[1, ] - z[nrow(z), ])) > 1e-16)
                z <- rbind(z, z[1, ])
            Polygon(as.matrix(z))
        })
        pols <- Polygons(lipol, ID = names(x)[i])
        return(pols)
    })
    return(SpatialPolygons(lipols, proj4string=CRS(as.character(projstr))))
}

#############
# Function to export specific levels of isopleths of a "kv" object
#############
exportKDE <- function(ud,fileName,dirName){

library (rgdal)

kv<-list()
class(kv) <- "kver"

kvtmp <- getverticeshr(ud, lev = 99)
kv$KHR99<- kvtmp[[1]]
kvtmp <- getverticeshr(ud, lev = 95)
kv$KHR95<- kvtmp[[1]]
kvtmp <- getverticeshr(ud, lev = 90)
kv$KHR90<- kvtmp[[1]]
kvtmp <- getverticeshr(ud, lev = 75)
kv$KHR75<- kvtmp[[1]]
kvtmp <- getverticeshr(ud, lev = 50)
kv$KHR50<- kvtmp[[1]]
kvtmp <- getverticeshr(ud, lev = 25)
kv$KHR25<- kvtmp[[1]]

spolTmp <- kver2spol(kv,"+proj=eqdc +ellps=WGS84 +lon_0=105w
+lat_0=19n +lat_1=22n +lat_2=19n +units=m +datum=WGS84")
dfTmp <- data.frame(Isopleth=c("99","95","90","75","50","25"),row.names=c("KHR99","KHR95","KHR90","KHR75","KHR50","KHR25"))
spdfTmp <- SpatialPolygonsDataFrame(spolTmp, dfTmp, match.ID = TRUE)
writeOGR(spdfTmp,dirName,fileName, "ESRI Shapefile")
}


Cheers,

--corrie


On Wed, Nov 25, 2009 at 1:35 PM, Mathias Tobler <mtlist at gmx.net> wrote:
> Hi Corrie,
>
> here some code I wrote to export different home range contours to a ESRI
> shapefiles. It currently does the 99, 95, 75, 50 and 25% level but you can
> easily change that to your needs. The parameter name is just the file name
> so the code bellow will create the following shapefile:
> C:/Shapefiles/HR/Animal1_KHR500.shp Please let me know if you have any
> quesitons.
>
> Best,
>
> Mathias
>
> exportshape<-function(ud,name,dir){
>
> library(shapefiles)
>
> kv<-list()
> class(kv) <- "kver"
>
> kvtmp <- getverticeshr(ud, lev = 99)
> kv$KHR99<- kvtmp[[1]]
>
> kvtmp <- getverticeshr(ud, lev = 95)
> kv$KHR95<- kvtmp[[1]]
>
> kvtmp <- getverticeshr(ud, lev = 75)
> kv$KHR75<- kvtmp[[1]]
>
> kvtmp <- getverticeshr(ud, lev = 50)
> kv$KHR50<- kvtmp[[1]]
>
> kvtmp <- getverticeshr(ud, lev = 25)
> kv$KHR25<- kvtmp[[1]]
>
>
> shp<-kver2shapefile(kv)
> filename<-paste(dir,name, sep="")
> write.shapefile(shp,filename,arcgis=TRUE)
>
> }
>
> ud <- kernelUD(xy, id, grid=600,ker="bivnorm", h=500)
> exportshape(ud,"Animal1_KHR500","C:/Shapefiles/HR/")
>
>
>
>
>
> At 10:17 AM 11/24/2009, Corrie Curtice wrote:
>
> Hello,
>
> Is there a straightforward method for exporting a kernel home range
> utilization distribution produced by kernelUD to an ESRI shapefile?  I
> have the following code:
>
> xy<-Turtles[,c("x","y")]
> id<-Turtles$Beach
> kud<-kernelUD(xy,id)
>
> I get a nice picture of the kernel home range utilization distribution
> with contours.  I want to create my maps in ArcMap (I'm not that good
> with maps in R yet), so I'd like to export each homerange UD as an
> ESRI shapefile, keeping the contour information so I can color ramp it
> appropriately and do some analysis in ArcMap.   This is what I think
> I'm supposed to do, from reading
> https://wiki.faunalia.it/dokuwiki/doku.php?id=public:animove_howto :
>
> kver<-getverticeshr(kud)
> spol <- kver2spol(kver)
> df <- data.frame(Beach="Baja",row.names=("Agua Blanca, Baja California
> Sur"))
> spdfBaja <- SpatialPolygonsDataFrame(spol, df, match.ID = TRUE)
> writeOGR(spdfBaja,paste(td,"R-Output/Shapefiles",sep=""),"bajaUD","ESRI
> Shapefile")
>
> But I think this only gets me one contour level (95%), right?  Once I
> get that into ArcMap it's just a single polygon. It doesn't look like
> getverticieshr takes a list of levels, just one. Do I need to repeat
> this and glue them together for all the levels? Is there a different /
> better way to do this?
>
> Also, somewhat related, I'd like to get my projection information into
> the SpatialPolygon object, but it's created in the kver2spol
> function... is there a way to set the CRS still?
>
> Thanks for any help or pointers.
>
> Corrie
> --
> Corrie Curtice
> Research Associate
> Marine Geospatial Ecology Lab
> Nicholas School of the Environment, Duke University
> http://mgel.env.duke.edu
> em: corrie.curtice at duke.edu
> _______________________________________________
> AniMov mailing list
> AniMov at faunalia.it
> http://lists.faunalia.it/cgi-bin/mailman/listinfo/animov


More information about the AniMov mailing list