1

I've written the following arcpy script to loop through a directory and intersect all the shapefiles in it with a certain other shapefile:

import arcpy, os
from arcpy import env
from arcpy.sa import *

#To overwrite output
arcpy.env.overwriteOutput = True

#Set environment settings
env.workspace = "F:/Tiled_Shapes/"

outws="F:/Clipped_Shapes/"

#checkout ArcGIS spatial analyst extension license
arcpy.CheckOutExtension("Spatial")
inshape = arcpy.ListDatasets("*","All")

clipfile = "F:/Clip_subset.shp"

for i in inshape:
  filename=os.path.splitext(i)[0]
  outPolygons= outws + str(filename) + '.shp'
  inFeatures = [i,clipfile]
  arcpy.Intersect_analysis(in_features=inFeatures,join_attributes = ALL, out_feature_class=outPolygons)

Whats unnerving, is that rather than getting an error, nothing happens. What am I doing wrong here? When I use the intersect feature on the same data in ArcMap, it works fine. Now when I try to do it to a whole directory its failing.

5
  • 3
    after your inshape = arcpy.ListDatasets("*","All") line, add a print inshape to see what's in the list
    – Midavalo
    Mar 10 '17 at 0:57
  • 4
    Why aren't you using Clip if you're trying to Clip? Are your datasets in F:/Tiled_Shapes/ ? you can skip "*","All" on ListDatasets as they are the default values, inshape = arcpy.ListDatasets() will do the exact same thing. Mar 10 '17 at 1:07
  • @Midalvo: I'm using intersect because I want to retain the attributes of the prior *.shp's. Basically, this is one part of generating a classification error matrix, so I want to know the identity of both polygons prior to intersecting. Also, I added the print inshape. It didn't work at first, but after applying GISge's fix, it did. Thank you. Mar 10 '17 at 14:35
  • You should edit your question to clarify which geoprocessing operation you're using: Clip or Intersect. They are not the same thing but you're using the terms interchangeably in your question and in your code, I'm a little worried you aren't doing the operation you think you're doing.
    – Dan C
    Mar 10 '17 at 14:57
  • @Dan C: Its doing exactly what I expect it to be doing. Here, I'm using intersect like a clip, but retaining the spatial data from the both files. In this case I want to exclude data that isn't spatially coincident between the two. I want to know what intersects what within the context of either shapefile. Mar 10 '17 at 15:00
1

In my experience, with arcpy list functions, it's always best to choose the most restrictive one (e.g. if you want to list rasters, use arcpy.ListRasters(), not arcpy.ListFiles().

Even though a shapefile is a dataset, they're not included in the possible formats for arcpy.ListDatasets(). So here, you should use arcpy.ListFeatureClasses() (technically a shapefile is a feature class, even if this term is mostly used for geodatabase feature classes).

And as @Midalvo mentioned, it's a good idea to use print statements to see what's returned by a list (or any function), when you don't get the expected results

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.