Learn R Programming

Another spatial library?

Yep. In a few of my other libraries I’ve made use of sf objects, but without importing the sf library itself. This is by design because sf is quite a ‘heavy’ library.

Therefore I’ve written / copied these methods for constructing the sf objects across a few different libraries.

So I thought it would be useful to have these in one place. And here they are.

Does it really make sf objects?

Yes and No. 

These functions do not perform any validity checks on the geometries. Nor do they set Coordinate Reference Systems, EPSG, PROJ4 or precision attributes.

What they do is convert R and Rcpp objects (vectors, matrices and data.frames) into the correct sf class structure, so you can then assign these values yourself.

Are there any catches?

Yes. Your data has to be ordered before coverting to sf.

Where are the examples?

They’re on the website. GO NOW!

(however, here’s a taster)


df <- data.frame(
  id = c(1,1,1,1,1,2,2,2,2,2)
  , x = c(1,2,2,1,1,3,4,4,3,3)
  , y = c(1,1,2,2,1,3,3,4,4,3)
)

df$val <- letters[df$id]

sfheaders::sf_linestring( df, x = "x", y = "y", linestring_id = "id", keep = TRUE )

# Simple feature collection with 2 features and 2 fields
# geometry type:  LINESTRING
# dimension:      XY
# bbox:           xmin: 1 ymin: 1 xmax: 4 ymax: 4
# epsg (SRID):    NA
# proj4string:    NA
#   id val                       geometry
# 1  1   a LINESTRING (1 1, 2 1, 2 2, ...
# 2  2   b LINESTRING (3 3, 4 3, 4 4, ...

sfheaders::sf_polygon( df, x = "x", y = "y", polygon_id = "id" , keep = TRUE )

# Simple feature collection with 2 features and 2 fields
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: 1 ymin: 1 xmax: 4 ymax: 4
# epsg (SRID):    NA
# proj4string:    NA
#   id val                       geometry
# 1  1   a POLYGON ((1 1, 2 1, 2 2, 1 ...
# 2  2   b POLYGON ((3 3, 4 3, 4 4, 3 ...

Copy Link

Version

Install

install.packages('sfheaders')

Monthly Downloads

38,919

Version

0.4.4

License

MIT + file LICENSE

Maintainer

David Cooley

Last Published

January 17th, 2024

Functions in sfheaders (0.4.4)

sfc_cast

sfc cast
sfg_multipolygon

sfg multipolygon
sfc_polygon

sfc POLYGON
sfc_point

sfc POINT
sfg_multilinestring

sfg multilinestring
sfg_multipoint

sfg multipoint
sfg_point

sfg point
sfc_multipoint

sfc MULTIPOINT
sfc_multipolygon

sfc MULTIPOLYGON
sfc_to_df

sfc to df
sfg_to_df

sfg to df
sfg_linestring

sfg linestring
sfg_polygon

sfg polygon
sf_multilinestring

sf MULTILINESTRING
sf_mline

Helper for sf MULTILINESTRING
sf_mpoly

Helper for sf MULTIPOLYGON
sf_line

Helper for sf LINESTRING
sf_multipoint

sf MULTIPOINT
sf_cast

sf cast
sf_bbox

sf bbox
sf_boxes

sf boxes
sf_mpt

Helper for sf MULTIPOINT
sf_linestring

sf LINESTRING
sf_poly

Helper for sf POLYGON
sf_to_df

sf to df
sfc_linestring

sfc LINESTRING
sf_point

sf POINT
sf_remove_holes

remove holes
sfc_multilinestring

sfc MULTILINESTRING
sf_multipolygon

sf MULTIPOLYGON
sf_pt

Helper for sf POINT
sf_polygon

sf POLYGON