|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Overview\n", |
| 8 | + "\n", |
| 9 | + "**You will learn:** a fast and easy way to use your Developer Subscription content manager and the [ArcGIS API for Python](https://developers.arcgis.com/python/guide/) to create a data schema in the cloud and populate features for use in your apps.\n", |
| 10 | + "\n", |
| 11 | + "With your [ArcGIS Developer Subscription](https://developers.arcgis.com/pricing/), you can use the [**Create Layer Dev Lab**](https://developers.arcgis.com/labs/data/create-a-new-dataset/) to learn how to create a new empty layer in your account. You can then use the [ArcGIS API for Python](https://developers.arcgis.com/python/) to easily populate the layer with actual features and attributes. \n", |
| 12 | + "\n", |
| 13 | + "The **Create Layer** tool lets you interactively define the name, feature type (points, lines or polygons), and attribute fields for a layer, and then save it to the cloud (ArcGIS Online). This process is called [publishing a hosted feature layer](https://doc.arcgis.com/en/arcgis-online/share-maps/publish-features.htm). Behind every feature layer is a [REST feature service](https://resources.arcgis.com/en/help/arcgis-rest-api/#/Feature_Service/02r3000000z2000000/), and the ArcGIS Platform provides many tools to populate this layer with data. The [features module](http://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html) and [geometry module](http://esri.github.io/arcgis-python-api/apidoc/html/arcgis.geometry.html) in the ArcGIS API for Python can be used with the [map widget](http://esri.github.io/arcgis-python-api/apidoc/html/arcgis.widgets.html) to interactively edit a feature layer by adding new features. Once created and populated, your apps can access this service to query, draw or edit the data.\n", |
| 14 | + "\n", |
| 15 | + "In this lab you will create a new point dataset (feature layer) with the Create Layer tool and store it in your own developer [ArcGIS Online](https://doc.arcgis.com/en/arcgis-online/share-maps/hosted-web-layers.htm) account. You'll then use the ArcGIS API for Python to add features into the new layer. The lab will create points near Los Angeles, CA.\n", |
| 16 | + "\n", |
| 17 | + "You will run this lab in a [Jupyter Notebook](/python/guide/using-the-jupyter-notebook-environment/) IDE that is hosted by Esri. Notebooks created here are temporary, hence you must download them if you want to preserve them for later. However, you can [install the ArcGIS API for Python](/python/guide/install-and-set-up/) locally on your computer and run this lab from it, in which case, the notebooks are stored locally on your machine." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "## Steps\n", |
| 25 | + "#### Create a connection to your Developer Subscription account\n", |
| 26 | + "\n", |
| 27 | + "Log into ArcGIS Online by making a GIS connection to ArcGIS Online using your developer account. Replace `username` and `password` with the credentials to your ArcGIS Developer account. \n", |
| 28 | + " \n", |
| 29 | + "It is not good security practice to leave account credentials in source code shared by others, but it is beyond the scope of this lab to go over security best practices. The [getpass module](https://pymotw.com/2/getpass/) for Python provides an easy way to hide passwords in shared code.\n", |
| 30 | + "\n", |
| 31 | + "You can use a print statement with some gis properties to verify sign-in." |
| 32 | + ] |
| 33 | + }, |
3 | 34 | { |
4 | 35 | "cell_type": "code", |
5 | 36 | "execution_count": null, |
6 | | - "metadata": { |
7 | | - "collapsed": false |
8 | | - }, |
| 37 | + "metadata": {}, |
9 | 38 | "outputs": [], |
10 | 39 | "source": [ |
11 | 40 | "import getpass\n", |
12 | | - "from arcgis.gis import GIS\n", |
13 | | - "from arcgis import geocoding\n", |
| 41 | + "from arcgis.gis import *\n", |
14 | 42 | "\n", |
15 | 43 | "password = getpass.getpass(\"Please enter password: \")\n", |
16 | | - "gis = GIS('https://browns.maps.arcgis.com', 'johnnyDev', password)\n", |
17 | | - "print(\"Successfully logged in to {} as {}\".format(gis.properties.urlKey + '.' + gis.properties.customBaseUrl,\n", |
18 | | - " gis.users.me.username))" |
| 44 | + "dev_gis = GIS('https://www.arcgis.com', 'johnnyDev', password)\n", |
| 45 | + "print(\"Successfully logged in to {} as {}\".format(dev_gis.properties.urlKey + '.' + dev_gis.properties.customBaseUrl,\n", |
| 46 | + " dev_gis.users.me.username))" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "#### Assign a map object to a variable. Center the map in Los Angeles with a zoom level of your choice." |
19 | 54 | ] |
20 | 55 | }, |
21 | 56 | { |
22 | 57 | "cell_type": "code", |
23 | 58 | "execution_count": null, |
24 | | - "metadata": { |
25 | | - "collapsed": false |
26 | | - }, |
| 59 | + "metadata": {}, |
27 | 60 | "outputs": [], |
28 | 61 | "source": [ |
29 | | - "map1 = gis.map('Los Angeles', 10)\n", |
| 62 | + "map1 = dev_gis.map('Los Angeles', 10)\n", |
30 | 63 | "map1" |
31 | 64 | ] |
32 | 65 | }, |
| 66 | + { |
| 67 | + "cell_type": "markdown", |
| 68 | + "metadata": {}, |
| 69 | + "source": [ |
| 70 | + "#### Search for the empty feature layer\n", |
| 71 | + "\n", |
| 72 | + "You can use the ArcGIS API for Python [Helper Objects](https://developers.arcgis.com/python/guide/using-the-GIS/#Helper-objects) to manage GIS resources. One such helper object is the [Content Manager](http://esri.github.io/arcgis-python-api/apidoc/html/arcgis.gis.toc.html#contentmanager). You use its [search method](https://developers.arcgis.com/python/guide/accessing-and-creating-your-content/#Searching-for-content) to retrieve items from your GIS. The ArcGIS API for Python Guide also documents the process of [searching your gis for feature layers](https://developers.arcgis.com/python/guide/working-with-feature-layers-and-features/#Searching-the-GIS-for-feature-layers).\n", |
| 73 | + "\n", |
| 74 | + "The ArcGIS API for Python is a front end to the [ArcGIS REST API](https://developers.arcgis.com/documentation/core-concepts/rest-api/). The full [search reference](http://resources.arcgis.com/en/help/arcgis-rest-api/#/Search_reference/02r3000000mn000000/) documentation details how to search for items, including proper use of wildcards for characters and phrases. You'll use a wildcard to search for the empty feature layer created in the first portion of this lab (Steps 1-8).\n", |
| 75 | + "\n", |
| 76 | + "The `search()` method returns a list of `Item` objects that match the search criteria. " |
| 77 | + ] |
| 78 | + }, |
33 | 79 | { |
34 | 80 | "cell_type": "code", |
35 | 81 | "execution_count": null, |
36 | | - "metadata": { |
37 | | - "collapsed": false |
38 | | - }, |
| 82 | + "metadata": {}, |
39 | 83 | "outputs": [], |
40 | 84 | "source": [ |
41 | | - "# retrieve feature layer to add attributes to\n", |
42 | | - "# search for feature layers\n", |
43 | | - "flyr_search_results = gis.content.search(query=\"\"\"title: \"Griffith*\" AND \n", |
| 85 | + "flyr_search_results = dev_gis.content.search(query=\"\"\"title: \"Griffith*\" AND \n", |
44 | 86 | " type: \"Feature Service\" \"\"\", \n", |
45 | 87 | " max_items=10)\n", |
46 | 88 | "flyr_search_results" |
47 | 89 | ] |
48 | 90 | }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "#### Retrieve the feature layer object from the search results\n", |
| 96 | + "\n", |
| 97 | + "Since the search returns a list, you'll use Python [indexing](https://docs.python.org/3/tutorial/introduction.html#lists) to retrieve the object you want. In this case, the item returned is a feature layer collection item, so you'll need to obtain the list of layers in the collection and use indexing to access the layer you want." |
| 98 | + ] |
| 99 | + }, |
49 | 100 | { |
50 | 101 | "cell_type": "code", |
51 | 102 | "execution_count": null, |
52 | | - "metadata": { |
53 | | - "collapsed": false |
54 | | - }, |
| 103 | + "metadata": {}, |
55 | 104 | "outputs": [], |
56 | 105 | "source": [ |
57 | | - "# retrieve the fl collection item, then the list of \n", |
58 | | - "# layers from the layers property, and the layer you want\n", |
59 | 106 | "flyr_collection_item = flyr_search_results[0]\n", |
60 | 107 | "flayers = flyr_collection_item.layers\n", |
61 | 108 | "flayer = flayers[0]\n", |
62 | 109 | "flayer.properties.name" |
63 | 110 | ] |
64 | 111 | }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "#### Define a function to serve as the event listener for the map widget on_click event\n", |
| 117 | + "\n", |
| 118 | + "You'll use a Python function to add fetures to the feature layer for locations you click on the map. Further illustrations of concepts and operations are available at [using the geometry service and geometry functions](https://developers.arcgis.com/python/sample-notebooks/using-geometry-functions/) in the [ArcGIS API for Python Guide](https://developers.arcgis.com/python/guide/). The API Reference documents the map widget [draw method](http://esri.github.io/arcgis-python-api/apidoc/html/arcgis.widgets.html#arcgis.widgets.MapView.draw) that you'll use to initially place graphic markers at the locations where you'll add features to the layer.\n", |
| 119 | + "\n", |
| 120 | + "The function creates a feature from the location clicked on the map. The **`OBJECTID`** attribute **MUST** be included in the attributes dictionary parameter to create a valid feature. You can also add additional attributes from the layer schema if desired." |
| 121 | + ] |
| 122 | + }, |
65 | 123 | { |
66 | 124 | "cell_type": "code", |
67 | 125 | "execution_count": null, |
|
70 | 128 | }, |
71 | 129 | "outputs": [], |
72 | 130 | "source": [ |
73 | | - "#from arcgis import geocoding\n", |
74 | 131 | "from arcgis import geometry\n", |
75 | 132 | "from arcgis import features\n", |
76 | 133 | "\n", |
77 | 134 | "def create_feature(map1, g):\n", |
78 | 135 | " try:\n", |
79 | 136 | " oid = 1\n", |
80 | | - " #geocoded = geocoding.reverse_geocode(g)\n", |
81 | | - " #print(geocoded['address']['Match_addr'])\n", |
82 | 137 | " pt = geometry.Point(g)\n", |
83 | 138 | " feat = features.Feature(geometry=pt, attributes={'OBJECTID': oid})\n", |
84 | 139 | " flayer.edit_features(adds=[feat])\n", |
85 | 140 | " print(str(g))\n", |
86 | 141 | " map1.draw(g)\n", |
87 | 142 | " oid += 1\n", |
88 | 143 | " except:\n", |
89 | | - " print(\"Couldn't match address. Try another place...\")" |
| 144 | + " print(\"Couldn't create the feature. Try again, please...\")" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "markdown", |
| 149 | + "metadata": {}, |
| 150 | + "source": [ |
| 151 | + "#### Execute the on_click event to begin adding locations to the map\n", |
| 152 | + "\n", |
| 153 | + "Use the function you defined above as the callback parameter for the map widget [on_click](http://esri.github.io/arcgis-python-api/apidoc/html/arcgis.widgets.html#arcgis.widgets.MapView.on_click) event. Click on the map to draw a graphic marker for each feature you want to add to the layer. The callback also outputs a string representation of the geometry." |
90 | 154 | ] |
91 | 155 | }, |
92 | 156 | { |
|
100 | 164 | "map1.on_click(create_feature)" |
101 | 165 | ] |
102 | 166 | }, |
| 167 | + { |
| 168 | + "cell_type": "markdown", |
| 169 | + "metadata": {}, |
| 170 | + "source": [ |
| 171 | + "#### Clear the graphic markers from the map\n", |
| 172 | + "\n", |
| 173 | + "You'll clear the graphics so you can subsequently add the layer to the map to verify features have been added." |
| 174 | + ] |
| 175 | + }, |
103 | 176 | { |
104 | 177 | "cell_type": "code", |
105 | 178 | "execution_count": null, |
|
111 | 184 | "map1.clear_graphics()" |
112 | 185 | ] |
113 | 186 | }, |
| 187 | + { |
| 188 | + "cell_type": "markdown", |
| 189 | + "metadata": {}, |
| 190 | + "source": [ |
| 191 | + "#### Use the map widget `add_layer` method to add the feature layer to the map\n", |
| 192 | + "\n", |
| 193 | + "You should see the locations you clicked in the previous steps now visualized as features in the feature layer, with symbology defined by the Create Layer tool." |
| 194 | + ] |
| 195 | + }, |
114 | 196 | { |
115 | 197 | "cell_type": "code", |
116 | 198 | "execution_count": null, |
|
126 | 208 | "metadata": { |
127 | 209 | "anaconda-cloud": {}, |
128 | 210 | "kernelspec": { |
129 | | - "display_name": "Python 3", |
| 211 | + "display_name": "Python [default]", |
130 | 212 | "language": "python", |
131 | 213 | "name": "python3" |
132 | 214 | }, |
|
140 | 222 | "name": "python", |
141 | 223 | "nbconvert_exporter": "python", |
142 | 224 | "pygments_lexer": "ipython3", |
143 | | - "version": "3.5.2" |
| 225 | + "version": "3.5.3" |
144 | 226 | } |
145 | 227 | }, |
146 | 228 | "nbformat": 4, |
|
0 commit comments