Skip to content

Instantly share code, notes, and snippets.

@pmacMaps
Created January 17, 2019 20:09
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 pmacMaps/3f63ef36082ca29e4ec9ff370bd30364 to your computer and use it in GitHub Desktop.
Save pmacMaps/3f63ef36082ca29e4ec9ff370bd30364 to your computer and use it in GitHub Desktop.
Get Buildings That Intersect Floodplains for Each Municipality Python Script Sample for Blog
# import arcpy and os modules
import arcpy, os
# Create text file for logging results of script
logFile = r'[path]\[to]\[folder]\Buildings_Intersect_Floodplains_Report.txt'
# variable to store messages for log file. Messages written in finally statement at end of script (not shown in sample)
logMsg = ''
# output location for shapefiles
out_folder_shp = r'[path]\[to]\[folder]\Buildings In Floodplains\Geodata'
# building footprints
bldg_footprints = r'[path]\[to]\[folder]\Geodata.gdb\Building_Footprints'
# fema floodplains
fema_floodplains = r'[path]\[to]\[folder]\Geodata.gdb\Floodplains'
# municipalities
muni = r'[path]\[to]\[folder]\Geodata.gdb\Municipality'
# municipalities fields
muni_fields = ['MUNI_NAME']
# create feature layer for municipalities
arcpy.MakeFeatureLayer_management(muni,'municipality')
# create feature layer for buildings
arcpy.MakeFeatureLayer_management(bldg_footprints, 'buildings')
# create feature layer for floodplains
arcpy.MakeFeatureLayer_management(fema_floodplains, 'floodplains')
# Create Search Cursor for Municipalities
# add message
logMsg += 'Creating search cursor on {}\n'.format(muni)
# create search cursor
with arcpy.da.SearchCursor(muni,muni_fields) as cursor:
# loop through each record in municipalities layer
for row in cursor:
# add message
logMsg += '\nSelecting buildings that intersect {}\n'.format(row[0])
# attribute selection on municipality
# SQL query
where_clause = "MUNI_NAME = '{}'".format(row[0])
arcpy.SelectLayerByAttribute_management('municipality', 'NEW_SELECTION', where_clause)
# select buildings that have centroid in municipality
arcpy.SelectLayerByLocation_management('buildings', 'HAVE_THEIR_CENTER_IN', 'municipality', selection_type='NEW_SELECTION')
# get count of buildings that intersect municipality
match_count_bldg = int(arcpy.GetCount_management('buildings')[0])
# make sure features are selected before moving forward
if match_count_bldg > 0:
# select from selected buildings that intersect floodplains
arcpy.SelectLayerByLocation_management('buildings', 'INTERSECT', 'floodplains', selection_type='SUBSET_SELECTION')
# get count of selected buildings
match_count_blg_floodplains = int(arcpy.GetCount_management('buildings')[0])
# make sure features are selected before moving forward
if match_count_blg_floodplains > 0:
# add message - number of buildings in municipality
logMsg += '\nThere are {} buildings that intersect floodplains in {}\n'.format(match_count_blg_floodplains, row[0])
# export layer
arcpy.CopyFeatures_management('buildings', os.path.join(out_folder_shp, 'Buildings_Intersect_Floodplains_{}.shp'.format(row[0])))
else:
logMsg += '\nNo buildings that are within {} intersect floodplains\n'.format(row[0])
else:
logMsg += '\nThere were no buildings that have their centroids in {}\n'.format(row[0])
# end if/else
# end for
# end with cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment