{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Demo 4a - Create and use Make Feature Layers and Count features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the comments, title, import line, variables.\n",
    "Change the workspace folder path to your data location"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Demo 5a - Create Feature Layer with Expression\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Demo 5a  Create Feature Layer using an expression\n",
    "# Created by:  Nathan Jennings\n",
    "#              www.urbandalespatial.com\n",
    "# Created on:  01.14.2011\n",
    "# Updated on:  12.13.2014\n",
    "# Copyright:   2011\n",
    "\n",
    "'''\n",
    "Example of using the Make Feature Layer tool and prints the effect of the\n",
    "query xpression (where clause) parameter when the where clause is used v.\n",
    "not used.\n",
    "\n",
    "The number of \"selected\" features are printed to the screen.\n",
    "\n",
    "\n",
    "'''\n",
    "print ('Demo 5a - Create Feature Layer with Expression\\n')\n",
    "\n",
    "import arcpy, sys, traceback\n",
    "\n",
    "# Change the path as needed\n",
    "#arcpy.env.workspace = \"C:\\\\PythonPrimer\\\\Chapter05\\\\Data\\\\\"\n",
    "arcpy.env.workspace = \"C:\\\\temp\\\\Chapter05\\\\Data\\\\\"\n",
    "\n",
    "streets = \"Sacramento_Streets.shp\"\n",
    "\n",
    "street_layer = \"street_layer\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write the try: block\n",
    "\n",
    "Note the arcpy.GetCount_management routine to determine how many features are in a feature class, feature layer, table, or table view.\n",
    "\n",
    "Use MakeFeatureLayer to create an in-memory representation of the feature class (that is on disk)\n",
    "\n",
    "NOTE:  the \"try:\" line is commented out so the script will run within Jupyter.  The actual demo5a.py script has the try: line added."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of features in the feature class Sacramento_Streets.shp : 9251\n",
      "Number of features in the feature layer street_layer without a query: 9251\n"
     ]
    }
   ],
   "source": [
    "#try:\n",
    "\n",
    "\n",
    "    # print the number of records in the feature class\n",
    "    result = arcpy.GetCount_management(streets) \n",
    "    print (\"Number of features in the feature class \" + streets + \" : \" + str(result))\n",
    "\n",
    "    # check to see if the feature layer exists; if so, delete it\n",
    "    if arcpy.Exists(street_layer):\n",
    "        arcpy.Delete_management(street_layer)\n",
    "\n",
    "    # make the feature layer\n",
    "    arcpy.MakeFeatureLayer_management(streets, street_layer)\n",
    "\n",
    "    # print the number of records in the feature layer without a query\n",
    "    result = arcpy.GetCount_management(street_layer) \n",
    "    print (\"Number of features in the feature layer \" + street_layer + \" without a query: \"+ str(result))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create the MakeFeatureLayer again for the streets feature class, but this time add a query (where clause) to the MakeFeatureLayer routine.\n",
    "\n",
    "Count the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of features in the feature layer street_layer with a query: 255\n"
     ]
    }
   ],
   "source": [
    "# check to see if the feature layer exists; if so, delete it\n",
    "    if arcpy.Exists(street_layer):\n",
    "        arcpy.Delete_management(street_layer)\n",
    "\n",
    "    # create a feature layer using a query\n",
    "\n",
    "    query = \"\"\"\"CLASS\" = 'H'\"\"\"\n",
    "\n",
    "    # make the feature layer using the query parameter\n",
    "    arcpy.MakeFeatureLayer_management(streets, street_layer, query)\n",
    "\n",
    "    # print the number of records in the feature layer with query\n",
    "    result = arcpy.GetCount_management(street_layer) \n",
    "    print (\"Number of features in the feature layer \" + street_layer + \" with a query: \"+ str(result))\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the exception block\n",
    "within the Jupyter notebook, this block will not run.  It is presented to show proper form."
   ]
  },
  {
   "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))"
   ]
  }
 ],
 "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
}
