1

I will try explain my code in a simpler way, because it is Portuguese.

I've been trying do make a Python Toolbox in order to select "sectors" of lines separated for some kind of points. For example, I have 10 lines, then a point, so I give the name "1" for this 10 lines, then I select other 15 lines and give the name "2", and after "3" for 13 lines, etc. Which line has a specific ObjectID, so I made a list of ObjectIDs to select the near lines and calculate field to change de name of the sector.

I'm trying with this code:

in_featuresL = parameters[0].valueAsText
in_featuresP = parameters[1].valueAsText

sector2 = 1

rowsL = arcpy.UpdateCursor(in_featuresL)

for row in rowsL:
    objectid = row.getValue("OBJECTID")
    sector = row.getValue("SECTOR")
    arcpy.AddMessage("Objectid " + str(objectid) + "  -  Sector " + str(sector))
    if setor > 0:
        arcpy.AddMessage("ALREADY HAS A SECTOR!")
    if setor == 0:
        arcpy.AddMessage(" ### Making de sector... ###")
        condition = """OBJECTID = {}""".format(objectid)
        arcpy.SelectLayerByAttribute_management(in_featuresL, "NEW_SELECTION", condition)
        x = 0
        while (x < 1):
            C1 = int(arcpy.GetCount_management(in_featuresL).getOutput(0))
            arcpy.SelectLayerByLocation_management(in_featuresP, "INTERSECT", in_featuresL)                 
            arcpy.SelectLayerByAttribute_management(in_featuresP, "REMOVE_FROM_SELECTION", "ELEMENTO = 'RG' OR ELEMENTO = 'rg' OR ELEMENTO = 'VB' OR ELEMENTO = 'EBAT' OR ELEMENTO = 'ETA' OR ELEMENTO = 'RES'")
            arcpy.SelectLayerByLocation_management(in_featuresL, "INTERSECT", in_featuresP)
            C2 = int(arcpy.GetCount_management(in_featuresL).getOutput(0))
            C3 = C2 - C1
            if C3 == 0:
                arcpy.AddMessage("SELECTED FOR CALCULATE FIELD: " + str(C2))
                arcpy.CalculateField_management(in_featuresL, "SECTOR", sector2)
                arcpy.AddMessage("Made the sector: " + str(sector2))
                sector2 = sector2 + 1
                x = 1               

    return

These are the Lines and Points data #

in_featuresL = parameters[0].valueAsText
in_featuresP = parameters[1].valueAsText

Here I start a counting parameter to put the name in the sectors #

sector2 = 1

Here I created the cursor rowsL for the lines #

rowsL = arcpy.UpdateCursor(in_featuresL)

Here I made the 'for' to take 1 by 1 #

for row in rowsL:
    objectid = row.getValue("OBJECTID")
    sector = row.getValue("SECTOR")
    if setor > 0:
        arcpy.AddMessage("ALREADY HAS A SECTOR!")

Actually here is my problem, when I change some values after in the code, when it returns to this part, I have changed some "SECTOR" values of some lines and this row.getValue("SECTOR") gets the same value of the original one!

If sector are 0 (in the beginning, all of them is 0), he makes the code and start to select the near lines.

    if sector == 0:
        arcpy.AddMessage(" ### Making de sector... ###")
        condition = """OBJECTID = {}""".format(objectid)
        arcpy.SelectLayerByAttribute_management(in_featuresL, "NEW_SELECTION", condition)
        x = 0
        while (x < 1):
            C1 = int(arcpy.GetCount_management(in_featuresL).getOutput(0))
            arcpy.SelectLayerByLocation_management(in_featuresP, "INTERSECT", in_featuresL)                 
            arcpy.SelectLayerByAttribute_management(in_featuresP, "REMOVE_FROM_SELECTION", "ELEMENTO = 'RG' OR ELEMENTO = 'rg' OR ELEMENTO = 'VB' OR ELEMENTO = 'EBAT' OR ELEMENTO = 'ETA' OR ELEMENTO = 'RES'")
            arcpy.SelectLayerByLocation_management(in_featuresL, "INTERSECT", in_featuresP)
            C2 = int(arcpy.GetCount_management(in_featuresL).getOutput(0))
            C3 = C2 - C1
            if C3 == 0:

Here, the near lines are selected, and I can make a CalculateField in order to Update the values #

                arcpy.AddMessage("SELECTED FOR CALCULATE FIELD: " + str(C2))
                arcpy.CalculateField_management(in_featuresL, "SECTOR", sector2)
                arcpy.AddMessage("Made the sector: " + str(sector2))
                sector2 = sector2 + 1
                x = 1       

Here, the the values of the selected lines are changed (I'm sure, because if I end the code in this part and I check the attribute table, it is already done)

So my problem is, it updates the values of the selected sector lines correctly, but when it comes back to the for, in this part:

for row in rowsL:
    objectid = row.getValue("OBJECTID")
    sector = row.getValue("SECTOR")
    if setor > 0:
        arcpy.AddMessage("ALREADY HAS A SECTOR!")

the row.getValue("SECTOR") get the value from the original one, and not from the values that I changed with the Calculate Field.

Maybe it is a little confuse, but I tried to explain the best way I can.

  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. – Midavalo Apr 13 '17 at 15:18
  • 1
    You should look into arcpy.da.UpdateCursor() - the data access cursors are much quicker and you can just specify a list of field names then use the list index number to refer to the field when updating. – Midavalo Apr 13 '17 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.