Breeding Ranges of Birds in ArcPy

There’s a great data set of worldwide bird ranges that you have to request. They have breeding, wintering, and resident ranges of every species for which they have data in shape files. Once you get it, though, you can work with it using GIS.

As usual, I like to script things. I downloaded all the bird ranges, so I wanted to get the ones in the U.S. Here’s how I automated it with a Python script. First, I did the “basic” setup, which includes loading your libraries of interest, setting your work space, etc.

import os  
import arcpy   
from arcpy import env   
arcpy.env.workspace = "your folder"
arcpy.MakeFeatureLayer_management(r"us_states.shp", "USA")   
 for shp in arcpy.ListFeatureClasses():   
      fileName,fileExtension = os.path.splitext(shp)  
      fileNameParts = fileName.split('_')  
      arcpy.MakeFeatureLayer_management(shp, "species")

“Select by location” in ArcPy doesn’t do what you might think on its own. You then need to assess if the shape file is empty (i.e. there is no overlap) before saving it.

arcpy.SelectLayerByLocation_management("species", "INTERSECT", "USA")
      if arcpy.management.GetCount("spp")[0] != "0":

You can do whatever you want from there, but at the end of the loop, remember to delete the feature class you made so the loop can start over.

arcpy.Delete_management("species")

It’s best to run this as a standalone script. The Python window in ArcGIS is more likely to crash with big jobs, because it’s still running “with” whatever Arc product you used to get to it. So, I wouldn’t recommend running big scripts in Map, at least.

Leave a Reply

Your email address will not be published. Required fields are marked *