+ Reply to Thread
Results 1 to 3 of 3

Thread: For Loop ?

  1. #1
    Oya Han?m
    Join Date
    Feb 2012
    Posts
    4
    Points
    0
    Answers Provided
    0


    0

    Default For Loop ?

    At the first for loop, it only evaluates ID_1=273. What is the missing point? I want to do all the statement for all features with ID_1s at the attribute table. Please give some feedbacks. Thank you.

    import arcpy
    from arcpy import env
    env.workspace = "D:\pomme de terre 2\pomme de terre2.gdb"
    env.overwriteOutput = 1

    fc1 = "Flowline"
    fc2 = "New_Poyline"

    for a in range(1,274):

    obj = "ID_1=%s" % (a)

    arcpy.SelectLayerByAttribute_management (fc1,"NEW_SELECTION",obj)
    arcpy.SelectLayerByLocation_management(fc1,"INTERSECT",fc1)
    arcpy.CopyFeatures_management(fc1,"hidden")
    arcpy.SelectLayerByAttribute_management (fc1,"CLEAR_SELECTION")

    coordList = []

    arcpy.FeatureVerticesToPoints_management("hidden", "vertices", "MID")
    arcpy.AddXY_management("vertices")
    rows = arcpy.SearchCursor("vertices")

    for row in rows:
    X = row.getValue("POINT_X")
    Y = row.getValue("POINT_Y")
    coordList.append([X, Y])

    del row, rows

    coordList.sort()

    point = arcpy.Point()
    array = arcpy.Array()

    for feature in coordList:
    point.X = feature[0]
    point.Y = feature[1]
    array.add(point)

    polyline = arcpy.Polyline(array)

    array.removeAll()
    arcpy.Delete_management("vertices")
    arcpy.Delete_management("hidden")
    arcpy.CopyFeatures_management(polyline, fc2)

  2. #2
    Dan Patterson

    Join Date
    Apr 2010
    Posts
    1,756
    Points
    443
    Answers Provided
    41


    0

    Default Re: For Loop ?

    enclose your code in the Code block symbol in the html editor (ie #)
    This line will fail
    env.workspace = "D:\pomme de terre 2\pomme de terre2.gdb"
    since you have to use raw notation
    env.workspace = r"D:\pomme de terre 2\pomme de terre2.gdb"
    or double backslashes or single foreslashes in python
    ie env.workspace = "D:\\pomme de terre 2\\pomme de terre2.gdb"
    may the spud be with you
    Geomatics, Carleton University, Ottawa, Canada
    http://obidangis.blogspot.ca/

  3. #3
    Greg Keith
    Join Date
    Jun 2010
    Posts
    964
    Points
    249
    Answers Provided
    27


    0

    Default Re: For Loop ?

    One advantage of formatting using code tags like Dan suggests is that others here can see if indenting is the problem. The way it's posted now, it looks like none of the code under your for statements is indented. That will cause a logic error, in that Python thinks you have an empty for loop and just moves sequentially through your code without looping.

    On the other hand, maybe your actual code is indented, but it doesn't display in the forum that way if you haven't used code tags. In that case you wouldn't have the no indent problem, but no one here can tell that.

+ Reply to Thread

Tags for this 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