1

I am trying to export polygons that i have created for home range data but when i go to export them get this error message:

Error in writeOGR(colkud95[1, ], ".", "colonyhomerange", driver = "ESRI Shapefile") : 
  Spatial Polygons objects should have valid ordering comments
use rgeos::createSPComment()
# main package for this analysis
library(adehabitatHR)  
library(sp)
library(rgdal)

tracks.utm <- dput(new("SpatialPointsDataFrame", data = structure(list(TripID = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1), `Breeding stage` = c(1, 1, 1, 1, 
1, 1, 1, 1, 1, 1)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")), coords.nrs = 1:2, coords = structure(c(174.78327, 
174.78326, 174.78332, 174.78323, 174.78325, 174.78277, 174.78238, 
174.78238, 174.78244, 174.78278, -41.09482, -41.09491, -41.09484, 
-41.09491, -41.09496, -41.09564, -41.09637, -41.09685, -41.09747, 
-41.09771), .Dim = c(10L, 2L), .Dimnames = list(NULL, c("x", 
"y"))), bbox = structure(c(174.78238, -41.09771, 174.78332, -41.09482
), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"), c("min", "max"
))), proj4string = new("CRS", 
projargs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs")))

# Organising variable 1 refers to Trip ID
colkud<-kernelUD(tracks.utm[,1],h="href", 
                 grid=1000,same4all=T)

# Organising variable 2 refers to Breeding stage
colkudb<-kernelUD(tracks.utm[,2],h="href", 
                  grid=1000,same4all=T)

kernel.area(colkud,percent=50)
kernel.area(colkudb,percent=50)
kernel.area(colkud,percent=95)
kernel.area(colkudb,percent=95)

colkud95<- getverticeshr(colkud, percent=95)
colkud50<- getverticeshr(colkud, percent=50)
colkud95b<- getverticeshr(colkudb, percent=95)
colkud50b<- getverticeshr(colkudb, percent=50)

# This is where the error occurs
writeOGR(colkud95,".",layer="colkud95",driver="ESRI Shapefile")

I haven't had any luck searching for an answer to this, my aim is to create polygons for home range and then export them as a shapefile to be used in QGIS.

3
  • 1
    You should try and make this a bit more reproducible. You've given us a sample of what I guess is HRtracks but then switch to tracks.utm which I guess is a transformed version of HRtracks? If you can add the transform line and get rid of as many irrelevant lines as possible, ie anything that doesn't contribute to constructing colkud95, that would make this a bit cleaner, leaner, and clearer - the fewer lines of code people think they have to run, the more likely they are to help!
    – Spacedman
    Jun 3, 2021 at 11:49
  • I've just tried the example given in the help for getverticeshr and it works, so its more likely to be something dependent on your data.
    – Spacedman
    Jun 3, 2021 at 12:37
  • Please note that your dput object was incorrect. This is the second time that I fixed the same object in one of your posts. If you continue to use this object in creating a reproducible example, please use these changes so that you have a resulting object for use in the example. We I edited your post, I also removed spurious code ie., removed reference to HRtracks since you never use the object and we do not have access to the data. Jun 3, 2021 at 14:54

1 Answer 1

2

First, check that you have a valid object to export to a shapefile in the first place. Your example is not reproducible and returns an error:

The grid is too small to allow the estimation of home-range

As I recall, depending on how the functions are parametrized, it is possible for adehabitatHR::getverticeshr to return a SpatialPolygons object, which lacks attributes and do not reflect the output in the functions example. You can check output using class(colkud95). For writing a shapefile, you are after a SpatialPolygonsDataFrame, if the object is a SpatialPolygons type you will need to convert it to an exportable object.

If you have a spatial object with attributes, I highly recommend using sf, as opposed to rgdal, to write the shapefile. It is faster and the syntax is a bit easier. You will have to coerce your sp object to sf but, that is easily done on the fly using as. The function st_write parses the correct format based on the file extension.

sf::st_write(as(colkud95, "sf"), "out_shpfile.shp")

Please note that if I increase the grid size to 2500, I can get your example to run but, the largest volume contour I can produce is 75%, which I find odd because if it can create a KDE it should be able to return all of the volume contours for the density estimate. The output is, in fact, a SpatialPolygonsDataFrame object. So, you really need to look closely at your output(s) as there is likely something invalid.

colkud <- kernelUD(tracks.utm[,1],h="href", grid=2500, 
                   same4all=TRUE)
  kernel.area(colkud, percent=50)
  kernel.area(colkud, percent=75)
  colkud50<- getverticeshr(colkud, percent=50)
  colkud75 <- getverticeshr(colkud, percent=75)

Here is a workaround using the raster class and directly calculating the percent volume(s) using spatialEco::raster.vol. In this example, we use adehabitatHR::estUDm2spixdf to create a KDE raster of an individual animal, calculate the 75% volume contour then, plot the result to compare with the result from adehabitatHR::kernel.area and adehabitatHR::getverticeshr. You can convert each raster volume contour to a polygon and then export to shapefiles.

library(raster)
library(spatialEco)

colkud.kde <- raster(adehabitatHR::estUDm2spixdf(colkud))
  kde75 <- raster.vol(colkud.kde, p = 0.75)
  colkud75 <- getverticeshr(colkud, percent=75)
    plot(kde75)
      plot(colkud75, add=TRUE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.