Select to view content in your preferred language

Python Script to read from shapefile and insert in matrix.

706
1
11-09-2010 05:47 AM
GeorgeBentley
Emerging Contributor
Hi Everyone,

I am working on a Python script that I could use some assistance with.  I am trying to take the values within the "P_Area" and "P_Area_1" fields that reside in the Srcfile (variable name for shapefile of interest and insert these values into the first two columns of a matrix.  Using Numpy I have managed to set up a n x m matrix with 179 rows and 360 columns.  I could use some assistance with the next step of reading the values from these two fields and writing them to the matrix.  Any help would be greatly appreciated.

I have attached the script that is in progress.
0 Kudos
1 Reply
ChrisMathers
Deactivated User
Well it looks like the you are making a cursor in your defined function and trying to use it ouside the function which will cause you problems. If you want a list that contains all of the values for a field I would do it this way.

cursor=gp.SearchCursor(Srcfile)
row=cursor.Next()
P_Area=[]
P_Area1=[]
while row:
    P_Area.append(row.GetValue(P_Area))
    P_Area1.append(row.GetValue(P_Area1))
del cursor


That gives you two lists. One for each field. How do drop them into a numpy matrix Im not sure since I havnt used it, but this is a better way to get the lists. It may be easier to put the numbers into the matrix as you are taking them from the cursor instead of using the lists. I dont see how you can load a list into a numpy matrix though, at least from the numpy docs.
0 Kudos