mablab / sftrack

sftrack: Modern classes for tracking and movement data
https://mablab.org/sftrack/
Other
53 stars 2 forks source link

Export as_sftraj object to shapefile #36

Closed fRIdanwismer closed 3 years ago

fRIdanwismer commented 3 years ago

Export as_sftraj object to shapefile using the wt_write() function. Currently there is a GDAL Error 1: Attempt to write non-linestring (POINT) geometry to ARC type shapefile.

basille commented 3 years ago

The problem here is that the geometry column of an sftraj object contains mixed geometry types (POINTs and LINESTRINGs because of incomplete steps), and GDAL is not able to handle this. This issue is actually well beyond the scope of sftrack, and possibly lies within sf (the geometry column of sftrack/sftraj objects are standard sfc columns).

One workaround is actually to remove all incomplete steps, and then export the trajectory with only valid steps. Here is a reproducible example. First create an sftraj object:

library("sftrack")
data(raccoon)
raccoon$timestamp <- as.POSIXct(raccoon$timestamp, tz = "EST5EDT")
my_sftrack <- as_sftrack(
  data = raccoon,
  coords = c("longitude","latitude"),
  time = "timestamp",
  group = "animal_id",
  crs = "+init=epsg:4326")
my_sftraj <- as_sftraj(my_sftrack)
head(my_sftraj)

Then subset and export:

sub_sftraj <- subset(my_sftraj, is_linestring(geometry))
head(sub_sftraj)
sf::st_write(sub_sftraj, "sub_traj.shp")

Note that the actual warnings are important:

1: In clean_columns(as.data.frame(obj), factorsAsCharacter) :
  Dropping column(s) sft_group of class(es) c_grouping
2: In CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options),  :
  GDAL Message 6: Field timestamp create as date field, though DateTime requested.
  1. The grouping column (sft_group) is ommited as GDAL cannot deal with it properly. Use group_labels if you want to have a textual representation of the groups before writing to a shapefile.
  2. Also the actual time is lost, and only the date is kept. I'm not sure why (again beyond sftrack).