If then/else python to meet criteria or end script

2496
3
01-20-2017 09:53 AM
DavidBuehler
Occasional Contributor III

I am going to be real honest I am not sure how to format the script in order to get it to check if there are "new" features and then process it, or just move on if there are no new ones.  Now that i have a working reverse geocoding script, I would like to take it to that that level. I am trying to minimize the locking time so that is why it exports out a subset first then joins it back and field calculates.  If there is a better way to do this I am all ears.  Otherwise, below is the code I have, and it will be run as a scheduled task.  Any help would be much appreciated.

Line 27 is what i am asking

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Dbuehler
#
# Created:     19/10/2016
# Copyright:   (c) Dbuehler 2016
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------
# The First part of this script pulls out concerns with missing addresses and places it in a temporary feature class for geocoding. It then reverse geocodes against that outputted selection, and cleans up the selection feature class.
#----------------------------------------------------------------------------------------------------

# import modules
import arcpy


# Set workspaces
scratchWorkspace = r'C:\GISScripts\Scratch\Scratch.gdb'

selectExport = r'C:\GISScripts\GRSTasksConnection.sde\GIS2.GIS.GeoConcernsV2'
missingAddresses = "Match_Address"
ci = "ConcernID"
sql = """{} is NULL AND {} is not NULL""".format(missingAddresses, ci)

#If there are any records that meet the sql variable from selectexport then proceed, else just print something and/or end


#print "Export Only missing Addresses"
#raw_input("Press enter to continue ;)")

# Select concerns that have missing addresses and send them to a scratch workspace for geocoding
arcpy.FeatureClassToFeatureClass_conversion(selectExport, scratchWorkspace, "NoAddresses", sql)

#print "Export Successful"
#print "Are You Ready to Start Reverse Geocoding?"
#raw_input("Press enter to continue ;)")

# Reverse Geocode the missing addresses that were selected from the main concern features

arcpy.ReverseGeocode_geocoding(in_features="C:/GISScripts/Scratch/Scratch.gdb/NoAddresses", in_address_locator="C:/GISScripts/Scratch/Scratch.gdb/NearestAddress", out_feature_class="C:/GISScripts/Scratch/Scratch.gdb/R1T", address_type="ADDRESS", search_distance="100 Meters")

#arcpy.ReverseGeocode_geocoding(in_features="C:/GISScripts/Scratch/Scratch.gdb/NoAddresses", in_address_locator="C:/GISScripts/Scratch/AddressLocator.gdb/NearestAddress", out_feature_class="C:/GISScripts/Scratch/ReversedGeoCoded.gdb/R1T", address_type="ADDRESS", search_distance="100 Meters")
#Keeps Bombing Upon completion of reverse geocoding against the file geodatabase. It does not make it to the delete management function
#print "Reverse Geocode Successful"

arcpy.Delete_management(r'C:\GISScripts\Scratch\Scratch.gdb\NoAddresses')

# print "Feature class deleted."

del selectExport
# print "deleted variable for connection. Would you like to Exit?"

# raw_input("Press enter to exit ;)")

# Set workspace for Editing Session

workspace = r'C:\GISScripts\GRSTasksConnection.sde'

#print "Workspace Created"
# Start edit session
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, False)
edit.startOperation()

#print "I am going to crash"
#print "Edit Session Started"

# Join and Calculate
targetFeatures = r'C:\GISScripts\GRSTasksConnection.sde\GIS2.GIS.GeoConcernsV2'
joinFeatures = r'C:\GISScripts\Scratch\Scratch.gdb\R1T'
#print "Joining Valaribles are created"

fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(targetFeatures)
fieldmappings.addTable(joinFeatures)
#print "Fieldmapping created"

layerName1 = "joinedTJ_view"
layerName2 = "GREAddress_view"
#print "Joining variables created"

#Failes somewhere in here
#raw_input("Press enter to continue ;)")
arcpy.MakeFeatureLayer_management(joinFeatures, "joinedTJ_view")
arcpy.MakeFeatureLayer_management(targetFeatures, "GREAddress_view")
#print "Made Layers"

arcpy.AddJoin_management("GREAddress_view", "ConcernID", "joinedTJ_view", "ConcernID", "KEEP_COMMON")

#print [i.name for i in arcpy.ListFields('GREAddress_view')]

#print "Joined Up Sir"


fieldName1 = u'COMGIS2.GIS.GeoConcernsV2.Match_Address'
fieldName2 = u'R1T.REV_Street'
calcExpression ="!"+ fieldName2 +"!"


# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "GeoReporting Features"
arcpy.CalculateField_management(layerName2, fieldName1, calcExpression, "PYTHON_9.3")

#print "Field Calculation Done"

arcpy.RemoveJoin_management(layerName2,"R1T")

#print "Removed Join"

arcpy.Delete_management(r'C:\GISScripts\Scratch\Scratch.gdb\R1T')

#print "Feature class deleted"

# Close edit session
edit.stopOperation()
edit.stopEditing(True)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
3 Replies
IanMurray
Frequent Contributor

Could you make a FeatureLayer using the input layer and SQL, then run a Get Count on the table to get the number of records.  If count greater than 0 proceed, else break.

