Skip to content

Commit 89f246a

Browse files
committed
updates to markdown in the create_data notebook.
1 parent be11d4c commit 89f246a

File tree

1 file changed

+111
-29
lines changed

1 file changed

+111
-29
lines changed

labs/create_data.ipynb

Lines changed: 111 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,125 @@
11
{
22
"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+
},
334
{
435
"cell_type": "code",
536
"execution_count": null,
6-
"metadata": {
7-
"collapsed": false
8-
},
37+
"metadata": {},
938
"outputs": [],
1039
"source": [
1140
"import getpass\n",
12-
"from arcgis.gis import GIS\n",
13-
"from arcgis import geocoding\n",
41+
"from arcgis.gis import *\n",
1442
"\n",
1543
"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."
1954
]
2055
},
2156
{
2257
"cell_type": "code",
2358
"execution_count": null,
24-
"metadata": {
25-
"collapsed": false
26-
},
59+
"metadata": {},
2760
"outputs": [],
2861
"source": [
29-
"map1 = gis.map('Los Angeles', 10)\n",
62+
"map1 = dev_gis.map('Los Angeles', 10)\n",
3063
"map1"
3164
]
3265
},
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+
},
3379
{
3480
"cell_type": "code",
3581
"execution_count": null,
36-
"metadata": {
37-
"collapsed": false
38-
},
82+
"metadata": {},
3983
"outputs": [],
4084
"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",
4486
" type: \"Feature Service\" \"\"\", \n",
4587
" max_items=10)\n",
4688
"flyr_search_results"
4789
]
4890
},
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+
},
49100
{
50101
"cell_type": "code",
51102
"execution_count": null,
52-
"metadata": {
53-
"collapsed": false
54-
},
103+
"metadata": {},
55104
"outputs": [],
56105
"source": [
57-
"# retrieve the fl collection item, then the list of \n",
58-
"# layers from the layers property, and the layer you want\n",
59106
"flyr_collection_item = flyr_search_results[0]\n",
60107
"flayers = flyr_collection_item.layers\n",
61108
"flayer = flayers[0]\n",
62109
"flayer.properties.name"
63110
]
64111
},
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+
},
65123
{
66124
"cell_type": "code",
67125
"execution_count": null,
@@ -70,23 +128,29 @@
70128
},
71129
"outputs": [],
72130
"source": [
73-
"#from arcgis import geocoding\n",
74131
"from arcgis import geometry\n",
75132
"from arcgis import features\n",
76133
"\n",
77134
"def create_feature(map1, g):\n",
78135
" try:\n",
79136
" oid = 1\n",
80-
" #geocoded = geocoding.reverse_geocode(g)\n",
81-
" #print(geocoded['address']['Match_addr'])\n",
82137
" pt = geometry.Point(g)\n",
83138
" feat = features.Feature(geometry=pt, attributes={'OBJECTID': oid})\n",
84139
" flayer.edit_features(adds=[feat])\n",
85140
" print(str(g))\n",
86141
" map1.draw(g)\n",
87142
" oid += 1\n",
88143
" 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."
90154
]
91155
},
92156
{
@@ -100,6 +164,15 @@
100164
"map1.on_click(create_feature)"
101165
]
102166
},
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+
},
103176
{
104177
"cell_type": "code",
105178
"execution_count": null,
@@ -111,6 +184,15 @@
111184
"map1.clear_graphics()"
112185
]
113186
},
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+
},
114196
{
115197
"cell_type": "code",
116198
"execution_count": null,
@@ -126,7 +208,7 @@
126208
"metadata": {
127209
"anaconda-cloud": {},
128210
"kernelspec": {
129-
"display_name": "Python 3",
211+
"display_name": "Python [default]",
130212
"language": "python",
131213
"name": "python3"
132214
},
@@ -140,7 +222,7 @@
140222
"name": "python",
141223
"nbconvert_exporter": "python",
142224
"pygments_lexer": "ipython3",
143-
"version": "3.5.2"
225+
"version": "3.5.3"
144226
}
145227
},
146228
"nbformat": 4,

0 commit comments

Comments
 (0)