# Linkage to the sf package

The `sf` package delivers a standardized way to encode spatial vector data, the so called Simple Features (SF). It binds to GDAL for reading and writing data, to GEOS for geometrical operations, and to Proj.4 for projection conversions and datum transformations. Therefore the `eRTG3D` package comes with some wrapper functions to convert the generated tracks to simple features. The resulting `sf, data.frame` objects enable access to the functionality of the sf package, which also includes an easy way to access spatial databases (PostGIS).

## Convert to sf

To convert tracks to a an object of type `sf, data.frame` use the `track2sf.3d()` function, which can take data.frames, matrices and move objects (containing one track!) as input. In case of data.frames and matrices, it is important that the coordinates are stored in the first three rows.

    LV95 <- "+init=epsg:2056"
    niclas <- track2sf.3d(niclas, CRS = LV95)
    is.sf.3d(niclas)
    #> [1] TRUE
    head(niclas, 3)
    #> Simple feature collection with 3 features and 5 fields
    #> geometry type:  POINT
    #> dimension:      XYZ
    #> bbox:           xmin: 2556476 ymin: 1188336 xmax: 2560477 ymax: 1189861
    #> epsg (SRID):    2056
    #> proj4string:    +proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs
    #>           a        g           t           l        d
    #> 1 0.3914766 1.557032          NA          NA       NA
    #> 2 0.4313395 1.537826  0.03986289 -0.01920514 2300.818
    #> 3 0.2866134 1.565712 -0.14472610  0.02788554 1993.003
    #>                         geometry
    #> 1 POINT Z (2556476 1188336 12...
    #> 2 POINT Z (2558565 1189297 13...
    #> 3 POINT Z (2560477 1189861 13...

## Convert from sf

To test if a track is of type `sf, data.frame` use the `is.sf()` function, and to convert a `sf, data.frame` back to a track data.frame use `sf2df.3d()`.

    niclas <- sf2df.3d(niclas)
    is.sf.3d(niclas)
    #> [1] FALSE