Skip to content

Commit

Permalink
update language
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Dec 1, 2023
1 parent 82e156d commit 5573a02
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"hash": "2dbaa08ab80f0f8b867b6bffe1fdcd40",
"hash": "fe1b8ab08937b8d016baeea33d20fb35",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Publishing from R\"\nsubtitle: \"Create Hosted ArcGIS Online or Enterprise Feature Services from R\"\nfreeze: true\n---\n\n\nWhile you may often consume data as an R user, you may also want to also publish data as a hosted feature service. In this tutorial we will go over how to publish an `sf` object <!-- or a `data.frame`--> to ArcGIS Online or Enterprise. \n\n## Authorization\n\nIn order to publish content to ArcGIS Online or Enterprise, we must first obtain an access token permitting us to do so. \n\n:::{.callout-caution}\nIf you have not yet set up your R environment for authorization, see [**Connecting to your Portal**](./connecting-to-a-portal.qmd). Ensure that the environment variables `ARCGIS_CLIENT` and `ARCGIS_USER` are set at minimum. If you are using Enterprise ensure that `ARCGIS_HOST` is properly set as well.\n:::\n\nWe must go through the code flow to set our credentials.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(arcgis)\n\ntoken <- auth_code() # <1>\nset_auth_token(token) # <2>\n#> Token set to environment variable `ARCGIS_TOKEN`\n```\n:::\n\n1. We create an access token\n2. We set it to an environment variable.\n\nNow that we have authorized to our Portal, we will be able to publish our content. \n\n## Publishing {sf} objects\n\nTo publish an `{sf}` object to our portal, we can use the function `publish_layer()`. The publishing process requires us to add an item to our portal and publish it. The `publish_layer()` function handles these steps for us. \n\nLet's read in the [North Carolina SIDS](https://cran.r-project.org/web/packages/spdep/vignettes/sids.html) dataset that comes packaged with `sf` and store it in an object called `nc`. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc <- sf::read_sf(system.file(\"shape/nc.shp\", package = \"sf\"))\nnc\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\nNow that we have an sf object and we have authorized with our portal, all that's left is to publish the item! \n\n`publish_layer()` has only two required arguments: \n\n- `x` the sf object or `data.frame`\n- `title` the title of layer we are creating\n\n\n::: {.cell}\n\n```{.r .cell-code}\nres <- publish_layer(nc, \"North Carolina SIDS\")\nres\n#> $services\n#> type\n#> 1 Feature Service\n#> serviceurl\n#> 1 https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North Carolina SIDS/FeatureServer\n#> size jobId serviceItemId\n#> 1 125766 f14451a7-325b-40b0-85c3-534bcf122806 32511ce0413f40d08303e267a7093be0\n#> encodedServiceURL\n#> 1 https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer\n```\n:::\n\n\n\n## Reading the published Feature Layer\n\nThe output of this function is a list that contains information about where the sf object was published.\nWe can retrieve the `encodedServiceUrl` from the response and read the response. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc_fserver <- arc_open(res[[c(\"services\", \"encodedServiceURL\")]])\nnc_fserver\n#> <FeatureServer <1 layer, 0 tables>>\n#> CRS: 4267\n#> Capabilities: Create,Delete,Query,Update,Editing\n#> 0: North Carolina SIDS (esriGeometryPolygon)\n```\n:::\n\n\nYou'll notice that this is a `FeatureServer`. All items that are published to a Portal become their own Feature Server with a single `FeatureLayer`.\n\nWe can extract a single layer from the `FeatureServer` using `get_layer()`. We provide the `FeatureServer` as the first argument and then the ID of the layer we want as the second argument. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nget_layer(nc_fserver, 0)\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\n\n\n\n## Publishing `data.frame`s \n\nPublishing a `data.frame` follows the same steps as those above. The difference is that it creates a `Table` object. Try repeating the same process but using the palmerpenguins dataset! \n\n\n::: {.cell}\n\n```{.r .cell-code}\n# install.packages(\"palmerpenguins\")\npalmerpenguins::penguins\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 344 × 8\n species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g\n <fct> <fct> <dbl> <dbl> <int> <int>\n 1 Adelie Torgersen 39.1 18.7 181 3750\n 2 Adelie Torgersen 39.5 17.4 186 3800\n 3 Adelie Torgersen 40.3 18 195 3250\n 4 Adelie Torgersen NA NA NA NA\n 5 Adelie Torgersen 36.7 19.3 193 3450\n 6 Adelie Torgersen 39.3 20.6 190 3650\n 7 Adelie Torgersen 38.9 17.8 181 3625\n 8 Adelie Torgersen 39.2 19.6 195 4675\n 9 Adelie Torgersen 34.1 18.1 193 3475\n10 Adelie Torgersen 42 20.2 190 4250\n# ℹ 334 more rows\n# ℹ 2 more variables: sex <fct>, year <int>\n```\n\n\n:::\n:::\n\n\n:::{.callout-tip title=\"Solution\" collapse=\"true\"}\n\n::: {.cell}\n\n```{.r .cell-code}\npublish_layer(palmerpenguins::penguins, \"Palmer Penguins\")\n```\n:::\n\n\n:::\n",
"markdown": "---\ntitle: \"Publishing from R\"\nsubtitle: \"Create Hosted ArcGIS Online or Enterprise Feature Services from R\"\nfreeze: true\n---\n\n\nWhile you may often consume data as an R user, you may also want to also publish data as a hosted feature service. In this tutorial we will go over how to publish an `sf` object <!-- or a `data.frame`--> to ArcGIS Online or Enterprise. \n\n## Authorization\n\nIn order to publish content to ArcGIS Online or Enterprise, we must first obtain an access token permitting us to do so. \n\n:::{.callout-caution}\nIf you have not yet set up your R environment for authorization, see [**Connecting to your Portal**](./connecting-to-a-portal.qmd). Ensure that the environment variables `ARCGIS_CLIENT` and `ARCGIS_USER` are set at minimum. If you are using Enterprise ensure that `ARCGIS_HOST` is properly set as well.\n:::\n\nWe must go through the code flow to set our credentials.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(arcgis)\n\ntoken <- auth_code() # <1>\nset_auth_token(token) # <2>\n#> Token set to environment variable `ARCGIS_TOKEN`\n```\n:::\n\n1. We create an access token\n2. We set it to an environment variable.\n\nNow that we have authorized to our Portal, we will be able to publish our content. \n\n## Publishing {sf} objects\n\nTo publish an `{sf}` object to our portal, we can use the function `publish_layer()`. The publishing process requires us to add an item to our portal and publish it. The `publish_layer()` function handles these steps for us. \n\nLet's read in the [North Carolina SIDS](https://cran.r-project.org/web/packages/spdep/vignettes/sids.html) dataset that comes packaged with `sf` and store it in an object called `nc`. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc <- sf::read_sf(system.file(\"shape/nc.shp\", package = \"sf\"))\nnc\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\nNow that we have an sf object and we have authorized with our portal, all that's left is to publish the item! \n\n`publish_layer()` has only two required arguments: \n\n- `x` the sf object or `data.frame`\n- `title` the title of layer we are creating\n\n\n::: {.cell}\n\n```{.r .cell-code}\nres <- publish_layer(nc, \"North Carolina SIDS\")\nres\n#> $services\n#> type\n#> 1 Feature Service\n#> serviceurl\n#> 1 https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North Carolina SIDS/FeatureServer\n#> size jobId serviceItemId\n#> 1 125766 f14451a7-325b-40b0-85c3-534bcf122806 32511ce0413f40d08303e267a7093be0\n#> encodedServiceURL\n#> 1 https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer\n```\n:::\n\n\nNow from your Portal's [Content listing](https://arcgis.com/home/content.html) you should see your feature service. If you open it up you should see something like the below.\n\n![](images/publish/nc-sids.png){width=70%}\n\n## Reading the published Feature Layer\n\nThe output of this function is a list that contains information about where the sf object was published.\nWe can retrieve the `encodedServiceUrl` from the response and read the response. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nnc_fserver <- arc_open(res[[c(\"services\", \"encodedServiceURL\")]])\nnc_fserver\n#> <FeatureServer <1 layer, 0 tables>>\n#> CRS: 4267\n#> Capabilities: Create,Delete,Query,Update,Editing\n#> 0: North Carolina SIDS (esriGeometryPolygon)\n```\n:::\n\n\nYou'll notice that this is a `FeatureServer`. All items that are published to a Portal become their own Feature Server with a single `FeatureLayer`.\n\nWe can extract a single layer from the `FeatureServer` using `get_layer()`. We provide the `FeatureServer` as the first argument and then the ID of the layer we want as the second argument. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nget_layer(nc_fserver, 0)\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\n\n\n\n## Publishing `data.frame`s \n\nPublishing a `data.frame` follows the same steps as those above. The difference is that it creates a `Table` object. Try repeating the same process but using the palmerpenguins dataset! \n\n\n::: {.cell}\n\n```{.r .cell-code}\n# install.packages(\"palmerpenguins\")\npalmerpenguins::penguins\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 344 × 8\n species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g\n <fct> <fct> <dbl> <dbl> <int> <int>\n 1 Adelie Torgersen 39.1 18.7 181 3750\n 2 Adelie Torgersen 39.5 17.4 186 3800\n 3 Adelie Torgersen 40.3 18 195 3250\n 4 Adelie Torgersen NA NA NA NA\n 5 Adelie Torgersen 36.7 19.3 193 3450\n 6 Adelie Torgersen 39.3 20.6 190 3650\n 7 Adelie Torgersen 38.9 17.8 181 3625\n 8 Adelie Torgersen 39.2 19.6 195 4675\n 9 Adelie Torgersen 34.1 18.1 193 3475\n10 Adelie Torgersen 42 20.2 190 4250\n# ℹ 334 more rows\n# ℹ 2 more variables: sex <fct>, year <int>\n```\n\n\n:::\n:::\n\n\n:::{.callout-tip title=\"Solution\" collapse=\"true\"}\n\n::: {.cell}\n\n```{.r .cell-code}\npublish_layer(palmerpenguins::penguins, \"Palmer Penguins\")\n```\n:::\n\n\n:::\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
Expand Down
Binary file added location-services/images/publish/nc-sids.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions location-services/publishing.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ res
#> 1 https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer
```

Now from your Portal's [Content listing](https://arcgis.com/home/content.html) you should see your feature service. If you open it up you should see something like the below.

![](images/publish/nc-sids.png){width=70%}

## Reading the published Feature Layer

Expand Down

0 comments on commit 5573a02

Please sign in to comment.