arcpy.MakeFeatureLayer_management(selectExport, 'temp_Layer', sql)

arcpy.GetCount_management('temp_Layer')

if int(result.getOutput(0)) > 0:
  pass

else:
  break
DavidBuehler
Occasional Contributor III

Good Morning Ian,

I must be missing something.  In figuring out why I have a syntax errors, what I have found is break is for a loop, and result in results.getOutput was not defined.  If I take out the break and replace it with a print statement, it actually just flies right through and does everything and does not stop.

Here is what I got so far. Lines 15-23 are based on what you responded with.  Suggestions?

# import modules
import arcpy


# Set workspaces
scratchWorkspace = r'C:\GISScripts\Scratch\Scratch.gdb'

selectExport = r'C:\GISScripts\Scratch\ReversedGeoCoded.gdb\GeoConcernsV2'
missingAddresses = "Match_Address"
ci = "ConcernID"
sql = """{} is NULL AND {} is not NULL""".format(missingAddresses, ci)

#If there are any records that meet the sql variable then proceed, else just print something and/or end

arcpy.MakeFeatureLayer_management(selectExport, 'temp_Layer', sql)

# It tells me result was not defined
result = arcpy.GetCount_management('temp_Layer')

if int(result.getOutput(0)) > 0:
    pass

# break kept giving me a syntax error which is due to it not being a loop
else:
    print "Goodbye!"

#print "Export Only missing Addresses"
#raw_input("Press enter to continue ;)")

# Select concerns that have missing addresses and send them to a scratch workspace for geocoding
arcpy.FeatureClassToFeatureClass_conversion(selectExport, scratchWorkspace, "NoAddresses", sql)

#print "Export Successful"
#print "Are You Ready to Start Reverse Geocoding?"
#raw_input("Press enter to continue ;)")

# Reverse Geocode the missing addresses that were selected from the main concern features

arcpy.ReverseGeocode_geocoding(in_features="C:/GISScripts/Scratch/Scratch.gdb/NoAddresses", in_address_locator="C:/GISScripts/Scratch/Scratch.gdb/NearestAddress", out_feature_class="C:/GISScripts/Scratch/Scratch.gdb/R1T", address_type="ADDRESS", search_distance="100 Meters")

#arcpy.ReverseGeocode_geocoding(in_features="C:/GISScripts/Scratch/Scratch.gdb/NoAddresses", in_address_locator="C:/GISScripts/Scratch/AddressLocator.gdb/NearestAddress", out_feature_class="C:/GISScripts/Scratch/ReversedGeoCoded.gdb/R1T", address_type="ADDRESS", search_distance="100 Meters")
#Keeps Bombing Upon completion of reverse geocoding against the file geodatabase. It does not make it to the delete management function
#print "Reverse Geocode Successful"

arcpy.Delete_management(r'C:\GISScripts\Scratch\Scratch.gdb\NoAddresses')

# print "Feature class deleted."

del selectExport
# print "deleted variable for connection. Would you like to Exit?"

# raw_input("Press enter to exit ;)")

# Set workspace for Editing Session

workspace = r'C:\GISScripts\Scratch\ReversedGeoCoded.gdb'

#print "Workspace Created"
# Start edit session
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, False)
edit.startOperation()

#print "I am going to crash"
#print "Edit Session Started"

# Join and Calculate
targetFeatures = r'C:\GISScripts\Scratch\ReversedGeoCoded.gdb\GeoConcernsV2'
joinFeatures = r'C:\GISScripts\Scratch\Scratch.gdb\R1T'
#print "Joining Valaribles are created"

fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(targetFeatures)
fieldmappings.addTable(joinFeatures)

print "Fieldmapping created"

layerName1 = "joinedTJ_view"
layerName2 = "GREAddress_view"

print "Joining variables created"

#Failes somewhere in here
#raw_input("Press enter to continue ;)")
arcpy.MakeFeatureLayer_management(joinFeatures, "joinedTJ_view")
arcpy.MakeFeatureLayer_management(targetFeatures, "GREAddress_view")

print "Made Layers"

arcpy.AddJoin_management("GREAddress_view", "ConcernID", "joinedTJ_view", "ConcernID", "KEEP_COMMON")

print [i.name for i in arcpy.ListFields('GREAddress_view')]

print "Joined Up Sir"


fieldName1 = u'GeoConcernsV2.Match_Address'
fieldName2 = u'R1T.REV_Street'
calcExpression ="!"+ fieldName2 +"!"


# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "GeoReporting Features"
arcpy.CalculateField_management(layerName2, fieldName1, calcExpression, "PYTHON_9.3")

print "Field Calculation Done"

arcpy.RemoveJoin_management(layerName2,"R1T")

print "Removed Join"

arcpy.Delete_management(r'C:\GISScripts\Scratch\Scratch.gdb\R1T')

print "Feature class deleted"

# Close edit session
edit.stopOperation()
edit.stopEditing(True)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
RandyBurton
MVP Alum

Perhaps something like:

include sys

if int(result.getOutput(0)) > 0:
    pass

else:
    print "Goodbye!"
    sys.exit(1)‍‍‍‍‍‍‍‍

or just:

include sys

if int(result.getOutput(0)) < 1:
    print "Goodbye!"
    sys.exit(1)