Skip to contents

A field form in rfp is a table of rows, one per field. rfp_form_build() turns that table into the two files a field crew uses: a GeoPackage with one column per field, and a QGIS style (.qml) that draws the form with its tabs, dropdowns, photo buttons and defaults. Changing a form means editing a row and building again, not editing the form in QGIS.

For a form whose columns are registered in crate, what each column means (its type, its allowed values, its range) comes from crate, and rfp’s table says only how the form shows it.

What does a form definition look like?

A schema is a CSV with one row per field. These columns are required:

Column What it says
field the column name in the GeoPackage
type text, integer, real, datetime or boolean
widget how the field is entered: text, textmulti, valuemap (a dropdown), photo, datetime, checkbox or range (a spinbox)
group the tab it sits on; Tab > Subgroup nests it, and a blank leaves the field off the form
label the name a crew reads beside the box
options for a dropdown its choices, for a spinbox min\|max\|step\|precision, for a checkbox checked\|unchecked
default a QGIS expression for the value a new record starts with
help a note for whoever maintains the form; it is not shown

Optional columns add layout order (order), a rule the value must pass (constraint, constraint_desc), computed fields (apply_on_update, read_only) and a way to clear a dropdown (null_choice). The full reference is inst/extdata/forms/schema/README.md in the package source.

Here is a small form with a tab of three fields and a photo:

schema <- data.frame(
  field = c("date_time_start", "site_id", "condition", "photo_site"),
  type = c("datetime", "text", "text", "text"),
  widget = c("datetime", "text", "valuemap", "photo"),
  group = c("Site", "Site", "Site", "Photos"),
  label = c("Date and time", "Site", "Condition", "Site photo"),
  options = c("", "", "Good=good|Fair=fair|Poor=poor", ""),
  default = c("now()", "", "", ""),
  help = c("When the visit started", "Site name", "Overall condition", ""),
  stringsAsFactors = FALSE
)
schema
#>             field     type   widget  group         label
#> 1 date_time_start datetime datetime   Site Date and time
#> 2         site_id     text     text   Site          Site
#> 3       condition     text valuemap   Site     Condition
#> 4      photo_site     text    photo Photos    Site photo
#>                         options default                   help
#> 1                                 now() When the visit started
#> 2                                                    Site name
#> 3 Good=good|Fair=fair|Poor=poor              Overall condition
#> 4

Good=good is a dropdown choice whose label differs from what it stores: a crew picks “Good”, and the GeoPackage holds good.

How do I build it?

rfp_form_build() takes a registered form type and an output path. While a form is still a draft, schema = points it at your CSV instead of the registered one. The type then supplies only the map symbol and colour:

dir <- withr::local_tempdir()
path_csv <- file.path(dir, "site_visit.csv")
write.csv(schema, path_csv, row.names = FALSE)

path_gpkg <- file.path(dir, "form_site_visit.gpkg")
rfp::rfp_form_build("observation", path_gpkg, schema = path_csv,
                    layer = "form_site_visit")
#> ✔ Built observation form: /tmp/Rtmp824m4e/file24c7274830a6/form_site_visit.gpkg (4 fields, /tmp/Rtmp824m4e/file24c7274830a6/form_site_visit.qml)

The GeoPackage has one column per row of the schema, in row order:

sf::st_read(path_gpkg, quiet = TRUE)
#> Simple feature collection with 0 features and 4 fields
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> Projected CRS: NAD83 / BC Albers
#> [1] date_time_start site_id         condition       photo_site     
#> [5] geom           
#> <0 rows> (or 0-length row.names)

It ships with no records. A form layer is spatial because the GeoPackage says so, so it needs no placeholder point.

The style beside it carries the form. Each dropdown choice is written as a stored value and the label a crew reads:

qml <- xml2::read_xml(sub("\\.gpkg$", ".qml", path_gpkg))
choices <- xml2::xml_find_all(
  qml, ".//field[@name='condition']//Option[@name='map']/Option/Option")
