AnsweredAssumed Answered

"If" Statement and Loop - ArcPy for ArcGIS Pro

Question asked by bisoftware on Jan 18, 2018
Latest reply on Jan 19, 2018 by bisoftware

I have this code that performs a Summarize Within on all company stores for every month of the year. However, not all the stores were open in some of the early months so I need to write some code that will identify that the "USER_PatientVisits" is equal to zero and then skip that store and move on to the next. I'm thinking this can be accomplished using an "If" statement and probably a loop; however, I'm not sure what exactly to write and how to bring it all together.

 

Here is the top portion of my code for January and February; the full code continues on through December:

 

import arcpy
import sys
import os
import datetime

#now = datetime.datetime.now()

arcpy.env.overwriteOutput = True

#Set the workspace
from arcpy import env
env.workspace = r"C:\arcGIS_Shared\Python\CenterHeatMaps.gdb"

#Declare variables
fc = 'Open_Store_Centers'
fields = ['USER_market_id','USER_Store_ID','USER_Store_Center_Name']
fieldname = 'USER_market_id'

#Define WHERE clause statement
whereclause = """{} = 2000""".format(arcpy.AddFieldDelimiters(fc, fieldname))

#Cursor to create list of centers to loop through
cursor = arcpy.da.SearchCursor(in_table = fc, field_names = fields, where_clause=whereclause, sql_clause=(None, 'ORDER BY USER_market_id, USER_Store_ID'))

#Loop to run SummarizeWithin by center
for row in (cursor):
    #  Set variables for SummarizeWithin
    #  Specify type of summarization, either Blocks or ZIPS, as part of the polys argument
    polys = "Market{0}".format(row[0])+"Blocks"
    points = "Jan_NWPatientVisits{0}".format(row[0])+"{0}".format(row[2])
    outFeatureClass = "Jan_NWSumWithin{0}".format(row[0])+"{0}".format(row[2])
    keepAll = 'ONLY_INTERSECTING'
    sumFields = [['USER_VisitCount', 'Sum']]
    addShapeSum = 'ADD_SHAPE_SUM'

    arcpy.SummarizeWithin_analysis(polys, points, outFeatureClass, keepAll, sumFields, addShapeSum, '')
   
    #Print Results
    print(row[2])
#    print("SumWithin Elapsed Time:" + SumWithinruntime)
#    count = arcpy.GetMessageCount()
#    print (arcpy.GetMessage(count-2))
    print (arcpy.GetMessages())
   
   
    #Declare variables
fc = 'Open_Store_Centers'
fields = ['USER_market_id','USER_Store_ID','USER_Store_Center_Name']
fieldname = 'USER_market_id'

#Define WHERE clause statement
whereclause = """{} = 2000""".format(arcpy.AddFieldDelimiters(fc, fieldname))

#Cursor to create list of centers to loop through
cursor = arcpy.da.SearchCursor(in_table = fc, field_names = fields, where_clause=whereclause, sql_clause=(None, 'ORDER BY USER_market_id, USER_Store_ID'))

#Loop to run SummarizeWithin by center
for row in (cursor):
    #  Set variables for SummarizeWithin
    #  Specify type of summarization, either Blocks or ZIPS, as part of the polys argument
    polys = "Market{0}".format(row[0])+"Blocks"
    points = "Feb_NWPatientVisits{0}".format(row[0])+"{0}".format(row[2])
    outFeatureClass = "Feb_NWSumWithin{0}".format(row[0])+"{0}".format(row[2])
    keepAll = 'ONLY_INTERSECTING'
    sumFields = [['USER_VisitCount', 'Sum']]
    addShapeSum = 'ADD_SHAPE_SUM'

    arcpy.SummarizeWithin_analysis(polys, points, outFeatureClass, keepAll, sumFields, addShapeSum, '')
   
    #Print Results
    print(row[2])
#    print("SumWithin Elapsed Time:" + SumWithinruntime)
#    count = arcpy.GetMessageCount()
#    print (arcpy.GetMessage(count-2))
    print (arcpy.GetMessages())

Outcomes