Created
July 25, 2017 00:04
-
-
Save anonymous/b673fb6aec581735a42bbcb9b8cd051d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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