Cumberland County is in the process of updating our Hazard Mitigation Plan. Our GIS Department is focused on developing a web map series application, and performing various spatial analyses to support the plan.
One of the first analyses we were asked to complete was to get a count of the number of structures in the floodplain, by municipality. As this would require running the same steps over and over again, I thought that writing a Python script that uses a for-in loop would save some time. And through this process, I was able to develop a more generic, re-usable geoprocessing tool. You can download the tool on GitHub.
This tool is great whenever you need to get all of the features from Dataset A (i.e., building structures) that intersect Dataset B (i.e., floodplains), for each feature in Dataset C (i.e., municipalities). Performing this type of analysis manually could be time-consuming. But creating a tool to automate these repeated steps saves you time now, and in the future when you complete a slightly different project.
In this article, I’ll review the Python script for this specific building structures in floodplain analysis. I’ll also share the geoprocessing tool I developed out of this process. Hopefully you can use the tool in one of your future projects. Let’s begin!
Looping Through The Dataset Using a Search Cursor
Because we want a separate list of buildings that intersect floodplains for each municipality, we’ll utilize a for-in loop using a Search Cursor (arcpy.da.SearchCursor) on the municipalities dataset. Below is a simplified diagram of the analyses that occur within each iteration of the loop. I’ll also outline these steps using a list:
- Create an attribute selection on a feature layer version of the municipality dataset using the current record in the loop. (feature layers are required for attribute or spatial selections in ArcPy)
- Select features from the building structures dataset that have their centroid in the selected municipality feature
- Execute code based upon whether any building structures are selected or not:
- Features are Selected:
- If features are selected, select features from the building structures (subset selection) that intersect the floodplains dataset.
- Export selected records to a new dataset in the project folder or geodatabase using the current municipality in the file name.
- If no building structures intersect the floodplains dataset, add a message stating this.
- No Features are Selected:
- Add a message that no building structures have their centroids in the municipality.
- Features are Selected:
Below is the sample code for the Search Cursor portion of the script:
Building the Geoprocessing Tool
Though I wrote this Python script for a specific purpose, I soon realized I could modify it to create a re-usable tool. This tool can be used whenever you need to get all of the features from Dataset A that intersect (or another spatial selection type) Dataset B, for each feature in Dataset C.
You can download the tool on GitHub. There could definitely be some improvements to the messages created for users. And maybe someday, I’ll make those (or maybe you can and submit a pull request!). I have some screenshots of the tool below. I’ve also listed a sample use case for the tool. I’m hoping you can use/modify/improve the tool for use with your organization.
The user form for the custom geoprocessing tool
Sample Use Case
Select all Class A Wild Trout Streams that are within [x] distance of an Oil/Gas well, by County. When performing a spatial selection that involves a distance, it is important to remember that the “tool evaluates a spatial relationship in the coordinate system of the Input Feature Layer data source (the feature class on disk)” (see Esri help). It is possible to set the output coordinate system to evaluate the spatial relationship in. That is another potential upgrade to this tool.
Here are the links to the datasets for this use case:
Messages from the custom geoprocessing tool