{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Demo 5b using Keyword Arguments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This script is the Demo 5b script but uses \"keyword arguments\" within a couple of the select routines.\n",
    "Keywords are not required, but can be used to rearrange the function parameters (see especially the Select Layer by Location routine below where this is done)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nExample of using the Make Feature Layer tool that is used to implement the\\nSelect Layer by Attribute and Select Layer by Location ArcGIS tools.  The\\nCopy Features and Copy Row tools are used to store the selected records.\\nCopy Features will store the selected geographic features and attributes from\\nthe parcel file, whereas, the Copy Rows will be used to store the selected\\nattributes from the streets file.\\n\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Demo 5b  Select Layer by Attribute and Select Layer by Location (using keyword arguments)\n",
    "# Created by:  Nathan Jennings\n",
    "#              www.urbandalespatial.com\n",
    "# Created on:  01.16.2011\n",
    "# Updated on:  02.03.2020\n",
    "# Copyright:   2015\n",
    "\n",
    "'''\n",
    "Example of using the Make Feature Layer tool that is used to implement the\n",
    "Select Layer by Attribute and Select Layer by Location ArcGIS tools.  The\n",
    "Copy Features and Copy Row tools are used to store the selected records.\n",
    "Copy Features will store the selected geographic features and attributes from\n",
    "the parcel file, whereas, the Copy Rows will be used to store the selected\n",
    "attributes from the streets file.\n",
    "\n",
    "'''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Demo 5b - Select Layer by Attribte and Location\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print ('Demo 5b - Select Layer by Attribte and Location\\n')\n",
    "\n",
    "import arcpy, sys, traceback\n",
    "\n",
    "#arcpy.env.workspace = \"C:\\\\PythonPrimer\\\\Chapter05\\\\Data\\\\\"\n",
    "arcpy.env.workspace = \"C:\\\\temp\\\\Chapter05\\\\Data\\\\\"\n",
    "\n",
    "streets = \"Sacramento_Streets.shp\"\n",
    "street_layer = \"street_layer\"\n",
    "\n",
    "parcels = \"City_Parcels.shp\"\n",
    "parcel_layer = \"parcel_layer\"\n",
    "search_dist = '200 FEET'\n",
    "\n",
    "out_parcel_fc = \"out_parcels.shp\"\n",
    "out_street_attributes = \"out_street_attributes.dbf\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the \"try:\" is commented out so the script will run within a Jupyter notebook.\n",
    "The Demo5b.py script using keywords will have the \"try:\" added in."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note below the Select Layer by Attribute using the \"where_clause\" keyword argument syntax."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "using keywords in SelectLayerByAttribute...\n",
      "Since no selection type is provided, the routine will use NEW_SELECTION\n",
      "Number of selected streets in the street layer street_layer with a query: 255\n"
     ]
    }
   ],
   "source": [
    "#try:\n",
    "\n",
    "    # 1. Make a feature layer for streets\n",
    "\n",
    "    if arcpy.Exists(street_layer):\n",
    "        arcpy.Delete_management(street_layer)\n",
    "    \n",
    "    arcpy.MakeFeatureLayer_management(streets, street_layer)\n",
    "\n",
    "    # 2. Create a query statement to select highways\n",
    "\n",
    "    # Create query statement\n",
    "    query = \"\"\"\"CLASS\" = 'H'\"\"\"\n",
    "\n",
    "    # 3. Select by attributes using the query\n",
    "\n",
    "    # Select features by attribute using query\n",
    "\n",
    "    # original SelectLayerByAttribute syntax\n",
    "    #arcpy.SelectLayerByAttribute_management(street_layer,\"NEW_SELECTION\", query)\n",
    "\n",
    "    print (\"using keywords in SelectLayerByAttribute...\")\n",
    "    print (\"Since no selection type is provided, the routine will use NEW_SELECTION\")\n",
    "    arcpy.SelectLayerByAttribute_management(street_layer, where_clause=query)\n",
    "\n",
    "    result = arcpy.GetCount_management(street_layer) \n",
    "    print (\"Number of selected streets in the street layer \" + street_layer + \" with a query: \"+ str(result))\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the use of different keyword arguments in the Select Layer by Location routine.  Some of the parameters have been rearranged.  Keyword arguments can be found in the Python section of the Help documentation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of selected streets in the street layer street_layer with a query: 255\n",
      "Number of selected parcels in the parcel layer parcel_layer\n",
      " within a distance of 200 FEET: 233\n"
     ]
    }
   ],
   "source": [
    "# 4. Select parcels that are 200 ft from highways\n",
    "\n",
    "    # 4a. Make feature layer\n",
    "\n",
    "    if arcpy.Exists(parcel_layer):\n",
    "        arcpy.Delete_management(parcel_layer)\n",
    "        \n",
    "    arcpy.MakeFeatureLayer_management(parcels, parcel_layer)\n",
    "\n",
    "    \n",
    "    # 4b. Select Parcels\n",
    "\n",
    "    # This is the original syntax for the SelectLayerByLocation routine\n",
    "    #arcpy.SelectLayerByLocation_management(parcel_layer, \"INTERSECT\", \\\n",
    "    #                                       street_layer, search_distance, \"NEW_SELECTION\") \n",
    "\n",
    "\n",
    "    #Here it is with keyword arguments.  Notice the order is changed, too.\n",
    "    # Notice also the selection type is not defined, so this will be a \"NEW_SELECTION\"\n",
    "    arcpy.SelectLayerByLocation_management(parcel_layer, select_features = street_layer, \\\n",
    "                                           search_distance = search_dist, overlap_type = \"INTERSECT\")\n",
    "\n",
    "    # 4c. Get the number of selected records and create a print statement for each\n",
    "    \n",
    "    # print the number of selected records in the respective layers with a query\n",
    "    result = arcpy.GetCount_management(street_layer) \n",
    "    print (\"Number of selected streets in the street layer \" + street_layer + \" with a query: \"+ str(result))\n",
    "\n",
    "    result = arcpy.GetCount_management(parcel_layer) \n",
    "    print (\"Number of selected parcels in the parcel layer \" + parcel_layer + \\\n",
    "          \"\\n within a distance of \" + search_dist + \": \"+ str(result))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write out a feature class and table of different sets of selected features or records."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied selected features from City_Parcels.shp to out_parcels.shp\n",
      "Copied selected attributes from Sacramento_Streets.shp to out_street_attributes.dbf\n",
      "Completed Script\n"
     ]
    }
   ],
   "source": [
    "# 5. Write out selected parcel features to file (SHP)\n",
    "\n",
    "    # Copy the selected polygon features to a new shapefile\n",
    "\n",
    "    if arcpy.Exists(out_parcel_fc):\n",
    "        arcpy.Delete_management(out_parcel_fc)\n",
    "        \n",
    "    arcpy.CopyFeatures_management(parcel_layer, out_parcel_fc)\n",
    "    print (\"Copied selected features from \" + parcels  + \" to \" + out_parcel_fc)\n",
    "\n",
    "    # 6. Write out selected street attributes to a table file (DBF)\n",
    "\n",
    "\n",
    "    # Copy the selected street segment attributes to a dBase table\n",
    "\n",
    "    if arcpy.Exists(out_street_attributes):\n",
    "        arcpy.Delete_management(out_street_attributes)\n",
    "        \n",
    "    arcpy.CopyRows_management(street_layer, out_street_attributes)\n",
    "    print (\"Copied selected attributes from \" + streets + \" to \" + out_street_attributes)\n",
    "\n",
    "    print (\"Completed Script\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exception code provided here for completion.  See the Demo5b.py script for proper syntax."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "except:\n",
    "    # https://pro.arcgis.com/en/pro-app/arcpy/get-started/error-handling-with-python.htm\n",
    "    \n",
    "    tb = sys.exc_info()[2]\n",
    "    tbinfo = traceback.format_tb(tb)[0]\n",
    "    pymsg = \"PYTHON ERRORS:\\nTraceback Info:\\n\" + tbinfo + \"\\nError Info:\\n\" + str(sys.exc_info()[1])\n",
    "    msgs = \"ARCPY ERRORS:\\n\" + arcpy.GetMessages(2) + \"\\n\"\n",
    "\n",
    "    arcpy.AddError(msgs)\n",
    "    arcpy.AddError(pymsg)\n",
    "\n",
    "    print (msgs)\n",
    "    print (pymsg)\n",
    "    \n",
    "    arcpy.AddMessage(arcpy.GetMessages(1))\n",
    "    print (arcpy.GetMessages(1))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
