Skip to content

Commit

Permalink
Merge pull request #28 from R-ArcGIS/update-image-dir
Browse files Browse the repository at this point in the history
reorganize images to match r-bridge-doc repo
  • Loading branch information
JosiahParry authored Sep 18, 2024
2 parents bee12ee + 126f4aa commit 8fdde7f
Show file tree
Hide file tree
Showing 49 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hash": "cc8b2115162b7a41626882bb61fc88e7",
"hash": "607e9cfa89600640ebf5c448a1a26ce7",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Overwrite Hosted Feature Layer\"\nfreeze: auto\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**](/docs/auth/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_arc_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**](/docs/layers/read-layers.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",
Expand Down
Loading

0 comments on commit 8fdde7f

Please sign in to comment.