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())
a blank? ooooo, here we go with null, none and nothingness again.
if points is not None:
#then do stuff
if points <> "":
#then do stuff
Nope.... I'm not gonna take the bait......
In a project that I've been tasked with, I have moved from several if, elif, else blocks to try:/except: blocks . Not sure if that approach is applicable here, but may be worth a try. Here I examine a size of on a numpy array, and if it has size I work with with it; if it isn't even created (which indicates it didn't need to be) I don't sweat it...
try:
name6
if name6.size <> 0:
hitList6 = _flatten(name6[0])
del name6
else:
pass
except NameError:
pass
Dan Patterson, notice the call to _flatten() ?
Why! that looks like a brilliant function! cool
This looks promising. Do you know where I would insert that function into Xander Bakker's looping example above?
did neither of my suggestions work? ie checking for None, or an empty string?
If they didn't, could you report the error so that we can figure out what is in the records that is causing the problem
I was not sure what to set "points" is not equal (<>) to (""), and what to put after it.
Here is what I have so far
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 = "May_CTPatientVisits{0}".format(row[0])+"{0}".format(row[2])
if points is not None:
outFeatureClass = "May2_CTSumWithin{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())
yes... posting code as it changes helps.
# ---- Current layout
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 = "May_CTPatientVisits{0}".format(row[0])+"{0}".format(row[2])
if points is not None:
# ---- proposed to check so see if row[0] has a value or is <null> or "" and empty string
for row in (cursor):
if points is not None:
# 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 = "May_CTPatientVisits{0}".format(row[0])+"{0}".format(row[2])
# ---- in otherwords, you don't want to do anything if the row[0] doesn't have a count
I tried this, but it says "points is undefined", so then I tried copying points = "May2_CTSumWithin{0}" and pasting it above "if points is not None:" which allows it to run, but still doesn't exclude the Stores with no data
for row in (cursor):
# Set variables for SummarizeWithin
# Specify type of summarization, either Blocks or ZIPS, as part of the polys argument
if points is not None:
polys = "Market{0}".format(row[0])+"Blocks"
points = "May_CTPatientVisits{0}".format(row[0])+"{0}".format(row[2])
outFeatureClass = "May2_CTSumWithin{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())
You have to assign something to it (create the variable) before you can test it and as I stated before it will never be None, so there is no need to check for it. What do you really want to do?