### Create MULTIPOLYGON using x, y, polygon_id, and linestring_id Source: https://dcooley.github.io/sfheaders/reference/sfc_multipolygon.html This example shows how to create a MULTIPOLYGON geometry by specifying columns for x, y coordinates, polygon IDs, and linestring IDs. It assumes the data frame `df` is defined. ```r sfc_multipolygon( df, x = "x", y = "y", polygon_id = "id1", linestring_id = "id2") #> [[1]] #> [[1]] #> [[1]][[1]] #> [,1] [,2] #> [1,] 0 0 #> [2,] 0 1 #> [3,] 1 1 #> [4,] 1 0 #> [5,] 0 0 #> #> #> [[2]] #> [[2]][[1]] #> [,1] [,2] #> [1,] 1 1 #> [2,] 1 2 #> [3,] 2 2 #> [4,] 2 1 #> [5,] 1 1 #> #> #> attr(,"class") #> [1] "XY" "MULTIPOLYGON" "sfg" #> #> attr(,"n_empty") #> [1] 0 #> attr(,"crs") #> $input #> [1] NA #> #> $wkt #> [1] NA #> #> attr(,"class") #> [1] "crs" #> attr(,"class") #> [1] "sfc_MULTIPOLYGON" "sfc" #> attr(,"precision") #> [1] 0 #> attr(,"bbox") #> xmin ymin xmax ymax #> 0 0 2 2 #> attr(,"class") #> [1] "bbox" ``` -------------------------------- ### Create Polygon with list_columns Source: https://dcooley.github.io/sfheaders/reference/sf_polygon.html This example shows how to create a polygon geometry using sf_polygon and specify a particular column ('val') to keep all its values. This is an alternative to keeping all non-geometry columns, allowing for more selective attribute retention. ```r sf_polygon( obj = df , polygon_id = "ml_id" , linestring_id = "l_id" , x = "x" , y = "y" , keep = TRUE , list_columns = "val" ) ``` -------------------------------- ### Create MULTIPOLYGON from DataFrame with polygon_id and linestring_id Source: https://dcooley.github.io/sfheaders/reference/sfc_multipolygon.html This example shows how to construct a MULTIPOLYGON geometry by mapping columns to polygon IDs and linestring IDs. It assumes the data frame `df` is defined. ```r sfc_multipolygon( df, polygon_id = "id1", linestring_id = "id2" ) #> [[1]] #> [[1]] #> [[1]][[1]] #> [,1] [,2] #> [1,] 0 0 #> [2,] 0 1 #> [3,] 1 1 #> [4,] 1 0 #> [5,] 0 0 #> #> #> [[2]] #> [[2]][[1]] #> [,1] [,2] #> [1,] 1 1 #> [2,] 1 2 #> [3,] 2 2 #> [4,] 2 1 #> [5,] 1 1 #> #> #> attr(,"class") #> [1] "XY" "MULTIPOLYGON" "sfg" #> #> attr(,"n_empty") #> [1] 0 #> attr(,"crs") #> $input #> [1] NA #> #> $wkt #> [1] NA #> #> attr(,"class") #> [1] "crs" #> attr(,"class") #> [1] "sfc_MULTIPOLYGON" "sfc" #> attr(,"precision") #> [1] 0 #> attr(,"bbox") #> xmin ymin xmax ymax #> 0 0 2 2 #> attr(,"class") #> [1] "bbox" ``` -------------------------------- ### Create MULTIPOLYGON using x, y, and linestring_id (alternative) Source: https://dcooley.github.io/sfheaders/reference/sfc_multipolygon.html This example shows an alternative way to create a MULTIPOLYGON geometry by specifying columns for x, y coordinates, and a different linestring ID. It assumes the data frame `df` is defined. ```r sfc_multipolygon( df, x = "x", y = "y", linestring_id = "id2") #> [[1]] #> [[1]] #> [[1]][[1]] #> [,1] [,2] #> [1,] 0 0 #> [2,] 0 1 #> [3,] 1 1 #> [4,] 1 0 #> [5,] 0 0 #> [6,] 1 1 #> [7,] 1 2 #> [8,] 2 2 #> [9,] 2 1 #> [10,] 1 1 #> [11,] 0 0 #> #> #> attr(,"class") #> [1] "XY" "MULTIPOLYGON" "sfg" #> #> attr(,"n_empty") #> [1] 0 #> attr(,"crs") #> $input #> [1] NA #> #> $wkt #> [1] NA #> #> attr(,"class") #> [1] "crs" #> attr(,"class") #> [1] "sfc_MULTIPOLYGON" "sfc" #> attr(,"precision") #> [1] 0 #> attr(,"bbox") #> xmin ymin xmax ymax #> 0 0 2 2 #> attr(,"class") #> [1] "bbox" ``` -------------------------------- ### Create MULTILINESTRING with X, Y, Z, and M coordinates Source: https://dcooley.github.io/sfheaders/reference/sfc_multilinestring.html This example shows how to create a MULTILINESTRING geometry collection that includes X, Y, Z, and M (measure) coordinates. This is useful for data that has an additional measurement associated with each point. ```R sfc_multilinestring( obj = df, x = "x", y = "y", z = "z", m = "m") #> [[1]] #> [[1]] #> [,1] [,2] [,3] [,4] #> [1,] -0.4265481 0.03355271 -0.41939126 0.0249345 #> [2,] -1.7649720 1.40451705 -1.14743104 -1.1129934 #> [3,] -1.5775738 0.10182842 0.97120272 1.5367085 #> [4,] 0.4821113 1.22779901 -0.08037733 0.2090207 #> [5,] -0.4196557 -1.86697916 2.35816628 -1.1120871 #> [6,] 0.4877076 -0.38331130 -0.70836002 0.4237897 #> [7,] 0.3871084 0.05269726 0.77481116 0.3068737 #> [8,] 0.1050004 -0.85110417 1.35594124 -0.2120821 #> [9,] -0.6866081 -0.65687778 0.71225610 -2.2662124 #> [10,] -0.1104188 -0.68395731 -0.97574961 -0.9917302 #> [11,] 0.2933001 0.49298159 0.36353954 -0.7830823 #> [12,] -2.3638442 -0.61778568 0.42342666 -0.2962116 #> [13,] -0.2841777 -1.19881923 -0.23036840 1.6439183 #> #> attr(,"class") #> [1] "XYZM" "MULTILINESTRING" "sfg" #> #> attr(,"n_empty") #> [1] 0 #> attr(,"crs") #> $input #> [1] NA #> #> $wkt #> [1] NA #> #> attr(,"class") #> [1] "crs" #> attr(,"class") #> [1] "sfc_MULTILINESTRING" "sfc" #> attr(,"precision") #> [1] 0 #> attr(,"bbox") #> xmin ymin xmax ymax #> -2.3638442 -1.8669792 0.4877076 1.4045171 #> attr(,"class") #> [1] "bbox" #> attr(,"z_range") #> zmin zmax #> -1.147431 2.358166 #> attr(,"class") #> [1] "z_range" #> attr(,"m_range") #> mmin mmax #> -2.266212 1.643918 #> attr(,"class") #> [1] "m_range" ``` -------------------------------- ### Create MULTIPOLYGON with x, y, and different linestring ID Source: https://dcooley.github.io/sfheaders/reference/sf_multipolygon.html Create a MULTIPOLYGON using 'x', 'y' coordinates and a 'linestring_id'. This example demonstrates how different 'linestring_id' values can lead to distinct geometric structures within the resulting MULTIPOLYGON. ```r df <- data.frame( id1 = c(1,1,1,1,1,2,2,2,2,2) , id2 = c(1,1,1,1,1,1,1,1,1,1) , x = c(0,0,1,1,0,1,1,2,2,1) , y = c(0,1,1,0,0,1,2,2,1,1) ) sf_multipolygon( df, x = "x", y = "y", linestring_id = "id2") ``` -------------------------------- ### Create a polygon from a data frame with properties Source: https://dcooley.github.io/sfheaders/reference/sf_polygon.html Constructs a POLYGON from a data frame, specifying coordinate and ID columns. This example demonstrates how to handle data frames with additional properties. ```R df <- data.frame( ml_id = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2) , l_id = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2) , x = rnorm(15) , y = rnorm(15) , z = rnorm(15) , m = rnorm(15) ) ``` -------------------------------- ### Create sf MULTILINESTRING with properties and coordinates Source: https://dcooley.github.io/sfheaders/reference/sf_mline.html Illustrates using sf_mline with a data frame containing 'x', 'y', 'z', and 'm' coordinates, along with 'multilinestring_id' and 'linestring_id'. This example shows how the function handles multiple linestrings within a multilinestring. ```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(df) ``` -------------------------------- ### Calculate Bounding Box of sf LineString Source: https://dcooley.github.io/sfheaders/reference/sf_boxes.html Demonstrates how to use the sf_boxes function to get the bounding box of a LineString object created using sfheaders. Requires the sfheaders package and data frame with coordinate information. ```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 ) # # > id1 geometry # # > 1 1 0, 2, 2, 0, 0, 0, 0, 2, 2, 0 # # > 2 2 3, 4, 4, 3, 3, 3, 3, 4, 4, 3 ``` -------------------------------- ### Create MULTIPOLYGON SFC Source: https://dcooley.github.io/sfheaders/reference/sfc_multipolygon.html This example demonstrates how to create a MULTIPOLYGON Simple Feature Collection using the sfc_multipolygon function. It requires a data frame with columns for multipolygon and polygon IDs, along with x and y coordinates. ```r df <- data.frame( id1 = c(1,1,1,1,1,2,2,2,2,2) , id2 = c(1,1,1,1,1,1,1,1,1,1) , x = c(0,0,1,1,0,1,1,2,2,1) , y = c(0,1,1,0,0,1,2,2,1,1) ) sfc_multipolygon( df, multipolygon_id = "id1", polygon_id = "id2") ``` -------------------------------- ### Extract Specific MultiLineString by ID Source: https://dcooley.github.io/sfheaders/reference/sf_multilinestring.html This example shows how to extract a specific MultiLineString geometry from a data frame by providing its unique ID. It returns the ID and the corresponding geometry. ```r sf_multilinestring( obj = df, multilinestring_id = 1, linestring_id = 2 ) #> id #> 1 1 #> 2 2 #> geometry #> 1 -0.11845380, -0.70156253, 1.12412292, -0.12050301, 0.67970583, 0.68267350, 0.20473938, -0.95377671, -1.18377260, 0.87582994, -0.47409377, 0.62025023, 0.37429602, 1.61446468, 0.86510713, -0.51003232, -0.96286689, 0.75064300, -1.02061225, 1.49759178, 1.25908901, -1.04488829, 0.21135836, -0.17156571, 1.68243773, -1.30170864, -1.01811538, -1.58608844, -0.20955933, -0.23664778, 0.22182987, 0.01253167 #> 2 0.73334343, 0.72955620, -0.20900603, 0.90042502, 2.24907115, 0.63216560, -0.01092492, -2.85779234, -0.29850981, -0.77337760, -0.03627684, -0.81707413, -0.38516694, -0.43113168, 1.92451440, 0.01867461, 0.07158526, 1.69689332, -0.10558793, -0.08450654 ``` -------------------------------- ### Construct sf MULTILINESTRING with sf_mline Source: https://dcooley.github.io/sfheaders/reference/sf_mline.html Demonstrates the basic usage of sf_mline with a matrix input, showing how it creates a MULTILINESTRING geometry. ```R m <- cbind(x = 0, y = 0, multilinestring_id = c(1, 1, 1), linestring_id = 1) sf_mline( m ) #> multilinestring_id geometry #> 1 1 0, 0, 0, 0, 0, 0 ``` -------------------------------- ### Constructing sfg POINT with matrix and data frame Source: https://dcooley.github.io/sfheaders/reference/sfg_point.html Shows how to create POINT objects using a matrix or a data frame as input. ```R sfg_point( matrix( 1:3, ncol = 3 ) ) #> [,1] [,2] [,3] #> [1,] 1 2 3 #> attr(,"class") #> [1] "XYZ" "POINT" "sfg" ``` ```R sfg_point( data.frame( x = 1, y = 2, z = 3 ) ) #> [,1] [,2] [,3] #> [1,] 1 2 3 #> attr(,"class") #> [1] "XYZ" "POINT" "sfg" ``` -------------------------------- ### Convert sfg polygon to data.frame Source: https://dcooley.github.io/sfheaders/reference/sfg_to_df.html This example demonstrates converting a more complex sfg polygon object, created from a matrix, into a data.frame. The input matrix defines the polygon's coordinates. ```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 ) ``` -------------------------------- ### Create MULTILINESTRING excluding a different column Source: https://dcooley.github.io/sfheaders/reference/sf_mline.html This example demonstrates using sf_mline while excluding column 5 from the input data frame 'df'. Note that this results in XYZ coordinates, not XYM. ```r (sfx <- sf_mline( obj = df[-5])) #> multilinestring_id #> 1 1 #> 2 2 #> geometry #> 1 -1.70158439, 1.47916781, 1.25388529, 0.50387735, -0.21811690, -0.92443920, 1.14208500, -1.12062976, -1.58624710, 1.99621267, -1.43717209, 1.55975592, -1.20297229, 0.06956897, -1.20852327, 0.03215314, 2.11473110, 0.07405923, 1.47458362, 1.56495566, 0.02425301, -0.42449104, -0.73362526, 1.07489079 #> 2 -0.81257986, -0.46096715, -2.23170826, 0.59883011, 1.25521156, -0.62462270, -0.45755993, 0.75857513, 1.19162471, -0.23471664, 0.54266610, -0.09908664, -1.26102906, 1.52006759, -1.27277672 ``` -------------------------------- ### Calculate bbox from sf objects Source: https://dcooley.github.io/sfheaders/reference/sf_bbox.html Shows how to calculate the bounding box for sf objects, which contain geometry and attribute data. ```R pt <- sf_point(obj = df, x = "x", y = "y", z = "id1") mpt <- sf_multipoint(obj = df, x = "x", y = "y", multipoint_id = "id1") ls <- sf_linestring(obj = df, x = "x", y = "y", linestring_id = "id1") mls <- sf_multilinestring(obj = df, x = "x", y = "y", multilinestring_id = "id1") p <- sf_polygon( obj = df , x = "x" , y = "y" , polygon_id = "id1" , linestring_id = "id2" , close = FALSE ) mp <- sf_multipolygon( obj = df , x = "x" , y = "y" , multipolygon_id = "id1" , linestring_id = "id2" , close = FALSE ) sf_bbox( pt ) sf_bbox( mpt ) sf_bbox( ls ) sf_bbox( mls ) sf_bbox( p ) sf_bbox( mp ) ``` -------------------------------- ### Calculate bbox from sfc objects Source: https://dcooley.github.io/sfheaders/reference/sf_bbox.html Demonstrates calculating the bounding box for collections of single geometry objects (sfc) like points, linestrings, and polygons. ```R 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 ) sf_bbox( pt ) sf_bbox( mpt ) sf_bbox( ls ) sf_bbox( mls ) sf_bbox( p ) sf_bbox( mp ) ``` -------------------------------- ### Constructing sfg POINT with numeric vectors Source: https://dcooley.github.io/sfheaders/reference/sfg_point.html Demonstrates creating POINT objects with different dimensions (XY, XYZ, XYZM) using numeric vectors. ```R sfg_point( 1:2 ) #> [,1] [,2] #> [1,] 1 2 #> attr(,"class") #> [1] "XY" "POINT" "sfg" ``` ```R sfg_point( 1:3 ) #> [,1] [,2] [,3] #> [1,] 1 2 3 #> attr(,"class") #> [1] "XYZ" "POINT" "sfg" ``` ```R sfg_point( 1:4 ) #> [,1] [,2] [,3] [,4] #> [1,] 1 2 3 4 #> attr(,"class") #> [1] "XYZM" "POINT" "sfg" ``` -------------------------------- ### Load sfheaders Package Source: https://dcooley.github.io/sfheaders/articles/examples.html Loads the sfheaders package. The sf package is also useful for printing, but is commented out here. ```r library(sfheaders) # library(sf) ## for nice printing (disabled on github actinos!) ``` -------------------------------- ### Calculate bbox from sfg objects Source: https://dcooley.github.io/sfheaders/reference/sf_bbox.html Shows how to calculate the bounding box for various single geometry objects (sfg) including points, linestrings, and polygons. ```R pt <- sfg_point(obj = df[1, ], x = "x", y = "y", z = "id1") mpt <- sfg_multipoint(obj = df, x = "x", y = "y") ls <- sfg_linestring(obj = df, x = "x", y = "y") mls <- sfg_multilinestring(obj = df, x = "x", y = "y") p <- sfg_polygon(obj = df, x = "x" , y = "y") mp <- sfg_multipolygon(obj = df, x = "x", y = "y", close = FALSE ) sf_bbox( pt ) sf_bbox( mpt ) sf_bbox( ls ) sf_bbox( mls ) sf_bbox( p ) sf_bbox( mp ) ``` -------------------------------- ### Construct sf POLYGON with reordered columns Source: https://dcooley.github.io/sfheaders/reference/sf_poly.html The sf_poly function uses column names for identification, so the order of columns in the input data frame does not matter. This example demonstrates creating an sf POLYGON object with reordered columns. ```R sf_poly(df[c(5, 3, 4, 1, 2)]) ``` -------------------------------- ### Create MULTIPOLYGON with multiple multipolygons and polygons Source: https://dcooley.github.io/sfheaders/reference/sf_multipolygon.html Construct a MULTIPOLYGON object from a data frame where 'multipolygon_id' and 'polygon_id' columns define multiple distinct polygons within multiple multipolygons. This example demonstrates handling more complex data structures. ```r df <- data.frame( id1 = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2) , id2 = c(1,1,1,1,1,2,2,2,2,2,1,1,1,1,1) , x = c(0,0,1,1,0,1,1,2,2,1,3,3,4,4,3) , y = c(0,1,1,0,0,1,2,2,1,1,3,4,4,3,3) ) sf_multipolygon( df, multipolygon_id = "id1", polygon_id = "id2") ``` -------------------------------- ### Create sf POINT from matrix with x, y Source: https://dcooley.github.io/sfheaders/reference/sf_pt.html Use sf_pt with a matrix containing 'x' and 'y' columns to create a simple POINT geometry. ```R x <- cbind(x = 1, y= 3) sf_pt( x ) ``` -------------------------------- ### Milliseconds Benchmark Output Source: https://dcooley.github.io/sfheaders/articles/examples.html This output shows the results of a benchmark run using milliseconds as the unit. It includes various statistical measures like min, max, mean, median, and quartiles for different metrics ('dt' and 'sfheaders'). ```text # Unit: milliseconds # expr min lq mean median uq max neval # dt 6599.67479 6654.12357 6779.23543 6750.19807 6833.46262 7058.71809 5 # sfheaders 21.07775 21.30438 23.20592 23.21665 25.06429 25.36654 5 ``` -------------------------------- ### Performance Benchmark: sfheaders vs data.table Source: https://dcooley.github.io/sfheaders/articles/examples.html This benchmark 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 ) ``` -------------------------------- ### Create a polygon with z and m coordinates Source: https://dcooley.github.io/sfheaders/reference/sf_polygon.html Constructs a POLYGON from a matrix, including z and m coordinate dimensions. The input matrix must 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, x = 1, y = 2, z = 3 ) ``` -------------------------------- ### Round-trip sf object using sf_pt and sf_to_df Source: https://dcooley.github.io/sfheaders/reference/sf_pt.html Demonstrates converting an sf object to a data frame and back using sf_pt and sf_to_df, preserving properties. ```R sf_pt(sf_to_df(sfx, fill = TRUE), keep = TRUE) ``` -------------------------------- ### Constructing sfg POINT with data frame and specified columns Source: https://dcooley.github.io/sfheaders/reference/sfg_point.html Illustrates creating POINT objects from a data frame, explicitly defining which columns represent x and y coordinates. ```R sfg_point( data.frame( x = 1, y = 2, z = 3 ), x = "x", y = "y" ) #> [,1] [,2] #> [1,] 1 2 #> attr(,"class") #> [1] "XY" "POINT" "sfg" ``` ```R sfg_point( data.frame( x = 1, y = 2, z = 3 ), x = 1, y = 3 ) #> [,1] [,2] #> [1,] 1 3 #> attr(,"class") #> [1] "XY" "POINT" "sfg" ``` -------------------------------- ### sf_point() Source: https://dcooley.github.io/sfheaders/reference/index.html Creates an sf POINT object. ```APIDOC ## sf_point() ### Description Creates an sf POINT object. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an sf POINT object. ``` -------------------------------- ### Create sf POINT from data frame Source: https://dcooley.github.io/sfheaders/articles/examples.html Demonstrates creating POINT geometries from a data frame. All columns other than 'id' are used for coordinates by default. ```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 ) # geometry # 1 1, 1, 1 # 2 1, 1, 4 # 3 2, 4, 4 # 4 2, 4, 1 # 5 3, 1, 1 ``` -------------------------------- ### Create sf POINT with specified coordinate columns Source: https://dcooley.github.io/sfheaders/articles/examples.html Demonstrates creating POINT geometries from a data frame when non-geometry columns are present. Explicitly specifies 'x' and 'y' for coordinates. ```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" ) ``` -------------------------------- ### sf_linestring() Source: https://dcooley.github.io/sfheaders/reference/index.html Creates an sf LINESTRING object. ```APIDOC ## sf_linestring() ### Description Creates an sf LINESTRING object. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an sf LINESTRING object. ``` -------------------------------- ### Create a 2D Polygon Source: https://dcooley.github.io/sfheaders/reference/sfc_polygon.html This snippet demonstrates creating a simple 2D polygon using the sfc_polygon function with x and y coordinates. The output shows the structure of the created polygon geometry. ```R sfc_polygon( obj = m, x = 2, y = 3 ) #> [[1]] #> [[1]] #> [,1] [,2] #> [1,] 0 0 #> [2,] 0 1 #> [3,] 1 1 #> [4,] 2 2 #> [5,] 2 3 #> [6,] 3 2 #> [7,] 0 0 #> #> 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 3 3 #> attr(,"class") #> [1] "bbox" ``` -------------------------------- ### sf_multipoint() Source: https://dcooley.github.io/sfheaders/reference/index.html Creates an sf MULTIPOINT object. ```APIDOC ## sf_multipoint() ### Description Creates an sf MULTIPOINT object. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an sf MULTIPOINT object. ``` -------------------------------- ### Sample Data Frame Creation Source: https://dcooley.github.io/sfheaders/reference/sf_polygon.html This code defines a sample data frame 'df' with columns for polygon IDs ('ml_id'), linestring IDs ('l_id'), and various other attributes (x, y, z, m, val). This data frame is used as input for the sf_polygon function. ```R df <- data.frame( ml_id = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2) , l_id = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2) , x = rnorm(15) , y = rnorm(15) , z = rnorm(15) , m = rnorm(15) , val = letters[1:15] , stringsAsFactors = FALSE ) ``` -------------------------------- ### Construct a simple sfc POLYGON Source: https://dcooley.github.io/sfheaders/reference/sfc_polygon.html Use sfc_polygon with a matrix of coordinates to create a basic POLYGON. The matrix should have at least two columns for x and y coordinates. ```R m <- matrix(c(0,0,0,0,1,1), ncol = 2 ) sfc_polygon( m ) ``` -------------------------------- ### Create Polygon and MultiPolygon geometries Source: https://dcooley.github.io/sfheaders/reference/sfc_cast.html Demonstrates the creation of Polygon and MultiPolygon geometries from a data frame, specifying IDs for polygons and inner rings (linestrings), and controlling polygon closure. ```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) ) 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 ) ``` -------------------------------- ### Calculate bbox from data.frame columns Source: https://dcooley.github.io/sfheaders/reference/sf_bbox.html Demonstrates calculating the bounding box from specific x and y columns within a data.frame. ```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_bbox( obj = df[, c("x","y")] ) sf_bbox( obj = df, x = "x", y = "y" ) ``` -------------------------------- ### sf_mpt() Source: https://dcooley.github.io/sfheaders/reference/index.html Helper function for creating sf MULTIPOINT objects. ```APIDOC ## sf_mpt() ### Description Helper function for creating sf MULTIPOINT objects. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an sf MULTIPOINT object. ``` -------------------------------- ### sfc_point() Source: https://dcooley.github.io/sfheaders/reference/index.html Creates an sfc POINT object. ```APIDOC ## sfc_point() ### Description Creates an sfc POINT object. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an sfc POINT object. ``` -------------------------------- ### Create an SFG Point (XYZ) Source: https://dcooley.github.io/sfheaders/articles/examples.html Creates a simple feature geometry point with XYZ coordinates from a numeric vector. ```r sfg_point( 1:3 ) # [,1] [,2] [,3] # [1,] 1 2 3 # attr(,"class") # [1] "XYZ" "POINT" "sfg" ``` -------------------------------- ### Construct sf MULTIPOLYGON with reordered columns Source: https://dcooley.github.io/sfheaders/reference/sf_mpoly.html Demonstrates that the order of columns in the input data frame does not matter for sf_mpoly, as it relies on column names. ```R sf_mpoly(df[c(6, 5, 3, 4, 1, 2)]) ``` -------------------------------- ### Create an SFG Point (XYZM) Source: https://dcooley.github.io/sfheaders/articles/examples.html Creates a simple feature geometry point with XYZM coordinates from a numeric vector. ```r sfg_point( 1:4 ) # [,1] [,2] [,3] [,4] # [1,] 1 2 3 4 # attr(,"class") # [1] "XYZM" "POINT" "sfg" ``` -------------------------------- ### Construct sf MULTIPOINT with data.frame Source: https://dcooley.github.io/sfheaders/reference/sf_mpt.html Using sf_mpt() with a data frame, demonstrating default behavior and with keep = TRUE. ```R x <- data.frame( id = 1:2, x = 1:2, y = 2:1, multipoint_id = 1) sf_mpt( x ) ``` ```R x <- data.frame( id = 1:2, x = 1:2, y = 2:1, multipoint_id = 1) sf_mpt( x, keep = TRUE) ``` -------------------------------- ### sfg_point() Source: https://dcooley.github.io/sfheaders/reference/index.html Creates an sfg POINT object. ```APIDOC ## sfg_point() ### Description Creates an sfg POINT object. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an sfg POINT object. ``` -------------------------------- ### Create MULTIPOLYGON using x, y, and linestring_id Source: https://dcooley.github.io/sfheaders/reference/sfc_multipolygon.html This snippet demonstrates creating a MULTIPOLYGON geometry by specifying columns for x, y coordinates, and linestring IDs. It assumes the data frame `df` is defined. ```r sfc_multipolygon( df, x = "x", y = "y", linestring_id = "id1") #> [[1]] #> [[1]] #> [[1]][[1]] #> [,1] [,2] #> [1,] 0 0 #> [2,] 0 1 #> [3,] 1 1 #> [4,] 1 0 #> [5,] 0 0 #> #> [[1]][[2]] #> [,1] [,2] #> [1,] 1 1 #> [2,] 1 2 #> [3,] 2 2 #> [4,] 2 1 #> [5,] 1 1 #> #> #> attr(,"class") #> [1] "XY" "MULTIPOLYGON" "sfg" #> #> attr(,"n_empty") #> [1] 0 #> attr(,"crs") #> $input #> [1] NA #> #> $wkt #> [1] NA #> #> attr(,"class") #> [1] "crs" #> attr(,"class") #> [1] "sfc_MULTIPOLYGON" "sfc" #> attr(,"precision") #> [1] 0 #> attr(,"bbox") #> xmin ymin xmax ymax #> 0 0 2 2 #> attr(,"class") #> [1] "bbox" ``` -------------------------------- ### sf_pt() Source: https://dcooley.github.io/sfheaders/reference/index.html Helper function for creating sf POINT objects. ```APIDOC ## sf_pt() ### Description Helper function for creating sf POINT objects. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an sf POINT object. ``` -------------------------------- ### Create a 3D Polygon Source: https://dcooley.github.io/sfheaders/reference/sf_polygon.html This snippet shows how to create a 3D polygon by including the z-coordinate. It requires a data frame and specifies the columns for x, y, and z coordinates. ```r sf_polygon( obj = df, x = 2, y = 3, z = 4) #> id #> 1 1 #> geometry #> 1 1.0000000, 1.0000000, 1.0000000, 2.0000000, 2.0000000, 2.0000000, 3.0000000, 3.0000000, 3.0000000, 1.0000000, 1.0000000, 1.0000000, 2.0000000, 2.0000000, 2.0000000, 1.0000000, 1.3676191, -0.5640758, 2.2137634, 1.0107082, 2.9263283, -1.2976272, -0.8911023, 0.6329409, -0.5569703, 0.6398364, -0.3090776, 1.1412638, -0.3209745, 0.6252955, -0.7352724, 1.3676191, -1.0529744, -2.0186193, -0.5162906, 0.9615230, -1.2323929, -1.9028400, -1.2544138, 0.3233006, 1.1991835, -0.5976572, 0.2424013, 0.9010702, 2.1705092, 1.0749764, 0.6061827, -1.0529744 ``` -------------------------------- ### sf_line() Source: https://dcooley.github.io/sfheaders/reference/index.html Helper function for creating sf LINESTRING objects. ```APIDOC ## sf_line() ### Description Helper function for creating sf LINESTRING objects. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an sf LINESTRING object. ``` -------------------------------- ### sf_bbox() Source: https://dcooley.github.io/sfheaders/reference/index.html Calculates the bounding box for a spatial object. ```APIDOC ## sf_bbox() ### Description Calculates the bounding box for a spatial object. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns the bounding box of the spatial object. ```