+ Reply to Thread
Results 1 to 5 of 5

Thread: trouble with clip in search cursor

  1. #1
    Jamie Kass
    Join Date
    Aug 2010
    Posts
    2
    Points
    0
    Answers Provided
    0


    0

    Default trouble with clip in search cursor

    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"

  2. #2
    Chris Fox

    Join Date
    Oct 2009
    Posts
    523
    Points
    301
    Answers Provided
    36


    0

    Default Re: trouble with clip in search cursor

    Hi Jamie,

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

    Code:
    arcpy.Clip_analysis(streams, rows, outshp)
    Instead try passing in just the geometry from the row. For example:

    Code:
    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.
    Chris Fox
    Esri

  3. #3
    Jamie Kass
    Join Date
    Apr 2010
    Posts
    96
    Points
    6
    Answers Provided
    1


    0

    Default Re: trouble with clip in search cursor

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

    *kicking myself*

  4. #4
    Donovan Cameron

    Join Date
    Aug 2010
    Posts
    477
    Points
    40
    Answers Provided
    3


    0

    Default Re: trouble with clip in search cursor

    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 Clip_wCursor.py and shapefiles with directories are in .7z gis.7z file):
    Code:
    # ---------------------------------------------------------------------------
    # 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:
    Code:
    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?
    Last edited by SaultDon; 03-30-2011 at 12:59 PM.


  5. #5
    Donovan Cameron

    Join Date
    Aug 2010
    Posts
    477
    Points
    40
    Answers Provided
    3


    0

    Default Re: trouble with clip in search cursor

    Quote Originally Posted by kass3 View Post
    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).

    Clip_wCursor_v2a.py


+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts