|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Create Data ArcGIS DevLab\n", |
| 8 | + "This is the completed solution for the [Create data](https://developers.arcgis.com/labs/data/python/create-data/) ArcGIS DevLab. [ArcGIS DevLabs](https://developers.arcgis.com/labs/) are short introductory tutorials to guide you through the three phases of building geospatial apps: Data, Design, Develop" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": null, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [], |
| 16 | + "source": [ |
| 17 | + "import getpass\n", |
| 18 | + "from arcgis.gis import *\n", |
| 19 | + "\n", |
| 20 | + "password = getpass.getpass(\"Please enter password: \")\n", |
| 21 | + "dev_gis = GIS('https://www.arcgis.com', 'username', password)\n", |
| 22 | + "print(\"Successfully logged in to {} as {}\".format(dev_gis.properties.urlKey + '.' + dev_gis.properties.customBaseUrl,\n", |
| 23 | + " dev_gis.users.me.username))" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": 2, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "data": { |
| 33 | + "text/plain": [ |
| 34 | + "[<Item title:\"Griffith Park Access2\" type:Feature Layer Collection owner:johnnyDev>]" |
| 35 | + ] |
| 36 | + }, |
| 37 | + "execution_count": 2, |
| 38 | + "metadata": {}, |
| 39 | + "output_type": "execute_result" |
| 40 | + } |
| 41 | + ], |
| 42 | + "source": [ |
| 43 | + "feature_layer_srch_results = dev_gis.content.search(query='title: \"Griffith*\" AND type: \"Feature Service\"', \n", |
| 44 | + " max_items=10)\n", |
| 45 | + "feature_layer_srch_results" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 3, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [ |
| 53 | + { |
| 54 | + "data": { |
| 55 | + "text/plain": [ |
| 56 | + "'griffith_park_access2'" |
| 57 | + ] |
| 58 | + }, |
| 59 | + "execution_count": 3, |
| 60 | + "metadata": {}, |
| 61 | + "output_type": "execute_result" |
| 62 | + } |
| 63 | + ], |
| 64 | + "source": [ |
| 65 | + "feature_layer_coll_item = feature_layer_srch_results[0]\n", |
| 66 | + "feature_layers = feature_layer_coll_item.layers\n", |
| 67 | + "feature_layer = feature_layers[0]\n", |
| 68 | + "feature_layer.properties.name" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": 4, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [ |
| 76 | + { |
| 77 | + "name": "stdout", |
| 78 | + "output_type": "stream", |
| 79 | + "text": [ |
| 80 | + "OBJECTID\n", |
| 81 | + "name\n", |
| 82 | + "type\n", |
| 83 | + "surface\n" |
| 84 | + ] |
| 85 | + } |
| 86 | + ], |
| 87 | + "source": [ |
| 88 | + "for field in feature_layer.properties['fields']:\n", |
| 89 | + " print(field['name'])" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 5, |
| 95 | + "metadata": { |
| 96 | + "collapsed": true |
| 97 | + }, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "from arcgis import geometry\n", |
| 101 | + "from arcgis import features\n", |
| 102 | + "\n", |
| 103 | + "def create_feature(map1, g):\n", |
| 104 | + " try:\n", |
| 105 | + " oid = 1\n", |
| 106 | + " pt = geometry.Point(g)\n", |
| 107 | + " feat = features.Feature(geometry=pt, attributes={'OBJECTID': 1,\n", |
| 108 | + " 'name': 'name',\n", |
| 109 | + " 'type': 'park',\n", |
| 110 | + " 'surface': 'dirt'})\n", |
| 111 | + " feature_layer.edit_features(adds=[feat])\n", |
| 112 | + " print(str(g))\n", |
| 113 | + " map1.draw(g)\n", |
| 114 | + " except:\n", |
| 115 | + " print(\"Couldn't create the feature. Try again, please...\")" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": 6, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [ |
| 123 | + { |
| 124 | + "data": { |
| 125 | + "application/vnd.jupyter.widget-view+json": { |
| 126 | + "model_id": "57be67419bbb49babb30726e9d29aa91" |
| 127 | + } |
| 128 | + }, |
| 129 | + "metadata": {}, |
| 130 | + "output_type": "display_data" |
| 131 | + }, |
| 132 | + { |
| 133 | + "name": "stdout", |
| 134 | + "output_type": "stream", |
| 135 | + "text": [ |
| 136 | + "{'y': 4031785.200371807, 'type': 'point', 'spatialReference': {'latestWkid': 3857, 'wkid': 102100}, 'x': -13174326.588164143}\n", |
| 137 | + "{'y': 4039276.029143757, 'type': 'point', 'spatialReference': {'latestWkid': 3857, 'wkid': 102100}, 'x': -13181205.920709811}\n", |
| 138 | + "{'y': 4024447.2456564275, 'type': 'point', 'spatialReference': {'latestWkid': 3857, 'wkid': 102100}, 'x': -13167447.255618475}\n" |
| 139 | + ] |
| 140 | + } |
| 141 | + ], |
| 142 | + "source": [ |
| 143 | + "map1 = dev_gis.map('Los Angeles', 10)\n", |
| 144 | + "map1" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": 7, |
| 150 | + "metadata": { |
| 151 | + "collapsed": true |
| 152 | + }, |
| 153 | + "outputs": [], |
| 154 | + "source": [ |
| 155 | + "map1.on_click(create_feature)" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": 8, |
| 161 | + "metadata": { |
| 162 | + "collapsed": true |
| 163 | + }, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "map1.clear_graphics()" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": 9, |
| 172 | + "metadata": { |
| 173 | + "collapsed": true |
| 174 | + }, |
| 175 | + "outputs": [], |
| 176 | + "source": [ |
| 177 | + "map1.add_layer(feature_layer)" |
| 178 | + ] |
| 179 | + } |
| 180 | + ], |
| 181 | + "metadata": { |
| 182 | + "anaconda-cloud": {}, |
| 183 | + "kernelspec": { |
| 184 | + "display_name": "Python [default]", |
| 185 | + "language": "python", |
| 186 | + "name": "python3" |
| 187 | + }, |
| 188 | + "language_info": { |
| 189 | + "codemirror_mode": { |
| 190 | + "name": "ipython", |
| 191 | + "version": 3 |
| 192 | + }, |
| 193 | + "file_extension": ".py", |
| 194 | + "mimetype": "text/x-python", |
| 195 | + "name": "python", |
| 196 | + "nbconvert_exporter": "python", |
| 197 | + "pygments_lexer": "ipython3", |
| 198 | + "version": "3.5.3" |
| 199 | + } |
| 200 | + }, |
| 201 | + "nbformat": 4, |
| 202 | + "nbformat_minor": 2 |
| 203 | +} |
0 commit comments