Select to view content in your preferred language

Moving values within FC works, but not for a List of FCs

6669
27
Jump to solution
01-21-2015 11:37 AM
GabrielBacca-Cortes
Deactivated User

Hi Everyone -

I have a code that moves values around within a FC [which is the result of an intersection] from one field to another so that I will have them all previously intersected values aligned in one single row. The code works well when I apply it to one FC at a time, but when I extend it to a List of FCs, it does not work.  I've tried fixing it by playing with the indentation of the UpdateCursor lines, but it didn't work. Any idea what might be happening/missing ?

On another token, it seems to me that what I am trying to do, it could be done a lot simpler, any ideas on how to optimize it ?

Thanks much !

Gabriel

import arcpy, os

from arcpy import env

# Set environment settings

env.workspace = r"C:\GIS\Track1\1_Orig\int2"

# Set local variables

copyFields = ("PorousMed","K_1","Pity_1","Leng_m")

insertFd1 = ['PM1','K1','P1','leng_m1']

insertFd2 = ['PM2','K2','P2','leng_m2']

insertFd3 = ['PM3','K3','P3','leng_m3']

insertFd4 = ['PM4','K4','P4','leng_m4']

insertFd5 = ['PM5','K5','P5','leng_m5']

valueList = []

try:

    fcList = arcpy.ListFeatureClasses("","POLYLINE")

    for fc in fcList:

        print fc

        rows = arcpy.da.SearchCursor(fc, copyFields)

        for row in rows:

            valueList.append(row[0])

            valueList.append(row[1])

            valueList.append(row[2])

            valueList.append(row[3])

            cursor = arcpy.UpdateCursor(fc, insertFd1)

            for row in cursor:

                    row.setValue("PM1",valueList[0])

                    row.setValue("K1",valueList[1])

                    row.setValue("poros1",valueList[2])

                    row.setValue("leng_m1",valueList[3])

                    cursor.updateRow(row)

            del cursor, row

            cursor = arcpy.UpdateCursor(fc, insertFd2)

            for row in cursor:

                    row.setValue("PM2",valueList[4])

                    row.setValue("K2",valueList[5])

                    row.setValue("poros2",valueList[6])

                    row.setValue("leng_m2",valueList[7])

                    cursor.updateRow(row)

            del cursor, row

            cursor = arcpy.UpdateCursor(fc, insertFd3)  

            for row in cursor:

                    row.setValue("PM3",valueList[8])

                    row.setValue("K3",valueList[9])

                    row.setValue("poros3",valueList[10])

                    row.setValue("leng_m3",valueList[11])

                    cursor.updateRow(row)

            del cursor, row

            cursor = arcpy.UpdateCursor(fc, insertFd4)      

            for row in cursor:

                    row.setValue("PM4",valueList[12])

                    row.setValue("K4",valueList[13])

                    row.setValue("poros4",valueList[14])

                    row.setValue("leng_m4",valueList[15])

                    cursor.updateRow(row)

            del cursor, row

            cursor = arcpy.UpdateCursor(fc, insertFd5)      

            for row in cursor:

                    row.setValue("PM5",valueList[16])

                    row.setValue("K5",valueList[17])

                    row.setValue("poros5",valueList[18])

                    row.setValue("leng_m5",valueList[19])

                    cursor.updateRow(row)

            del cursor, row

   

except:

    print arcpy.GetMessages(0)

    print arcpy.GetMessages(1)

    print arcpy.GetMessages(2)

0 Kudos
27 Replies
JoshuaBixby
MVP Esteemed Contributor

I wouldn't go as far as saying you can only use one cursor at a time.  There are several factors that affect how and whether multiple cursors will work together on a data set.  The types of locks that the cursors require are one factor.  The order of which cursors are opened first can be a factor.  The backend data storage format is another factor.  When it comes to using multiple cursors to access spatial data, having the data in an enterprise DBMS makes a big difference.

SusanJones
Frequent Contributor

Totally agree - storage format is part of it. For tis particular task I have been using File Geodatbase

0 Kudos
GabrielBacca-Cortes
Deactivated User

