2

I am rather new to Python but I am trying to define a function that will take the output of one of my prior functions (5 buffered stream files) and intersect each unique streamBuffer file with an LULC shapefile. So, I want to create 5 unique intersection shapefiles.

My problem comes when trying to figure out how to input this into a variable that will fit inside the arcpy Intersection_analysis tool.

My code currently looks like this:

    def identHabitat(self):
         '''Creates a polygon shapefile by intersecting the shapefiles 
         resulting from calcBuffer, identCover, and identDev. The result 
         shows areas within a specific watershed where habitat suitable for 
         the desired species could be found.'''
         workspace = arcpy.env.workspace = (sys.argv[7])
         arcpy.env.overwriteOutput = True
         buffList = arcpy.ListFeatureClasses("*buff_{0}*".format(self.abbrv))
         unionFile = arcpy.ListFeatureClasses("*{0}_LULC*".format(self.abbrv))
         inFeatures = [unionFile]
         for fc in buffList:
              outFeatures = "{0}_Habitat".format(self.abbrv)
              inFeatures.extend(fc)
              try:
                  intLayers = arcpy.Intersect_analysis(inFeatures, outFeatures)
                  message = "Habitat successfully identified!"
              except:
                  message = "Habitat could not be identified, please try again."
         return message

I put a breakpoint on the line defining the workspace and watched the "inFeatures" variable. This was its value:

enter image description here

Obviously, this is not what I'm trying to do. I want my variable to have a value of: [[u'nopa_LULC.shp'], fc]

And again, I'm new to Python so I'm sure some of this is not the best coding you've ever seen.

2

inFeatures.extend (fc) adds a string to the list outFeatures. fc is a string, and .extend iterates the string and adds each character to the list unionFile. unionFile itself is a list of a list, since unionFile = arcpy.ListFeatureClasses("*{0}_LULC*".format(self.abbrv)) creates a list, and then inFeatures = [unionFile] creates a list of that list.

Here's the pertinent code:

workspace = arcpy.env.workspace = (sys.argv[7])
arcpy.env.overwriteOutput = True
buffList = arcpy.ListFeatureClasses("*buff_{0}*".format(self.abbrv))
unionFile = arcpy.ListFeatureClasses("*{0}_LULC*".format(self.abbrv)) [0] #get first item in list??
# inFeatures = [unionFile] #not needed
for fc in buffList:
  outFeatures = "{0}_Habitat".format(self.abbrv)
  #inFeatures.extend(fc) #also not needed
  try:
      intLayers = arcpy.Intersect_analysis([fc, unionFile], outFeatures) #first input of intersect is a list of all feature classes to intersect
      message = "Habitat successfully identified!"
  except:
      message = "Habitat could not be identified, please try again."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.