4 Replies Latest reply on Apr 5, 2011 9:06 AM by geonetadmin

    trouble with clip in search cursor

    geonetadmin
      Original User: jkass

      Having trouble with this script. Trying to clip the streams layer by each row of a watershed polygon. Getting the error "in Clip raise e". Any suggestions?

      import arcpy
      from arcpy import env
      import os

      arcpy.env.workspace = "path"
      arcpy.env.overwriteoutput = 1
      streams = "streams.shp"
      rows = arcpy.SearchCursor("ws_boundary.shp")
      for row in rows:
              wsname = str(row.DWRHANAME)
              outshp = "streams\\str" + "_" + wsname.replace(" ", "_") + ".shp"
              arcpy.Clip_analysis(streams, rows, outshp)
              print str(row.DWRHANAME) + "_has finished"
        • Re: trouble with clip in search cursor
          cfox-esristaff
          Hi Jamie,

          The problem is you are attempting to pass a cursor object in as the clip features paramater:

          arcpy.Clip_analysis(streams, rows, outshp)
          


          Instead try passing in just the geometry from the row. For example:

          feat = row.Shape #Where Shape is equal to the shape field in the feature class
          arcpy.Clip_analysis(streams, feat, outshp)
          


          Also looking at it you may have a problem with the outshp variable as you are setting it equal to streams.shp\\str_XX_XX.shp which is not a valid output feature class path.
          • Re: trouble with clip in search cursor
            geonetadmin
            Original User: kass3

            Thank you! Would would have thought that the "shape" is used to "clip"?

            *kicking myself*
            • Re: trouble with clip in search cursor
              SaultDon
              I am trying to do the same thing, and I am new to Python so am looking for some input on this script I am trying to run.

              Using ArcGIS 9.3.1, I have a shapefile of index grids in shapefile format with a text field called [NAME] that contains a UID for each grid (ie, Grid-1, Grid-2, etc...)

              I would like to clip the lines to these grids with a subset of lines for each grid.

              Below is my script thus far (also attached as .py [ATTACH]5647[/ATTACH] and shapefiles with directories are in .7z [ATTACH]5646[/ATTACH] file):
              # ---------------------------------------------------------------------------
              # Clip_wCursor.py
              # Created for ArcGIS 9.3.1
              #   (clip generated by ArcGIS/ModelBuilder)
              # Attempted cursor mods by a newb, Donovan
              # ---------------------------------------------------------------------------

              # Import system modules
              import sys, string, os, arcgisscripting

              # Create the Geoprocessor object
              gp = arcgisscripting.create()

              # Load required toolboxes...
              gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")

              # assign variables...
              grids = "C:\\gis\\data\\grids.shp"
              lines = "C:\\gis\\data\\lines.shp"
              rows = gp.SearchCursor(grids)
              row = rows.Next()
              feat = row.Shape
              for row in rows:
                  gname = str(row.NAME)
              Output = "grd" + "_" + gname + ".shp"

              # Process: Clip...
              gp.Clip_analysis(lines, feat, Output, "")


              I encounter the error:
              Traceback (most recent call last):
                File "C:\gis\script\Clip_wCursor.py", line 23, in <module>
                  for row in rows:
              TypeError: 'geoprocessing cursor object' object is not iterable


              Is it because I have incorrectly defined my row variable?
              • Re: trouble with clip in search cursor
                geonetadmin
                Original User: SaultDon

                Thank you! Would would have thought that the "shape" is used to "clip"?

                *kicking myself*


                Got it working, here is my final script if anyone needs to see 'exactly' how it works (like I did lol).

                [ATTACH]5753[/ATTACH]