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.

Dan C
12.3k12 gold badges63 silver badges88 bronze badges
asked Mar 10, 2017 at 0:46
5
  • 3
    after your inshape = arcpy.ListDatasets("*","All") line, add a print inshape to see what's in the list Commented Mar 10, 2017 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. Commented Mar 10, 2017 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. Commented Mar 10, 2017 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. Commented Mar 10, 2017 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. Commented Mar 10, 2017 at 15:00

1 Answer 1

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

answered Mar 10, 2017 at 14:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.