thanks Susan for your response. The problems are somewhat different as in your code all the values within each field are taken from one FC and inserted into the fields of another FC, but I modified my code based on that Search-Insert Cursor logic and this is what I got:   [let me remind you that in my case each line from the array/valueList goes into a different set of fields, so that I indexed 'valueList' for as many 'lines' or number of inserts I have]

# Import system modules

import arcpy, os

from arcpy import env

# Set environment settings

env.workspace = r"C:\GIS\Track1\1_Orig\Track_int2"

# Set local variables

copyFields = ("PorousMed","Kcond_1","Porosity_1","Leng_m")

insertFd1 = ['PM1','Kcond1','Poros1','leng_m1']

insertFd2 = ['PM2','Kcond2','Poros2','leng_m2']

insertFd3 = ['PM3','Kcond3','Poros3','leng_m3']

insertFd4 = ['PM4','Kcond4','Poros4','leng_m4']

insertFd5 = ['PM5','Kcond5','Poros5','leng_m5']

try:

    fcList = arcpy.ListFeatureClasses("","POLYLINE")

    for fc in fcList:

        print fc

        vals = arcpy.da.SearchCursor(fc, copyFields)

        valueList = []

        for row in vals:

            line = [vals[0],vals[1],vals[2],vals[3]]

            valueList.append(line)

        del vals

       

        cursor = arcpy.da.InsertCursor(fc, insertFd1)

        for row in rows:

            cursor.insertRow(valueList[0])

        del cursor

        cursor = arcpy.da.InsertCursor(fc, insertFd2)

        for row in rows:

            cursor.insertRow(valueList[1])

        del cursor

           

        cursor = arcpy.da.InsertCursor(fc, insertFd3)

        for row in rows:

            cursor.insertRow(valueList[2])

        del cursor

        cursor = arcpy.da.InsertCursor(fc, insertFd4)

        for row in rows:

            cursor.insertRow(valueList[3])

        del cursor

        cursor = arcpy.da.InsertCursor(fc, insertFd5)

        for row in rows:

            cursor.insertRow(valueList[4])

        del cursor

except:

    print arcpy.GetMessages(0)

    print arcpy.GetMessages(1)

    print arcpy.GetMessages(2)

Unfortunately, this didn't work.  I think the partitioning within each indexed 'valueList' among the respective fields didn't happen.  any thoughts ?

thanks !

0 Kudos
XanderBakker
Esri Esteemed Contributor

I just posted the code and attached the result using your shapefiles. Let me know if it is what you were looking for.

0 Kudos
GabrielBacca-Cortes
Deactivated User

Thanks Xander - yes, it does work. Only that through 'valueList', the code moves the second set of values [1] instead of the first set [0] from 'flds_in' to PM1-K1-P1-lengm1. I've been trying to fix this, but honestly I am having difficulties understanding the code enough to modify it, 🙂

For the first FC 'xy_1.shp', I'd like to move to PM1-Kcond1-Poros1-Lengm1, the first row of values from 'flds_in':

['Gravelly silt', 0.25, 4.2924, 0.41] not the second row: ['Silty Sand', 10.5, 11.2832, 0.36].

Also, and this is my lack of understanding of the code, but when I print 'valueList', running the code only for the first FC in order to see what the code does more clearly, it prints:

[[u'Silty Sand', 10.5, 8.9952, 0.36], [u'Medium Sand', 85.2, 12.1677, 0.35]]

in which the terms for the third field [Leng_m#] are wrong but they are correct when the code runs and its entirety and delivers the final result:

[['Silty Sand', 10.5, 11.2832, 0.36], ['Medium Sand', 85.2, 5.9327, 0.35]]

Thanks again !

Gabriel

0 Kudos
XanderBakker
Esri Esteemed Contributor

Yep you are right.

Change the code on line 53 into:

                        lst_vals = list(valueList[num-1])

The "-1" is missing in the previous code

0 Kudos
XanderBakker
Esri Esteemed Contributor

Let me know if you are interested in some additional explanation on the code... then I'll include it.

0 Kudos
GabrielBacca-Cortes
Deactivated User

Great -  that did the trick for both observations I made.  thanks !

0 Kudos