data.frame(label = xml2::xml_attr(choices, "name"),
           stored = xml2::xml_attr(choices, "value"))
#>   label stored
#> 1  Good   good
#> 2  Fair   fair
#> 3  Poor   poor

How do I add a field?

Add a row and build again. A field appended to the end becomes the last GeoPackage column, and order places it anywhere on the form without moving the column:

schema <- rbind(schema, data.frame(
  field = "notes", type = "text", widget = "textmulti", group = "Site",
  label = "Notes", options = "", default = "", help = "Anything else",
  stringsAsFactors = FALSE))
write.csv(schema, path_csv, row.names = FALSE)
rfp::rfp_form_build("observation", path_gpkg, schema = path_csv,
                    layer = "form_site_visit")
#> ✔ Built observation form: /tmp/Rtmp824m4e/file24c7274830a6/form_site_visit.gpkg (5 fields, /tmp/Rtmp824m4e/file24c7274830a6/form_site_visit.qml)
names(sf::st_read(path_gpkg, quiet = TRUE))
#> [1] "date_time_start" "site_id"         "condition"       "photo_site"     
#> [5] "notes"           "geom"

A rebuild with nothing changed writes the style again and leaves the GeoPackage alone, so the GeoPackage only shows up as changed when the schema really changed. Once a form is in a project and holding records, a schema change stops by default; on_records = "migrate" carries the records into the new columns.

How does a draft become a form a project can use?

A form is registered with a row in inst/lookups/rfp_form_types.csv: its type, the schema CSV, its geometry, its map symbol and colour, and a label expression. Once registered, rfp_form_build(type, path) needs no schema =, and rfp_project_create(forms = type) puts the form into a new project. Adding a form changes data files only; the builder’s code does not change.

Where does a crate-backed form get its meaning?

crate holds the schemas of the forms New Graph records data with, so that data from every project can be checked and combined against one definition. For a form registered there, the registry row names it in crate_schema:

types <- rfp::rfp_form_types()
types[nzchar(types$crate_schema), c("type", "crate_schema")]
#> # A tibble: 4 × 2
#>   type                    crate_schema                    
#>   <chr>                   <chr>                           
#> 1 pscis                   nge/form_pscis                  
#> 2 fiss_site               nge/form_fiss_site              
#> 3 monitoring_fish_passage nge/form_monitoring_fish_passage
#> 4 monitoring_general      nge/form_monitoring_general

crate gives each column its type, allowed values and range:

cols <- crate::crt_schema_cols("nge", "form_pscis")
cols <- cols[cols$name %in% c("habitat_value", "blockage_issues"), ]
cols[, c("name", "type", "range_min", "range_max")]
#> # A tibble: 2 × 4
#>   name            type    range_min range_max
#>   <chr>           <chr>       <dbl>     <dbl>
#> 1 habitat_value   string         NA        NA
#> 2 blockage_issues integer         1         3
cols$values[cols$name == "habitat_value"]
#> [[1]]
#> [1] "High"   "Medium" "Low"

so rfp’s CSV for that form carries no type or help column, leaves the dropdown’s options blank, and gives the spinbox only its step and precision:

csv <- read.csv(system.file("extdata", "forms", "schema", "pscis.csv",
                            package = "rfp"))
csv[csv$field %in% c("habitat_value", "blockage_issues"),
    c("field", "widget", "group", "label", "options")]
#>              field   widget                    group
#> 43   habitat_value valuemap                   Stream
#> 62 blockage_issues    range Climate > Condition Risk
#>                                             label options
#> 43            Habitat Value - justify in comments        
#> 62 Blockage Issues 1 (0-30%) 2 (>30-75%) 3 (>75%)     1|0

The build combines the two. The CSV must list exactly the form’s own columns in crate, in the same order, or the build stops and names the difference. Those are crate’s columns minus three kinds the CSV never lists: the capture envelope (project, record_id and the like, added when records are harvested), the record’s position (x, y, source_epsg), and columns only older versions of the form carried. To change what a crate-backed form records, change crate; to change how it looks, change the CSV.