AnsweredAssumed Answered

How to Python construct methods with ArcPy?

Question asked by geoffreywestgis on Oct 21, 2016
Latest reply on Oct 21, 2016 by geoffreywestgis

I have a Python application where the same ArcPy functions are called 20+ times.  I have begun constructing a method for this application to make the code more readable and Pythonic.  However I have gotten stuck and need a bit of assistance in moving forward with what I have.  The objective of this application is to create a feature layer, summary stats table, and join to another feature class.  I am working with a polygon and line feature layer and a polygon feature class that I am executing the join on.    The unique parameters for each ArcPy function are where clauses, output summary statistics tables, statistics fields, polygon layer to join to, and fields to keep from the join.  My example is below, what needs to be added/modified to this so that I can successfully add the my other parameters to my SummaryStats function to execute the method for all other parameters, i.e. whereclauses, feature classes etc.

 

 

 

 

import arcpy
import DistrictConfig
import logging
import logging.handlers
import ConfigParser

LOGGER_NAME = 'korterra_district'
arcpy.env.workspace = "C:\KorTerraTables\tables_db.gdb"
arcpy.env.overwriteOutput = True
from datetime import datetime
startTime = datetime.now()
logger = logging.getLogger(LOGGER_NAME)

distfc = DistrictConfig.distfc
linefc = DistrictConfig.linefc
polyfc = DistrictConfig.polyfc
totalexpression = DistrictConfig.TotalExpression
totalscline = DistrictConfig.TotalSCLine
totalscpoly = DistrictConfig.TotalSCPoly
statsfield = DistrictConfig.StatsField
fields = DistrictConfig.fields
totalCalc = DistrictConfig.TotalCalc



class GeodatabaseConnection():
    """ Defines a connection to a geodatabase """
    def __init__(DistrictConfig):

     def DistrictFunction(DistrictConfig, distfc, linefc, totalexpression, totalscline, statsfield, fields, totalCalc):
        print "Calculating Total Line Tickets per District"
        arcpy.MakeFeatureLayer_management(linefc, "line_layer_test_total", totalexpression)
        arcpy.Statistics_analysis("line_layer_test_total", totalscline , statsfield, "DISTRICT")
        arcpy.JoinField_management(distfc, "NAME", totalscline, "DISTRICT", fields)

        with arcpy.da.UpdateCursor(distfc, ["FREQUENCY"]) as cursor:
            for row in cursor:
                if row[0] == None:
                    row[0] = 0
                    cursor.updateRow(row)

        #Calculate and Join Totals per District Polys
        arcpy.MakeFeatureLayer_management(polyfc, "poly_layer_test_total", totalexpression)
        arcpy.Statistics_analysis("poly_layer_test_total", totalscpoly , statsfield, "DISTRICT")
        arcpy.JoinField_management(distfc, "NAME", totalscpoly, "DISTRICT", fields)

        with arcpy.da.UpdateCursor(distfc, ["FREQUENCY_1"]) as cursor:
            for row in cursor:
                if row[0] == None:
                    row[0] = 0
                    cursor.updateRow(row)
        arcpy.CalculateField_management(distfc, "TOTAL_LONG", TotalCalc)
        arcpy.DeleteField_management(distfc, ["FREQUENCY", "FREQUENCY_1"])

    def SummaryStats(DistrictConfig):
        try:
            DistrictFunction(DistrictConfig, distfc, linefc, totalexpression, totalscline, statsfield, fields, totalCalc)
        except Exception as e:
            logger.info(e.message, exc_info=True)
            raise e

Outcomes