Ejemplo n.º 1
0
def split_ndom(img_path, delete=False):
    in_raster = arcpy.sa.Raster(img_path)
    if delete:
        if os.path.exists(resources.temp_split_ndom):
            shutil.rmtree(resources.temp_split_ndom)
    basename = "{}_".format(os.path.splitext(os.path.basename(img_path))[0])
    os.makedirs(os.path.join(resources.temp_split_ndom, basename),
                exist_ok=True)
    arcpy.SplitRaster_management(in_raster,
                                 os.path.join(resources.temp_split_ndom,
                                              basename),
                                 basename,
                                 "SIZE_OF_TILE",
                                 tile_size="512 512",
                                 overlap=256)
    imgs = [
        os.path.join(resources.temp_split_ndom, basename,
                     img) for img in os.listdir(
                         os.path.join(resources.temp_split_ndom, basename))
        if img.lower().endswith(".tif")
    ]
    return imgs
Ejemplo n.º 2
0
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# clipRaster2.py
# Created on: 2022-04-11 15:44:00.00000
#   (generated by JCT)
# Description: 分割栅格
# ps: 要用python2.7+arcpy  或者 ArcGIS自带的python
# ---------------------------------------------------------------------------

# python27
# Import arcpy module
import arcpy
year="2016"
for i in range(9,13):
    month = str(i)
    # Local variables:
    v2019tif = "F:\\result\\"+year+"_" + month + "\\"+year+"_" + month + ".tif"
    clip16 = "F:\\result\\"+year+"_" + month + "\\clip16"
    clip16__2_ = clip16
    parallelProcessingFactor = "1"

    # Process: 分割栅格
    tempEnvironment0 = arcpy.env.parallelProcessingFactor
    arcpy.env.parallelProcessingFactor = parallelProcessingFactor
    arcpy.SplitRaster_management(v2019tif, clip16, year+"_" + month, "NUMBER_OF_TILES", "TIFF", "NEAREST", "8 16",
                                 "2048 2048", "0", "PIXELS", "", "", "", "NONE", "DEFAULT", "")# 8 16 可以修改
    arcpy.env.parallelProcessingFactor = tempEnvironment0
    print(month+" is ok" )


Ejemplo n.º 3
0
    XMin = imageOpened.extent.XMin
    YMin = imageOpened.extent.YMin

    # Create the string that contains the origin (offset) from which to start the tiling
    origin = str(XMin - ((overlap / 2) * pixelSize)) + " " + str(YMin - (
        (overlap / 2) * pixelSize))

    # Create the string that contains the tile size
    tileSizeString = str(tileSize) + " " + str(tileSize)

    print "Tiling input..."
    print "origin: " + origin + " " + "tile size: " + tileSizeString

    # Tile the image using the tile size and overlap parameters.
    arcpy.SplitRaster_management(inputFile, tileFolderPath, inputFile.replace('.tif', "_"+str(tileSize)+"_x_"+str(tileSize)+"_tile_"), "SIZE_OF_TILE",\
                                 "TIFF", "BILINEAR","#", tileSizeString, overlap, "PIXELS",\
                                 "#", origin)

    print "The tiles have now been created in: " + tileFolderPath

    #######################################################################
    # Run the LFT on all tiles
    #######################################################################

    # Reset arcpy workspace in order to use arcpy.ListRasters again
    arcpy.env.workspace = (tileFolderPath)

    # List Rasters in tile folder
    theInputTiles = arcpy.ListRasters()

    # Iterate the LFT on all the tiles in the folder
Ejemplo n.º 4
0
            # if os.path.exists(dirgrids+"\\errores.txt"):
            # txt =  open(dirgrids+"\\errores.txt", "r")
            # for grds in txt:
            # raster = grds.split("_")[0]+"_"+grds.split("_")[1]
            # arcpy.SplitRaster_management(raster, dirgrids, raster + "_", "NUMBER_OF_TILES",  "GRID", "#", "6 3", "#", "0", "DEGREES", "#", "#")
            # print "\t", os.path.basename(raster), "fix splited"

            ###### Get a list of raster in workspace
            arcpy.env.workspace = dirBase + "\\" + rcp + "\\global_" + str(
                res) + "\\" + model + "\\" + ens + "\\" + period
            rasterList = sorted(arcpy.ListRasters("", "GRID"))
            for raster in rasterList:
                if not arcpy.Exists(dirgrids + "\\" + raster + "_17"):
                    arcpy.SplitRaster_management(raster, dirgrids,
                                                 raster + "_",
                                                 "NUMBER_OF_TILES", "GRID",
                                                 "#", "6 3", "#", "0",
                                                 "DEGREES", "#", "#")
                    print "\t ", os.path.basename(
                        raster), " splited"  # + "_"+str(i)
                else:
                    print "\t ", os.path.basename(raster), " splited"
            ####### Convert to Asciis
            # print "\n .> Convert to Asciis: ", rcp, model, str(res), ens, period, "\n"

            #########Get a list of raster in processing dir
            arcpy.env.workspace = dirgrids
            rasterList = sorted(arcpy.ListRasters("", "GRID"))
            for raster in rasterList:

                ############# Convert to ESRI-Ascii
Ejemplo n.º 5
0
import arcpy
import sys

imgPath = sys.argv[1]
imgDir = sys.argv[2]
imgPrefixName = sys.argv[3]
imgSuffixName = sys.argv[4]
gridPath = sys.argv[5]

arcpy.SplitRaster_management(imgPath, imgDir, imgPrefixName, "POLYGON_FEATURES", imgSuffixName, "NEAREST",
                             split_polygon_feature_class=gridPath)
Ejemplo n.º 6
0
def Guidos_Post(city, inDir, workFld):
    import traceback, time, arcpy, os, subprocess
    from arcpy import env
    arcpy.CheckOutExtension('Spatial')

#-------- DIRECTORY SETUP ------------------------------------------------
    """ Working Directory """
    if arcpy.Exists(str(workFld) + '/' + city + '_GUIDOS.gdb') == False:
        arcpy.CreateFileGDB_management(str(workFld), str(city) + '_GUIDOS.gdb')
    else:
        pass
    workDir = str(workFld) + '/' + city + '_GUIDOS.gdb'
    arcpy.env.workspace = workDir

    """ Report File Directory """
    reportfileDir = str(workFld) + '/Logs'
    """ Frequent Directory """
    freqDir = str(workFld) + '/' + city +'_Freq.gdb'
    """ Final Geodatabase """
    finalDir = str(workFld) + '/' + city + '_Final.gdb'

    """ Projection File Directory """
    prjDir = str(inDir) + '/Prj'

    """ Split Raster Directory """
    if os.path.isdir(str(workFld) + '/' + city + '_Split') == True:
        pass
    else:
        os.makedirs(str(workFld) + '/' + city + '_Split')
    splitDir = str(workFld) + '/' + city + '_Split'

    """ Set Workspace Environments """
    arcpy.env.workspace = workDir
    arcpy.env.scratch = str(inDir) + '/Scratch.gdb'
    arcpy.env.overwriteOutput = True

    #-----------------------------------------------------------------------------
    # BEGIN ANALYSIS
	#-----------------------------------------------------------------------------
    try:
        #-------- LOGFILE CREATION ---------------------------------------------
        """ Create report file for each metric """
##        try:
        loglist = sorted(f for f in os.listdir(reportfileDir) if f.startswith(str(city) + '_Conn'))
        tmpName = loglist[-1]
##        except:
##            tmpName = city + '_Conn_' + time.strftime('%Y%m%d_%H-%M')  + '.txt'
        reportfileName = reportfileDir + '/' + tmpName

        try:
			reportFile = open(reportfileName, 'a')
        except:
			reportFile = open(reportfileName, 'w')
			print 'No log for GUIDOS_Prep'

        try:
            loglist = sorted (f for f in os.listdir(reportfileDir) if f.startswith(str(city) + '_Reuse'))
            tmpName = loglist[-1]
        except:
            tmpName = city + '_Reuse_' + time.strftime('%Y%m%d_%H-%M')  + '.txt'
        reportfileName = reportfileDir + '/' + tmpName

        try:
			ReuseRF = open(reportfileName, 'a')
        except:
            ReuseRF = open(reportfileName, 'w')
            print 'Creating Reuse Log'

        print 'Connectivity Start Time: ' + time.asctime()
        reportFile.write("For each piece, convert the raster from a tiled TIFF to a striped TIFF.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        #-------- PROCESSING LAYERS ----------------------------------------------
        """ Set Environments """
        #Figure out the correct UTM Zone
        prjNumb = arcpy.Describe(str(freqDir) + '/LC').spatialReference.name
        prjNumb = prjNumb[-3:]
        prjfileUTM = prjDir + '/NAD 1983 UTM Zone ' + prjNumb + '.prj'

        AllRast = 'yes'

        """ -------- RUN GUIDOS ---------------------------------------------- """
        arcpy.env.workspace = splitDir
    	guidlist = arcpy.ListRasters('S_FWW*')
    	""" For each raster piece, run GUIDOS """
    	for r in guidlist:
			guidexe = str(inDir) + '/mspa_win64.exe'
			inrast = str(splitDir) + '/' + str(r)
			outrast = 'G_' + str(r)
			outdir = str(splitDir) + '/'
			argu = (guidexe, '-i', inrast, '-o', outrast, '-eew', '30', '-internal', '0', '-odir', outdir, '-transition', '0')
			subprocess.call(argu)

			""" If GUIDOS worked, Reproject the rasters """
			if arcpy.Exists(outrast) == True:
				# Project the output raster
				oLeft = arcpy.GetRasterProperties_management(r, "LEFT").getOutput(0)
				oBottom = arcpy.GetRasterProperties_management(r, "BOTTOM").getOutput(0)
				nBottom = arcpy.GetRasterProperties_management(outrast, 'BOTTOM').getOutput(0)

				xMove = float(oLeft) + 0.5
				yMove = float(oBottom) - float(nBottom)

				arcpy.Shift_management(outrast, 'Sh_' + str(r), xMove, yMove, r)

				numb = r.replace('S_FWW_WB_', '')

				arcpy.env.extent = "MAXOF"
				arcpy.env.snapRaster = str(splitDir) + '/' + str(r)
				descLC = arcpy.Describe(str(freqDir) + '/LC')
				arcpy.ProjectRaster_management('Sh_' + str(r), 'Conn_' + str(numb), descLC.spatialReference, '', '', '', '', descLC.spatialReference)
			else:
				""" If GUIDOS didn't work, quit after loop """
				AllRast = 'no'
				print str(outrast) + ' not run properly'

        reportFile.write("For each piece, run the GUIDOS (Graphical User Interface for the Description of image Objects and their Shapes) v2.1 MSPA (Morphological Spatial Pattern Analysis) Standalone Tool (http://forest.jrc.ec.europa.eu/download/software/guidos) using 8 neighbor connectivity, 30 pixel edge, transition off, and intext off.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        reportFile.write("Project each GUIDOS piece into UTM, shifting to the location of the original piece.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        """-------- If all the GUIDOS Rasters worked, Post-Process ------------ """
        if AllRast == 'yes':
            """-------- Split the Raster As Needs, Process Each Piece ----------------- """
            """ Check if the raster should be split """
            columns = arcpy.GetRasterProperties_management(splitDir + '/FWW_WB.tif', 'COLUMNCOUNT').getOutput(0)
            xsplit = int(float(columns) / 8000) + 1
            rows = arcpy.GetRasterProperties_management(splitDir + '/FWW_WB.tif', 'ROWCOUNT').getOutput(0)
            ysplit = int (float(rows) / 8000) + 1

            """-------- If no split, run the analysis --------------------------------- """
            if xsplit*ysplit == 1:
                """ Copy Raster """
                arcpy.CopyRaster_management(splitDir + '/Conn.TIF', workDir + '/Conn')
                reportFile.write("Copy Tiff into Working GDB.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

                """-------- If split, run the analysis on each piece and recombine --------- """
            else:
                """ Set Environments """
                arcpy.env.extent = 'FWW_WB.tif'
                arcpy.env.snapRaster = 'FWW_WB.tif'

                """ Split the Raster """
                arcpy.SplitRaster_management('FWW_WB.tif', splitDir, 'NoO_WB_', 'NUMBER_OF_TILES', 'TIFF', '', str(xsplit) + ' ' + str(ysplit), '',  '', '')
                reportFile.write("Split the reclassified land cover into the same pieces as previous but with no overlap.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

                """ Clip the overlap off the tiles """
                pieces = int(xsplit) * int(ysplit)
                for tiles in range(int(pieces)):
                    if arcpy.Exists('NoO_WB_' + str(tiles) + '.TIF') == True:
                        """ Set Environments """
                        arcpy.env.extent = 'NoO_WB_' + str(tiles) + '.TIF'
                        arcpy.env.snapRaster = 'NoO_WB_' + str(tiles) + '.TIF'

                        """ Extract the Area of Interest """
                        EbM = arcpy.sa.ExtractByMask('Conn_' + str(tiles) + '.TIF', 'NoO_WB_' + str(tiles) + '.TIF')
                        EbM.save(workDir + '/Conn_' + str(tiles))
                    else:
                        pass
                reportFile.write("Clip each GUIDOS output to the corresponding piece of the second raster split to eliminate overlap.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

                """ Set Environments """
                arcpy.env.extent = workDir + '/FWW_WB'
                arcpy.env.snapRaster = workDir + '/FWW_WB'
                arcpy.env.workspace = workDir

                """ Mosaic tiles together """
                RastList = arcpy.ListRasters('Conn_*')
                arcpy.MosaicToNewRaster_management(RastList, workDir, 'Conn', '', '8_BIT_UNSIGNED', 1, 1, '', '')
                reportFile.write("Mosaic all of the clipped GUIDOS output tiles into one raster.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            """ Burn water into the Output Raster """
            watercon = arcpy.sa.Con(freqDir + '/LC', 10, 'Conn', 'value = 10')
            watercon.save('Conn_WithWat')
            reportFile.write("Using the original land cover, burn water pixels into raster using a conditional statement if Land Cover Value = 10; for true: 10; for false: GUIDOS raster.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            """--------- Clip the EA Boundaries to the County Lines, if necessary ----- """
            if arcpy.Exists(str(freqDir) + '/Bnd_Cty') == False:
                """ Copy Counties to Frequent and Project """
                arcpy.MakeFeatureLayer_management(str(inDir) + '/Input.gdb/Counties_Alb', 'Cty')
                arcpy.SelectLayerByLocation_management('Cty', 'CONTAINS', 'BG_Alb', '', 'NEW_SELECTION')
                arcpy.FeatureClassToFeatureClass_conversion(str(inDir) + '/Input.gdb/Counties_Alb', str(freqDir), 'Counties_Alb')
                arcpy.SelectLayerByAttribute_management('Cty', 'CLEAR_SELECTION')
                descLC = arcpy.Describe(str(freqDir) + '/LC')
                arcpy.Project_management('Counties_Alb', 'Counties', descLC.spatialReference)

                """ Clip Boundary to County Lines """
                arcpy.Clip_analysis(str(freqDir) + '/Bnd', str(freqDir) + '/Counties', str(freqDir) + '/Bnd_Cty')
                reportFile.write("Clip the EnviroAtlas Community Boundary to the county lines for the community to limit the output to land area.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')
                ReuseRF.write("Bnd_Cty--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            else:
                reportFile.write("Clip the EnviroAtlas Community Boundary to the county lines for the community to limit the output to land area.--Bnd_Cty" + '--\n')

        	"""-------- Check that the Analysis Area is covered by the LC -------------- """
        	""" Create a Polygon Version of the LC """
            if arcpy.Exists(freqDir + '/LC_Poly') == False:
                arcpy.env.snapRaster = str(freqDir) + '/LC'
                arcpy.env.extent = str(freqDir) + '/LC'
                ReC = arcpy.sa.Reclassify(str(freqDir) + '/LC', 'Value', arcpy.sa.RemapValue([[0,0],[10,1],[20,1],[21,1],[22,1],[30,1],[40,1],[52,1],[70,1],[80,1],[82,1],[91,1],[92,1]]))
                ReC.save(str(freqDir) + '/AreaIO')
                arcpy.RasterToPolygon_conversion(str(freqDir) + '/AreaIO', str(freqDir) + '/LC_Poly', 'SIMPLIFY')
                arcpy.env.extent = workDir + '/FWW_WB'
                arcpy.env.snapRaster = workDir + '/FWW_WB'

            """ Buffer the LC Polygon by -500m """
            if arcpy.Exists(freqDir + '/Bnd_Cty_500m') == False:
    			arcpy.Buffer_analysis(str(freqDir) + '/Bnd_Cty', str(freqDir) + '/Bnd_Cty_500m', '500 meters')
    			arcpy.EliminatePolygonPart_management(str(freqDir) + '/Bnd_Cty_500m', str(freqDir) + '/Bnd_Cty_500m_EP', 'PERCENT', '', '30', 'CONTAINED_ONLY')
    			arcpy.Delete_management(str(freqDir) + '/Bnd_Cty_500m')
    			arcpy.Rename_management(str(freqDir) + '/Bnd_Cty_500m_EP', str(freqDir) + '/Bnd_Cty_500m')

            """ Identify whether LC is large enough """
            arcpy.MakeFeatureLayer_management(str(freqDir) + '/LC_Poly', 'LClyr')
            arcpy.MakeFeatureLayer_management(str(freqDir) + '/Bnd_Cty_500m', 'BC_500lyr')

            arcpy.SelectLayerByLocation_management('BC_500lyr', 'COMPLETELY_WITHIN', 'LClyr', '', 'NEW_SELECTION')
            bigEnough = float(arcpy.GetCount_management('BC_500lyr').getOutput(0))
            arcpy.SelectLayerByAttribute_management('BC_500lyr', 'CLEAR_SELECTION')

            """ If the LC isn't large enough, edit erroneous BGS """
            if bigEnough == 0:
    			arcpy.Clip_analysis(freqDir + '/Bnd_Cty', freqDir + '/LC_Poly', 'Bnd_Cty_LC')
    			reportFile.write("Because the community boundary extends beyond the Land Cover, clip the boundary to the land cover.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            """ Clip the Raster to Bnd_Cty """
            try:
    			EbM = arcpy.sa.ExtractByMask('Conn_WithWat', 'Bnd_Cty_LC')
            except:
    			EbM = arcpy.sa.ExtractByMask('Conn_WithWat', freqDir + '/Bnd_Cty')
            EbM.save('Conn_Bnd')
            reportFile.write("Extract by Mask the area of the projected raster that is within the clipped EnviroAtlas Community Boundary.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            """ Add Colormap """
            arcpy.AddColormap_management('Conn_Bnd', '', str(inDir) + '/Templates/GUIDOS2.clr')

            """ Convert to TIFF for sharing """
            arcpy.CopyRaster_management('Conn_Bnd', splitDir + '/' + str(city) + '_Conn.tif')
            reportFile.write("Add a default GUIDOS colormap to the final raster from any of the original GUIDOS output tiles.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            """ Convert to Web Mercator """
            prjfileWM = prjDir + '/WGS 1984 Web Mercator (auxiliary sphere).prj'
            if arcpy.Exists(freqDir + '/Bnd_WM') == False:
                arcpy.Project_management(freqDir + '/Bnd', freqDir + '/Bnd_WM', prjfileWM)
            arcpy.env.extent = freqDir + '/Bnd_WM'
            arcpy.ProjectRaster_management('Conn_Bnd', 'Conn_WM', prjfileWM)
            reportFile.write("Convert raster into TIFF format for distribution.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            """ Copy to Final Directory """
            arcpy.CopyRaster_management('Conn_WM', finalDir + '/' + str(city) + '_Conn')
    ##            reportFile.write("Step 15--Copy to Final GDB--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            print 'GUIDOS End Time: ' + time.asctime()

        else:
            print 'Some MSPA functions did not complete. Please run manually.'
        #-------------------------------------------------------------------------

        #-------- COMPELETE LOGFILES ---------------------------------------------
        reportFile.close()
    	ReuseRF.close()

	#-----------------------------------------------------------------------------
    # END ANALYSIS
	#-----------------------------------------------------------------------------
    except:
        """ This part of the script executes if anything went wrong in the main script above """
        #-------- PRINT ERRORS ---------------------------------------------------
        print "\nSomething went wrong.\n\n"
        print "Python Traceback Message below:"
        print traceback.format_exc()
        print "\nArcMap Error Messages below:"
        print arcpy.GetMessages(2)
        print "\nArcMap Warning Messages below:"
        print arcpy.GetMessages(1)

        #-------- COMPLETE LOGFILE ------------------------------------------------
        reportFile.write("\nSomething went wrong.\n\n")
        reportFile.write("Pyton Traceback Message below:")
        reportFile.write(traceback.format_exc())
        reportFile.write("\nArcMap Error Messages below:")
        reportFile.write(arcpy.GetMessages(2))
        reportFile.write("\nArcMap Warning Messages below:")
        reportFile.write(arcpy.GetMessages(1))

        reportFile.write( "\n\nEnded at " + time.asctime() + '\n')
        reportFile.write("\n---End of Log File---\n")

        if reportFile:
            reportFile.close()
Ejemplo n.º 7
0
def ImpP(city, inDir, workFld):
    import traceback, time, arcpy, os
    from arcpy import env
    arcpy.CheckOutExtension('Spatial')

    #-------- DIRECTORY SETUP ------------------------------------------------
    """ Working Directory """
    try:
        arcpy.CreateFileGDB_management(str(workFld),
                                       str(city) + '_ImpProx.gdb')
    except:
        pass
    workGDB = str(workFld) + '/' + str(city) + '_ImpProx.gdb'
    """ Window Views of Water Directory """
    try:
        arcpy.CreateFileGDB_management(str(workFld),
                                       str(city) + '_WaterWV.gdb')
    except:
        pass
    rgGDB = str(workFld) + '/' + str(city) + '_WaterWV.gdb'
    """ Report File Directory """
    reportfileDir = str(workFld) + '/Logs'
    """ Frequent Directory """
    freqDir = str(workFld) + '/' + city + '_Freq.gdb'
    """ Final Geodatabase """
    finalDir = str(workFld) + '/' + city + '_Final.gdb'
    """ Projection File Directory """
    prjDir = str(inDir) + '/Prj'
    """ Split Raster Directory """
    if os.path.isdir(str(workFld) + '/' + city + '_Split') == True:
        pass
    else:
        os.makedirs(str(workFld) + '/' + city + '_Split')
    splitDir = str(workFld) + '/' + city + '_Split'
    """ Set Workspace Environments """
    arcpy.env.workspace = workGDB
    arcpy.env.scratch = str(inDir) + '/Scratch.gdb'
    arcpy.env.overwriteOutput = True

    #-----------------------------------------------------------------------------
    # BEGIN ANALYSIS
    #-----------------------------------------------------------------------------
    try:
        #-------- LOGFILE CREATION ---------------------------------------------
        """ Create report file for each metric """
        tmpName = city + '_ImpProx_' + time.strftime('%Y%m%d_%H-%M')
        reportfileName = reportfileDir + '/' + tmpName + '.txt'
        reportFile = open(reportfileName, 'w')

        try:
            loglist = sorted(f for f in os.listdir(reportfileDir)
                             if f.startswith(str(city) + '_Reuse'))
            tmpName = loglist[-1]
        except:
            tmpName = city + '_Reuse_' + time.strftime('%Y%m%d_%H-%M') + '.txt'
        reportfileName = reportfileDir + '/' + tmpName

        try:
            ReuseRF = open(reportfileName, 'a')
        except:
            ReuseRF = open(reportfileName, 'w')
            print 'Creating Reuse Log'
        """ Write out first line of report file """
        print 'Impervious Proximity Start Time: ' + time.asctime()
        reportFile.write(
            "Begin with EnviroAtlas 1-meter Land Cover for the EnviroAtlas community--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        #-------- PROCESSING LAYERS ----------------------------------------------
        """ Set Environments """
        arcpy.env.extent = str(freqDir) + '/LC'
        arcpy.env.snapRaster = str(freqDir) + '/LC'
        """ Set Projection Files """
        prjfileALB = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj'
        prjNumb = arcpy.Describe(str(freqDir) + '/LC').spatialReference.name
        prjNumb = prjNumb[-3:]
        prjfileUTM = prjDir + '/NAD 1983 UTM Zone ' + prjNumb + '.prj'
        """-------- Reclassify LC into Binary Impervious ----------------------------- """
        if arcpy.Exists(freqDir + '/ImpIO') == False:
            #Run the Imp Space reclassification
            outReclass5 = arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, 0], [10, 0], [20, 1], [21,
                                                                1], [22, 1],
                                     [30, 0], [40, 0], [52, 0], [70, 0],
                                     [80, 0], [82, 0], [91, 0], [92, 0]]))
            outReclass5.save(freqDir + '/ImpIO')
            del outReclass5
            reportFile.write(
                "Reclassify the Land Cover into Binary Impervious. (Impervious - 20 = 1; All Else = 0)--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("ImpIO--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        else:
            reportFile.write(
                "Reclassify the Land Cover into Binary Impervious. (Impervious - 20 = 1; All Else = 0)--ImpIO"
                + '--\n')
        """-------- Create 1001m Moving Window ------------------------------------- """
        outFocalStat4 = arcpy.sa.FocalStatistics(
            freqDir + '/ImpIO', arcpy.sa.NbrRectangle(1001, 1001, 'CELL'),
            'SUM', 'NODATA')
        outFocalStat4.save('Imp_1001R')
        arcpy.CalculateStatistics_management('Imp_1001R')
        del outFocalStat4
        print("1001m moving winow complete... " + time.asctime())
        reportFile.write(
            "Compute Focal Statistics on the Impervious Binary with 1001 pixel wide by 1001 pixel high rectangular window with statistics type = sum.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Reclassify Moving Window into 20% Breaks ----------------------- """
        outReclass3 = arcpy.sa.Reclassify(
            'Imp_1001R', 'Value',
            arcpy.sa.RemapRange([[0, 200400.2, 20], [200400.2, 400800.4, 40],
                                 [400800.4, 601200.6, 60],
                                 [601200.6, 801600.80, 80],
                                 [801600.8, 1002001, 100]]), 'NODATA')
        outReclass3.save('Imp_Pct')
        del outReclass3
        print("1001m moving winow reclassification complete... " +
              time.asctime())
        reportFile.write(
            "Reclassify the Focal Statistics into 5 classes at 20% intervals.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Check that the Analysis Area is covered by the LC -------------- """
        """ Create a Polygon Version of the LC """
        if arcpy.Exists(freqDir + '/LC_Poly') == False:
            arcpy.env.extent = freqDir + '/LC'
            arcpy.env.snapRaster = freqDir + '/LC'
            ReC = arcpy.sa.arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 1], [21,
                                                                1], [22, 1],
                                     [30, 1], [40, 1], [52, 1], [70, 1],
                                     [80, 1], [82, 1], [91, 1], [92, 1]]))
            ReC.save(str(freqDir) + '/AreaIO')
            del ReC
            arcpy.RasterToPolygon_conversion(
                str(freqDir) + '/AreaIO',
                str(freqDir) + '/LC_Poly', 'SIMPLIFY')
            arcpy.EliminatePolygonPart_management(
                str(freqDir) + '/LC_Poly',
                str(freqDir) + '/LC_Poly_EP', 'PERCENT', '', '5',
                'CONTAINED_ONLY')
            arcpy.Delete_management(str(freqDir) + '/LC_Poly')
            arcpy.Rename_management(
                str(freqDir) + '/LC_Poly_EP',
                str(freqDir) + '/LC_Poly')
            """ Buffer the LC Polygon by -500m """
        if arcpy.Exists(freqDir + '/Bnd_Cty_500m') == False:
            arcpy.Buffer_analysis(
                str(freqDir) + '/Bnd_Cty',
                str(freqDir) + '/Bnd_Cty_500m', '500 meters')
            arcpy.EliminatePolygonPart_management(
                str(freqDir) + '/Bnd_Cty_500m',
                str(freqDir) + '/Bnd_Cty_500m_EP', 'PERCENT', '', '30',
                'CONTAINED_ONLY')
            arcpy.Delete_management(str(freqDir) + '/Bnd_Cty_500m')
            arcpy.Rename_management(
                str(freqDir) + '/Bnd_Cty_500m_EP',
                str(freqDir) + '/Bnd_Cty_500m')
            """ Identify whether LC is large enough """
        arcpy.MakeFeatureLayer_management(str(freqDir) + '/LC_Poly', 'LClyr')
        arcpy.MakeFeatureLayer_management(
            str(freqDir) + '/Bnd_Cty_500m', 'BC_500lyr')

        arcpy.SelectLayerByLocation_management('BC_500lyr',
                                               'COMPLETELY_WITHIN', 'LClyr',
                                               '', 'NEW_SELECTION')
        bigEnough = float(arcpy.GetCount_management('BC_500lyr').getOutput(0))
        arcpy.SelectLayerByAttribute_management('BC_500lyr', 'CLEAR_SELECTION')
        """ Create the Water Binary, if necessary """
        if arcpy.Exists(freqDir + '/WaterIO') == True:
            reportFile.write(
                "Create a water binary from the 1-M EnviroAtlas Land Cover. (Water - 10 = 1; All Else = 0).--WaterIO"
                + '--\n')
        else:
            outReclass3 = arcpy.sa.Reclassify(
                freqDir + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 0], [21,
                                                                0], [22, 0],
                                     [30, 0], [40, 0], [52, 0], [70, 0],
                                     [80, 0], [82, 0], [91, 0], [92, 0]]))
            outReclass3.save(freqDir + '/WaterIO')
            del outReclass3
            reportFile.write(
                "Create a water binary from the 1-M EnviroAtlas Land Cover. (Water - 10 = 1; All Else = 0.)--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("WaterIO--" + time.strftime('%Y%m%d--%H%M%S') +
                          '--\n')
        """ Create the Water Region Group, if necessary """
        if arcpy.Exists(rgGDB + '/WatIO_300') == True:
            reportFile.write(
                "Run the region group tool to group waterbodies together with options number of neighbors to use: EIGHT, zone grouping method: WITHIN, and ADD_LINK (add link field to output).--WaterRG"
                + '--\n')
        else:
            RG = arcpy.sa.RegionGroup(freqDir + '/WaterIO', 'EIGHT', 'WITHIN',
                                      'ADD_LINK')
            RG.save(rgGDB + '/WatIO_300')
            del RG
            arcpy.AddField_management(rgGDB + '/WatIO_300', 'Count_2',
                                      'DOUBLE')
            arcpy.CalculateField_management(rgGDB + '/WatIO_300', 'Count_2',
                                            '!COUNT!', 'PYTHON')
            reportFile.write(
                "Run the region group tool to group waterbodies together with options number of neighbors to use: EIGHT, zone grouping method: WITHIN, and ADD_LINK (add link field to output).--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("WaterRG--" + time.strftime('%Y%m%d--%H%M%S') +
                          '--\n')
        """ Limit the WatIO_300 to the Extent of Imp_Pct """
        ##        if bigEnough == 0:
        ##            EbM = arcpy.sa.ExtractByMask(rgGDB + '/WatIO_300', 'Imp_Pct')
        ##            EbM.save('WatRG_Lim_1001')
        ##            del EbM
        ##            print("Extract by mask complete... " + time.asctime())
        ##            reportFile.write("Restrict the extent of the water bodies raster to that of the percent impervious raster.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        if bigEnough == 0:
            desc = arcpy.Describe('Imp_Pct')
            rectangle = "%s %s %s %s" % (desc.extent.XMin, desc.extent.YMin,
                                         desc.extent.XMax, desc.extent.YMax)
            IP01 = arcpy.sa.Reclassify('Imp_Pct', 'Value',
                                       arcpy.sa.RemapRange([[0, 100, 1]]),
                                       'NODATA')
            arcpy.RasterToPolygon_conversion(IP01, 'Imp_Pct_poly',
                                             "NO_SIMPLIFY", "Value")
            del IP01
            arcpy.Clip_management(rgGDB + '/WatIO_300', rectangle,
                                  'WatRG_Lim_251', 'Imp_Pct_poly', '#',
                                  'ClippingGeometry')
            reportFile.write(
                "Restrict the extent of the water bodies raster to that of the percent impervious raster.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Burn water into the Imp_Pct """
        if bigEnough == 0:
            burnwater = arcpy.sa.Con('WatRG_Lim_1001', '-99999', 'Imp_Pct',
                                     'Count_2 > 299 AND LINK = 1')
        else:
            burnwater = arcpy.sa.Con(rgGDB + '/WatIO_300', '-99999', 'Imp_Pct',
                                     'Count_2 > 299 AND LINK = 1')
        burnwater.save('Imp_Pct_Wat')
        del burnwater
        print("Water burned into Imp_Pct... " + time.asctime())
        reportFile.write(
            "Using the region group output, burn water bodies over 300m2 into the impervious percent raster using a conditional statement if Region Group Count > 299 AND Link = 1; for true: -99999; for false: impervious percent raster--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Convert to Esri GRID format """
        try:
            arcpy.Delete_management(splitDir + '/imp_pct_wat')
        except:
            pass
        arcpy.RasterToOtherFormat_conversion('Imp_Pct_Wat', splitDir, 'GRID')
        reportFile.write("Convert the raster into Esri GRID format--" +
                         time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Split the Raster As Needs, Process Each Piece ----------------- """
        """ Check if the raster should be split """
        columns = arcpy.GetRasterProperties_management(
            'Imp_Pct_Wat', 'COLUMNCOUNT').getOutput(0)
        xsplit = int(float(columns) / 40000) + 1
        rows = arcpy.GetRasterProperties_management('Imp_Pct_Wat',
                                                    'ROWCOUNT').getOutput(0)
        ysplit = int(float(rows) / 40000) + 1
        """--------- Clip the EA Boundaries to the County Lines, if necessary ----- """
        if arcpy.Exists(str(freqDir) + '/Bnd_Cty') == False:
            """ Copy Counties to Frequent and Project """
            arcpy.MakeFeatureLayer_management(
                str(inDir) + '/Input.gdb/Counties_Alb', 'Cty')
            arcpy.SelectLayerByLocation_management('Cty', 'CONTAINS', 'BG_Alb',
                                                   '', 'NEW_SELECTION')
            arcpy.FeatureClassToFeatureClass_conversion(
                str(inDir) + '/Input.gdb/Counties_Alb', str(freqDir),
                'Counties_Alb')
            arcpy.SelectLayerByAttribute_management('Cty', 'CLEAR_SELECTION')
            descLC = arcpy.Describe(str(freqDir) + '/LC')
            arcpy.Project_management('Counties_Alb', 'Counties',
                                     descLC.spatialReference)
            """ Clip Boundary to County Lines """
            arcpy.Clip_analysis(
                str(freqDir) + '/Bnd',
                str(freqDir) + '/Counties',
                str(freqDir) + '/Bnd_Cty')
            reportFile.write(
                "Clip the EnviroAtlas Community Boundary to the county lines for the community to limit the output to land area.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("Bnd_Cty--" + time.strftime('%Y%m%d--%H%M%S') +
                          '--\n')
        else:
            reportFile.write(
                "Clip the EnviroAtlas Community Boundary to the county lines for the community to limit the output to land area.--Bnd_Cty"
                + '--\n')
            """-------- If no split, run the analysis --------------------------------- """
        if xsplit * ysplit == 1:
            """ Convert Raster to Polygon """
            arcpy.RasterToPolygon_conversion('Imp_Pct_Wat', 'Poly_Imp',
                                             'NO_SIMPLIFY')
            reportFile.write(
                "Convert the raster into a polygon feature class.--" +
                time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Clip the polygons to Bnd_Cty """
            arcpy.Clip_analysis('Poly_Imp', freqDir + '/Bnd_Cty', 'Clip_Imp')
            reportFile.write(
                "Clip the polygon feature class to the clipped EnviroAtlas community boundary.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Dissolve the polygons """
            arcpy.Dissolve_management('Clip_Imp', 'ImpDiss', 'gridcode')
            reportFile.write(
                "Dissolve the clipped polygons based on grid_code.--" +
                time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """-------- If split, run the analysis on each piece and recombine --------- """
        else:
            """ Delete the raster, if necessary """
            xy = (xsplit * ysplit)
            for rast in range(xy):
                try:
                    arcpy.Delete_management(splitDir + '/impp_' + str(rast))
                except:
                    pass
            """ Split the Raster """
            print("Raster too big, splitting into " + str(xy) + " pieces... " +
                  time.asctime())
            arcpy.SplitRaster_management(splitDir + '/imp_pct_wat', splitDir,
                                         'impp_', 'NUMBER_OF_TILES', 'GRID',
                                         '',
                                         str(xsplit) + ' ' + str(ysplit))
            print("Raster split complete... " + time.asctime())
            reportFile.write(
                "Split the raster into pieces of no more than 40,000x40,000 pixels.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ For each raster: """
            for Chunk in range(0, xy):
                try:
                    result = float(
                        arcpy.GetRasterProperties_management(
                            splitDir + '/impp_' + str(Chunk),
                            'MEAN').getOutput(0))
                    """ If the raster piece has data: """
                    if (result <> 0):
                        """ Set Environments """
                        arcpy.env.snapRaster = 'Imp_1001R'
                        arcpy.env.extent = 'Imp_1001R'
                        """ Copy the piece back to the Working Directory """
                        arcpy.RasterToOtherFormat_conversion(
                            splitDir + '/impp_' + str(Chunk), workGDB)
                        """ Convert Raster to Polygon """
                        arcpy.RasterToPolygon_conversion(
                            'impp_' + str(Chunk), 'ImpPoly_' + str(Chunk),
                            'NO_SIMPLIFY')
                        """ Clip the polygons to Bnd_Cty """
                        arcpy.Clip_analysis('ImpPoly_' + str(Chunk),
                                            freqDir + '/Bnd_Cty',
                                            'ImpClip_' + str(Chunk))
                        """ Dissolve the polygons """
                        arcpy.Dissolve_management('ImpClip_' + str(Chunk),
                                                  'ImpD1_' + str(Chunk),
                                                  'gridcode')
                        print("Processed Chunk " + str(Chunk) + " / " +
                              str(xy) + "..." + time.asctime())
                    else:
                        pass
                except:
                    pass
            reportFile.write(
                "Convert each of the raster pieces into a polygon feature class.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            reportFile.write(
                "Clip the polygon feature classes to the clipped EnviroAtlas community boundary.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            reportFile.write("Dissolve each piece based on grid_code.--" +
                             time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Merge the polygons back together """
            fcList = arcpy.ListFeatureClasses('ImpD1*')
            arcpy.Merge_management(fcList, 'ImpDiss')
            reportFile.write("Merge the pieces back together.--" +
                             time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Dissolve the pieces again """
        arcpy.Dissolve_management('ImpDiss', 'ImpProx_UTM', 'gridcode')
        reportFile.write("Dissolve again based on grid_code.--" +
                         time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Change gridcode to ImpProxP """
        arcpy.AlterField_management('ImpProx_UTM', 'gridcode', 'ImpProxP')
        reportFile.write("Rename field 'gridcode' into field 'ImpProxP'--" +
                         time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ If the LC isn't large enough, edit erroneous BGS """
        if bigEnough == 0:
            ##            """ Extract the area where the boundary extends beyond the analysis area """
            ##            arcpy.Erase_analysis(freqDir + '/Bnd_Cty', 'ImpProx_UTM', 'EE_Area')
            ##            arcpy.AddField_management('EE_Area', 'ImpProxP', 'LONG')
            ##            arcpy.CalculateField_management('EE_Area', 'ImpProxP', -99998, 'PYTHON_9.3')
            ##            """ Append the non-analyzed area onto the main output """
            ##            arcpy.Append_management('EE_Area', 'ImpProx_UTM')
            ##            """ Dissolve the output, again """
            ##            arcpy.Dissolve_management('ImpProx_UTM', 'ImpProx_EE_D', 'ImpProxP')
            ##            reportFile.write("Due to the extent of the Land Cover, the analysis area is smaller than the EnviroAtlas Community Boundary. Extract the area within the boundary that is not within the analysis area, add field 'ImpProxP' = -99998 and append on to the output file. Dissolve based on ImpProxP.--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Buffer each GreClip feature by 1 cm and erase from Bnd_Cty """
            fcList = arcpy.ListFeatureClasses('ImpClip*')
            for idx, fc in enumerate(fcList):
                arcpy.Buffer_analysis(fc, (fc + "_1cmbuff"), "1 Centimeters",
                                      "FULL", "ROUND", "ALL")
                #arcpy.Buffer_analysis(fc + "_inbuff", fc + "outbuff", "1 Centimeter", "FULL", "ROUND", "ALL")
                if idx == 0:
                    arcpy.Erase_analysis(freqDir + '/Bnd_Cty',
                                         (fc + "_1cmbuff"),
                                         'EE_Area_' + str(idx))
                else:
                    arcpy.Erase_analysis('EE_Area_' + str(idx - 1),
                                         fc + "_1cmbuff",
                                         'EE_Area_' + str(idx))
                a = idx
                print("Processed Chunk " + str(Chunk) + " / " + str(xy) +
                      "..." + time.asctime())
            arcpy.Buffer_analysis('EE_Area_' + str(a), 'EE_Area_2cmbuff',
                                  '2 Centimeters', "FULL", "ROUND", "ALL")
            arcpy.Clip_analysis('EE_Area_2cmbuff', freqDir + '/Bnd_Cty',
                                'EE_Area')
            arcpy.AddField_management('EE_Area', 'ImpProxP', 'LONG')
            arcpy.CalculateField_management('EE_Area', 'ImpProxP', -99998,
                                            'PYTHON_9.3')
            """ Append the non-analyzed area onto the main output """
            arcpy.Append_management('EE_Area', 'ImpProx_UTM')
            """ Dissolve the output, again """
            arcpy.Dissolve_management('ImpProx_UTM', 'ImpProx_EE_D',
                                      'ImpProxP')
            print("Big enough workflow complete... " + time.asctime())
            reportFile.write(
                "Due to the extent of the Land Cover, the analysis area is smaller than the EnviroAtlas Community Boundary. Extract the area within the boundary that is not within the analysis area, add field 'ImpProxP' = -99998 and append on to the output file. Dissolve based on ImpProxP.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Project the output """
        try:
            arcpy.Project_management('ImpProx_EE_D', city + '_ImpProx',
                                     prjfileALB)
        except:
            arcpy.Project_management('ImpProx_UTM', city + '_ImpProx',
                                     prjfileALB)
        reportFile.write("Project the feature class into Albers.--" +
                         time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Add to Final Directory """
        arcpy.FeatureClassToFeatureClass_conversion(city + '_ImpProx',
                                                    finalDir,
                                                    city + '_ImpProx')
        reportFile.write("Copy the feature class to the final geodatabase.--" +
                         time.strftime('%Y%m%d--%H%M%S') + '--\n')
        print 'Impervious Proximity End Time: ' + time.asctime() + '\n'

        #-------- COMPELETE LOGFILES ---------------------------------------------
        reportFile.close()
        ReuseRF.close()

#-----------------------------------------------------------------------------
# END ANALYSIS
#-----------------------------------------------------------------------------
    except:
        """ This part of the script executes if anything went wrong in the main script above """
        #-------- PRINT ERRORS ---------------------------------------------------
        print "\nSomething went wrong.\n\n"
        print "Python Traceback Message below:"
        print traceback.format_exc()
        print "\nArcMap Error Messages below:"
        print arcpy.GetMessages(2)
        print "\nArcMap Warning Messages below:"
        print arcpy.GetMessages(1)

        #-------- COMPLETE LOGFILE ------------------------------------------------
        reportFile.write("\nSomething went wrong.\n\n")
        reportFile.write("Pyton Traceback Message below:")
        reportFile.write(traceback.format_exc())
        reportFile.write("\nArcMap Error Messages below:")
        reportFile.write(arcpy.GetMessages(2))
        reportFile.write("\nArcMap Warning Messages below:")
        reportFile.write(arcpy.GetMessages(1))

        reportFile.write("\n\nEnded at " + time.asctime() + '\n')
        reportFile.write("\n---End of Log File---\n")

        if reportFile:
            reportFile.close()
Ejemplo n.º 8
0
Cr = "CornOnly"
basedir = "Z:/Users/rilllydi/MidwestSALUS/Soils_in_CDL/States/" + Cr + "/"

# Set environment settings
#env.workspace = basedir
#env.scratchWorkspace = r"Z:/Users/rilllydi/MidwestSALUS/scratch.gdb"
#env.overwriteOutput = True

states = ['mi','oh','in','il','ia','sd','mn','mo','wi']
#states = ['wi']

for st in states:
    rasterfile = basedir + st + "_soil_" + Cr + "_WGS84.tif"
    splitfolder = basedir + st + "_split_55/"
    name = st + "_soil_" + Cr + "_WGS84_"
    
    print rasterfile
    print splitfolder
    print name
    
    if not os.path.exists(splitfolder):
        os.makedirs(splitfolder)
    
    print "created folder"
    
    nodataval = '-32768'
    ### Split the raster (must use "NEAREST" option so the pixel values will not change!)
    #arcpy.SplitRaster_management(rasterfile, splitfolder, name, "SIZE_OF_TILE",\
    #                             "TIFF", "NEAREST",nodata_value=nodataval) # default tile size is 128 X 128
    arcpy.SplitRaster_management(rasterfile, splitfolder, name, "NUMBER_OF_TILES",\
                                 "TIFF", "NEAREST",num_rasters="5 5",nodata_value=nodataval) # 9 rasters (3 in the x direction, 3 in the y direction)
Ejemplo n.º 9
0
def WVW(city, inDir, workFld):
    import traceback, time, arcpy, os
    from arcpy import env
    arcpy.CheckOutExtension('Spatial')

    #-------- DIRECTORY SETUP ------------------------------------------------
    """ Working Directory """
    try:
        arcpy.CreateFileGDB_management(str(workFld),
                                       str(city) + '_WaterWV.gdb')
    except:
        print 'WaterWV GDB already exists'
    workDir = str(workFld) + '/' + city + '_WaterWV.gdb'
    arcpy.env.workspace = workDir
    """ Report File Directory """
    reportfileDir = str(workFld) + '/Logs'
    """ Frequent Directory """
    freqDir = str(workFld) + '/' + city + '_Freq.gdb'
    """ Final Geodatabase """
    finalDir = str(workFld) + '/' + city + '_Final.gdb'
    """ Projection File Directory """
    prjDir = str(inDir) + '/Prj'
    prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj'
    """ Split Raster Directory """
    if os.path.isdir(str(workFld) + '/' + city + '_Split') == True:
        pass
    else:
        os.makedirs(str(workFld) + '/' + city + '_Split')
    splitDir = str(workFld) + '/' + city + '_Split'
    """ Set Workspace Environments """
    arcpy.env.workspace = workDir
    arcpy.env.scratch = str(inDir) + '/Scratch.gdb'
    arcpy.env.overwriteOutput = True

    print("Directory and Environment set-up complete...--" +
          time.strftime('%Y%m%d--%H%M%S'))

    #-----------------------------------------------------------------------------
    # BEGIN ANALYSIS
    #-----------------------------------------------------------------------------
    try:
        #-------- LOGFILE CREATION ---------------------------------------------
        """ Create report file for each metric """
        tmpName = city + '_WaterWV_' + time.strftime('%Y%m%d_%H-%M')
        reportfileName = reportfileDir + '/' + tmpName + '.txt'
        reportFile = open(reportfileName, 'w')

        try:
            loglist = sorted(f for f in os.listdir(reportfileDir)
                             if f.startswith(str(city) + '_Reuse'))
            tmpName = loglist[-1]
        except:
            tmpName = city + '_Reuse_' + time.strftime('%Y%m%d_%H-%M') + '.txt'
        reportfileName = reportfileDir + '/' + tmpName

        try:
            ReuseRF = open(reportfileName, 'a')
        except:
            ReuseRF = open(reportfileName, 'w')
            print 'Creating Reuse Log'
        """ Write out first line of report file """
        print 'Water Views Start Time: ' + time.asctime()
        reportFile.write(
            "Begin with EnviroAtlas 1-meter Land Cover for the EnviroAtlas community--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        #-------- PROCESSING LAYERS ----------------------------------------------
        """ Set Environments """
        arcpy.env.snapRaster = freqDir + '/LC'
        arcpy.env.extent = freqDir + '/LC'
        arcpy.env.overwriteOutput = True
        """-------- Reclassify LC into Binary Water ----------------------------- """
        if arcpy.Exists(str(freqDir) + '/WaterIO') == False:
            outReclass = arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 0], [21,
                                                                0], [22, 0],
                                     [30, 0], [40, 0], [52, 0], [70, 0],
                                     [80, 0], [82, 0], [91, 0], [92, 0]]))
            outReclass.save(str(freqDir) + '/WaterIO')
            print("Reclassify to water binary complete...--" +
                  time.strftime('%Y%m%d--%H%M%S'))
            reportFile.write(
                "Reclassify the Land Cover into a Water binary. (Water - 10 = 1; All Else = 0).--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("WaterIO--" + time.strftime('%Y%m%d--%H%M%S') +
                          '--\n')
        else:
            print("Reclassify to water previously completed...--" +
                  time.strftime('%Y%m%d--%H%M%S'))
            reportFile.write(
                "Reclassify the Land Cover into a Water binary. (Water - 10 = 1; All Else = 0).--WaterIO"
                + '--\n')
        """-------- Create the Water Bodies Raster ------------------ """
        """ Create the Water Region Group """
        if arcpy.Exists('WatIO_300') == False:
            RG = arcpy.sa.RegionGroup(freqDir + '/WaterIO', 'EIGHT', 'WITHIN',
                                      'ADD_LINK')
            RG.save('WatIO_300')
            arcpy.AddField_management('WatIO_300', 'Count_2', 'DOUBLE')
            arcpy.CalculateField_management('WatIO_300', 'Count_2', '!COUNT!',
                                            'PYTHON')
            print("Water region groups created...--" +
                  time.strftime('%Y%m%d--%H%M%S'))
            reportFile.write(
                "Run the region group tool to group waterbodies together with options number of neighbors to use: EIGHT, zone grouping method: WITHIN, and ADD_LINK (add link field to output).--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("WaterRG--" + time.strftime('%Y%m%d--%H%M%S') +
                          '--\n')

        else:
            print("Water region groups previously created...--" +
                  time.strftime('%Y%m%d--%H%M%S'))
            reportFile.write(
                "Run the region group tool to group waterbodies together with options number of neighbors to use: EIGHT, zone grouping method: WITHIN, and ADD_LINK (add link field to output).--WaterRG"
                + '\n')
        """ Extract Water Bodies From the Region Group """
        EBA = arcpy.sa.ExtractByAttributes('WatIO_300',
                                           'Count_2 > 299 AND LINK = 1')
        EBA.save('WatBod_300')
        reportFile.write(
            "Extract by Attributes from the Region Group raster groups of pixels where Count > 299 and Link = 1.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Split the Raster As Needs, Process Each Piece ----------------- """
        """ Check if the raster should be split """
        columns = arcpy.GetRasterProperties_management(
            'WatBod_300', 'COLUMNCOUNT').getOutput(0)
        xsplit = int(float(columns) / 40000) + 1
        rows = arcpy.GetRasterProperties_management('WatBod_300',
                                                    'ROWCOUNT').getOutput(0)
        ysplit = int(float(rows) / 40000) + 1
        print("Determining if split is necessary...--" +
              time.strftime('%Y%m%d--%H%M%S'))
        """-------- If no split, run the analysis --------------------------------- """
        if xsplit * ysplit == 1:
            """ Convert Raster to Polygon """
            arcpy.RasterToPolygon_conversion('WatBod_300', 'Poly_WatBod',
                                             'NO_SIMPLIFY')
            reportFile.write("Convert the raster into polygons--" +
                             time.strftime('%Y%m%d--%H%M%S') + '--\n')
            print("\t Raster is small enough, split not necessary...--" +
                  time.strftime('%Y%m%d--%H%M%S'))
            """-------- If split, run the analysis on each piece and recombine --------- """
        else:
            """ Delete the raster, if necessary """
            xy = (xsplit * ysplit)
            for rast in range(xy):
                try:
                    arcpy.Delete_management(splitDir + '/watbod_' + str(rast))
                except:
                    pass
            try:
                arcpy.Delete_management(splitDir + '/watbod_300')
            except:
                pass
            """ Split the Raster """
            print("\t Raster is big, spliting into " + str(xsplit) +
                  " rows and " + str(ysplit) + " columns...--" +
                  time.strftime('%Y%m%d--%H%M%S'))
            arcpy.RasterToOtherFormat_conversion('WatBod_300', splitDir,
                                                 'GRID')
            arcpy.SplitRaster_management(splitDir + '/WatBod_300', splitDir,
                                         'WatBod_', 'NUMBER_OF_TILES', 'GRID',
                                         '',
                                         str(xsplit) + ' ' + str(ysplit))
            reportFile.write(
                "Split the raster into pieces for easier processing. The Python script determines the number of pieces based on the number of rows and columns in the raster where no piece can have a side larger than 40,000 cells.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ For each raster: """
            prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj'
            xy = xsplit * ysplit
            for Chunk in range(0, xy):
                try:
                    result = float(
                        arcpy.GetRasterProperties_management(
                            splitDir + '/WatBod_' + str(Chunk),
                            'MEAN').getOutput(0))
                    """ If the raster piece has data: """
                    if (result > 0):
                        """ Set Environments """
                        arcpy.env.snapRaster = freqDir + '/WaterIO'
                        arcpy.env.extent = freqDir + '/WaterIO'
                        """ Copy the piece back to the Working Directory """
                        arcpy.RasterToOtherFormat_conversion(
                            splitDir + '/WatBod_' + str(Chunk), workDir)
                        """ Convert Raster to Polygon """
                        arcpy.RasterToPolygon_conversion(
                            'WatBod_' + str(Chunk), 'Poly_' + str(Chunk),
                            'NO_SIMPLIFY')
                        print("Processed Chunk " + str(Chunk) + " / " +
                              str(xy) + "..." + time.asctime())
                    else:
                        pass
                except:
                    pass
            reportFile.write(
                "Convert the pieces individually into polygons then recombine them into one feature class.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            WVchunks = arcpy.ListFeatureClasses('Poly_*')
            """ Merge the polygons back together """
            arcpy.Merge_management(WVchunks, 'Poly_WatBod')
            print(
                "All raster chunks with data converted to polygons and merged...--"
                + time.strftime('%Y%m%d--%H%M%S'))
            reportFile.write("Merge the pieces back together.--" +
                             time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- End of Split Processing ---------------------------------------- """
        """ Buffer pieces by 50m """
        arcpy.Buffer_analysis('Poly_WatBod', 'WatView_Buff', '50 METERS',
                              'FULL', '', 'NONE')
        reportFile.write("Buffer the Water Bodies by 50 Meters.--" +
                         time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Project into Albers """
        arcpy.Project_management('WatView_Buff', 'WatView_Alb', prjfile)
        reportFile.write("Project the buffer polygons into Albers.--" +
                         time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Set Environments """
        arcpy.env.snapRaster = freqDir + '/Dasy'
        arcpy.env.extent = freqDir + '/Dasy'
        """ Extract Daysmetric Pixels """
        EbM_P = arcpy.sa.ExtractByMask(freqDir + '/Dasy', 'WatView_Alb')
        EbM_P.save('Pop_WaterView')
        print("Dasy pixels extracted for all buffered water polygons...--" +
              time.strftime('%Y%m%d--%H%M%S'))
        reportFile.write(
            "Extract by mask the EnviroAtlas Dasymetric (2011/October 2015) pixels using the projected buffer to produce a raster showing population with potential views of water.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Calculate the Extracted Dasy Population with Each CBG """
        arcpy.sa.ZonalStatisticsAsTable(freqDir + '/BG_Alb', 'bgrp',
                                        'Pop_WaterView', 'BG_WWV', 'DATA',
                                        'SUM')
        reportFile.write(
            "Calculate Zonal Statistics as Table for the extracted dasymetrics with the zones being the 2010 block groups for the EnviroAtlas community.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Calculate Total Dasy Population, if necessary ------------------ """
        """ Use the existing data """
        fieldNames = [f.name for f in arcpy.ListFields(freqDir + '/BG_Alb')]
        if 'Dasy_Pop' in fieldNames:
            reportFile.write(
                "Calculate Zonal Statistics as a Table for the EnviroAtlas Dasymetrics (2011/October 2015) with the zones being the 2010 block groups within the EnviroAtlas community boundary. Add resulting population sums to the community block groups as attribute Dasy_Pop--Dasy_Pop"
                + '--\n')
            """ Create population data """
        else:
            arcpy.AddField_management(freqDir + '/BG_Alb', 'Dasy_Pop', 'LONG')
            arcpy.sa.ZonalStatisticsAsTable(freqDir + '/BG_Alb', 'bgrp',
                                            freqDir + '/Dasy',
                                            freqDir + '/Dasy_ZS', '', 'SUM')
            arcpy.JoinField_management(freqDir + '/BG_Alb', 'bgrp',
                                       freqDir + '/Dasy_ZS', 'bgrp', ['SUM'])
            arcpy.CalculateField_management(freqDir + '/BG_Alb', 'Dasy_Pop',
                                            '!SUM!', 'PYTHON_9.3')
            arcpy.DeleteField_management(freqDir + '/BG_Alb', ['SUM'])
            arcpy.JoinField_management(freqDir + '/BG', 'bgrp',
                                       freqDir + '/BG_Alb', 'bgrp',
                                       ['Dasy_Pop'])
            reportFile.write(
                "Calculate Zonal Statistics as a Table for the EnviroAtlas Dasymetrics (2011/October 2015) with the zones being the 2010 block groups within the EnviroAtlas community boundary. Add resulting population sums to the community block groups as attribute Dasy_Pop--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("Dasy_Pop--" + time.strftime('%Y%m%d--%H%M%S') +
                          '--\n')
        """-------- Create Final Table --------------------------------------------- """
        arcpy.TableToTable_conversion(freqDir + '/BG_Alb', workDir, 'WatWV',
                                      '', 'bgrp')
        arcpy.DeleteField_management('WatWV', [
            'PLx2_Pop', 'PLx2_Pct', 'SUM_HOUSIN', 'NonWhite', 'LandA_M',
            'Density', 'under_1', 'under_1pct', 'under_13', 'under_13pc',
            'over_70', 'over_70pct', 'Shape_Length', 'Shape_Leng',
            'NonWhite_Pop', 'NonWt_Pct', 'Area_M', 'Shape_Le_1', 'Shape_Area',
            'ALAND', 'NonWhite_P', 'H_Income_M', 'State'
        ])
        WaterView = 'WatWV'
        reportFile.write(
            "Create a new table based on the EnviroAtlas community block groups table retaining the BGRP and Dasy_Pop fields--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Add fields to new table """
        arcpy.AddField_management(WaterView, 'WVW_Pop', 'LONG')
        arcpy.AddField_management(WaterView, 'WVW_Pct', 'FLOAT', 5, 2)
        reportFile.write(
            "Add fields to the new table for WVW_Pop (long), WVW_Pct (float).--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Join Each Table to the final table and WVW_Pop """
        arcpy.JoinField_management(WaterView, 'bgrp', 'BG_WWV', 'bgrp',
                                   ['SUM'])
        arcpy.CalculateField_management(WaterView, 'WVW_Pop', '!SUM!',
                                        'PYTHON_9.3')
        arcpy.MakeTableView_management(WaterView, 'WaterView_Tbl')
        arcpy.SelectLayerByAttribute_management('WaterView_Tbl',
                                                'NEW_SELECTION', 'SUM IS NULL')
        arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pop', 0,
                                        'PYTHON_9.3')
        arcpy.SelectLayerByAttribute_management('WaterView_Tbl',
                                                'CLEAR_SELECTION')
        arcpy.DeleteField_management('WaterView_Tbl', 'SUM')
        reportFile.write(
            "Join the zonal statistics table with the new table to calculate the new fields: WVW_Pop = zonal statistics.SUM; remove join--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Calculate WVW_Pct """
        arcpy.SelectLayerByAttribute_management('WaterView_Tbl',
                                                'NEW_SELECTION', 'Dasy_Pop >0')
        arcpy.CalculateField_management(
            'WaterView_Tbl', 'WVW_Pct',
            '"%.2f" % (float(!WVW_Pop!)/float(!Dasy_Pop!) * 100)',
            'PYTHON_9.3')
        arcpy.SelectLayerByAttribute_management('WaterView_Tbl',
                                                'CLEAR_SELECTION')
        reportFile.write(
            "Calculate field WVW_Pct = WVW_Pop / Dasy_Pop * 100 (limited to 2 decimal places).--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Calculate NULL values, where applicable """
        arcpy.SelectLayerByAttribute_management('WaterView_Tbl',
                                                'NEW_SELECTION',
                                                'Dasy_Pop = 0')
        arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pct', '-99999',
                                        'PYTHON_9.3')
        arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pop', '-99999',
                                        'PYTHON_9.3')
        arcpy.SelectLayerByAttribute_management('WaterView_Tbl',
                                                'CLEAR_SELECTION')
        arcpy.DeleteField_management('WaterView_Tbl', [
            'SUM_POP10', 'EAID', 'NonWhite', 'LandA_M', 'Density', 'Dasy_Pop',
            'SUM'
        ])
        print(
            "Statistics for all fields and buffered water bodies calculated...--"
            + time.strftime('%Y%m%d--%H%M%S'))
        reportFile.write(
            "Calculate fields where Dasy_Pop = 0: All Fields = -99999.--" +
            time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Check that the Analysis Area is covered by the LC -------------- """
        """ Create a Polygon Version of the LC """
        if arcpy.Exists(freqDir + '/LC_Poly') == False:
            arcpy.env.snapRaster = freqDir + '/LC'
            arcpy.env.extent = freqDir + '/LC'
            ReC = arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 1], [21,
                                                                1], [22, 1],
                                     [30, 1], [40, 1], [52, 1], [70, 1],
                                     [80, 1], [82, 1], [91, 1], [92, 1]]))
            ReC.save(str(freqDir) + '/AreaIO')
            arcpy.RasterToPolygon_conversion(
                str(freqDir) + '/AreaIO',
                str(freqDir) + '/LC_Poly', 'SIMPLIFY')
            arcpy.EliminatePolygonPart_management(
                str(freqDir) + '/LC_Poly',
                str(freqDir) + '/LC_Poly_EP', 'PERCENT', '', '5',
                'CONTAINED_ONLY')
            arcpy.Delete_management(str(freqDir) + '/LC_Poly')
            arcpy.Rename_management(
                str(freqDir) + '/LC_Poly_EP',
                str(freqDir) + '/LC_Poly')
        """ Buffer the LC Polygon by -500m """
        if arcpy.Exists(freqDir + '/Bnd_Cty_500m') == False:
            arcpy.Buffer_analysis(
                str(freqDir) + '/Bnd_Cty',
                str(freqDir) + '/Bnd_Cty_500m', '500 meters')
            arcpy.EliminatePolygonPart_management(
                str(freqDir) + '/Bnd_Cty_500m',
                str(freqDir) + '/Bnd_Cty_500m_EP', 'PERCENT', '', '30',
                'CONTAINED_ONLY')
            arcpy.Delete_management(str(freqDir) + '/Bnd_Cty_500m')
            arcpy.Rename_management(
                str(freqDir) + '/Bnd_Cty_500m_EP',
                str(freqDir) + '/Bnd_Cty_500m')
        """ Identify whether LC is large enough """
        arcpy.MakeFeatureLayer_management(str(freqDir) + '/LC_Poly', 'LClyr')
        arcpy.MakeFeatureLayer_management(
            str(freqDir) + '/Bnd_Cty_500m', 'BC_500lyr')

        arcpy.SelectLayerByLocation_management('BC_500lyr',
                                               'COMPLETELY_WITHIN', 'LClyr',
                                               '', 'NEW_SELECTION')
        bigEnough = float(arcpy.GetCount_management('BC_500lyr').getOutput(0))
        arcpy.SelectLayerByAttribute_management('BC_500lyr', 'CLEAR_SELECTION')
        """ If the LC isn't large enough, edit erroneous BGS """
        if bigEnough == 0:
            """ Identify BGs within 50m of the LC edge """
            print(
                "Land Cover does not extend beyond BG boundary, editing possibly erroneous BGs...--"
                + time.strftime('%Y%m%d--%H%M%S'))
            arcpy.Buffer_analysis(
                str(freqDir) + '/LC_Poly', 'LC_Poly_Minus50', '-50 meters')
            arcpy.MakeFeatureLayer_management('LC_Poly_Minus50', 'Minus50')
            arcpy.MakeFeatureLayer_management(freqDir + '/BG', 'BG')

            arcpy.SelectLayerByLocation_management('BG', 'COMPLETELY_WITHIN',
                                                   'Minus50', '',
                                                   'NEW_SELECTION', 'INVERT')

            bgValue = float(arcpy.GetCount_management('BG').getOutput(0))
            """ For all BGs too close to the LC edge, assign both fields a value of -99998 """
            if bgValue > 0:
                bgrps = []
                cursor = arcpy.SearchCursor('BG')
                for row in cursor:
                    value = row.getValue('bgrp')
                    bgrps.append(value)
                bgrps = list(set(bgrps))
                expression = ''
                for bgrp in bgrps:
                    expression = expression + " OR bgrp = '" + str(bgrp) + "'"
                expression = expression[4:]
                arcpy.SelectLayerByAttribute_management(
                    'WaterView_Tbl', 'NEW_SELECTION', expression)
                arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pop',
                                                '-99998', 'PYTHON_9.3')
                arcpy.CalculateField_management('WaterView_Tbl', 'WVW_Pct',
                                                '-99998', 'PYTHON_9.3')
                arcpy.SelectLayerByAttribute_management(
                    'WaterView_Tbl', 'CLEAR_SELECTION')
        reportFile.write(
            "Calculate Field for BGs within 50m of the edge of the land cover, WVW_Pop and WVW_Pct = -99998.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Create final table """
        arcpy.CopyRows_management('WaterView_Tbl', 'WaterWV_Fnl')

        try:
            arcpy.Delete_management(finalDir + '/' + str(city) + '_WaterWV')
        except:
            pass
        arcpy.TableToTable_conversion('WaterWV_Fnl', finalDir,
                                      city + '_WaterWV')
        allFields = [
            f.name
            for f in arcpy.ListFields(finalDir + '/' + city + '_WaterWV')
        ]
        for field in allFields:
            if field not in ['bgrp', 'OBJECTID', 'WVW_Pop', 'WVW_Pct']:
                arcpy.DeleteField_management(
                    finalDir + '/' + city + '_WaterWV', [field])
        reportFile.write(
            "Export the fields to be displayed in the EnviroAtlas to a final gdb table. WVW_Pop, WVW_Pct--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        print 'Window Views of Water End Time: ' + time.asctime() + '\n'

        #-------- COMPELETE LOGFILES ---------------------------------------------
        reportFile.close()
        ReuseRF.close()

#-----------------------------------------------------------------------------
# END ANALYSIS
#-----------------------------------------------------------------------------
    except:
        """ This part of the script executes if anything went wrong in the main script above """
        #-------- PRINT ERRORS ---------------------------------------------------
        print "\nSomething went wrong.\n\n"
        print "Python Traceback Message below:"
        print traceback.format_exc()
        print "\nArcMap Error Messages below:"
        print arcpy.GetMessages(2)
        print "\nArcMap Warning Messages below:"
        print arcpy.GetMessages(1)

        #-------- COMPLETE LOGFILE ------------------------------------------------
        reportFile.write("\nSomething went wrong.\n\n")
        reportFile.write("Pyton Traceback Message below:")
        reportFile.write(traceback.format_exc())
        reportFile.write("\nArcMap Error Messages below:")
        reportFile.write(arcpy.GetMessages(2))
        reportFile.write("\nArcMap Warning Messages below:")
        reportFile.write(arcpy.GetMessages(1))

        reportFile.write("\n\nEnded at " + time.asctime() + '\n')
        reportFile.write("\n---End of Log File---\n")

        if reportFile:
            reportFile.close()
Ejemplo n.º 10
0
					if not arcpy.Exists(diroutGridsVar + "\\" + rcp+"_"+period+"_"+model+"_"+raster + "_" + str(int(str(countryDic [country]).split(" ")[0]) * int(str(countryDic [country]).split(" ")[1]) - 1)+".tif"):
						
						# trashList = sorted(glob.glob(diroutGridsVar + "\\" + raster + "*.*"))
						# for trashfile in trashList:
							# os.remove(trashfile)
						
						print "\tspliting .. ",raster
						
						# rasterdeleteList = sorted(glob.glob(diroutGridsVar + "\\" + os.path.basename(raster) + "_*"))
						# for rasterdelete in rasterdeleteList:
							# arcpy.Delete_management(rasterdelete)
						if model != "current":
							# arcpy.SplitRaster_management(raster, diroutGridsVar, "baseline_1960_1990_"+model+"_"+raster + "_", "NUMBER_OF_TILES", "TIFF", "#", str(countryDic [country]), "#", "0", "PIXELS", "#", "#")
						# else:
							arcpy.SplitRaster_management(raster, diroutGridsVar, rcp+"_"+period+"_"+model+"_"+raster + "_", "NUMBER_OF_TILES", "TIFF", "#", str(countryDic [country]), "#", "0", "PIXELS", "#", "#")
						# arcpy.SplitRaster_management(raster,diroutGridsVar,raster + "_","NUMBER_OF_TILES","TIFF","NEAREST","4 4","2048 2048","0","PIXELS","#","#")
						# arcpy.SplitRaster_management(raster, diroutGridsVar, raster + "_", "NUMBER_OF_TILES", "GRID", "#", str(countryDic [country]), "#", "0", "PIXELS", "#", "#")
						print "\t" + raster,"tiled"					
						
						# trashList = sorted(glob.glob(diroutGridsVar + "\\" + raster + "*.*"))
						# for trashfile in trashList:
							# os.remove(trashfile)
						
					else:
						print "\t" + raster,"tiled"
				
		#Compressing and delete input files
		# os.system("7za a -mmt8 " + dirbase + "\\SRES_" + sres + "\\downscaled\\" + country + "_" + str(resolution) + ".zip " + dirbase + "\\SRES_" + sres + "\\downscaled\\" + country + "_" + str(resolution))
		# os.system("rmdir /s /q " + dirbase + "\\SRES_" + sres + "\\downscaled\\" + country + "_" + str(resolution))
		
Ejemplo n.º 11
0
def WVT(city, inDir, workFld):
    import traceback, time, arcpy, os
    from arcpy import env
    arcpy.CheckOutExtension('Spatial')

    #-------- DIRECTORY SETUP ------------------------------------------------
    """ Working Directory """
    try:
        arcpy.CreateFileGDB_management(str(workFld), str(city) + '_TreeWV.gdb')
    except:
        print 'TreeWV GDB already exists'
    workDir = str(workFld) + '/' + city + '_TreeWV.gdb'
    arcpy.env.workspace = workDir
    """ Report File Directory """
    reportfileDir = str(workFld) + '/Logs'
    """ Frequent Directory """
    freqDir = str(workFld) + '/' + city + '_Freq.gdb'
    """ Final Geodatabase """
    finalDir = str(workFld) + '/' + city + '_Final.gdb'
    """ Projection File Directory """
    prjDir = str(inDir) + '/Prj'
    prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj'
    """ Split Raster Directory """
    if os.path.isdir(str(workFld) + '/' + city + '_Split') == True:
        pass
    else:
        os.makedirs(str(workFld) + '/' + city + '_Split')
    splitDir = str(workFld) + '/' + city + '_Split'
    """ Set Workspace Environments """
    arcpy.env.workspace = workDir
    arcpy.env.scratch = str(inDir) + '/Scratch.gdb'
    arcpy.env.overwriteOutput = True

    #-----------------------------------------------------------------------------
    # BEGIN ANALYSIS
    #-----------------------------------------------------------------------------
    try:
        #-------- LOGFILE CREATION ---------------------------------------------
        """ Create report file for each metric """
        tmpName = city + '_TreeWV_' + time.strftime('%Y%m%d_%H-%M')
        reportfileName = reportfileDir + '/' + tmpName + '.txt'
        reportFile = open(reportfileName, 'w')

        try:
            loglist = sorted(f for f in os.listdir(reportfileDir)
                             if f.startswith(str(city) + '_Reuse'))
            tmpName = loglist[-1]
        except:
            tmpName = city + '_Reuse_' + time.strftime('%Y%m%d_%H-%M') + '.txt'
        reportfileName = reportfileDir + '/' + tmpName

        try:
            ReuseRF = open(reportfileName, 'a')
        except:
            ReuseRF = open(reportfileName, 'w')
            print 'Creating Reuse Log'
        """ Write out first line of report file """
        print 'Window Views of Trees Start Time: ' + time.asctime()
        reportFile.write(
            "Begin with EnviroAtlas 1-meter Land Cover for the EnviroAtlas community--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        #-------- PROCESSING LAYERS ----------------------------------------------
        """ Set Environments """
        arcpy.env.snapRaster = freqDir + '/LC'
        arcpy.env.extent = freqDir + '/LC'
        arcpy.env.overwriteOutput = True
        """-------- Reclassify LC into Binary Forest ----------------------------- """
        if arcpy.Exists(str(freqDir) + '/MForestIO') == False:
            outReclass = arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, 0], [10, 0], [20, 0], [21,
                                                                0], [22, 0],
                                     [30, 0], [40, 1], [52, 0], [70, 0],
                                     [80, 0], [82, 1], [91, 1], [92, 0]]))
            outReclass.save(str(freqDir) + '/MForestIO')
            reportFile.write(
                "Reclassify the Land Cover into a Forest binary REPLACE-MFE" +
                time.strftime('%Y%m%d--%H%M%S') + '--\n')
            print("Forest area reclassified to binary raster..." +
                  time.asctime())
            ReuseRF.write("MForestIO--" + time.strftime('%Y%m%d--%H%M%S') +
                          '--\n')

        else:
            print("Forest binary raster already exists..." + time.asctime())
            reportFile.write(
                "Reclassify the Land Cover into a Forest binary REPLACE-MFE--MForestIO"
                + '--\n')
        """-------- Create 50m Moving Window ------------------------------------- """
        MW = arcpy.sa.FocalStatistics(freqDir + '/MForestIO',
                                      arcpy.sa.NbrCircle(50, 'CELL'), 'SUM',
                                      'NODATA')
        MW.save('MFor_50C')
        print("Moving window complete..." + time.asctime())
        reportFile.write(
            "Run Focal Statistics on the Forest Binary Raster with a circular window of 50 meters and statistics = SUM.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Reclassify Moving Window into Trees/NoTrees ------------------- """
        ReC = arcpy.sa.Reclassify(
            'MFor_50C', 'Value',
            arcpy.sa.RemapRange([[0, 0.99999, 1], [0.99999, 10300, 0]]))
        ReC.save('NoForView')
        print(
            "Moving window completed and reclassified to tree / no trees..." +
            time.asctime())
        reportFile.write(
            "Reclassify the Focal Statistics into Forest (>0 -> 0) or No Forest (0 -> 1).--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Split the Raster As Needs, Process Each Piece ----------------- """
        """ Check if the raster should be split """
        columns = arcpy.GetRasterProperties_management(
            'NoForView', 'COLUMNCOUNT').getOutput(0)
        xsplit = int(float(columns) / 20000) + 1
        rows = arcpy.GetRasterProperties_management('NoForView',
                                                    'ROWCOUNT').getOutput(0)
        ysplit = int(float(rows) / 20000) + 1
        """-------- If no split, run the analysis --------------------------------- """
        if xsplit * ysplit == 1:
            """ Convert Raster to Polygon """
            arcpy.RasterToPolygon_conversion('NoForView', 'NFVP_Whole',
                                             'NO_SIMPLIFY')
            """ Extract areas with no forest in 50m """
            arcpy.Select_analysis('NFVP_Whole', 'NFVS_Whole', 'gridcode=1')
            reportFile.write(
                "Convert the raster into a polygon and select the features where gridcode = 1.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Project into Albers for use with Dasymetric """
            arcpy.Project_management('NFVS_Whole', 'NFVA_Whole', prjfile)
            print("Raster small enough, carry on..." + time.asctime())
            reportFile.write("Convert the polygons into Albers projection.--" +
                             time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """-------- If split, run the analysis on each piece and recombine --------- """
        else:
            """ Delete the raster, if necessary """
            xy = (xsplit * ysplit)
            for rast in range(xy):
                try:
                    arcpy.Delete_management(splitDir + '/nfvsp_' + str(rast))
                except:
                    pass
            try:
                arcpy.Delete_management(splitDir + '/noforview')
            except:
                pass
            """ Split the Raster """
            arcpy.RasterToOtherFormat_conversion('NoForView', splitDir, 'GRID')
            print("Raster too big, splitting into " + str(xsplit) +
                  " rows and " + str(ysplit) + " columns..." + time.asctime())
            arcpy.SplitRaster_management(splitDir + '/NoForView', splitDir,
                                         'NFVSp_', 'NUMBER_OF_TILES', 'GRID',
                                         '',
                                         str(xsplit) + ' ' + str(ysplit))
            reportFile.write(
                "Split the raster into pieces for easier processing. The Python script determines the number of pieces based on the number of rows and columns in the raster where no piece can have a side larger than 20,000 cells--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ For each raster: """
            prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj'
            xy = (xsplit * ysplit)
            for Chunk in range(0, xy):
                try:
                    result = float(
                        arcpy.GetRasterProperties_management(
                            splitDir + '/NFVSp_' + str(Chunk),
                            'MEAN').getOutput(0))
                    """ If the raster piece has data: """
                    if (result > 0):
                        """ Set Environments """
                        arcpy.env.snapRaster = freqDir + '/MForestIO'
                        arcpy.env.extent = freqDir + '/MForestIO'
                        """ Copy the piece back to the Working Directory """
                        arcpy.RasterToOtherFormat_conversion(
                            splitDir + '/NFVSp_' + str(Chunk), workDir)
                        """ Convert Raster to Polygon """
                        arcpy.RasterToPolygon_conversion(
                            'NFVSp_' + str(Chunk), 'NFVP_' + str(Chunk),
                            'NO_SIMPLIFY')
                        """ Extract areas with no forest in 50m """
                        arcpy.Select_analysis('NFVP_' + str(Chunk),
                                              'NFVS_' + str(Chunk),
                                              'gridcode=1')
                        """ Project into Albers for use with Dasymetric """
                        arcpy.Project_management('NFVS_' + str(Chunk),
                                                 'NFVA_' + str(Chunk), prjfile)
                        print("Chunk " + str(Chunk) + " / " + str(xy) +
                              " processed..." + time.asctime())
                except:
                    pass
            reportFile.write(
                "For each piece, convert the raster into a polygon and select the features where gridcode = 1--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            reportFile.write(
                "For each piece, convert the polygons into Albers projection.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Combine the resulting polygons """
            NFVchunks = arcpy.ListFeatureClasses('NFVA_*')
            arcpy.Merge_management(NFVchunks, workDir + '/NFVA_Whole')
            print("All chunks remerged..." + time.asctime())
            reportFile.write(
                "Merge all of the projected polygons together.--" +
                time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Set Environments """
        arcpy.env.snapRaster = freqDir + '/Dasy'
        arcpy.env.extent = freqDir + '/Dasy'
        """-------- End of Split Processing ---------------------------------------- """
        """ Extract Dasymetric Pixels where there is no forest in 50m """
        EbM = arcpy.sa.ExtractByMask(freqDir + '/Dasy', 'NFVA_Whole')
        EbM.save('Pop_NoForView')
        reportFile.write(
            "Extract by Mask the EnviroAtlas Dasymetric (2011/October 2015) pixels within the polygons--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Calculate the Extracted Dasy Population with Each CBG """
        arcpy.sa.ZonalStatisticsAsTable(freqDir + '/BG_Alb', 'bgrp',
                                        'Pop_NoForView', 'BG_TWV', 'DATA',
                                        'SUM')
        reportFile.write(
            "Calculate Zonal Statistics as Table for the extracted dasymetrics with the zones being the 2010 block groups for the EnviroAtlas community.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Calculate Total Dasy Population, if necessary ------------------ """
        """ Use the existing data """
        fieldNames = [f.name for f in arcpy.ListFields(freqDir + '/BG_Alb')]
        if 'Dasy_Pop' in fieldNames:
            reportFile.write(
                "Calculate Zonal Statistics as a Table for the EnviroAtlas Dasymetrics (2011/October 2015) with the zones being the 2010 block groups within the EnviroAtlas community boundary. Add resulting population sums to the community block groups as attribute Dasy_Pop--Dasy_Pop"
                + '--\n')
            """ Create population data """
        else:
            arcpy.AddField_management(freqDir + '/BG_Alb', 'Dasy_Pop', 'LONG')
            ZonalStatisticsAsTable(freqDir + '/BG_Alb', 'bgrp',
                                   freqDir + '/Dasy', freqDir + '/Dasy_ZS', '',
                                   'SUM')
            arcpy.JoinField_management(freqDir + '/BG_Alb', 'bgrp',
                                       freqDir + '/Dasy_ZS', 'bgrp', ['SUM'])
            arcpy.CalculateField_management(freqDir + '/BG_Alb', 'Dasy_Pop',
                                            '!SUM!', 'PYTHON_9.3')
            arcpy.DeleteField_management(freqDir + '/BG_Alb', ['SUM'])
            arcpy.JoinField_management(freqDir + '/BG', 'bgrp',
                                       freqDir + '/BG_Alb', 'bgrp',
                                       ['Dasy_Pop'])
            reportFile.write(
                "Calculate Zonal Statistics as a Table for the EnviroAtlas Dasymetrics (2011/October 2015) with the zones being the 2010 block groups within the EnviroAtlas community boundary. Add resulting population sums to the community block groups as attribute Dasy_Pop--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("Dasy_Pop--" + time.strftime('%Y%m%d--%H%M%S') +
                          '--\n')
        """-------- Create Final Table --------------------------------------------- """
        arcpy.TableToTable_conversion(freqDir + '/BG_Alb', workDir, 'TreeWV',
                                      '', 'bgrp')
        arcpy.DeleteField_management('TreeWV', [
            'PLx2_Pop', 'PLx2_Pct', 'SUM_HOUSIN', 'NonWhite', 'LandA_M',
            'Density', 'under_1', 'under_1pct', 'under_13', 'under_13pc',
            'over_70', 'over_70pct', 'Shape_Length', 'Shape_Leng',
            'NonWhite_Pop', 'NonWt_Pct', 'Area_M', 'Shape_Le_1', 'Shape_Area',
            'ALAND', 'NonWhite_P', 'H_Income_M', 'State'
        ])
        TreeView = 'TreeWV'
        reportFile.write(
            "Create a new table based on the EnviroAtlas community block groups table retaining the BGRP and Dasy_Pop fields--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Add fields to new table """
        arcpy.AddField_management(TreeView, 'WVT_Pop', 'LONG')
        arcpy.AddField_management(TreeView, 'WVT_Pct', 'FLOAT', 5, 2)
        reportFile.write(
            "Add fields to the new table for WVT_Pop (long), WVT_Pct (float).--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Join Each Table to the final table and WVW_Pop """
        arcpy.JoinField_management(TreeView, 'bgrp', 'BG_TWV', 'bgrp', ['SUM'])
        arcpy.CalculateField_management(TreeView, 'WVT_Pop', '!SUM!',
                                        'PYTHON_9.3')
        arcpy.MakeTableView_management(TreeView, 'TreeView_Tbl')
        arcpy.SelectLayerByAttribute_management('TreeView_Tbl',
                                                'NEW_SELECTION', 'SUM IS NULL')
        arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pop', 0,
                                        'PYTHON_9.3')
        arcpy.SelectLayerByAttribute_management('TreeView_Tbl',
                                                'CLEAR_SELECTION')
        arcpy.DeleteField_management('TreeView_Tbl', 'SUM')
        reportFile.write(
            "Join the zonal statistics table with the new table to calculate the new fields: WVT_Pop = zonal statistics.SUM; remove join--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Calculate WVT_Pct """
        arcpy.SelectLayerByAttribute_management('TreeView_Tbl',
                                                'NEW_SELECTION', 'Dasy_Pop >0')
        arcpy.CalculateField_management(
            'TreeView_Tbl', 'WVT_Pct',
            '"%.2f" % (float(!WVT_Pop!)/float(!Dasy_Pop!) * 100)',
            'PYTHON_9.3')
        arcpy.SelectLayerByAttribute_management('TreeView_Tbl',
                                                'CLEAR_SELECTION')
        reportFile.write(
            "Calculate field WVT_Pct = WVT_Pop / Dasy_Pop * 100 (limited to 2 decimal places).--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Calculate NULL values, where applicable """
        arcpy.SelectLayerByAttribute_management('TreeView_Tbl',
                                                'NEW_SELECTION',
                                                'Dasy_Pop = 0')
        arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pct', '-99999',
                                        'PYTHON_9.3')
        arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pop', '-99999',
                                        'PYTHON_9.3')
        arcpy.SelectLayerByAttribute_management('TreeView_Tbl',
                                                'CLEAR_SELECTION')
        arcpy.DeleteField_management('TreeView_Tbl', [
            'SUM_POP10', 'EAID', 'NonWhite', 'LandA_M', 'Density', 'Dasy_Pop',
            'SUM'
        ])
        print("Dasy raster summarized to BGs and stats calculated..." +
              time.asctime())
        reportFile.write(
            "Calculate fields where Dasy_Pop = 0: All Fields = -99999.--" +
            time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Check that the Analysis Area is covered by the LC -------------- """
        """ Create a Polygon Version of the LC """
        if arcpy.Exists(freqDir + '/LC_Poly') == False:
            arcpy.env.extent = freqDir + '/LC'
            arcpy.env.snapRaster = freqDir + '/LC'
            ReC = arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 1], [21,
                                                                1], [22, 1],
                                     [30, 1], [40, 1], [52, 1], [70, 1],
                                     [80, 1], [82, 1], [91, 1], [92, 1]]))
            ReC.save(str(freqDir) + '/AreaIO')
            arcpy.RasterToPolygon_conversion(
                str(freqDir) + '/AreaIO',
                str(freqDir) + '/LC_Poly', 'SIMPLIFY')
            arcpy.EliminatePolygonPart_management(
                str(freqDir) + '/LC_Poly',
                str(freqDir) + '/LC_Poly_EP', 'PERCENT', '', '5',
                'CONTAINED_ONLY')
            arcpy.Delete_management(str(freqDir) + '/LC_Poly')
            arcpy.Rename_management(
                str(freqDir) + '/LC_Poly_EP',
                str(freqDir) + '/LC_Poly')
        """ Buffer the LC Polygon by -500m """
        if arcpy.Exists(freqDir + '/Bnd_Cty_500m') == False:
            arcpy.Buffer_analysis(
                str(freqDir) + '/Bnd_Cty',
                str(freqDir) + '/Bnd_Cty_500m', '500 meters')
            arcpy.EliminatePolygonPart_management(
                str(freqDir) + '/Bnd_Cty_500m',
                str(freqDir) + '/Bnd_Cty_500m_EP', 'PERCENT', '', '30',
                'CONTAINED_ONLY')
            arcpy.Delete_management(str(freqDir) + '/Bnd_Cty_500m')
            arcpy.Rename_management(
                str(freqDir) + '/Bnd_Cty_500m_EP',
                str(freqDir) + '/Bnd_Cty_500m')
        """ Identify whether LC is large enough """
        arcpy.MakeFeatureLayer_management(str(freqDir) + '/LC_Poly', 'LClyr')
        arcpy.MakeFeatureLayer_management(
            str(freqDir) + '/Bnd_Cty_500m', 'BC_500lyr')

        arcpy.SelectLayerByLocation_management('BC_500lyr',
                                               'COMPLETELY_WITHIN', 'LClyr',
                                               '', 'NEW_SELECTION')
        bigEnough = float(arcpy.GetCount_management('BC_500lyr').getOutput(0))
        arcpy.SelectLayerByAttribute_management('BC_500lyr', 'CLEAR_SELECTION')
        """ If the LC isn't large enough, edit erroneous BGS """
        if bigEnough == 0:
            """ Identify BGs within 50m of the LC edge """
            arcpy.Buffer_analysis(
                str(freqDir) + '/LC_Poly', 'LC_Poly_Minus50', '-50 meters',
                'FULL', 'FLAT', 'ALL')
            arcpy.MakeFeatureLayer_management('LC_Poly_Minus50', 'Minus50')
            arcpy.MakeFeatureLayer_management(freqDir + '/BG', 'BG')

            arcpy.SelectLayerByLocation_management('BG', 'COMPLETELY_WITHIN',
                                                   'Minus50', '',
                                                   'NEW_SELECTION', 'INVERT')

            bgValue = float(arcpy.GetCount_management('BG').getOutput(0))
            print("LC extends beyond BG boundary, carry on..." +
                  time.asctime())
            """ For all BGs too close to the LC edge, assign both fields a value of -99998 """
            if bgValue > 0:
                bgrps = []
                cursor = arcpy.SearchCursor('BG')
                for row in cursor:
                    value = row.getValue('bgrp')
                    bgrps.append(value)
                bgrps = list(set(bgrps))
                expression = ''
                for bgrp in bgrps:
                    expression = expression + " OR bgrp = '" + str(bgrp) + "'"
                expression = expression[4:]
                arcpy.SelectLayerByAttribute_management(
                    'TreeView_Tbl', 'NEW_SELECTION', expression)
                arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pop',
                                                '-99998', 'PYTHON_9.3')
                arcpy.CalculateField_management('TreeView_Tbl', 'WVT_Pct',
                                                '-99998', 'PYTHON_9.3')
                arcpy.SelectLayerByAttribute_management(
                    'TreeView_Tbl', 'CLEAR_SELECTION')
            print(
                "LC doesn't extend beyond BGs, removing border BGs from analysis..."
                + time.asctime())
            reportFile.write(
                "Calculate Field for BGs within 50m of the edge of the land cover, WVT_Pop and WVW_Pct = -99998.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Create final table """
        arcpy.CopyRows_management('TreeView_Tbl', 'TreeWV_Fnl')
        try:
            arcpy.Delete_management(finalDir + '/' + str(city) + '_TreeWV')
        except:
            pass
        arcpy.TableToTable_conversion('TreeWV_Fnl', finalDir, city + '_TreeWV')
        allFields = [
            f.name for f in arcpy.ListFields(finalDir + '/' + city + '_TreeWV')
        ]
        for field in allFields:
            if field not in ['bgrp', 'OBJECTID', 'WVT_Pop', 'WVT_Pct']:
                arcpy.DeleteField_management(finalDir + '/' + city + '_TreeWV',
                                             [field])
            print 'Window Views of Trees End Time: ' + time.asctime() + '\n'
        reportFile.write(
            "Export the fields to be displayed in the EnviroAtlas to a final gdb table. WVT_Pop, WVT_Pct--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        #-------- COMPELETE LOGFILES ---------------------------------------------
        reportFile.close()
        ReuseRF.close()

#-----------------------------------------------------------------------------
# END ANALYSIS
#-----------------------------------------------------------------------------
    except:
        """ This part of the script executes if anything went wrong in the main script above """
        #-------- PRINT ERRORS ---------------------------------------------------
        print "\nSomething went wrong.\n\n"
        print "Python Traceback Message below:"
        print traceback.format_exc()
        print "\nArcMap Error Messages below:"
        print arcpy.GetMessages(2)
        print "\nArcMap Warning Messages below:"
        print arcpy.GetMessages(1)

        #-------- COMPLETE LOGFILE ------------------------------------------------
        reportFile.write("\nSomething went wrong.\n\n")
        reportFile.write("Pyton Traceback Message below:")
        reportFile.write(traceback.format_exc())
        reportFile.write("\nArcMap Error Messages below:")
        reportFile.write(arcpy.GetMessages(2))
        reportFile.write("\nArcMap Warning Messages below:")
        reportFile.write(arcpy.GetMessages(1))

        reportFile.write("\n\nEnded at " + time.asctime() + '\n')
        reportFile.write("\n---End of Log File---\n")

        if reportFile:
            reportFile.close()
Ejemplo n.º 12
0
def Guidos_Prep(city, inDir, workFld):
    import traceback, time, arcpy, os
    from arcpy import env
    arcpy.CheckOutExtension('Spatial')

    #-------- DIRECTORY SETUP ------------------------------------------------
    """ Working Directory """
    try:
        arcpy.CreateFileGDB_management(str(workFld), str(city) + '_GUIDOS.gdb')
    except:
        print 'GUIDOS GDB already exists'
    workDir = str(workFld) + '/' + city + '_GUIDOS.gdb'
    arcpy.env.workspace = workDir
    """ Report File Directory """
    reportfileDir = str(workFld) + '/Logs'
    """ Frequent Directory """
    freqDir = str(workFld) + '/' + city + '_Freq.gdb'
    """ Final Geodatabase """
    finalDir = str(workFld) + '/' + city + '_Final.gdb'
    """ Projection File Directory """
    prjDir = str(inDir) + '/Prj'
    prjfile = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj'
    """ Split Raster Directory """
    if os.path.isdir(str(workFld) + '/' + city + '_Split') == True:
        pass
    else:
        os.makedirs(str(workFld) + '/' + city + '_Split')
    splitDir = str(workFld) + '/' + city + '_Split'
    """ Set Workspace Environments """
    arcpy.env.workspace = workDir
    arcpy.env.scratch = str(inDir) + '/Scratch.gdb'
    arcpy.env.overwriteOutput = True

    #-----------------------------------------------------------------------------
    # BEGIN ANALYSIS
    #-----------------------------------------------------------------------------
    try:
        #-------- LOGFILE CREATION ---------------------------------------------
        """ Create report file for each metric """
        tmpName = city + '_Conn_' + time.strftime('%Y%m%d_%H-%M')
        reportfileName = reportfileDir + '/' + tmpName + '.txt'
        reportFile = open(reportfileName, 'w')
        """ Write out first line of report file """
        print 'Connectivity Data Prep Start Time: ' + time.asctime()
        reportFile.write(
            "Begin with EnviroAtlas 1-meter Land Cover for the EnviroAtlas community--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        #-------- PROCESSING LAYERS ----------------------------------------------
        """ Set Environments """
        arcpy.env.extent = freqDir + '/LC'
        arcpy.env.snapRaster = freqDir + '/LC'
        """ -------- Reclassify LC into Forest Foreground, All Else Background ----- """
        outReclass2 = arcpy.sa.Reclassify(
            freqDir + '/LC', 'Value',
            arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 1], [21, 1], [22, 1],
                                 [30, 1], [40, 2], [52, 1], [52, 1], [70, 1],
                                 [80, 1], [82, 1], [91, 2], [92, 1],
                                 ["NODATA", 0]]))
        outReclass2.save('FWW_WB')
        reportFile.write(
            "Reclassify the land cover into Foreground (REPLACE-GUID), and Background (All Else = 1). --"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Split the Raster As Needs, Process Each Piece ----------------- """
        """ Check if the raster should be split """
        columns = arcpy.GetRasterProperties_management(
            'FWW_WB', 'COLUMNCOUNT').getOutput(0)
        xsplit = int(float(columns) / 8000) + 1
        rows = arcpy.GetRasterProperties_management('FWW_WB',
                                                    'ROWCOUNT').getOutput(0)
        ysplit = int(float(rows) / 8000) + 1
        """ Delete the raster, if necessary """
        xy = (xsplit * ysplit)
        for rast in range(xy):
            try:
                arcpy.Delete_management(splitDir + '/FWW_WB_' + str(rast) +
                                        '.tif')
            except:
                pass
        try:
            arcpy.Delete_management(splitDir + '/fww_wb')
        except:
            pass
        try:
            arcpy.Delete_management(splitDir + '/fww_wb.tif')
        except:
            pass
        """-------- If no split, run the analysis --------------------------------- """
        if xsplit * ysplit == 1:
            print 'FWW_' + raster + ' is small enough as is.'
            arcpy.CopyRaster_management('FWW_WB', splitDir + '/FWW_WB.tif', '',
                                        '', 0, '', '', '8_BIT_UNSIGNED', '',
                                        '')
            reportFile.write("Convert Raster into Tiff.--" +
                             time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """-------- If split, run the analysis on each piece and recombine --------- """
        else:
            arcpy.CopyRaster_management('FWW_WB', splitDir + '/FWW_WB.tif', '',
                                        '', 0, '', '', '8_BIT_UNSIGNED', '',
                                        '')
            arcpy.SplitRaster_management(splitDir + '/FWW_WB.tif', splitDir,
                                         'FWW_WB_', 'NUMBER_OF_TILES', 'TIFF',
                                         '',
                                         str(xsplit) + ' ' + str(ysplit), '',
                                         '1000', 'PIXELS')
            reportFile.write(
                "Split the reclassified land cover into pieces of 10,000 x 10,000 pixels or fewer with a 1,000 pixel overlap. Convert to Tiff.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        print 'Connectivity Data Prep End Time: ' + time.asctime() + '\n'
        print 'Be sure to run the BatchStripedTile Model in ArcMap before GUIDOS.py runs.\n'

        #-------- COMPELETE LOGFILES ---------------------------------------------
        reportFile.close()

#-----------------------------------------------------------------------------
# END ANALYSIS
#-----------------------------------------------------------------------------
    except:
        """ This part of the script executes if anything went wrong in the main script above """
        #-------- PRINT ERRORS ---------------------------------------------------
        print "\nSomething went wrong.\n\n"
        print "Python Traceback Message below:"
        print traceback.format_exc()
        print "\nArcMap Error Messages below:"
        print arcpy.GetMessages(2)
        print "\nArcMap Warning Messages below:"
        print arcpy.GetMessages(1)

        #-------- COMPLETE LOGFILE ------------------------------------------------
        reportFile.write("\nSomething went wrong.\n\n")
        reportFile.write("Pyton Traceback Message below:")
        reportFile.write(traceback.format_exc())
        reportFile.write("\nArcMap Error Messages below:")
        reportFile.write(arcpy.GetMessages(2))
        reportFile.write("\nArcMap Warning Messages below:")
        reportFile.write(arcpy.GetMessages(1))

        reportFile.write("\n\nEnded at " + time.asctime() + '\n')
        reportFile.write("\n---End of Log File---\n")

        if reportFile:
            reportFile.close()
                        os.remove(trashfile)

                    if not arcpy.Exists(diroutGridsVar + "\\" + raster +
                                        "_161"):

                        print "\tspliting .. ", raster

                        rasterdeleteList = sorted(
                            glob.glob(diroutGridsVar + "\\" +
                                      os.path.basename(raster) + "_*"))
                        for rasterdelete in rasterdeleteList:
                            arcpy.Delete_management(rasterdelete)

                        arcpy.SplitRaster_management(raster, diroutGridsVar,
                                                     raster + "_",
                                                     "NUMBER_OF_TILES", "GRID",
                                                     "#", "18 9 ", "#", "0",
                                                     "PIXELS", "#", "#")
                        print "\t" + raster, "tiled"

                        trashList = sorted(
                            glob.glob(diroutGridsVar + "\\" + raster + "*.*"))
                        for trashfile in trashList:
                            os.remove(trashfile)

                    else:
                        print "\t" + raster, "tiled"

            #Create check file
            checkTXT = open(
                dirout + "\\" + sres + "_" + model + "_tiles_world_done.txt",
Ejemplo n.º 14
0
##====================================
##Split Raster
##Usage: SplitRaster_management in_raster out_folder out_base_name SIZE_OF_TILE
##                              | NUMBER_OF_TILES | TIFF | BMP | ENVI | ESRI BIL |
##                              ESRI BIP | ESRI BSQ | GIF | GRID | IMAGINE IMAGE |
##                              JP2 | JPG | PNG {NEAREST | BILINEAR | CUBIC |
##                              MAJORITY} {num_rasters} {tile_size} {overlap}
##                              {PIXELS | METERS | FEET | DEGREES | KILOMETERS |
##                              MILES} {cell_size} {origin}

try:
    import arcpy

    # arcpy.env.workspace = r"\\myServer\PrjWorkspace\RasGP"

    ##Equally split a large TIFF image by number of images
    arcpy.SplitRaster_management(in_raster = "D:/qiefen/2016/yinchuan20000.TIF", out_folder = "C:/Users/qq619/Desktop/qiefen", out_base_name = "qiefen", split_method = "SIZE_OF_TILE", format = "TIFF", resampling_type = "NEAREST", tile_size = "1000 1000", overlap = "0", units = "PIXELS")

    ##Equally split a large TIFF image by size of images
    # arcpy.SplitRaster_management("large.tif", "splitras", "size2", "SIZE_OF_TILE", \
    #                              "TIFF", "BILINEAR", "#", "3500 3500", "4", "PIXELS", \
    #                              "#", "-50 60")

except:
    print "Split Raster exsample failed."
    print arcpy.GetMessages()
    InExpression2 = OutRaster + " * $$Ymap"
    gp.SingleOutputMapAlgebra_sa(InExpression2, OutRaster2)

print "\n CREATING LON RASTER"
OutRaster3 = dirout + "\\longitude"
if not arcpy.Exists(OutRaster3):
    InExpression3 = OutRaster + " * $$Xmap"
    gp.SingleOutputMapAlgebra_sa(InExpression3, OutRaster3)

if int(ntiles) > 1:

    # dim = str(int(ntiles) ** 0.5) + " " + str(int(ntiles) ** 0.5) + " "
    dim = "1 2 "
    print "\n SPLIT IN TILES"
    arcpy.SplitRaster_management(OutAlt, dirout, "altitude", "NUMBER_OF_TILES",
                                 "GRID", "#", dim, "#", "5", "DEGREES", "#",
                                 "#")
    arcpy.SplitRaster_management(OutRaster2, dirout, "latitude",
                                 "NUMBER_OF_TILES", "GRID", "#", dim, "#", "5",
                                 "DEGREES", "#", "#")
    arcpy.SplitRaster_management(OutRaster3, dirout, "longitude",
                                 "NUMBER_OF_TILES", "GRID", "#", dim, "#", "5",
                                 "DEGREES", "#", "#")

    varlist = "altitude", "latitude", "longitude"

    print "\n CONVERTING TO ASCII BY TILES"
    for i in range(0, int(ntiles), 1):

        diroutasc = dirout + "\\tile-" + str(i + 1)
        if not os.path.exists(diroutasc):
Ejemplo n.º 16
0
import os, shutil
import arcpy

#------------------------------------------------------------------------
# process the data
#------------------------------------------------------------------------

if os.path.exists(workspace + "/split_rasters"):
    shutil.rmtree(workspace + "/split_rasters")
os.makedirs(workspace + "/split_rasters")

print "raster subsets will be placed in " + workspace + "/split_rasters"

arcpy.env.workspace = workspace
rasterList = arcpy.ListRasters()

for raster in rasterList:

    print 'splitting', raster + '_'
    if raster[-4] == '.':
        outBaseName = raster[0:-4] + '_'
    else:
        outBaseName = raster

    arcpy.SplitRaster_management(raster, workspace + "/split_rasters",
                                 outBaseName, "NUMBER_OF_TILES", fileType,
                                 "NEAREST",
                                 str(cols) + " " + str(rows), "0 0", "0",
                                 "PIXELS", "#", "#")
Ejemplo n.º 17
0
	
print "\n CREATING MASK"
OutRaster = dirout + "\\mask"
if not gp.Exists(OutRaster):
	InExpression = "(" + OutAlt + " * 0) + 1"
	arcpy.gp.RasterCalculator_sa(InExpression, OutRaster)
	
print "\n CREATING LAT RASTER"
OutRaster2 = dirout + "\\latitude"
if not gp.Exists(OutRaster2):
	InExpression2 = OutRaster + " * $$Ymap"
	arcpy.gp.RasterCalculator_sa(InExpression2, OutRaster2)
	

print "\n CREATING LON RASTER"
OutRaster3 = dirout + "\\longitude"
if not gp.Exists(OutRaster3):
	InExpression3 = OutRaster + " * $$Xmap"
	arcpy.gp.RasterCalculator_sa(InExpression3, OutRaster3)
	
	
print "\n SPLIT IN TILES"
arcpy.SplitRaster_management(raster, diroutGridsVar, raster + "_", "NUMBER_OF_TILES", "GRID", "#", str(countryDic [country]), "#", "0", "PIXELS", "#", "#")

	
gp.RasterToASCII_conversion(OutAlt, dirout + "\\alt-prj-" + region + ".asc")
gp.RasterToASCII_conversion(OutRaster2, dirout + "\\lat-prj-" + region + ".asc")
gp.RasterToASCII_conversion(OutRaster3, dirout + "\\lon-prj-" + region + ".asc")
gp.delete_management(OutAlt)
gp.delete_management(OutRaster)
print "\t ..done!!"
Ejemplo n.º 18
0
def GSTCnWR(city, inDir, workFld):
    import traceback, time, arcpy, os
    from arcpy import env
    arcpy.CheckOutExtension('Spatial')

    #-------- DIRECTORY SETUP ------------------------------------------------
    """ Working Directory """
    try:
        arcpy.CreateFileGDB_management(str(workFld),
                                       str(city) + '_GSTCnWR.gdb')
    except:
        pass
    workGDB = str(workFld) + '/' + str(city) + '_GSTCnWR.gdb'
    """ Report File Directory """
    reportfileDir = str(workFld) + '/Logs'
    """ Frequent Directory """
    freqDir = str(workFld) + '/' + city + '_Freq.gdb'
    """ Final Geodatabase """
    finalDir = str(workFld) + '/' + city + '_Final.gdb'
    """ Projection File Directory """
    prjDir = str(inDir) + '/Prj'
    prjfileALB = prjDir + '/USA Contiguous Albers Equal Area Conic USGS.prj'
    prjfileWM = prjDir + '/WGS 1984 Web Mercator (auxiliary sphere).prj'
    """ Input Walkable Roads Data """
    walkroads = str(inDir) + '/' + 'Parks.gdb/' + str(city) + '_Walk_Road_Alb'
    """ Split Raster Directory """
    if os.path.isdir(str(workFld) + '/' + city + '_Split') == True:
        pass
    else:
        os.makedirs(str(workFld) + '/' + city + '_Split')
    splitDir = str(workFld) + '/' + city + '_Split'
    """ Set Workspace Environments """
    arcpy.env.workspace = workGDB
    arcpy.env.scratch = str(inDir) + '/Scratch.gdb'
    arcpy.env.overwriteOutput = True

    #-----------------------------------------------------------------------------
    # BEGIN ANALYSIS
    #-----------------------------------------------------------------------------
    try:
        #-------- LOGFILE CREATION ---------------------------------------------
        """ Create report file for each metric """
        tmpName = city + '_PctStGS_' + time.strftime('%Y%m%d_%H-%M')
        reportfileName = reportfileDir + '/' + tmpName + '.txt'
        gsRF = open(reportfileName, 'w')

        tmpName = city + '_PctStTC_' + time.strftime('%Y%m%d_%H-%M')
        reportfileName = reportfileDir + '/' + tmpName + '.txt'
        tcRF = open(reportfileName, 'w')

        try:
            loglist = sorted(f for f in os.listdir(reportfileDir)
                             if f.startswith(str(city) + '_Reuse'))
            tmpName = loglist[-1]
        except:
            tmpName = city + '_Reuse_' + time.strftime('%Y%m%d_%H-%M') + '.txt'
        reportfileName = reportfileDir + '/' + tmpName

        try:
            ReuseRF = open(reportfileName, 'a')
        except:
            ReuseRF = open(reportfileName, 'w')
            print 'Creating Reuse Log'
        """ Write out first lines of report file """
        print 'Green Space and Tree Cover Along Walkable Roads Start Time: ' + time.asctime(
        )
        gsRF.write(
            "Begin with the 2011 Walkable Roads and the 1-M Land Cover for the EnviroAtlas community, created by the US EPA EnviroAtlas Team.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        tcRF.write(
            "Begin with the 2011 Walkable Roads Layer and the 1-M Land Cover for the EnviroAtlas community, created by the US EPA EnviroAtlas Team.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        gsRF.write(
            "The walkable roads layer was created from 2011 NavTeq Streets by removing roads with multiple entrance/exit ramps, roads with speed limits above 54 mph, roads with functional class less than 3, and roads where pedestrians are not allowed.--201611--\n"
        )
        tcRF.write(
            "The walkable roads layer was created from 2011 NavTeq Streets by removing roads with multiple entrance/exit ramps, roads with speed limits above 54 mph, roads with functional class less than 3, and roads where pedestrians are not allowed.--201611--\n"
        )

        #-------- PROCESSING LAYERS ----------------------------------------------
        """ Set Environments """
        arcpy.env.extent = str(freqDir) + '/LC'
        arcpy.env.snapRaster = str(freqDir) + '/LC'

        #-------- PREPARE LAND COVER --------------------------------------
        """-------- Reclassify LC into Binary Green Space with Water ----------------------------- """
        if arcpy.Exists(str(freqDir) + '/GBS_Ionly') == False:
            outReclass = arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, "NODATA"], [10, 1], [20, "NODATA"],
                                     [21, "NODATA"], [22, "NODATA"],
                                     [30, "NODATA"], [40, 1], [52, 1], [70, 1],
                                     [80, 1], [82, 1], [91, 1], [92, 1]]))
            outReclass.save(str(freqDir) + '/GBS_Ionly')
            gsRF.write("Reclassify into green space + water. REPLACE-GSBE--" +
                       time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write('GBSI--' + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        else:
            gsRF.write(
                "Reclassify into green space + water. REPLACE-GSBE--GBSI" +
                '--\n')
        """-------- Reclassify LC into Binary Forest ----------------------------- """
        if arcpy.Exists(str(freqDir) + '/Forest_Ionly') == False:
            outReclass = arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, "NODATA"], [10, "NODATA"],
                                     [20, "NODATA"], [21, "NODATA"],
                                     [22, "NODATA"], [30, "NODATA"], [40, 1],
                                     [52, "NODATA"], [70, "NODATA"],
                                     [80, "NODATA"], [82, 1], [91, 1],
                                     [92, "NODATA"]]))
            outReclass.save(str(freqDir) + '/Forest_Ionly')
            tcRF.write(
                "Reclassify the Land Cover into Binary Forest. REPLACE-MFIE--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            ReuseRF.write("ForI--" + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        else:
            tcRF.write(
                "Reclassify the Land Cover into Binary Forest. REPLACE-MFIE--ForI"
                + '--\n')
        """ Convert rasters to TIFF """
        try:
            arcpy.Delete_management(splitDir + '/gbs_ionly')
        except:
            pass
        arcpy.RasterToOtherFormat_conversion(
            str(freqDir) + '/GBS_Ionly', splitDir, 'GRID')
        gsRF.write(
            "Convert the reclassified raster into GeoTIFF for partial processing.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        try:
            arcpy.Delete_management(splitDir + '/forest_ionly')
        except:
            pass
        arcpy.RasterToOtherFormat_conversion(
            str(freqDir) + '/Forest_Ionly', splitDir, 'GRID')
        tcRF.write(
            "Convert the reclassified raster into GeoTIFF for partial processing.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Split the Raster As Needs, Process Each Piece ----------------- """
        """ Check if the raster should be split """
        columns = arcpy.GetRasterProperties_management(
            str(freqDir) + '/Forest_Ionly', 'COLUMNCOUNT').getOutput(0)
        xsplit = int(float(columns) / 40000) + 1
        rows = arcpy.GetRasterProperties_management(
            str(freqDir) + '/Forest_Ionly', 'ROWCOUNT').getOutput(0)
        ysplit = int(float(rows) / 40000) + 1
        """-------- If no split, run the analysis --------------------------------- """
        if xsplit * ysplit == 1:
            """ Convert Rasters to Polygons """
            arcpy.RasterToPolygon_conversion(
                str(freqDir) + '/GBS_Ionly', workGDB + '/GBSPoly', 'SIMPLIFY')
            gsRF.write("Convert the TIFF into Polygons.--" +
                       time.strftime('%Y%m%d--%H%M%S') + '--\n')
            arcpy.RasterToPolygon_conversion(
                str(freqDir) + '/Forest_Ionly', workGDB + '/ForPoly',
                'SIMPLIFY')
            tcRF.write("Convert the TIFF into Polygons.--" +
                       time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """-------- If split, run the analysis on each piece and recombine --------- """
        else:
            """ Delete the raster, if necessary """
            xy = (xsplit * ysplit)
            for rast in range(xy):
                try:
                    arcpy.Delete_management(splitDir + '/gbsp_' + str(rast))
                    arcpy.Delete_management(splitDir + '/forp_' + str(rast))
                except:
                    pass
            """ Split the Raster """
            arcpy.SplitRaster_management(splitDir + '/gbs_ionly', splitDir,
                                         'gbsp_', 'NUMBER_OF_TILES', 'GRID',
                                         '',
                                         str(xsplit) + ' ' + str(ysplit))
            gsRF.write(
                "Split the reclassified raster into pieces of no more than 40,000x40,000 pixels, if necessary.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')

            arcpy.SplitRaster_management(splitDir + '/forest_ionly', splitDir,
                                         'forp_', 'NUMBER_OF_TILES', 'GRID',
                                         '',
                                         str(xsplit) + ' ' + str(ysplit))
            tcRF.write(
                "Split the reclassified raster into pieces of no more than 40,000x40,000 pixels, if necessary.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Set Environments """
            arcpy.env.snapRaster = str(freqDir) + '/GBS_Ionly'
            arcpy.env.extent = str(freqDir) + '/GBS_Ionly'
            """ Convert Raster to Polygon for Green Space"""
            for Chunk in range(0, xy):
                try:
                    result = float(
                        arcpy.GetRasterProperties_management(
                            splitDir + '/gbsp_' + str(Chunk),
                            'MEAN').getOutput(0))
                    if (result <> 0):
                        arcpy.RasterToOtherFormat_conversion(
                            splitDir + '/gbsp_' + str(Chunk), workGDB)
                        arcpy.RasterToPolygon_conversion(
                            'gbsp_' + str(Chunk), 'GBSPoly_' + str(Chunk),
                            'SIMPLIFY')
                        arcpy.RepairGeometry_management('GBSPoly_' +
                                                        str(Chunk))
                    else:
                        pass
                except:
                    pass
            """ Convert Raster to Polygon for Tree Cover"""
            for Chunk in range(0, xy):
                try:
                    result = float(
                        arcpy.GetRasterProperties_management(
                            splitDir + '/forp_' + str(Chunk),
                            'MEAN').getOutput(0))
                    if (result <> 0):
                        arcpy.RasterToOtherFormat_conversion(
                            splitDir + '/forp_' + str(Chunk), workGDB)
                        arcpy.RasterToPolygon_conversion(
                            'forp_' + str(Chunk), 'ForPoly_' + str(Chunk),
                            'SIMPLIFY')
                        arcpy.RepairGeometry_management('ForPoly_' +
                                                        str(Chunk))
                    else:
                        pass
                except:
                    pass

            gsRF.write("Convert each raster into polygons.--" +
                       time.strftime('%Y%m%d--%H%M%S') + '--\n')
            tcRF.write("Convert each raster into polygons.--" +
                       time.strftime('%Y%m%d--%H%M%S') + '--\n')
            """ Merge the polygons back together"""
            fcList = arcpy.ListFeatureClasses('GBSPoly_*')
            arcpy.Merge_management(fcList, 'GBSPoly')
            gsRF.write("Merge the polygons into one feature class.--" +
                       time.strftime('%Y%m%d--%H%M%S') + '--\n')

            fcList = arcpy.ListFeatureClasses('ForPoly_*')
            arcpy.Merge_management(fcList, 'ForPoly')
            tcRF.write("Merge the polygons into one feature class.--" +
                       time.strftime('%Y%m%d--%H%M%S') + '--\n')

#-------- PREPARE ROADS DATA --------------------------------------
        """ Copy Walkable Roads to the Working Directory """
        try:
            arcpy.CopyFeatures_management(walkroads, 'Walk_Road_Alb')
            gsRF.write(
                "Clip the EnviroAtlas Walkable Roads layer to the EnviroAtlas community boundary and project into UTM.--ANALYST-TIME--\n"
            )
            tcRF.write(
                "Clip the EnviroAtlas Walkable Roads layer to the EnviroAtlas community boundary and project into UTM.--ANALYST-TIME--\n"
            )
        except:
            arcpy.Clip_analysis(inDir + '/Input.gdb/Walkable_Roads',
                                freqDir + '/Bnd_5km', 'Walk_Road_Alb')
            gsRF.write(
                "Clip the NavTeq Streets layer to the EnviroAtlas community boundary and project into UTM.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
            tcRF.write(
                "Clip the NavTeq Streets layer to the EnviroAtlas community boundary and project into UTM.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Project roads into UTM """
        descLC = arcpy.Describe(str(freqDir) + '/LC')
        arcpy.Project_management('Walk_Road_Alb', 'Walk_Road_UTM',
                                 descLC.spatialReference)
        walkroads = 'Walk_Road_UTM'
        """ Create Intersection Points """
        arcpy.UnsplitLine_management(walkroads, 'WalkRd_Unsplit', "ST_NAME")
        arcpy.Intersect_analysis(['WalkRd_Unsplit', 'WalkRd_Unsplit'],
                                 'Intersections', "ONLY_FID", '', "POINT")
        arcpy.DeleteIdentical_management('Intersections', "shape")
        gsRF.write(
            "Unsplit the Walkable roads and intersecting them with themselves to create a point at each intersection.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        tcRF.write(
            "Unsplit the Walkable roads and intersecting them with themselves to create a point at each intersection.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        #-------- RUN TREE COVER ANALYSIS --------------------------------------
        """ Make New Road Attributes """
        arcpy.AddField_management(walkroads, "totLanes", "SHORT")
        arcpy.AddField_management(walkroads, "dirTrvl", "SHORT")
        arcpy.AddField_management(walkroads, "numLanes", "SHORT")
        arcpy.CalculateField_management(walkroads, "totLanes",
                                        "!TO_LANES! + !FROM_LANES!",
                                        "PYTHON_9.3")
        codeblock = """def makeBin(x):
                if x == 'B':
                    return 2
                else:
                    return 1"""
        arcpy.CalculateField_management(walkroads, "dirTrvl",
                                        "makeBin(!DIR_TRAVEL!)", "PYTHON_9.3",
                                        codeblock)
        arcpy.CalculateField_management(walkroads, "numLanes", "!totLanes!",
                                        "PYTHON_9.3")
        with arcpy.da.UpdateCursor(walkroads, ('numLanes', ),
                                   "numLanes = 0") as cursor:
            for row in cursor:
                row[0] = 2
                cursor.updateRow(row)

        tcRF.write(
            "Add and calculate new fields: totLanes (short) = TO_LANES + FROM_LANES; dirTrvl (short) = 2 if DIR_TRAVEL='B'; else = 1; numLanes (short) = totLanes if totLanes > 0; else = 2--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Dissolve Roads """
        arcpy.Dissolve_management(walkroads, 'RdsByLanes_For',
                                  ["numLanes", "dirTrvl"])
        tcRF.write(
            "Dissolve the Walkable Roads by number of lanes (totLanes) and direction of travel (dirTrvl).--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Split Roads at Intersections """
        arcpy.SplitLineAtPoint_management('RdsByLanes_For', 'Intersections',
                                          'RdsByBlk_For', "3 Meters")
        tcRF.write(
            "Split the dissolved walkable roads at each intersection point to create road blocks.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Create the Focal Areas --------------------------"""
        """ Create Max Buffer """
        arcpy.AddField_management('RdsByBlk_For', "Max_Buff", "FLOAT")
        arcpy.CalculateField_management(
            'RdsByBlk_For', "Max_Buff",
            "(!numLanes! * 3.5 + 2.5 * !dirTrvl!)/2 + 7.5", "PYTHON_9.3")
        arcpy.Buffer_analysis('RdsByBlk_For', 'Max_Buff', "Max_Buff", "FULL",
                              "FLAT")
        tcRF.write(
            'Calculate the estimated width of the road (number of lanes * 3.5 meters/lane + 2.5 meters of parking lanes per direction of travel) plus the focus area: Mx_Buff (float) = (numLanes*3.5 + 2.5*dirTrvl)/2 + 7.5. Buffer the roads by this value.--'
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Create Min Buffer """
        arcpy.AddField_management('RdsByBlk_For', "Min_Buff", "FLOAT")
        arcpy.CalculateField_management(
            'RdsByBlk_For', "Min_Buff",
            "(!numLanes! * 3.5 + 2.5 * !dirTrvl!)/2 - 1", "PYTHON_9.3")
        arcpy.Buffer_analysis('RdsByBlk_For', 'Min_Buff', "Min_Buff", "FULL",
                              "FLAT")
        tcRF.write(
            "Calculate the width of the road less one meter (to account for narrower roads; number of lanes * 3.5 meters/lane + 2.5 meters of parking lanes per direction of travel): Mn_Buff (float)= (numLanes*3.5 + 2.5*dirTrvl)/2 - 1. Buffer the roads by this value.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Subtract Buffers to Create Final Buffer """
        arcpy.Erase_analysis("Max_Buff", 'Min_Buff', 'FocalArea_For')
        tcRF.write(
            "Create the focus area feature class by erasing the second, smaller buffer from the first, larger buffer.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Calculate Percent Forest in Focal Area ----------------"""
        arcpy.AddField_management('FocalArea_For', "road_ID", "FLOAT")
        arcpy.AddField_management('FocalArea_For', "seg_area", "FLOAT")
        arcpy.CalculateField_management('FocalArea_For', "road_ID",
                                        "!ORIG_FID!", "PYTHON_9.3")
        arcpy.CalculateField_management('FocalArea_For', "seg_area",
                                        "!Shape_area!", "PYTHON_9.3")
        """-------- Check that the Analysis Area is covered by the LC -------------- """
        """ Create a Polygon Version of the LC """
        if arcpy.Exists(freqDir + '/LC_Poly') == False:
            arcpy.env.snapRaster = freqDir + '/LC'
            arcpy.env.extent = freqDir + '/LC'
            ReC = arcpy.sa.Reclassify(
                str(freqDir) + '/LC', 'Value',
                arcpy.sa.RemapValue([[0, 0], [10, 1], [20, 1], [21,
                                                                1], [22, 1],
                                     [30, 1], [40, 1], [52, 1], [70, 1],
                                     [80, 1], [82, 1], [91, 1], [92, 1]]))
            ReC.save(str(freqDir) + '/AreaIO')
            arcpy.RasterToPolygon_conversion(
                str(freqDir) + '/AreaIO',
                str(freqDir) + '/LC_Poly', 'SIMPLIFY')
            arcpy.EliminatePolygonPart_management(
                str(freqDir) + '/LC_Poly',
                str(freqDir) + '/LC_Poly_EP', 'PERCENT', '', '5',
                'CONTAINED_ONLY')
            arcpy.Delete_management(str(freqDir) + '/LC_Poly')
            arcpy.Rename_management(
                str(freqDir) + '/LC_Poly_EP',
                str(freqDir) + '/LC_Poly')
            arcpy.env.snapRaster = str(freqDir) + '/GBS_Ionly'
            arcpy.env.extent = str(freqDir) + '/GBS_Ionly'
        """ Buffer the LC Polygon by -500m """
        if arcpy.Exists(freqDir + '/Bnd_Cty_500m') == False:
            arcpy.Buffer_analysis(
                str(freqDir) + '/Bnd_Cty',
                str(freqDir) + '/Bnd_Cty_500m', '500 meters')
            arcpy.EliminatePolygonPart_management(
                str(freqDir) + '/Bnd_Cty_500m',
                str(freqDir) + '/Bnd_Cty_500m_EP', 'PERCENT', '', '30',
                'CONTAINED_ONLY')
            arcpy.Delete_management(str(freqDir) + '/Bnd_Cty_500m')
            arcpy.Rename_management(
                str(freqDir) + '/Bnd_Cty_500m_EP',
                str(freqDir) + '/Bnd_Cty_500m')
        """ Identify whether LC is large enough """
        arcpy.MakeFeatureLayer_management(str(freqDir) + '/LC_Poly', 'LClyr')
        arcpy.MakeFeatureLayer_management(
            str(freqDir) + '/Bnd_Cty_500m', 'BC_500lyr')

        arcpy.SelectLayerByLocation_management('BC_500lyr',
                                               'COMPLETELY_WITHIN', 'LClyr',
                                               '', 'NEW_SELECTION')
        bigEnough = float(arcpy.GetCount_management('BC_500lyr').getOutput(0))
        arcpy.SelectLayerByAttribute_management('BC_500lyr', 'CLEAR_SELECTION')
        """ If the LC isn't large enough, limit the output """
        if bigEnough == 0:
            arcpy.MakeFeatureLayer_management("FocalArea_For", 'FA_For_lyr')
            arcpy.SelectLayerByLocation_management('FA_For_Lyr',
                                                   'COMPLETELY_WITHIN',
                                                   freqDir + '/LC_Poly', '',
                                                   'NEW_SELECTION')
            arcpy.CopyFeatures_management('FA_For_Lyr', 'FocalArea_For_EE')
            arcpy.SelectLayerByAttribute_management('FA_For_lyr',
                                                    'CLEAR_SELECTION')
            tcRF.write(
                "Due to the extent of the Land Cover, the analysis area is smaller than the EnviroAtlas Community Boundary. Remove any features that are not within the analysis area.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """-------- Calculate Percent Forest per Block ------------------------------ """
        """ Intersect the tree cover polygons with the focus areas """
        arcpy.RepairGeometry_management("ForPoly")
        try:
            arcpy.Intersect_analysis(["FocalArea_For_EE", "ForPoly"],
                                     'SideWalk_For', "ALL", "", "INPUT")
        except:
            arcpy.Intersect_analysis(["FocalArea_For", "ForPoly"],
                                     'SideWalk_For', "ALL", "", "INPUT")
        tcRF.write(
            "Intersect the focus areas with the forest polygon layer to generate forest alongside roads.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Dissolve the roads by road_ID """
        arcpy.Dissolve_management('SideWalk_For', 'SiWa_For_Blk', ["road_ID"],
                                  [["seg_area", "FIRST"]])
        tcRF.write(
            "Dissolve the forest along roads by the road identifier field: road_ID, maintain the Shape_Area field.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Calcualte Percent Tree Cover """
        arcpy.AddField_management('SiWa_For_Blk', "Pct_TC", "FLOAT")
        arcpy.CalculateField_management('SiWa_For_Blk', "Pct_TC",
                                        "(!Shape_Area!*100)/!FIRST_seg_area!",
                                        "PYTHON_9.3")
        tcRF.write(
            "Calculate new variable in the forest grouped by road identifier layer: Pct_TC (float) = Shape_Area*100 / seg_area.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Add the Percent Tree Cover back to the Block Segments """
        arcpy.JoinField_management('RdsByBlk_For', "OBJECTID", 'SiWa_For_Blk',
                                   "road_ID", "Pct_TC")
        tcRF.write(
            "Join the Pct_TC field to the road layer based on the road_ID field.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        codeblock1 = """def changenull(x):
            if x < 0:
                return 0
            else:
                return x"""
        arcpy.CalculateField_management('RdsByBlk_For', "Pct_TC",
                                        "changenull(!Pct_TC!)", "PYTHON_9.3",
                                        codeblock1)
        arcpy.DeleteField_management(
            'RdsByBlk_For', ['dirTrvl', 'numLanes', 'Mx_buff', 'Mn_buff'])
        tcRF.write("Change null values in the Pct_TC field to 0.--" +
                   time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Clip to the Boundary """
        arcpy.Clip_analysis('RdsByBlk_For', freqDir + '/Bnd_Cty',
                            'PctStTC_Bnd')
        tcRF.write("Clip road file to the community boundary.--" +
                   time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Project into Albers """
        arcpy.Project_management('PctStTC_Bnd', city + '_PctStTC', prjfileALB)
        tcRF.write("Project the road file into Albers.--" +
                   time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Add to Final Directory """
        arcpy.FeatureClassToFeatureClass_conversion(city + '_PctStTC',
                                                    finalDir,
                                                    city + '_PctStTC')
        tcRF.write(
            "Copy the feature class to the final geodatabase for use in EnviroAtlas.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Convert to Raster """
        arcpy.PolylineToRaster_conversion(
            'PctStTC_Bnd', 'Pct_TC', splitDir + '/' + city + '_PctStTC.tif',
            '', '', 10)
        tcRF.write(
            "Convert the road file to raster with the Pct_TC as the value field and a cell size of 10m.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        #-------- RUN GREEN SPACE ANALYSIS --------------------------------------
        """ Dissolve roads and split at intersections """
        arcpy.Dissolve_management(walkroads, 'GS_WalkRds')
        gsRF.write("Dissolve the Walkable Roads.--" +
                   time.strftime('%Y%m%d--%H%M%S') + '--\n')
        arcpy.SplitLineAtPoint_management('GS_WalkRds', 'Intersections',
                                          'RdsByBlk_GS', "3 Meters")
        gsRF.write(
            "Split the dissolved walkable roads at each intersection point to create road blocks.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Create 50m Green Space Buffers """
        arcpy.Buffer_analysis('RdsByBlk_GS', 'FocalArea_GS', "25 Meters",
                              "FULL", "FLAT")
        gsRF.write(
            "Create the focus areas by buffering the road blocks by 25 meters with options FULL, FLAT, and NONE.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Create segment IDs and calculate segment areas """
        arcpy.AddField_management('FocalArea_GS', "road_ID", "FLOAT")
        arcpy.AddField_management('FocalArea_GS', "seg_area", "FLOAT")
        arcpy.CalculateField_management('FocalArea_GS', "road_ID",
                                        "!ORIG_FID!", "PYTHON_9.3")
        arcpy.CalculateField_management('FocalArea_GS', "seg_area",
                                        "!Shape_area!", "PYTHON_9.3")
        gsRF.write(
            "Add and calculate fields road_ID (float) = ORIG_FID and seg_area (float) = Shape_area in the focus area feature class.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ If the LC isn't large enough, limit the output """
        if bigEnough == 0:
            arcpy.MakeFeatureLayer_management("FocalArea_GS", 'FA_GS_lyr')
            arcpy.SelectLayerByLocation_management('FA_GS_Lyr',
                                                   'COMPLETELY_WITHIN',
                                                   freqDir + '/LC_Poly', '',
                                                   'NEW_SELECTION')
            arcpy.CopyFeatures_management('FA_GS_Lyr', 'FocalArea_GS_EE')
            arcpy.SelectLayerByAttribute_management('FA_GS_lyr',
                                                    'CLEAR_SELECTION')
            gsRF.write(
                "Due to the extent of the Land Cover, the analysis area is smaller than the EnviroAtlas Community Boundary. Remove any features that are not within the analysis area.--"
                + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Intersect the Green Space Polygons with the Green Space Focus Area """
        arcpy.RepairGeometry_management("GBSPoly")
        try:
            arcpy.Intersect_analysis(['FocalArea_GS_EE', "GBSPoly"],
                                     'SideWalk_GS', "ALL", "", "INPUT")

        except:
            arcpy.Intersect_analysis(['FocalArea_GS', 'GBSPoly'],
                                     'SideWalk_GS', "ALL", "", "INPUT")
        gsRF.write(
            "Intersect the focus areas with the green space polygon layer to generate green space alongside roads.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Dissolve the road segments by segement and maintain the road ID """
        arcpy.Dissolve_management('SideWalk_GS', 'SiWa_GS_Blk', ["road_ID"],
                                  [["seg_area", "FIRST"]])
        gsRF.write(
            "Dissolve the green space along roads by the road identifier field: road_ID, maintain the Shape_Area field.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Calculate percent green space by segment """
        arcpy.AddField_management('SiWa_GS_Blk', "Pct_GS", "FLOAT")
        arcpy.CalculateField_management('SiWa_GS_Blk', "Pct_GS",
                                        "(!Shape_Area!*100)/!FIRST_seg_area!",
                                        "PYTHON_9.3")
        gsRF.write(
            "Calculate new variable in the green space grouped by road identifier layer: Pct_GS (float) = Shape_Area*100 / seg_area.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Add the percent green space to the road segments """
        arcpy.JoinField_management('RdsByBlk_GS', "OBJECTID", 'SiWa_GS_Blk',
                                   "road_ID", "Pct_GS")
        gsRF.write(
            "Join the Pct_GS field to the road layer based on the road_ID field.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Convert Nulls to 0 """
        codeblock2 = """def changenull(x):
            if x < 0:
                return 0
            else:
                return x"""
        arcpy.CalculateField_management('RdsByBlk_GS', "Pct_GS",
                                        "changenull(!Pct_GS!)", "PYTHON_9.3",
                                        codeblock2)
        arcpy.DeleteField_management(
            'RdsByBlk_GS', ['dirTrvl', 'numLanes', 'Mx_buff', 'Mn_buff'])
        gsRF.write("Change null values in the Pct_GS field to 0.--" +
                   time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Clip to the Boundary """
        arcpy.Clip_analysis('RdsByBlk_GS', freqDir + '/Bnd_Cty', 'PctStGS_Bnd')
        gsRF.write("Clip road file to the community boundary.--" +
                   time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Project into Albers """
        arcpy.Project_management('PctStGS_Bnd', city + '_PctStGS', prjfileALB)
        gsRF.write("Project the road file into Albers.--" +
                   time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Add to Final Directory """
        arcpy.FeatureClassToFeatureClass_conversion(city + '_PctStGS',
                                                    finalDir,
                                                    city + '_PctStGS')
        gsRF.write(
            "Copy the feature class to the final geodatabase for use in EnviroAtlas.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')
        """ Convert to Raster """
        arcpy.PolylineToRaster_conversion(
            'PctStGS_Bnd', 'Pct_GS', splitDir + '/' + city + '_PctStGS.tif',
            '', '', 10)
        gsRF.write(
            "Convert the road file to raster with the Pct_GS as the value field and a cell size of 10m.--"
            + time.strftime('%Y%m%d--%H%M%S') + '--\n')

        print 'Green Space and Tree Cover Along Walkable Roads End Time: ' + time.asctime(
        ) + '\n'

        #-------- Block group summarized metrics (Ferdouz Cochran) ---------------
        """ Intersect street data with block groups """
        arcpy.Intersect_analysis([city + '_PctStTC', freqDir + '/' + 'BG_Alb'],
                                 city + '_BG_PctStTC', "ALL", "", "LINE")
        arcpy.Intersect_analysis([city + '_PctStGS', freqDir + '/' + 'BG_Alb'],
                                 city + '_BG_PctStGS', "ALL", "", "LINE")
        """ Calculate percent of total street length GS/TC """
        arcpy.AddField_management(city + '_BG_PctStTC', 'TC_length', "DOUBLE")
        arcpy.CalculateField_management(city + "_BG_PctStTC", 'TC_length',
                                        '(!Pct_TC! / 100.0) * !Shape_Length!',
                                        "PYTHON_9.3")
        arcpy.AddField_management(city + '_BG_PctStGS', 'GS_length', "DOUBLE")
        arcpy.CalculateField_management(city + "_BG_PctStGS", 'GS_length',
                                        '(!Pct_GS! / 100.0) * !Shape_Length!',
                                        "PYTHON_9.3")
        """ Summarize at block group level """
        arcpy.Statistics_analysis(
            city + '_BG_PctStTC', city + '_BG_PctStTC_sumstat',
            [["Shape_Length", "SUM"], ["TC_length", "SUM"]], "bgrp")
        arcpy.Statistics_analysis(
            city + '_BG_PctStGS', city + '_BG_PctStGS_sumstat',
            [["Shape_Length", "SUM"], ["GS_length", "SUM"]], "bgrp")
        """ Calculate percent of block group streets with GS/TC """
        arcpy.AddField_management(city + '_BG_PctStTC_sumstat', 'BG_PctStTC',
                                  "DOUBLE")
        arcpy.CalculateField_management(
            city + "_BG_PctStTC_sumstat", 'BG_PctStTC',
            '(!SUM_TC_length! / !SUM_Shape_Length!) * 100.0', "PYTHON_9.3")
        arcpy.AddField_management(city + '_BG_PctStGS_sumstat', 'BG_PctStGS',
                                  "DOUBLE")
        arcpy.CalculateField_management(
            city + "_BG_PctStGS_sumstat", 'BG_PctStGS',
            '(!SUM_GS_length! / !SUM_Shape_Length!) * 100.0', "PYTHON_9.3")
        """ Copy and join to BG features """
        arcpy.CopyFeatures_management(freqDir + '/' + 'BG_Alb',
                                      city + 'BG_GSTCnWR_summary')
        flds = [f.name for f in arcpy.ListFields(city + 'BG_GSTCnWR_summary')]
        for field in flds:
            if field not in ['bgrp']:
                try:
                    arcpy.DeleteField_management(city + 'BG_GSTCnWR_summary',
                                                 field)
                except:
                    pass
            else:
                pass
        arcpy.JoinField_management(city + 'BG_GSTCnWR_summary', 'bgrp',
                                   city + '_BG_PctStTC_sumstat', 'bgrp',
                                   'BG_PctStTC')
        arcpy.JoinField_management(city + 'BG_GSTCnWR_summary', 'bgrp',
                                   city + '_BG_PctStGS_sumstat', 'bgrp',
                                   'BG_PctStGS')

        #-------- COMPELETE LOGFILES ---------------------------------------------
        gsRF.close()
        tcRF.close()
        ReuseRF.close()

#-----------------------------------------------------------------------------
# END ANALYSIS
#-----------------------------------------------------------------------------
    except:
        """ This part of the script executes if anything went wrong in the main script above """
        #-------- PRINT ERRORS ---------------------------------------------------
        print "\nSomething went wrong.\n\n"
        print "Python Traceback Message below:"
        print traceback.format_exc()
        print "\nArcMap Error Messages below:"
        print arcpy.GetMessages(2)
        print "\nArcMap Warning Messages below:"
        print arcpy.GetMessages(1)

        #-------- COMPLETE LOGFILE ------------------------------------------------
        gsRF.write("\nSomething went wrong.\n\n")
        gsRF.write("Pyton Traceback Message below:")
        gsRF.write(traceback.format_exc())
        gsRF.write("\nArcMap Error Messages below:")
        gsRF.write(arcpy.GetMessages(2))
        gsRF.write("\nArcMap Warning Messages below:")
        gsRF.write(arcpy.GetMessages(1))

        gsRF.write("\n\nEnded at " + time.asctime() + '\n')
        gsRF.write("\n---End of Log File---\n")

        if gsRF:
            gsRF.close()
        if tcRF:
            tcRF.close()