Title: | Process ArcGIS Protocol Buffer FeatureCollections |
---|---|
Description: | Fast processing of ArcGIS FeatureCollection protocol buffers in R. It is designed to work seamlessly with 'httr2' and integrates with 'sf'. |
Authors: | Josiah Parry [aut, cre] |
Maintainer: | Josiah Parry <[email protected]> |
License: | Apache License (>= 2) |
Version: | 0.1.6.9000 |
Built: | 2024-11-21 19:20:50 UTC |
Source: | https://github.com/r-arcgis/arcpbf |
Fast processing of ArcGIS FeatureCollection protocol buffers in R. It is designed to work seamlessly with 'httr2' and integrates with 'sf'.
Maintainer: Josiah Parry [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/R-ArcGIS/arcpbf/issues
Read a pbf file as a raw vector
open_pbf(path)
open_pbf(path)
path |
the path to the |
a raw vector
count_fp <- system.file("count.pbf", package = "arcpbf") oid_fp <- system.file("ids.pbf", package = "arcpbf") tbl_fp <- system.file("small-table.pbf", package = "arcpbf") fc_fp <- system.file("small-points.pbf", package = "arcpbf") count_raw <- open_pbf(count_fp) oid_raw <- open_pbf(oid_fp) tbl_raw <- open_pbf(tbl_fp) fc_raw <- open_pbf(fc_fp)
count_fp <- system.file("count.pbf", package = "arcpbf") oid_fp <- system.file("ids.pbf", package = "arcpbf") tbl_fp <- system.file("small-table.pbf", package = "arcpbf") fc_fp <- system.file("small-points.pbf", package = "arcpbf") count_raw <- open_pbf(count_fp) oid_raw <- open_pbf(oid_fp) tbl_raw <- open_pbf(tbl_fp) fc_raw <- open_pbf(fc_fp)
Applies post-processing to the results of process_pbf()
post_process_pbf(x, use_sf = TRUE)
post_process_pbf(x, use_sf = TRUE)
x |
an object as returned by |
use_sf |
default |
If x
is a list object, the results will be row-binded. This is appropriate
if each element in the list is a data.frame
or a feature result with
geometry. However, if each element is not the same, the post-processing
will error. If you cannot be certain that all elements that you will be
post processing will be the same, post-process each list element
independently.
An object of class data.frame
, sf
, or a scalar integer vector.
See process_pbf()
for more details.
tbl_fp <- system.file("small-table.pbf", package = "arcpbf") fc_fp <- system.file("small-points.pbf", package = "arcpbf") # table feature collection fc <- read_pbf(tbl_fp) head(post_process_pbf(fc)) # feature collection with geometry fc <- read_pbf(fc_fp) head(post_process_pbf(fc))
tbl_fp <- system.file("small-table.pbf", package = "arcpbf") fc_fp <- system.file("small-points.pbf", package = "arcpbf") # table feature collection fc <- read_pbf(tbl_fp) head(post_process_pbf(fc)) # feature collection with geometry fc <- read_pbf(fc_fp) head(post_process_pbf(fc))
Process a pbf from a raw vector or a list of raw vectors.
process_pbf(proto)
process_pbf(proto)
proto |
either a raw vector or a list of raw vectors containing a FeatureCollection pbf |
There are three types of PBF FeatureCollection responses that may be returned.
In the case the PBF is a FeatureResult
and use_sf = FALSE
, a data.frame
is returned with the spatial reference stored in the crs
attribute.
Otherwise an sf
object is returned.
The PBF can also return a count result, for example if the query parameter
returnCountOnly
is set to true
. In this case, a scalar integer vector
is returned.
In the case that the query parameter returnIdsOnly
is true
, a
data.frame
is returned containing the object IDs and the column name
set to the object ID field name in the feature service.
For count results, a scalar integer.
For object ID results a data.frame
with one column.
For pbfs that contain geometries, a list of 3 elements:
attributes
is a data.frame
of the fields of the FeatureCollection
geometry
is an sfc object without a computed bounding box or coordinate reference system set
sr
is a named list of the spatial reference of the feature collection
Important: Use post_process_pbf()
to convert to an sf
object with a computed bounding box and CRS.
count_fp <- system.file("count.pbf", package = "arcpbf") oid_fp <- system.file("ids.pbf", package = "arcpbf") tbl_fp <- system.file("small-table.pbf", package = "arcpbf") fc_fp <- system.file("small-points.pbf", package = "arcpbf") # count response count_raw <- open_pbf(count_fp) process_pbf(count_raw) # object id response oid_raw <- open_pbf(oid_fp) head(process_pbf(oid_raw)) # table feature collection tbl_raw <- open_pbf(tbl_fp) process_pbf(tbl_raw) # feature collection with geometry fc_raw <- open_pbf(fc_fp) process_pbf(fc_raw)
count_fp <- system.file("count.pbf", package = "arcpbf") oid_fp <- system.file("ids.pbf", package = "arcpbf") tbl_fp <- system.file("small-table.pbf", package = "arcpbf") fc_fp <- system.file("small-points.pbf", package = "arcpbf") # count response count_raw <- open_pbf(count_fp) process_pbf(count_raw) # object id response oid_raw <- open_pbf(oid_fp) head(process_pbf(oid_raw)) # table feature collection tbl_raw <- open_pbf(tbl_fp) process_pbf(tbl_raw) # feature collection with geometry fc_raw <- open_pbf(fc_fp) process_pbf(fc_raw)
Given a binary file containing a FeatureCollection protocol buffer (pbf), read its contents into R as an R object.
read_pbf(path, post_process = TRUE, use_sf = TRUE)
read_pbf(path, post_process = TRUE, use_sf = TRUE)
path |
a scalar character of the path to the pbf file |
post_process |
default |
use_sf |
default |
Either a data.frame, list, scalar integer, or sf object if
post_process = TRUE
and use_sf = TRUE
.
See process_pbf()
for more.
count_fp <- system.file("count.pbf", package = "arcpbf") oid_fp <- system.file("ids.pbf", package = "arcpbf") tbl_fp <- system.file("small-table.pbf", package = "arcpbf") fc_fp <- system.file("small-points.pbf", package = "arcpbf") # count response read_pbf(count_fp) # object id response head(read_pbf(oid_fp)) # table feature collection read_pbf(tbl_fp) # feature collection with geometry read_pbf(fc_fp)
count_fp <- system.file("count.pbf", package = "arcpbf") oid_fp <- system.file("ids.pbf", package = "arcpbf") tbl_fp <- system.file("small-table.pbf", package = "arcpbf") fc_fp <- system.file("small-points.pbf", package = "arcpbf") # count response read_pbf(count_fp) # object id response head(read_pbf(oid_fp)) # table feature collection read_pbf(tbl_fp) # feature collection with geometry read_pbf(fc_fp)
Processes httr2_response
objects that return FeatureCollection PBFs.
resp_body_pbf(resp, post_process = TRUE, use_sf = TRUE) resps_data_pbf(resps, post_process = TRUE, use_sf = TRUE)
resp_body_pbf(resp, post_process = TRUE, use_sf = TRUE) resps_data_pbf(resps, post_process = TRUE, use_sf = TRUE)
resp |
A httr2 response object, created by |
post_process |
default |
use_sf |
default |
resps |
a list of |
Responses of type application/x-protobuf
are automatically processed using
process_pbf()
with optional post-processing applied. Theses functions
assume that the body of the responses are an Esri FeatureCollection
protocol buffer.
When running multiple requests in parallel using
httr2::req_perform_parallel()
the responses are returned as a list of
responses. resps_data_pbf()
processes the responses in a vectorized
manner.
Results are post-processed by default and return sf objects if applicable. This may not be desirable if heterogeneous response types are expected. For example, if one list element contains a count result and another contains an object ID result.
See post_process_pbf()
for more details.
Note: Knowledge Graph protocol buffers and other protobuf formats are not supported and will result in an error if used with these functions.
A processed FeatureCollection pbf. Either a scalar integer, named list, data.frame, or an sf object if post-processing is applied.
if (rlang::is_installed(c("httr2", "sf")) && interactive()) { base_url <- file.path( "https://services.arcgis.com/P3ePLMYs2RVChkJx", "arcgis", "rest", "services", "ACS_Population_by_Race_and_Hispanic_Origin_Boundaries", "FeatureServer", "2", "query", fsep = "/" ) # create the base request req <- httr2::request(base_url) # fill query parameters req <- httr2::req_url_query( req, where = "1=1", outFeilds = "objectid", resultRecordCount = 1, f = "pbf" ) # make the request resp <- httr2::req_perform(req) # parse the request resp_body_pbf(resp) # simulate response from multi_req_perform resps <- list(resp, resp, resp) # process them all at once resps_data_pbf(resps) }
if (rlang::is_installed(c("httr2", "sf")) && interactive()) { base_url <- file.path( "https://services.arcgis.com/P3ePLMYs2RVChkJx", "arcgis", "rest", "services", "ACS_Population_by_Race_and_Hispanic_Origin_Boundaries", "FeatureServer", "2", "query", fsep = "/" ) # create the base request req <- httr2::request(base_url) # fill query parameters req <- httr2::req_url_query( req, where = "1=1", outFeilds = "objectid", resultRecordCount = 1, f = "pbf" ) # make the request resp <- httr2::req_perform(req) # parse the request resp_body_pbf(resp) # simulate response from multi_req_perform resps <- list(resp, resp, resp) # process them all at once resps_data_pbf(resps) }