{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Bulk service registration\n", "A relatively common use case is when users have existing content published as web services that they want to register in ArcGIS Online. The following workflow takes a service with multiple layers and registers them as individual items, using layer properties to populate metadata fields. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# import all the necessary modules\n", "from arcgis.gis import GIS\n", "from IPython.display import display\n", "import arcgis.features\n", "from getpass import getpass" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "········\n" ] } ], "source": [ "# login to an existing arcgis online organization\n", "gis = GIS(\"https://citygov.maps.arcgis.com/\", \"phammons_citygov\", getpass())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Point to an existing service\n", "First we need to grab the url to an existing service, point it to our gis variable, and store that as a feature layer collection in another variable. We can do that with the FeatureLayerCollection method:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "url = 'https://maps.vcgi.vermont.gov/arcgis/rest/services/EGC_services/OPENDATA_VCGI_BOUNDARIES_SP_NOCACHE_v1/MapServer'\n", "boundaries = arcgis.features.FeatureLayerCollection(url, gis)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a folder\n", "Then we create a folder for these layers to live in:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'id': 'f38c8548802b4576b07d861f6fdb17b5',\n", " 'title': 'boundaries',\n", " 'username': 'phammons_citygov'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gis.content.create_folder(folder = 'boundaries')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Register the layers\n", "Now that we've finished our preliminary stuff, we can get to the fun part -- registering layers! The following block of code iterates through each layer in the service, populates some metadata fields with service properties (title, description, url), and defines others manually (tags, type). Finally all of this info is used to create the layer and move it to the folder we created in the previous step:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for layer in boundaries.layers:\n", " layer_properties = {\n", " 'title': layer.properties['name'],\n", " 'description': layer.properties['description'],\n", " 'tags': 'chittenden, boundaries, bulk service registration, python api',\n", " 'type': 'Feature Service',\n", " 'url': layer.url\n", " }\n", " create_item = gis.content.add(item_properties=layer_properties, data=layer.url)\n", " create_item.move(folder= 'boundaries')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now those layers are available as individual items in the ```boundaries``` folder:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT NAD27 Orthophoto Boundaries - polygons\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT NAD83 Orthophoto Boundaries - polygons\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT House Districts 2002 - polygons\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT Boundaries - county polygons\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT Boundaries - all lines\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT Senate Districts 1992 - lines\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT School Districts - 2014\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT NAD83 Orthophoto 3x3 Quadrant Boundaries - polygons\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT Senate Districts 2012 - polygons\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", " VT Supervisory Unions - 2014\n", " \n", "
Feature Layer Collection by phammons_citygov\n", "
Last Modified: October 11, 2017\n", "
0 comments, 0 views\n", "
\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "boundaries = gis.content.search(query=\"tags:bulk service registration\")\n", "for layer in boundaries:\n", " display(layer)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }