AnsweredAssumed Answered

If then/else python to meet criteria or end script

Question asked by buehlerd on Jan 20, 2017
Latest reply on Jan 23, 2017 by rvburton

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)

Outcomes