# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] slideshow={"slide_type": "slide"}
# # Lesson 06
# ## Analysis Module within ArcPy

# + [markdown] slideshow={"slide_type": "slide"}
# # Objectives
#
# - Examine the Analysis Tools
# - Examine the Feature Analysis Tools
# - Learn about the `tempfile` module
#

# + [markdown] slideshow={"slide_type": "slide"}
# ## Analysis Toolbox
#
# - Collection of tools that perform the most fundamental GIS operations
# -  Five toolsets
#     + Extract
#     + Overlay
#     + Pairwise Overlay
#     + Proximity
#     + Statistics
#

# + [markdown] slideshow={"slide_type": "subslide"}
# ### Analysis Toolbox Example
#
# - Here we need to split the center road line by each district

# + slideshow={"slide_type": "fragment"}
import os
import arcpy
from arcpy import env
from arcpy import da

env.overwriteOutput = True
results = []
cnt_lines = "./data/Centerlines.shp"

with da.SearchCursor("./data/School_Districts.shp", "SHAPE@") as srows:
    for idx, row in enumerate(srows):
        out_fc = os.path.join(env.scratchGDB, f"road_line_{idx}")
        geom = row[0]
        out_fc = arcpy.analysis.Clip(cnt_lines, geom, out_feature_class=out_fc)[0]
        results.append(out_fc)
results

# + [markdown] slideshow={"slide_type": "-"}
# ### Example Notes:
#
# - Mixed cursor with standard tools
# - Used geometry as input to tools

# + [markdown] slideshow={"slide_type": "slide"}
# # The `tempfile` Module

# + slideshow={"slide_type": "-"}
import tempfile

# + [markdown] slideshow={"slide_type": "-"}
# **Purpose:** Create temporary file system objects such as files and directories

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Why Use `tempfile`?
#
# 1. Creating temporary files with unique names
# 2. Helps prevent the theft of data
# 3. Built in secure file generation
# 4. Automatic file deletion when process finishes

# + [markdown] slideshow={"slide_type": "slide"}
# ## Temporary Files
#
# - Used with non-persistent files that are not shared between applications
# - Files have no true name/extension
# -

with tempfile.TemporaryFile() as temp:
    print('temp:')
    print('  {!r}'.format(temp))
    print('temp.name:')
    print('  {!r}'.format(temp.name))

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Temporary Files Continued
#
# - There is the ability to read/write data into the temporary file
#     + Defaults into `w+b`
# -

with tempfile.TemporaryFile() as temp:
    temp.write(b'Python is so Amazing') # notice the b for bytes
    temp.seek(0) # rewind to the start of the file
    print(temp.read()) # read the file

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Temporary Files Continued
#
# - Allows for the usage of other write modes.
# -

with tempfile.TemporaryFile(mode='w+t') as writer: # text data writer 
    writer.writelines(['**Shopping List**\n','Bread\n', 'Eggs\n', 'Milk\n'])
    writer.seek(0)
    for line in writer:
        print(line)

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Named Temporary Files
#
# - Create temporary files that can be shared during the context of that process
# -

import pathlib
with tempfile.NamedTemporaryFile() as temp:
    print('temp:')
    print('  {0}'.format(temp))
    print('temp.name:')
    print('  {0}'.format(temp.name))

    f = pathlib.Path(temp.name)
f"Do I Exist Anymore? {f.exists()}"

# ## Predicted Named Temporary Files
#
# - Sometimes a file extension, prefix, or suffix is needed for a given temporary file
#

with tempfile.NamedTemporaryFile(suffix='.txt',
                                 prefix='adv_python_') as temp:
    print('temp:')
    print('  ', temp)
    print('temp.name:')
    print('  ', temp.name)

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Temporary Directories
#
# - There are built in commands to access the OS' temporary folder location
# - `ArcPy` has temporary storage locations just like `tempfile`
# -

print('gettempdir():', tempfile.gettempdir())
print('gettempprefix():', tempfile.gettempprefix())

env.scratchFolder, env.scratchGDB

# + [markdown] slideshow={"slide_type": "subslide"}
# ## Building Temporary Directories
#
# - Sometimes a temporary sub-folder is needed to store information for one piece the process
#     + Think downloading information
# -

pth = None
with tempfile.TemporaryDirectory(suffix='_storage',
                                 prefix='adv_python_') as temp:
    print(f'temp folder: {temp}')
    pth = os.path.join(temp, "test.txt")
    with open(pth, 'w') as writer:
        writer.write('hello there!')
    print("DO I EXIST: ", os.path.isfile(pth))
print("DO I EXIST: ", os.path.isfile(pth))
    

# + [markdown] slideshow={"slide_type": "slide"}
# ## Recall out original example

# +
import os
import arcpy
from arcpy import env
from arcpy import da

env.overwriteOutput = True

results = []
cnt_lines = "./data/Centerlines.shp"

with da.SearchCursor("./data/School_Districts.shp", "SHAPE@") as srows:
    for idx, row in enumerate(srows):
        out_fc = os.path.join(env.scratchGDB, f"road_line_{idx}")
        geom = row[0]
        out_fc = arcpy.analysis.Clip(cnt_lines, geom, out_feature_class=out_fc)[0]
        results.append(out_fc)
results

# + [markdown] slideshow={"slide_type": "-"}
# - Notice we manually set unique feature class names.

# + [markdown] slideshow={"slide_type": "slide"}
# ## Enhancements to Analysis
#
# - Leverage `CreateUniqueNames`
# - Use `tempfile` to generate folders and intermediate files
# - Leverage scratch GDB, scratch folders and in_memory workspaces

# +
import os
import arcpy
import pandas as pd
from arcpy import env
from arcpy import da

env.overwriteOutput = True
dfs = []
results = []
cnt_lines = "./data/Centerlines.shp"

with da.SearchCursor("./data/School_Districts.shp", ["SHAPE@", 'NAME_']) as srows:
    
    for idx, row in enumerate(srows):
        out_fc = arcpy.CreateUniqueName(os.path.join(env.scratchGDB, "road_line"))
        geom = row[0]
        road_lines = arcpy.analysis.Clip(cnt_lines, geom, out_feature_class=out_fc)[0]
        arcpy.management.AddFields(road_lines, "LENGTH_MI FLOAT # # # #;POINTS LONG # # # #")
        arcpy.management.CalculateGeometryAttributes(road_lines, [["length_mi", "LENGTH_GEODESIC"], 
                                                                  ["points", "POINT_COUNT"]], "MILES_US")
        with da.SearchCursor(road_lines, "*") as rows:
            flds = rows.fields
            for r in rows:
                r = dict(zip(flds, r))
                r['SCHOOL_DISTICT'] = row[1]
                results.append(r)
                del r
            del rows
        del idx, row

# + [markdown] slideshow={"slide_type": "slide"}
# ## Examine Number of Roads in Each District

# + slideshow={"slide_type": "-"}
df = pd.DataFrame(results)

# + slideshow={"slide_type": "-"}
import numpy as np
from matplotlib import cm

# + slideshow={"slide_type": "-"}
color = cm.plasma(np.linspace(.4,.8, 10))
df.SCHOOL_DISTICT.value_counts().plot(kind='bar', color=color)
# -

with da.SearchCursor(results[0], "*") as rows:
    print(rows.fields)
    for row in rows:
        print(row)
        break

StarcoderdataJupyterScriptsDedupFiltered