{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Offline Areas from Feature Layer\n",
    "\n",
    "This notebook demonstrates how to create map areas for a webmap based on a polygon feature layer.  The resulting map areas can be downloaded and used in ArcGIS Field Maps.\n",
    "\n",
    "To use this notebook, you need to update variables at the top of the script, including:\n",
    "1. `offline_map_item_id` to the item id of the webmap you would like to create areas for\n",
    "2. `feature_layer_item_id` to the item id of the feature layer to use as input areas, `feature_layer_id` to specify the specific layer and `area_name_attribute` for the attribute of the layer to use to name new map areas\n",
    "3. `output_title_template`, `output_snippet_template`, `output_tags` and `output_folder` to specify properties for the output map areas \n",
    "\n",
    "This notebook assumes that:\n",
    "1. You are the owner of the webmap\n",
    "2. Your webmap has a raster tile basemap, like Imagery\n",
    "3. You are using Enterprise 10.7+ or ArcGIS Online with ArcGIS API for Python v2.3.x\n",
    "\n",
    "For more information on working with map areas see:\n",
    "- [Managing offline map areas](https://developers.arcgis.com/python/guide/managing-offline-map-areas/)\n",
    "- [OfflineMapAreaManager API reference](https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#offlinemapareamanager)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "trusted": true
   },
   "outputs": [],
   "source": [
    "from arcgis.gis import GIS\n",
    "from arcgis.mapping import WebMap\n",
    "\n",
    "#The item id of the webmap that you want to create offline areas for\n",
    "offline_map_item_id = '07a9a65edb07470e9781a5a493f1d92c'\n",
    "#The item id of the feature layer to use for the areas you want to create\n",
    "#If the feature layer has more than 16 features, only the first 16 features will be queried\n",
    "feature_layer_item_id = '12e3f9dadda048e993d504362cf815b4'\n",
    "#The id of the layer to use\n",
    "feature_layer_id = 0\n",
    "#Field name of the attribute to use to name the areas that are created\n",
    "area_name_attribute = 'Sextant'\n",
    "\n",
    "#Properties for the output areas\n",
    "output_title_template = '{} Area'\n",
    "output_snippet_template = 'A map that contains parks and trails in the {} sextant of Portland, OR, USA.'\n",
    "output_tags='test'\n",
    "output_folder = 'pdx'\n",
    "#Min scale for map areas - World scale\n",
    "map_area_min_scale = 147914382\n",
    "#Max scale for map areas - Neighborhood scale\n",
    "map_area_max_scale = 20000\n",
    "\n",
    "#Using built-in user\n",
    "#For information on different authentication schemes see\n",
    "#https://developers.arcgis.com/python/guide/working-with-different-authentication-schemes/\n",
    "#and for protecting your credentials see\n",
    "#https://developers.arcgis.com/python/guide/working-with-different-authentication-schemes/#protecting-your-credentials\n",
    "gis = GIS('home')\n",
    "\n",
    "offline_map_item = gis.content.get(offline_map_item_id)\n",
    "offline_map = WebMap(offline_map_item)\n",
    "\n",
    "offline_areas_item = gis.content.get(feature_layer_item_id)\n",
    "offline_areas = offline_areas_item.layers[feature_layer_id].query(result_record_count=16, return_all_records=False)\n",
    "\n",
    "#If recreating areas for the same map, could cleanup and remove existing preplanned areas\n",
    "#for ids in offline_map.offline_areas.list():\n",
    "#    ids.delete()\n",
    "    \n",
    "for offline_area in offline_areas.features:\n",
    "    area_name = offline_area.attributes[area_name_attribute]\n",
    "\n",
    "    print('Creating offline map area for ' + area_name)\n",
    "    \n",
    "    item_prop = {'title': output_title_template.format(area_name),\n",
    "                 'snippet': output_snippet_template.format(area_name),\n",
    "                 'tags': [output_tags]}\n",
    "\n",
    "    try:        \n",
    "        map_area = offline_map.offline_areas.create(area=offline_area.geometry,\n",
    "                                                    item_properties=item_prop,\n",
    "                                                    folder=output_folder,\n",
    "                                                    min_scale=map_area_min_scale,\n",
    "                                                    max_scale=map_area_max_scale)\n",
    "\n",
    "    except:\n",
    "        print('Failed creating map area for ' + area_name)\n"
   ]
  }
 ],
 "metadata": {
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "7.0"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.9.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}