df2sf: From ordinary data frame to sf to construct SFE object

View source: R/df2sf.R

df2sfR Documentation

From ordinary data frame to sf to construct SFE object

Description

While the SpatialFeatureExperiment constructor and *Geometry replacement methods can convert properly formatted ordinary data frames into sf objects which are used to store the geometries internally, the user might want to do the conversion, check if the geometry is valid, and inspect and fix any invalid geometries.

Usage

df2sf(
  df,
  spatialCoordsNames = c("x", "y"),
  spotDiameter = NA,
  geometryType = c("POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING",
    "MULTIPOLYGON"),
  BPPARAM = SerialParam()
)

Arguments

df

An ordinary data frame, i.e. not sf. Or a matrix that can be converted to a data frame.

spatialCoordsNames

Column names in df that specify spatial coordinates.

spotDiameter

Spot diameter for technologies with arrays of spots of fixed diameter per slide, such as Visium, ST, DBiT-seq, and slide-seq. The diameter must be in the same unit as the coordinates in the *Geometry arguments. Ignored for geometries that are not POINT or MULTIPOINT.

geometryType

Type of geometry to convert the ordinary data frame to. If the geometry in df is de facto points, then this argument will be ignored and the returned sf will have geometry type POINT. For any geometry type where one geometry is specified by multiple coordinates, the data frame df must have a column "ID" specifying which coordinate belongs to which geometry. For MULTI* geometries, there must be a "group" column specifying which coordinates for which MULTI geometry.

BPPARAM

An optional BiocParallelParam instance, passed to df2sf to parallelize the conversion of data frames with coordinates to sf geometries.

Value

An sf object.

Examples

# Points, use spotDiameter to convert to circle polygons
# This is done to Visium spots
pts_df <- readRDS(system.file("extdata/pts_df.rds",
    package = "SpatialFeatureExperiment"
))
sf_use <- df2sf(pts_df, geometryType = "POINT", spotDiameter = 0.1)
# Linestring
ls_df <- readRDS(system.file("extdata/ls_df.rds",
    package = "SpatialFeatureExperiment"
))
sf_use <- df2sf(ls_df, geometryType = "LINESTRING")
# Polygon
pol_df <- readRDS(system.file("extdata/pol_df.rds",
    package = "SpatialFeatureExperiment"
))
sf_use <- df2sf(pol_df,
    geometryType = "POLYGON",
    spatialCoordsNames = c("V1", "V2")
)
# Multipolygon
mpol_df <- readRDS(system.file("extdata/mpol_df.rds",
    package = "SpatialFeatureExperiment"
))
sf_use <- df2sf(mpol_df,
    geometryType = "MULTIPOLYGON",
    spatialCoordsNames = c("V1", "V2")
)
# Multiple sample_ids present
multipts_df <- readRDS(system.file("extdata/multipts_df.rds",
    package = "SpatialFeatureExperiment"
))
sf_use <- df2sf(multipts_df, geometryType = "MULTIPOINT")

pachterlab/SpatialFeatureExperiment documentation built on March 11, 2024, 11:11 p.m.