There are two ways you can use the C++ code

  1. creating a Rcpp::cppFunction()
  2. Linking to sfheaders in a package

The first option works, but is a bit pointless because if you’re in R anyway you would probably just call the R functions.

The second option is the whole reason I build this library!

Linking to a package

The reason all the C++ code is in header files in inst/include is so you can call this code from another library. To do so

  1. Set the LinkingTo

For example, here’s the Description file of the gpx package I’m working on.

LinkingTo

  1. Include the sfheaders header files you need in a .cpp file

include

  1. call the functions

functions

These screenshots are taken from my gpx library. Feel free to have a look and see how I use them.

Using cppFunction

Here are some example cppFunction() calls you can use to make various sf objects

sfg_POINT


cppFunction(
  includes = '
    #include "sfheaders/sfg/sfg.hpp"
  ',
  code = '
    SEXP a_point( SEXP x ) {
      return sfheaders::sfg::sfg_point( x );
    }
  '
  , plugins = "cpp11"
  , depends = "sfheaders"
)

## vector
a_point( c(1,2) )
#  POINT (1 2)
## single-row matrix
a_point( matrix(c(1,2), ncol = 2) )
#  POINT (1 2)
## single-row data.frame
a_point( data.frame(x =1, y = 2) )
#  POINT (1 2)

sfc_MULTIPOINT


cppFunction(
  includes = '
    #include "sfheaders/sfc/multipoint/sfc_multipoint.hpp"
  ',
  code = '
    SEXP a_multipoint( SEXP x ) {
      return sfheaders::sfc::sfc_multipoint( x );
    }
  '
  , plugins = "cpp11"
  , depends = "sfheaders"
)

x <- 1:2
a_multipoint( x )
#  Geometry set for 1 feature 
#  geometry type:  MULTIPOINT
#  dimension:      XY
#  bbox:           xmin: 1 ymin: 2 xmax: 1 ymax: 2
#  z_range:        zmin: NA zmax: NA
#  m_range:        mmin: NA mmax: NA
#  epsg (SRID):    NA
#  proj4string:    NA
#  MULTIPOINT (1 2)

x <- matrix(c(1,2,3,4,5,6), ncol = 2)
a_multipoint( x )
#  Geometry set for 1 feature 
#  geometry type:  MULTIPOINT
#  dimension:      XY
#  bbox:           xmin: 1 ymin: 4 xmax: 3 ymax: 6
#  z_range:        zmin: NA zmax: NA
#  m_range:        mmin: NA mmax: NA
#  epsg (SRID):    NA
#  proj4string:    NA
#  MULTIPOINT (1 4, 2 5, 3 6)

x <- data.frame(x = 1:3, y = 4:6)
a_multipoint( x )
#  Geometry set for 1 feature 
#  geometry type:  MULTIPOINT
#  dimension:      XY
#  bbox:           xmin: 1 ymin: 4 xmax: 3 ymax: 6
#  z_range:        zmin: NA zmax: NA
#  m_range:        mmin: NA mmax: NA
#  epsg (SRID):    NA
#  proj4string:    NA
#  MULTIPOINT (1 4, 2 5, 3 6)

sf_LINESTRING


cppFunction(
  includes = '
    #include "sfheaders/sf/linestring/sf_linestring.hpp"
  ',
  code = '
    SEXP a_linestring( SEXP x, SEXP geometry_columns, SEXP id_column ) {
      return sfheaders::sf::sf_linestring( x, geometry_columns, id_column );
    }
  ',
  plugins = "cpp11",
  depends = "sfheaders"
)

x <- 1:2
a_linestring( x, NULL, NULL )
#  Simple feature collection with 1 feature and 1 field
#  geometry type:  LINESTRING
#  dimension:      XY
#  bbox:           xmin: 1 ymin: 2 xmax: 1 ymax: 2
#  z_range:        zmin: NA zmax: NA
#  m_range:        mmin: NA mmax: NA
#  epsg (SRID):    NA
#  proj4string:    NA
#    id         geometry
#  1  1 LINESTRING (1 2)

x <- matrix(c(1,2,3,4,5,6), ncol = 2)
a_linestring( x, NULL, NULL )
#  Simple feature collection with 1 feature and 1 field
#  geometry type:  LINESTRING
#  dimension:      XY
#  bbox:           xmin: 1 ymin: 4 xmax: 3 ymax: 6
#  z_range:        zmin: NA zmax: NA
#  m_range:        mmin: NA mmax: NA
#  epsg (SRID):    NA
#  proj4string:    NA
#    id                   geometry
#  1  1 LINESTRING (1 4, 2 5, 3 6)

x <- data.frame(x = 1:6, y = 4:9, id = c(rep(1,3), rep(2,3)))
a_linestring( x, NULL, "id" )
#  Simple feature collection with 2 features and 1 field
#  geometry type:  LINESTRING
#  dimension:      XY
#  bbox:           xmin: 1 ymin: 4 xmax: 6 ymax: 9
#  z_range:        zmin: NA zmax: NA
#  m_range:        mmin: NA mmax: NA
#  epsg (SRID):    NA
#  proj4string:    NA
#    id                   geometry
#  1  1 LINESTRING (1 4, 2 5, 3 6)
#  2  2 LINESTRING (4 7, 5 8, 6 9)

x <- data.frame(x = 1:6, y = 4:9, z = 8:10, id = c(rep(1,3), rep(2,3)))
a_linestring( x, c("x","y"), "id" )
#  Simple feature collection with 2 features and 1 field
#  geometry type:  LINESTRING
#  dimension:      XY
#  bbox:           xmin: 1 ymin: 4 xmax: 6 ymax: 9
#  z_range:        zmin: NA zmax: NA
#  m_range:        mmin: NA mmax: NA
#  epsg (SRID):    NA
#  proj4string:    NA
#    id                   geometry
#  1  1 LINESTRING (1 4, 2 5, 3 6)
#  2  2 LINESTRING (4 7, 5 8, 6 9)

a_linestring( x, c("x","y", "z"), "id" )
#  Simple feature collection with 2 features and 1 field
#  geometry type:  LINESTRING
#  dimension:      XYZ
#  bbox:           xmin: 1 ymin: 4 xmax: 6 ymax: 9
#  z_range:        zmin: 8 zmax: 10
#  m_range:        mmin: NA mmax: NA
#  epsg (SRID):    NA
#  proj4string:    NA
#    id                       geometry
#  1  1 LINESTRING Z (1 4 8, 2 5 9,...
#  2  2 LINESTRING Z (4 7 8, 5 8 9,...