### sf_mline usage with matrix input Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_mline.html Example demonstrating the use of sf_mline with a simple matrix input. ```APIDOC ## Example: Matrix Input ```R m <- cbind(x = 0, y = 0, multilinestring_id = c(1, 1, 1), linestring_id = 1) sf_mline( m ) ``` ### Output ``` multilinestring_id geometry 1 1 0, 0, 0, 0, 0, 0 ``` ``` -------------------------------- ### sf_mline usage with data frame input Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_mline.html Example demonstrating the use of sf_mline with a data frame input, including x, y, z, and m coordinates. ```APIDOC ## Example: Data Frame Input ```R df <- data.frame( multilinestring_id = c(1,1,1,1,1,1,1,1,2,2,2,2,2) , linestring_id = c(1,1,1,2,2,3,3,3,1,1,1,2,2) , x = rnorm(13) , y = rnorm(13) , z = rnorm(13) , m = rnorm(13) ) sf_mline( obj = df) ``` ### Output ``` multilinestring_id 1 1 2 2 geometry ``` ``` -------------------------------- ### Calculate bounding box for linestrings Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_boxes.html Demonstrates how to use sf_boxes to get the bounding box of sf linestring objects. Requires the sfheaders package and a data frame with coordinates. ```R df <- data.frame( id1 = c(1,1,1,1,1,1,1,1,2,2,2,2) , id2 = c(1,1,1,1,2,2,2,2,1,1,1,1) , x = c(0,0,1,1,1,1,2,2,3,4,4,3) , y = c(0,1,1,0,1,2,2,1,3,3,4,4) ) sf_line <- sfheaders::sf_linestring( obj = df , x = "x" , y = "y" , linestring_id = "id1" ) sf_boxes( sf_line ) ``` -------------------------------- ### Create MultiLineString with X and Y Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_multilinestring.html This example demonstrates creating a MultiLineString using only x and y coordinate columns. The 'obj' parameter should be your data frame. ```r sf_multilinestring( obj = df, x = 3, y = 4) ``` -------------------------------- ### Create Polygon with X and Y Coordinates Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_polygon.html This example demonstrates creating a polygon using only x and y coordinates from a data frame. Specify the 'obj' and the 'x' and 'y' column names. ```r sf_polygon( obj = df, x = 2, y = 3) ``` -------------------------------- ### Create MULTIPOLYGON with polygon_id and linestring_id Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_multipolygon.html This example demonstrates creating a MULTIPOLYGON sfc object using both polygon_id and linestring_id to define the structure. The output shows the nested structure of the MULTIPOLYGON. ```R sfc_multipolygon( df, x = "x", y = "y", polygon_id = "id1", linestring_id = "id2") ``` -------------------------------- ### Create Polygon with X, Y, Z, and M Coordinates (Numeric Indices) Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_polygon.html This example shows how to create a polygon geometry using numeric indices for x, y, z, and m coordinates. Pass your data frame to 'obj' and the column indices to 'x', 'y', 'z', and 'm'. ```r sf_polygon( obj = df, x = 2, y = 3, z = 4, m = 5) ``` -------------------------------- ### Round-trip Multilinestring with Fill and Keep Options Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_mline.html This example shows how to perform a round-trip conversion of a multilinestring object using sf_mline with the `fill = TRUE` and `keep = TRUE` options. This is necessary to preserve all fields during the conversion process. ```R sf_mline(sf_to_df(sfx, fill = TRUE), keep = TRUE) ``` -------------------------------- ### Create Polygons with Numeric IDs Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_polygon.html This example shows how to use sf_polygon when the polygon and linestring IDs are numeric values instead of strings. The function correctly identifies and uses these numeric IDs to form polygons. ```r sf_polygon( obj = df, polygon_id = 1, linestring_id = 2 ) ``` -------------------------------- ### Create MULTIPOLYGON with only linestring_id Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_multipolygon.html This example shows how to create a MULTIPOLYGON sfc object when only a linestring_id is provided. The function groups points into linestrings based on this ID. ```R sfc_multipolygon( df, x = "x", y = "y", linestring_id = "id1") ``` -------------------------------- ### Create Multilinestring with XYZ Coordinates Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_mline.html This example shows how to create a multilinestring object when the input data frame contains XYZ coordinates, as opposed to XYM. Note the comment indicating this gives XYZ, not XYM. ```R (sfx <- sf_mline( obj = df[-5])) ``` -------------------------------- ### Create MULTIPOLYGON with only polygon_id Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_multipolygon.html This example demonstrates creating a MULTIPOLYGON sfc object using only the polygon_id. Each unique value in polygon_id will define a separate polygon. ```R sfc_multipolygon( df, x = "x", y = "y", polygon_id = "id2") ``` -------------------------------- ### Construct sfg MULTIPOLYGON with Z and M coordinates Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfg_multipolygon.html This example demonstrates how to construct an sfg MULTIPOLYGON object including both Z and M coordinates. Ensure your data frame has columns for both and specify their names using the 'z' and 'm' arguments. ```R df <- data.frame( polygon_id = c(rep(1, 5), rep(2, 10)) , line_id = c(rep(1, 10), rep(2, 5)) , x = c(0,0,1,1,0,2,2,5,5,2,3,3,4,4,3) , y = c(0,1,1,0,0,2,5,5,2,2,3,4,4,3,3) , z = c(1) , m = c(1) ) sfg_multipolygon( df, x = "x", y = "y", z = "z", m = "m", polygon_id = "polygon_id", linestring_id = "line_id" ) ``` -------------------------------- ### Convert MultiPoint to Polygon using sfc_cast Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_cast.html This example shows how to convert a multipoint geometry collection (sfc_multipoint) into a polygon geometry using sfc_cast. The output displays the resulting polygon coordinates. ```R sfc_cast( mpt, "POLYGON" ) #> [[1]] #> [[1]] #> [,1] [,2] #> [1,] 0 0 #> [2,] 0 1 #> [3,] 1 1 #> [4,] 1 0 #> [5,] 1 1 #> [6,] 1 2 #> [7,] 2 2 #> [8,] 2 1 #> [9,] 0 0 #> #> attr(,"class") #> [1] "XY" "POLYGON" "sfg" #> #> [[2]] #> [[1]] #> [,1] [,2] #> [1,] 3 3 #> [2,] 4 3 #> [3,] 4 4 #> [4,] 3 4 #> [5,] 3 3 #> #> attr(,"class") #> [1] "XY" "POLYGON" "sfg" #> #> attr(,"n_empty") #> [1] 0 #> attr(,"crs") #> $input #> [1] NA #> #> $wkt #> [1] NA #> #> attr(,"class") #> [1] "crs" #> attr(,"class") #> [1] "sfc_POLYGON" "sfc" #> attr(,"precision") #> [1] 0 #> attr(,"bbox") #> xmin ymin xmax ymax #> 0 0 4 4 #> attr(,"class") #> [1] "bbox" ``` -------------------------------- ### Create XYZ Polygon using Linestring ID from Data Frame Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfg_polygon.html Constructs an SFG polygon from a data frame, using a specified column for linestring IDs and inferring X and Y from other columns. This example also implicitly creates an XYZ geometry. ```R df <- data.frame( x = 1:12, y = 1:12, z = 13:24, id = c(rep(1,6), rep(2,6))) sfg_polygon( df, linestring_id = "id" ) ``` -------------------------------- ### Create LINESTRING from data frame with specified columns Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_linestring.html Construct a LINESTRING sfc object from a data frame by explicitly naming the x and y geometry columns. This example also demonstrates how the function handles data frames with an 'id' column. ```R x <- data.frame( id = 1:2, x = 1:2, y = 2:1 ) sfc_linestring( x, x = "x", y = "y" ) ``` -------------------------------- ### Construct sfg MULTIPOLYGON from data frame with explicit column names Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfg_multipolygon.html This example shows how to construct an sfg MULTIPOLYGON object from a data frame by explicitly naming the columns for x, y, polygon IDs, and linestring IDs. This provides more control over the geometry construction. ```R df <- data.frame( polygon_id = c(rep(1, 5), rep(2, 10)) , line_id = c(rep(1, 10), rep(2, 5)) , x = c(0,0,1,1,0,2,2,5,5,2,3,3,4,4,3) , y = c(0,1,1,0,0,2,5,5,2,2,3,4,4,3,3) , z = c(1) , m = c(1) ) sfg_multipolygon( df, x = "x", y = "y", polygon_id = "polygon_id", linestring_id = "line_id" ) ``` -------------------------------- ### Create sf_point specifying x and y columns Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Shows how to create point geometries when non-geometry columns exist, by explicitly specifying 'x' and 'y'. ```R df <- data.frame( id = c(1,1,2,2,3) , x = c(1,1,4,4,1) , y = c(1,4,4,1,1) ) df$val <- letters[ df$id ] sf_point( df, x = "x", y = "y" ) ``` -------------------------------- ### Create sf_linestring keeping all columns Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Demonstrates creating linestring geometries while retaining all original columns by setting 'keep = TRUE'. ```R df <- data.frame( id = c(1,1,2,2,3) , x = c(1,1,4,4,1) , y = c(1,4,4,1,1) ) df$val <- letters[ df$id ] sf_linestring( df, x = "x", y = "y", linestring_id = "id", keep = TRUE ) ``` -------------------------------- ### Create MULTILINESTRING with explicit object Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_multilinestring.html Construct a MULTILINESTRING by passing a matrix to the 'obj' argument of sfc_multilinestring. This example demonstrates creating a MULTILINESTRING with Z coordinates. ```R m <- matrix(c(0,0,0,0,0,1,0,1,1,1,2,2,1,2,3), ncol = 3, byrow = TRUE) sfc_multilinestring( obj = m ) ``` -------------------------------- ### Create sf_linestring specifying x, y, and linestring_id Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Illustrates creating linestring geometries by specifying 'x', 'y', and 'linestring_id' columns. ```R df <- data.frame( id = c(1,1,2,2,3) , x = c(1,1,4,4,1) , y = c(1,4,4,1,1) ) df$val <- letters[ df$id ] sf_linestring( df, x = "x", y = "y", linestring_id = "id" ) ``` -------------------------------- ### Convert sf MultiLineString to MultiPolygon Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_cast.html This example demonstrates casting an sf MULTILINESTRING object to a MULTIPOLYGON. This operation can be complex and depends on the input data structure. ```R df <- data.frame( id1 = c(1,1,1,1,1,1,1,1,2,2,2,2) , id2 = c(1,1,1,1,2,2,2,2,1,1,1,1) , x = c(0,0,1,1,1,1,2,2,3,4,4,3) , y = c(0,1,1,0,1,2,2,1,3,3,4,4) ) mls <- sf_multilinestring(obj = df, x = "x", y = "y", multilinestring_id = "id1") sf_cast( mls, "MULTIPOLYGON" ) ``` -------------------------------- ### Construct sf POINT with swapped coordinate columns Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_point.html Demonstrates how to assign columns to x and y coordinates in sf_point, even if they are not in the conventional order. ```R sf_point( obj = x, x = "y", y = "x" ) ``` -------------------------------- ### Filter MultiLineString by IDs Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_multilinestring.html This example shows how to filter MultiLineString objects by specific MultiLineString and LineString IDs. The function returns a data frame with the filtered geometries. ```r sf_multilinestring( obj = df, multilinestring_id = 1, linestring_id = 2 ) ``` -------------------------------- ### Construct MULTIPOINT from data frame (default columns) Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_multipoint.html When using a data frame, `sfc_multipoint` can infer 'x' and 'y' columns if they exist. This example shows default behavior. ```r x <- data.frame( id = 1:2, x = 1:2, y = 2:1 ) sfc_multipoint( x ) ``` -------------------------------- ### Create an XYZM Linestring sfg Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Demonstrates creating a linestring geometry with a measure using numeric coordinates. ```R sfg_linestring( 1:4 ) # [,1] [,2] [,3] [,4] # [1,] 1 2 3 4 # attr(,"class") # [1] "XYZM" "LINESTRING" "sfg" ``` -------------------------------- ### Convert sfg Polygon to data.frame Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfg_to_df.html Shows how to convert a polygon geometry (sfg) to a data.frame. This example involves creating a matrix for the polygon coordinates and specifying IDs for linestrings. ```R m <- cbind( matrix( 1:24, ncol = 2 ), c(rep(1, 6), rep(2, 6) ) ) sfg <- sfg_polygon( obj = m, x = 1, y = 2, linestring_id = 3 ) df <- sfg_to_df( sfg ) ``` -------------------------------- ### Convert sf MultiPolygon to LineString Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_cast.html This example shows how to cast an sf MULTIPOLYGON object to LINESTRING using sf_cast. This operation effectively extracts the boundary lines of the polygons. ```R df <- data.frame( id1 = c(1,1,1,1,1,1,1,1,2,2,2,2) , id2 = c(1,1,1,1,2,2,2,2,1,1,1,1) , x = c(0,0,1,1,1,1,2,2,3,4,4,3) , y = c(0,1,1,0,1,2,2,1,3,3,4,4) ) mp <- sf_multipolygon( obj = df , x = "x" , y = "y" , multipolygon_id = "id1" , linestring_id = "id2" , close = FALSE ) sf_cast( mp, "LINESTRING" ) ``` -------------------------------- ### Create an XY Point sfg Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Demonstrates creating a 2D point geometry using numeric coordinates. ```R sfg_point( 1:2 ) # [,1] [,2] # [1,] 1 2 # attr(,"class") # [1] "XY" "POINT" "sfg" ``` -------------------------------- ### Create an XYZ Point sfg Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Demonstrates creating a 3D point geometry using numeric coordinates. ```R sfg_point( 1:3 ) # [,1] [,2] [,3] # [1,] 1 2 3 # attr(,"class") # [1] "XYZ" "POINT" "sfg" ``` -------------------------------- ### Update Corrupted Bounding Box Attribute Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_bbox.html This example demonstrates how to update a corrupted bounding box attribute of an sf object using sf_bbox to recalculate the correct bounding box. ```R attr( mpt, "bbox" ) <- c(1:5) mpt ## incorrect values ``` ```R attr( mpt, "bbox" ) <- sf_bbox( mpt ) mpt ## back to correct values ``` -------------------------------- ### Construct sf POLYGON specifying x, y, and z columns Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_polygon.html Create a 3D POLYGON by specifying the x, y, and z coordinate columns from a matrix. ```R sf_polygon( obj = m, x = 1, y = 2, z = 3 ) ``` -------------------------------- ### sf_point() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Creates an sf POINT object. ```APIDOC ## sf_point() ### Description Creates an sf POINT object. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Performance benchmark: sfheaders vs data.table Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Compares the performance of sfheaders::sf_linestring against a data.table approach for creating linestrings from a large data frame. ```R n <- 1e5 df <- data.frame( id = rep(1:(n/5), each = 5) , x = rnorm(n) , y = rnorm(n) ) library(data.table) library(microbenchmark) dt <- as.data.table( df ) microbenchmark( dt = { sf <- dt[ , { geometry <- sf::st_linestring( x = matrix( c( x, y ), ncol = 2, byrow = T )) geometry <- sf::st_sf( geometry = sf::st_sfc( geometry ) ) } , by = id ] sf <- sf::st_as_sf( sf ) }, sfheaders = { sfh <- sfheaders::sf_linestring( obj = df , linestring_id = "id" ) }, times = 5 ) ``` -------------------------------- ### sf_linestring() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Creates an sf LINESTRING object. ```APIDOC ## sf_linestring() ### Description Creates an sf LINESTRING object. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Construct sf POLYGON with 3D coordinates Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_polygon.html Create a POLYGON with x, y, and z coordinates from a matrix. The matrix should be ordered correctly. ```R m <- matrix(c(0,0,0,0,0,1,0,1,1,1,2,2,1,2,3,1,3,4), ncol = 3, byrow = TRUE) sf_polygon( obj = m ) ``` -------------------------------- ### Create LINESTRING from data frame with XYZ coordinates Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_linestring.html This example shows how sfc_linestring can interpret a data frame with 'x', 'y', and 'z' columns to create a 3D LINESTRING sfc object. The function automatically infers the Z dimension. ```R x <- data.frame( id = 1:2, x = 1:2, y = 2:1 ) sfc_linestring( x ) ``` -------------------------------- ### Convert sfg Point to data.frame Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfg_to_df.html Demonstrates converting a simple point geometry (sfg) to a data.frame using sfg_to_df. Ensure the sfheaders package is loaded. ```R sfg <- sfg_point( obj = c(1,2) ) df <- sfg_to_df( sfg ) ``` -------------------------------- ### Convert sf object to data frame Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Shows how to convert an sf object (linestring in this case) back to a data frame using sf_to_df. ```R df <- data.frame( id = c(1,1,2,2,3) , x = c(1,1,4,4,1) , y = c(1,4,4,1,1) ) df$val <- letters[ df$id ] sf <- sf_linestring( df, x = "x", y = "y", linestring_id = "id", keep = TRUE ) sf_to_df( sf ) ``` -------------------------------- ### Create sfg_POINT with C++ Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/Cpp.html Use this C++ function to create an sfg_POINT object. It accepts vectors, matrices, or data frames as input. ```cpp cppFunction( includes = '#include "sfheaders/sfg/sfg.hpp"' , code = ' SEXP a_point( SEXP x ) { return sfheaders::sfg::sfg_point( x ); } ' , plugins = "cpp11" , depends = c("geometries", "sfheaders") ) ``` ```r a_point( c(1,3) ) # [,1] [,2] # [1,] 1 3 # attr(,"class") # [1] "XY" "POINT" "sfg" ## single-row matrix a_point( matrix(c(1,2), ncol = 2) ) # [,1] [,2] # [1,] 1 2 # attr(,"class") # [1] "XY" "POINT" "sfg" ## single-row data.frame a_point( data.frame(x = 1, y = 2) ) # [,1] [,2] # [1,] 1 2 # attr(,"class") # [1] "XY" "POINT" "sfg" ``` -------------------------------- ### Create sf LINESTRING from matrix Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_linestring.html Use this to create a basic LINESTRING sf object from a matrix of coordinates. The matrix should be ordered sequentially for each linestring. ```R x <- matrix( c(1:8), ncol = 2 ) sf_linestring( x ) ``` -------------------------------- ### sf_multipoint() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Creates an sf MULTIPOINT object. ```APIDOC ## sf_multipoint() ### Description Creates an sf MULTIPOINT object. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### sf_bbox() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Calculates the bounding box for spatial data. ```APIDOC ## sf_bbox() ### Description Calculates the bounding box for spatial data. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Create Data Frame and Geometry Objects Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_cast.html This code sets up a data frame and creates various simple feature geometry objects (points, multipoints, linestrings, multilinestrings, polygons, multipolygons) from it. These objects serve as input for the sfc_cast function. ```R df <- data.frame( id1 = c(1,1,1,1,1,1,1,1,2,2,2,2) , id2 = c(1,1,1,1,2,2,2,2,1,1,1,1) , x = c(0,0,1,1,1,1,2,2,3,4,4,3) , y = c(0,1,1,0,1,2,2,1,3,3,4,4) ) pt <- sfc_point(obj = df, x = "x", y = "y", z = "id1") mpt <- sfc_multipoint(obj = df, x = "x", y = "y", multipoint_id = "id1") ls <- sfc_linestring(obj = df, x = "x", y = "y", linestring_id = "id1") mls <- sfc_multilinestring(obj = df, x = "x", y = "y", multilinestring_id = "id1") p <- sfc_polygon( obj = df , x = "x" , y = "y" , polygon_id = "id1" , linestring_id = "id2" , close = FALSE ) mp <- sfc_multipolygon( obj = df , x = "x" , y = "y" , multipolygon_id = "id1" , linestring_id = "id2" , close = FALSE ) ``` -------------------------------- ### Create sf_point from data frame Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Demonstrates creating a simple point geometry from a data frame where 'id' is retained. ```R df <- data.frame( id = c(1,1,2,2,3) , x = c(1,1,4,4,1) , y = c(1,4,4,1,1) ) sf_point( df ) ``` -------------------------------- ### Construct MULTIPOINT from data frame (swapped columns) Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_multipoint.html Demonstrates how to specify different columns for 'x' and 'y' geometry when constructing a MULTIPOINT sfc object from a data frame. ```r sfc_multipoint( x, x = "y", y = "x" ) ``` -------------------------------- ### Round-trip sf MULTIPOINT using sf_mpt Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_mpt.html Demonstrates converting an sf MULTIPOINT object to a data frame and back using sf_mpt. ```R ## we trivially round-trip with sf_mpt() sf_mpt(sf_to_df(sfx)) ``` -------------------------------- ### sf_mpt() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Helper function for creating sf MULTIPOINT objects. ```APIDOC ## sf_mpt() ### Description Helper function for creating sf MULTIPOINT objects. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Create a simple polygon from a matrix Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_polygon.html Use a matrix of coordinates to construct a basic POLYGON sfc object. The input matrix should define the vertices of the polygon. ```R m <- matrix(c(0,0,0,0,1,1), ncol = 2 ) sfc_polygon( m ) ``` -------------------------------- ### sfc_point() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Creates an sfc POINT object. ```APIDOC ## sfc_point() ### Description Creates an sfc POINT object. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Create sf POINT from Matrix Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_pt.html Constructs an sf POINT object from a matrix with 'x' and 'y' columns. Handles matrices with 'z' coordinates as well. ```R x <- cbind(x = 1, y= 3) sf_pt( x ) ``` ```R sf_pt(cbind(x, z = 2)) ``` -------------------------------- ### Round-trip sf object with sf_line Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_line.html Demonstrates converting an sf object to a data frame and then back to an sf object using sf_line, preserving the LINESTRING geometries. ```r sf_line(sf_to_df(sfx)) ``` -------------------------------- ### sfg_point() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Creates an sfg POINT object. ```APIDOC ## sfg_point() ### Description Creates an sfg POINT object. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Create an XYZM Point sfg Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Demonstrates creating a 3D point geometry with a measure using numeric coordinates. ```R sfg_point( 1:4 ) # [,1] [,2] [,3] [,4] # [1,] 1 2 3 4 # attr(,"class") # [1] "XYZM" "POINT" "sfg" ``` -------------------------------- ### sf_pt() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Helper function for creating sf POINT objects. ```APIDOC ## sf_pt() ### Description Helper function for creating sf POINT objects. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### sf_line() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Helper function for creating sf LINESTRING objects. ```APIDOC ## sf_line() ### Description Helper function for creating sf LINESTRING objects. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Create an XY Linestring sfg from data frame Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Demonstrates creating a 2D linestring geometry from a data frame containing x and y coordinates. ```R df <- data.frame( x = c(1,1,4,4,1) , y = c(1,4,4,1,1) ) sfg_linestring(df) # [,1] [,2] # [1,] 1 1 # [2,] 1 4 # [3,] 4 4 # [4,] 4 1 # [5,] 1 1 # attr(,"class") # [1] "XY" "LINESTRING" "sfg" ``` -------------------------------- ### Convert Points to LineString using sfc_cast Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_cast.html Demonstrates converting a simple feature geometry collection of points (sfc_point) to a linestring geometry using sfc_cast. The output shows the resulting linestring coordinates. ```R sfc_cast( pt, "LINESTRING" ) #> [[1]] #> [,1] [,2] [,3] #> [1,] 0 0 1 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[2]] #> [,1] [,2] [,3] #> [1,] 0 1 1 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[3]] #> [,1] [,2] [,3] #> [1,] 1 1 1 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[4]] #> [,1] [,2] [,3] #> [1,] 1 0 1 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[5]] #> [,1] [,2] [,3] #> [1,] 1 1 1 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[6]] #> [,1] [,2] [,3] #> [1,] 1 2 1 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[7]] #> [,1] [,2] [,3] #> [1,] 2 2 1 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[8]] #> [,1] [,2] [,3] #> [1,] 2 1 1 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[9]] #> [,1] [,2] [,3] #> [1,] 3 3 2 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[10]] #> [,1] [,2] [,3] #> [1,] 4 3 2 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[11]] #> [,1] [,2] [,3] #> [1,] 4 4 2 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> [[12]] #> [,1] [,2] [,3] #> [1,] 3 4 2 #> attr(,"class") #> [1] "XYZ" "LINESTRING" "sfg" #> #> attr(,"n_empty") #> [1] 0 #> attr(,"crs") #> $input #> [1] NA #> #> $wkt #> [1] NA #> #> attr(,"class") #> [1] "crs" #> attr(,"class") #> [1] "sfc_LINESTRING" "sfc" #> attr(,"precision") #> [1] 0 #> attr(,"bbox") #> xmin ymin xmax ymax #> 0 0 4 4 #> attr(,"class") #> [1] "bbox" #> attr(,"z_range") #> zmin zmax #> 1 2 #> attr(,"class") #> [1] "z_range" ``` -------------------------------- ### Construct sf POINT from a matrix Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_point.html Provide a matrix to sf_point to create multiple POINT geometries. Each column is treated as a coordinate. ```R x <- matrix( c(1:10) , ncol = 2 ) sf_point( x ) ``` -------------------------------- ### Create sf_multipoint with multipoint_id Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Shows how to create multipoint geometries from a data frame, using the 'id' column to group points. ```R df <- data.frame( id = c(1,1,2,2,3) , x = c(1,1,4,4,1) , y = c(1,4,4,1,1) ) sf_multipoint( df, multipoint_id = "id" ) ``` -------------------------------- ### sfc_linestring() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Creates an sfc LINESTRING object. ```APIDOC ## sfc_linestring() ### Description Creates an sfc LINESTRING object. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Create MultiLineString with X, Y, Z, and M (Column Indices) Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_multilinestring.html This snippet shows how to create a MultiLineString using column indices for x, y, z, and m. This is useful when column names are not readily available or for positional referencing. ```r sf_multilinestring( obj = df, x = 3, y = 4, z = 5, m = 6 ) ``` -------------------------------- ### Create sf_linestring with linestring_id Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/examples.html Illustrates creating linestring geometries from a data frame, using the 'id' column for grouping. ```R df <- data.frame( id = c(1,1,2,2,3) , x = c(1,1,4,4,1) , y = c(1,4,4,1,1) ) sf_linestring( df, linestring_id = "id" ) ``` -------------------------------- ### sf_boxes Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_boxes.html Returns the bounding box of each geometry. ```APIDOC ## sf_boxes ### Description Returns the bounding box of each geometry. ### Arguments * `obj` (sf, sfc or sfg object) - The input geometry object. ### Example ```R df <- data.frame( id1 = c(1,1,1,1,1,1,1,1,2,2,2,2) , id2 = c(1,1,1,1,2,2,2,2,1,1,1,1) , x = c(0,0,1,1,1,1,2,2,3,4,4,3) , y = c(0,1,1,0,1,2,2,1,3,3,4,4) ) sf_line <- sfheaders::sf_linestring( obj = df , x = "x" , y = "y" , linestring_id = "id1" ) sf_boxes( sf_line ) ``` ``` -------------------------------- ### Round-trip sf POINT with Properties Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sf_pt.html Demonstrates a round-trip conversion: converting an sf object with properties back to a data frame and then creating a new sf POINT object, preserving the properties. ```R ## we trivially round-trip with sf_pt() sf_pt(sf_to_df(sfx, fill = TRUE), keep = TRUE) ``` -------------------------------- ### Create sf_LINESTRING with C++ Source: https://github.com/dcooley/sfheaders/blob/master/docs/articles/Cpp.html This C++ function constructs an sf_LINESTRING object. It requires input data, optionally geometry columns, and an ID column for grouping. ```cpp 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 = c("geometries", "sfheaders") ) ``` ```r x <- 1:2 a_linestring( x, NULL, NULL ) # id geometry # 1 1 1, 2 x <- matrix(c(1,2,3,4,5,6), ncol = 2) a_linestring( x, NULL, NULL ) # id geometry # 1 1 1, 2, 3, 4, 5, 6 x <- data.frame(x = 1:6, y = 4:9, id = c(rep(1,3), rep(2,3))) a_linestring( x, NULL, "id" ) # id geometry # 1 1 1, 2, 3, 4, 5, 6 # 2 2 4, 5, 6, 7, 8, 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" ) # id geometry # 1 1 1, 2, 3, 4, 5, 6 # 2 2 4, 5, 6, 7, 8, 9 a_linestring( x, c("x","y", "z"), "id" ) # id geometry # 1 1 1, 2, 3, 4, 5, 6, 8, 9, 10 # 2 2 4, 5, 6, 7, 8, 9, 8, 9, 10 ``` -------------------------------- ### sfc_multipoint() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Creates an sfc MULTIPOINT object. ```APIDOC ## sfc_multipoint() ### Description Creates an sfc MULTIPOINT object. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### sf_mline() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Helper function for creating sf MULTILINESTRING objects. ```APIDOC ## sf_mline() ### Description Helper function for creating sf MULTILINESTRING objects. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Create LINESTRING from data frame with swapped x and y columns Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_linestring.html Demonstrates creating a LINESTRING sfc object from a data frame where the 'x' and 'y' column roles are swapped. This highlights the flexibility in mapping data frame columns to geometry axes. ```R x <- data.frame( id = 1:2, x = 1:2, y = 2:1 ) sfc_linestring( x, x = "y", y = "x" ) ``` -------------------------------- ### sf_poly() Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/index.html Helper function for creating sf POLYGON objects. ```APIDOC ## sf_poly() ### Description Helper function for creating sf POLYGON objects. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Error Handling N/A ``` -------------------------------- ### Create sfc_LINESTRING with Z and M coordinates Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_linestring.html Constructs a LINESTRING sfc object from a data frame, specifying columns for X, Y, Z, M, and a linestring identifier. Requires a data frame with at least x, y, z, m, and id columns. ```R sfc_linestring(df, x = "x", y = "y", z = "z", m = "m", linestring_id = "id") ``` -------------------------------- ### Convert sfc to data frame Source: https://github.com/dcooley/sfheaders/blob/master/docs/reference/sfc_to_df.html Demonstrates the basic usage of sfc_to_df to convert an sfc linestring object to a data frame. ```R x <- matrix( c(1:16), ncol = 2 ) sfc <- sfc_linestring( x ) df <- sfc_to_df( sfc ) ```