Skip to content

Instantly share code, notes, and snippets.

Created July 25, 2017 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b673fb6aec581735a42bbcb9b8cd051d to your computer and use it in GitHub Desktop.
Save anonymous/b673fb6aec581735a42bbcb9b8cd051d to your computer and use it in GitHub Desktop.
def joinFeature(fcPath, joinTable, pathFGDB, year, stateName, eventType):
# Set targets for join
message('Preparing for join procedure...')
inFeature = fcPath
layerNameJoin = 'stormJoin_' + year
layerName = 'storm_' + year
joinField = 'EVENT_ID'
outNameJoin = stateName + '_' + eventType.replace(' ', '_') + '_' + year + '_join'
outFeatureJoin = os.path.join(pathFGDB, outNameJoin)
outName = stateName + '_' + eventType.replace(' ', '_') + '_' + year
outFeature = os.path.join(pathFGDB, outName)
try:
# Make feature layer for first attribute selection
arcpy.MakeFeatureLayer_management(inFeature, layerName)
# Join the feature layer to the CSV
arcpy.AddJoin_management(layerName, joinField, joinTable, joinField)
message('...join procedure complete.')
# Select by attributes
# State and Storm query
eventField = 'EVENT_TYPE'
stateField = 'STATE'
stateQuery = '"' + stateField + '" = ' + "'" + stateName + "'"
stormQuery = '"' + eventField + '" = ' + "'" + eventType + "'"
attributeQuery = stateQuery + ' AND ' + stormQuery
message('Selecting from user input varaibles: ' + stateName + ', ' + eventType)
arcpy.SelectLayerByAttribute_management(layerName, 'NEW_SELECTION', attributeQuery)
attributeCount = int(str(arcpy.GetCount_management(layerName)))
if not attributeCount > 0:
arcpy.Delete_management(layerNameJoin)
arcpy.Delete_management(layerName)
arcpy.Delete_management(joinTable)
message('No features selected.')
continue
message('Features selected.')
# Copy the layer to a new permanent feature class
message('Creating new feature class.')
arcpy.CopyFeatures_management(layerName, outFeature)
# Clean up
message('Cleaning up extraneous files.')
arcpy.Delete_management(layerNameJoin)
arcpy.Delete_management(layerName)
arcpy.Delete_management(outFeatureJoin)
arcpy.Delete_management(inFeatures)
arcpy.Delete_management(joinTable)
except(arcpy.ExecuteError):
message('The NOAA location csv was not generated or no cases were selected. No features were joined.')
arcpy.Delete_management(joinTable)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment