-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d946740
commit d4029e0
Showing
19 changed files
with
702 additions
and
10 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
_freeze/docs/editing/add-features/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"hash": "7426f50c65f2ce3255d1d6fba02c3968", | ||
"result": { | ||
"engine": "knitr", | ||
"markdown": "---\ntitle: Adding features\n---\n\n\n\n\nProgrammatically, adding, deleting, or updating features using `{arcgis}` is a straightforward process. In this workflow, we illustrate how to add, update, or delete features from an existing hosted feature layer or table. \n\nWe will go over the functions:\n\n - `add_features()`\n - `update_features()`\n - `delete_features()`\n \n## Pre-requisites\n\nWe will use the the *North Carolina SIDS* dataset we created in the [**Publishing from R**](../publishing.qmd) tutorial. To follow along, be sure that you have followed that tutorial and have a `FeatureLayer` that you can modify. If you have not yet configured your environment to authorize with an online portal, start at [**Connecting to your portal**](../connecting-to-a-portal.qmd).\n\n## Adding features\n\nFor this example, we will add a single feature to the North Carolina SIDS dataset that is a summary over the entire state. Before we can begin, we must load the package and authorize ourselves as a user. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(arcgis)\n\ntoken <- auth_code()\nset_auth_token(token)\n```\n:::\n\n\nNext, we will create the feature that we want to add using the `sf` package. We'll read in the `nc.shp` file from the `sf` package.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(sf)\nnc_sf <- read_sf(system.file(\"shape/nc.shp\", package = \"sf\"))\nnc_sf\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\nSimple feature collection with 100 features and 14 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965\nGeodetic CRS: NAD27\n# A tibble: 100 × 15\n AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74\n <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <int> <dbl> <dbl> <dbl>\n 1 0.114 1.44 1825 1825 Ashe 37009 37009 5 1091 1 10\n 2 0.061 1.23 1827 1827 Alle… 37005 37005 3 487 0 10\n 3 0.143 1.63 1828 1828 Surry 37171 37171 86 3188 5 208\n 4 0.07 2.97 1831 1831 Curr… 37053 37053 27 508 1 123\n 5 0.153 2.21 1832 1832 Nort… 37131 37131 66 1421 9 1066\n 6 0.097 1.67 1833 1833 Hert… 37091 37091 46 1452 7 954\n 7 0.062 1.55 1834 1834 Camd… 37029 37029 15 286 0 115\n 8 0.091 1.28 1835 1835 Gates 37073 37073 37 420 0 254\n 9 0.118 1.42 1836 1836 Warr… 37185 37185 93 968 4 748\n10 0.124 1.43 1837 1837 Stok… 37169 37169 85 1612 1 160\n# ℹ 90 more rows\n# ℹ 4 more variables: BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>,\n# geometry <MULTIPOLYGON [°]>\n```\n\n\n:::\n:::\n\n\nLet's calculate the average birth rate, SIDS rate, and the non-white birth rate and SIDS rate for the entire state. We will add this as a single feature to our existing feature layer. To do so, we will use the R package [`dplyr`](https://dplyr.tidyverse.org/) for manipulating our data.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(dplyr)\n\nnc_summary <- nc_sf |> \n summarise(\n across( # <1>\n .cols = c(ends_with(\"74\"), ends_with(\"79\")), # <2>\n .fns = mean # <3>\n ),\n NAME = \"Total\" # <4>\n ) \n\nnc_summary\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\nSimple feature collection with 1 feature and 7 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965\nGeodetic CRS: NAD27\n# A tibble: 1 × 8\n BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 NAME geometry\n <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <MULTIPOLYGON [°]>\n1 3300. 6.67 1051. 4224. 8.36 1353. Total (((-75.97629 36.51793, -75.9772…\n```\n\n\n:::\n:::\n\n1. The [`across()`](https://dplyr.tidyverse.org/reference/across.html) function applies a function to multiple columns at once.\n2. We specify the columns we will be applying a function to in `.cols`. We use the `tidyselect` helpers to catch any columns that end with `74` or `79`.\n3. The `.fns` argument specifies which functions will be applied to the columns. In this case, we apply on the `mean()` function to calculate the average. \n4. The `NAME` field is set manually to the value of `\"Total\"` to indicate that it is not a county. \n\nIn order to add this new aggregate feature to the `FeatureLayer` we must create a reference to the layer using `arc_open()`. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc_url <- \"https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer/0\" \n\nnc <- arc_open(nc_url)\n```\n:::\n\n```\n<FeatureLayer>\nName: North Carolina SIDS\nGeometry Type: esriGeometryPolygon\nCRS: 4267\nCapabilities: Create,Delete,Query,Update,Editing\n```\n:::{.callout-note}\nThe url you use here will be different than the one you see. Be sure to grab the correct url from the content listing for your item. \n:::\n\nNow that we have a `FeatureLayer` object we can add features to it using `add_features()`. There are a few key arguments to the function:\n\n- `x` is the `FeatureLayer` object that we want to add features to \n- `.data` is an `sf` object that we want to add to the `FeatureLayer`\n- `match_on` determines how to match sf columns to `FeatureLayer` fields\n\nBy default, `add_features()` will compare the column names of the `sf` object to that of the `FeatureLayer`. We can find the field names and aliases for a `FeatureLayer` by using the `list_fields()` function. Pass the results to `tibble::as_tibble()` to make them more readable.\n\nSince we know that the column names match those of the `FeatureLayer`, we can pass `nc_summary` directly to `add_feature()`.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nadd_res <- add_features(nc, nc_summary)\nadd_res\n```\n:::\n\n```\n objectId uniqueId globalId success\n1 101 101 NA TRUE\n```\n\n:::{.callout-tip}\nIf you are adding many features at one time, consider changing the value of `chunk_size`. By default, `add_features()` will add up to 2000 features at a time and send the requests in parallel. Depending on the geometry type and precision, it may be worthwhile to make that number smaller. If the data are truly massive, consider breaking up the task into smaller manageable chunks. \n:::\n\nOnce we've added the results to the `FeatureLayer`, we may want to refresh the object to catch any important changes to the metadata. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc <- refresh_layer(nc)\n```\n:::\n\n```\n<FeatureLayer>\nName: North Carolina SIDS\nGeometry Type: esriGeometryPolygon\nCRS: 4267\nCapabilities: Create,Delete,Query,Update,Editing\n```\n\nWe can see that the `FeatureLayer` now has 101 features as opposed to the original 100. To sanity check, we can query `nc` to see how the value comes back.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc_avgs <- nc |> \n filter(NAME == \"Total\") |> \n collect()\n\nnc_avgs\n```\n:::\n\n```\nSimple feature collection with 1 feature and 15 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965\nGeodetic CRS: NAD27\n object_id AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry\n1 101 NA NA NA NA Total NA NA NA 3299.62 6.67 1050.81 4223.92 8.36 1352.81 MULTIPOLYGON (((-75.9248 36...\n```\n", | ||
"supporting": [], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
_freeze/docs/editing/overwrite-features/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"hash": "296b79211db0afeba03ac04d1a72ee9b", | ||
"result": { | ||
"engine": "knitr", | ||
"markdown": "---\ntitle: \"Overwrite Hosted Feature Layer\"\nfreeze: true\n--- \n\n\n\n\nFrom time to time as the owner of a Feature Layer, you may need to completely overwrite the data in the service. Overwriting a web layer from ArcGIS Pro may lead to a loss of associated pop-ups and symbology. One way to get around this is to truncate the feature service and append new data to the same service. \n\nFor this example, we need to be the owner of a Feature Service. As such, we will use the North Carolina SIDS dataset we created in the [**Publishing from R**](../layers/publishing.qmd) tutorial. If you have not done that tutorial, complete it first. \n\n## Truncating a Feature Layer\n\nTruncating a Feature Layer deletes every single record in the service and resets the auto-increment of the object ID. Truncating a service does not change the field definitions or permit us to add or remove fields. If you wish to do so, publish a new layer instead. \n\nBefore we can modify a service, we must first authorize ourselves with the portal. To do so we will use the `auth_code()` authorization flow. If you have not yet configured you environment to authorize with your portal, follow the [**Connecting to your Portal**](../connecting-to-a-portal.qmd) tutorial. \n\nFirst load `arcgis`.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(arcgis)\n```\n:::\n\n```\nAttaching core arcgis packages:\n - {arcgisutils} v0.3.0\n - {arcgislayers} v0.2.0\n```\n\nNext, authorize with the portal and set the access token.\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntoken <- auth_code()\nset_auth_token(token)\n```\n:::\n\n```\nToken set to environment variable `ARCGIS_TOKEN`\n```\n\nNow that we have verified our identity with our portal we can create a `FeatureLayer` object in R from our hosted service. From your [content listing](https://arcgis.com/home/content.html) find the Feature Layer url. \n\n:::{.callout-tip}\nRevisit the \"Obtaining a feature layer url\" section of the [**Read hosted data**](../read-data.qmd#obtaining-a-feature-layer-url) tutorial if you forgot how to retrieve the service url.\n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nfurl <- \"https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer/0\" # <1>\n\nnc <- arc_open(furl) # <2>\nnc\n```\n:::\n\n```\n<FeatureLayer>\nName: North Carolina SIDS\nGeometry Type: esriGeometryPolygon\nCRS: 4267\nCapabilities: Create,Delete,Query,Update,Editing\n```\n\n:::{.aside}\nThis is the url of your hosted feature service. Yours will be different than the URL shown here. Note that the `/0` indicates the layer index. You can often copy the url from under the URL section on the right hand menu and append the `/0` to it. \n:::\n\n\nBefore we can truncate the `FeatureLayer`, we should check to see that the layer itself supports this operation. The `supportsTruncate` attribute will return `TRUE` if we can truncate it. If not, we're out of luck and need to create an entirely new service! \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc[[\"supportsTruncate\"]]\n```\n:::\n\n\nSince we know that we can truncate the service, we can go ahead and do so. \n\n\n::: {.cell}\n\n```{.r .cell-code}\ntruncate_res <- truncate_layer(nc)\ntruncate_res\n```\n:::\n\nWe store the result into `truncate_res` to see the results. Let's now go ahead and refresh our layer and check to see if the changes have taken place. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc <- refresh_layer(nc)\nnc\n```\n:::\n\n```\n<FeatureLayer>\nName: North Carolina SIDS\nGeometry Type: esriGeometryPolygon\nCapabilities: Create,Delete,Query,Update,Editing\n```\n\nAfter refreshing the layer we can see that there are now 0 features! Success! There are still 15 fields and we still have the same name and geometry type. \n\n## Adding features\n\nNow that we have deleted all of the features of the layer, lets go ahead and add some new ones. Let's read the `nc.shp` file from sf into memory, give it a slight modification, and add those features to our service. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(sf)\n```\n\n::: {.cell-output .cell-output-stderr}\n\n```\nLinking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE\n```\n\n\n:::\n\n```{.r .cell-code}\nnc_sf <- read_sf(system.file(\"shape/nc.shp\", package = \"sf\"))\nnc_sf\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\nSimple feature collection with 100 features and 14 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965\nGeodetic CRS: NAD27\n# A tibble: 100 × 15\n AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74\n <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <int> <dbl> <dbl> <dbl>\n 1 0.114 1.44 1825 1825 Ashe 37009 37009 5 1091 1 10\n 2 0.061 1.23 1827 1827 Alle… 37005 37005 3 487 0 10\n 3 0.143 1.63 1828 1828 Surry 37171 37171 86 3188 5 208\n 4 0.07 2.97 1831 1831 Curr… 37053 37053 27 508 1 123\n 5 0.153 2.21 1832 1832 Nort… 37131 37131 66 1421 9 1066\n 6 0.097 1.67 1833 1833 Hert… 37091 37091 46 1452 7 954\n 7 0.062 1.55 1834 1834 Camd… 37029 37029 15 286 0 115\n 8 0.091 1.28 1835 1835 Gates 37073 37073 37 420 0 254\n 9 0.118 1.42 1836 1836 Warr… 37185 37185 93 968 4 748\n10 0.124 1.43 1837 1837 Stok… 37169 37169 85 1612 1 160\n# ℹ 90 more rows\n# ℹ 4 more variables: BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>,\n# geometry <MULTIPOLYGON [°]>\n```\n\n\n:::\n:::\n\n\nRather than publish the polygons as they are, let's calculate the convex hull of each shape and publish those. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc_convex <- st_convex_hull(nc_sf)\nplot(st_geometry(nc_convex))\n```\n:::\n\n\nLet's take this sf object and add them as features to our now empty `FeatureLayer`. To add features, we use the `add_features()` function. The first argument is the `FeatureLayer` (or `Table`) that we are adding features to. The second is the `sf` object that we will be adding to the layer.\n\n:::{.callout-tip}\nIt is important to note that the column names of the `sf` object must match the names of the fields in the `FeatureLayer`, otherwise `arcgis` does not know which column matches which field. \n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nadd_res <- add_features(nc, nc_convex)\n```\n:::\n\n```\nWarning: CRS missing from `x` cannot verify matching CRS.\n```\n\nWe receive a warning because there is no spatial reference in the hosted `FeatureLayer` after truncating. Print the `add_res` object to see if each feature was successfully added.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nhead(add_res)\n```\n:::\n\n```\n objectId uniqueId globalId success\n1 1 1 NA TRUE\n2 2 2 NA TRUE\n3 3 3 NA TRUE\n4 4 4 NA TRUE\n5 5 5 NA TRUE\n6 6 6 NA TRUE\n```\n\nNow that we have added our features, let us refresh the layer again.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc <- refresh_layer(nc)\n```\n:::\n\n```\n<FeatureLayer>\nName: North Carolina SIDS\nGeometry Type: esriGeometryPolygon\nCRS: 4267\nCapabilities: Create,Delete,Query,Update,Editing\n```\n\nIf you view the hosted Feature Layer in the map viewer, you should now see the convex hulls.\n\n![](images/nc-convex-hulls.png)\n\n\n", | ||
"supporting": [], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
_freeze/docs/editing/update-features/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"hash": "ca8b525b1df3f025c22a9a7102c54cb1", | ||
"result": { | ||
"engine": "knitr", | ||
"markdown": "---\ntitle: \"Updating features\"\n---\n\n\n\n\nIn the previous section we added a new feature that is the average of our numeric columns and stored the results in the variable `nc_avgs`. When looking at it, we can see that the `AREA` AND `PERIMETER` values are missing. These might be helpful at a later point. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(arcgis)\nset_arc_token(auth_code())\n\nnc_url <- \"https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer/0\" \n\nnc <- arc_open(nc_url)\n```\n:::\n\n\n\nIn this section we will use the function `update_features()` to modify these values. First, let's create a new object called `to_update` that has the `AREA` and `PERIMETER` computed. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc_area_perim <- nc_avgs |> \n mutate(\n AREA = st_area(geometry) / 1e10,\n PERIMETER = s2::s2_perimeter(geometry) / 1e5\n )\n\nnc_area_perim\n```\n:::\n\n```\nSimple feature collection with 1 feature and 15 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965\nGeodetic CRS: NAD27\n object_id AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry\n1 101 12.70259 [m^2] 33.58819 NA NA Total NA NA NA 3299.62 6.67 1050.81 4223.92 8.36 1352.81 MULTIPOLYGON (((-75.9248 36...\n```\n\nLike `add_features()`, we need to be able to match columns to their respective fields. The `match_on` argument is used to specify if the column names match the field name or field alias. \n\nIn the case of `update_features()` we also need to be able to match the features in the `sf` dataset to the _exact_ feature in the `FeatureLayer`. We do this by providing the object ID of the feature. This tells ArcGIS _which_ features we are actually going to update. \n\nWhen using `update_features()` we should be aware that _every_ column present in the `sf` object will be updated _including the geometry_. For this reason, we should select only those columns which we truly wish to update.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nto_update <- nc_area_perim |> \n st_drop_geometry() |> \n select(object_id, AREA, PERIMETER)\n\nto_update\n```\n:::\n\n\nHere we use `sf::st_drop_geometry()`to remove the geometry of our object since we do not want to update the geometry in our `FeatureLayer`. We also only select the `object_id`, `AREA`, and `PERIMETER` columns so that we do not make errant updates. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nupdate_res <- update_features(nc, to_update)\n```\n:::\n\n```\n$updateResults\n objectId uniqueId globalId success\n1 101 101 NA TRUE\n```\n\nOur update process was successful! We can repeat our previous query to verify this. \n\n\n::: {.cell}\n\n```{.r .cell-code}\n nc |> \n filter(NAME == \"Total\") |> \n collect()\n```\n:::\n\n```\nSimple feature collection with 1 feature and 15 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965\nGeodetic CRS: NAD27\n object_id AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry\n1 101 12.70259 33.58819 NA NA Total NA NA NA 3299.62 6.67 1050.81 4223.92 8.36 1352.81 MULTIPOLYGON (((-75.9248 36...\n```", | ||
"supporting": [], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
Oops, something went wrong.