Skip to content

Commit

Permalink
add freeze blog
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Dec 2, 2023
1 parent 2851656 commit 205f27f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/quarto-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
# From https://github.com/r-lib/actions/tree/v2-branch/setup-r
# - name: Setup R
# uses: r-lib/actions/setup-r@v2
# with:
# use-public-rspm: true
# - uses: r-lib/actions/setup-renv@v2

# NOTE: If Publishing to GitHub Pages, set the permissions correctly (see top of this yaml)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hash": "4e186dd538bcbf752eb80fb2c6e21f30",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Overwrite Hosted Feature Layer\"\nsubtitle: \"Using the truncate and append workflow\"\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**](../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 to it. 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```\n#> Attaching core arcgis packages:\n#> - {arcgisutils} v0.1.0\n#> - {arcgislayers} v0.1.0\n```\n\nNext, authorize with the service itself 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```\n#> Token set to environment variable `ARCGIS_TOKEN`\n```\n\nNow that we have verified our identity with our portal we can create a `FeatureLayer` object 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 <100 features, 15 fields>>\n#> Name: North Carolina SIDS\n#> Geometry Type: esriGeometryPolygon\n#> CRS: 4267\n#> Capabilities: Create,Delete,Query,Update,Editing\n```\n\n:::{.aside}\nThis is the url of your hosted feature service. This will be different than what is 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 <0 features, 15 fields>>\n#> Name: North Carolina SIDS\n#> Geometry Type: esriGeometryPolygon\n#> Capabilities: 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#> Warning: CRS missing from `x` cannot verify matching CRS.\n```\n:::\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\n#> 1 1 1 NA TRUE\n#> 2 2 2 NA TRUE\n#> 3 3 3 NA TRUE\n#> 4 4 4 NA TRUE\n#> 5 5 5 NA TRUE\n#> 6 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 <100 features, 15 fields>>\n#> Name: North Carolina SIDS\n#> Geometry Type: esriGeometryPolygon\n#> CRS: 4267\n#> Capabilities: 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
}
}
1 change: 1 addition & 0 deletions location-services/workflows/overwrite-feature-service.qmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: "Overwrite Hosted Feature Layer"
subtitle: "Using the truncate and append workflow"
freeze: true
---

```{r include = FALSE}
Expand Down

0 comments on commit 205f27f

Please sign in to comment.