writeShapefile producing invalid geometries

168 views
Skip to first unread message

Brittany B

unread,
Aug 9, 2022, 4:19:59 PM8/9/22
to ctmm R user group
Hello! First of all, I want to thank you so much for incorporating the ability to implement hard boundaries within the akde function in this version of ctmm. Everything is working beautifully for a species of bass I am working with in a riverine environment, but when I export the 95% UD polygons I get invalid/self-intersecting geometries in the "high" estimate (see attached image). Any quick fixes for this issue?

Best,

Brittany
invalid_geometries.JPG)

Christen Fleming

unread,
Aug 10, 2022, 8:48:41 AM8/10/22
to ctmm R user group
Hi Brittany,

Weird. When you plot the contours in ctmm, do they look messed up like this, or is it only after exporting? In either case, if you can send me a minimal working example (data + script), then I can take a look at it.

Best,
Chris

Brittany B

unread,
Aug 10, 2022, 9:34:39 AM8/10/22
to ctmm R user group
Sure thing--they look great when plotted in ctmm, and the errors only occur once exported. I imported the polygon boundary layer as a shapefile--can I send that over to you via e-mail? 

Here's data from one tag:

LotekID    Lat    Long    DateTime
58    27.40528    -81.15264    2021/02/08 14:40:00+00
58    27.41154    -81.14277    2021/02/18 14:48:00+00
58    27.41135    -81.14317    2021/03/04 00:00:00+00
58    27.41168    -81.14264    2021/03/08 16:07:00+00
58    27.41178    -81.14234    2021/03/15 16:01:00+00
58    27.41044    -81.1447    2021/03/22 15:59:59+00
58    27.41235    -81.14223    2021/03/30 15:23:00+00
58    27.41007    -81.14509    2021/04/05 15:57:00+00
58    27.4119    -81.14245    2021/04/23 15:30:00+00
58    27.41071    -81.1391    2021/06/03 11:31:00+00

Script:

locs_xy<-locs_df[c("Long","Lat")]
coordinates(locs_xy)<-~Long+Lat
proj4string(locs_xy)<-CRS("+init=epsg:4326")
ref<-CRS(SRS_string = "EPSG:32617")
locs_utm<-spTransform(locs_xy, CRSobj=ref)
locs_utm_spdf<-SpatialPointsDataFrame(locs_utm, data=locs_df)
locs_utm_spdf$Name<-as.factor(locs_utm_spdf$LotekID)
locs_utm_spdf$hdop<-1 #locations were typically exact with confirmed visual sighting

ref<-CRS("+init=epsg:32617")
GPS.HDOP<-as.data.frame(locs_utm_spdf$hdop)
hdop<-as.data.frame(locs_utm_spdf$GPS.HDOP)
names(GPS.HDOP)[1]<-"GPS.HDOP"
date<-as.POSIXct(locs_utm_spdf$DateTime)
xy<-as.data.frame(cbind(locs_utm_spdf$Long,locs_utm_spdf$Lat))
ids<-as.factor(locs_utm_spdf$Name)
trajectories<-as.ltraj(xy=xy, date=date, id=ids, typeII = T, infolocs=GPS.HDOP)

# Create Move object
locs_move<-move(trajectories)
proj4string(locs_move)<-"+proj=utm +zone=17 +datum=WGS84 +units=m +no_defs"

# Create telemetry object
locs_telem<-as.telemetry(locs_move, keep = FALSE, projection="+proj=utm +zone=17 +datum=WGS84 +units=m +no_defs")

## Setting the RMS UERE
uere(locs_telem)<-1
L58<-locs_telem$X58_58
SVF<-variogram(L58)
SVf_fit<-variogram.fit(SVF) #Interactive fit, make sure to check error box for irregular sampling
L58_sel<-ctmm.select(L58, CTMM=GUESS, IC="AICc", verbose=TRUE)
L58_fit<-ctmm.fit(L58, GUESS)
L58_akde<-akde(L58, L58_fit, SP=SP1_spdf, SP.in=T,weights=T, grid=2,debias = T, smooth=T)
writeShapefile(L58_akde, ".",level.UD=0.95, level=0.95, "L58", overwrite=T)


Thank you for looking into this!

Best,

Brittany

Christen Fleming

unread,
Aug 10, 2022, 10:40:34 AM8/10/22
to ctmm R user group
Yes, please send me everything via email.

Best,
Chris

Christen Fleming

unread,
Aug 10, 2022, 12:27:22 PM8/10/22
to ctmm R user group
Hi Brittany,

You are calculating an AKDE with a model from ctmm.fit rather than ctmm.select. The ctmm.fit model is far from the selected model and has extremely wide confidence intervals, which cause some coverage area polygons with <4 vertices, which sp doesn't like (I tried removing those, but sp just gave a different error instead). If you use the result of ctmm.select instead, it looks fine after exporting.

If this happens on a selected model where it matters, then let me know and I'll see if using sf instead will prevent this.

Best,
Chris
Reply all
Reply to author
Forward
0 new messages