11

I try to explain here what I am trying to do:

I have one shapefile and one independent dbf table with the same fields. In the dbf table all the fields are populated but in the shapefile attribute table just one, lets name it "OneField". What I want to do is to check that the values from "OneField"(Shapefile) are the same as the values in "OneField"(dbf table) and if so, to populate the remaining empty fields in the shapefile attribute table with the ones in the independent dbf table.

At the moment I am trying just to copy the values from the independent dbf table to the shapefile attribute table but I am stuck.(when I run this code I get a message that pythonwin stopped working and nothing happens to the tables). Can you give me a hand please?

Here is the code:

import arcpy

table = "link/to/table.dbf"
fc = "link/to/shapefile.shp"

# Create a search cursor

rowsTable = arcpy.SearchCursor(table)

# Create an update cursor

rowsFc = arcpy.UpdateCursor(fc) 

for row in rowsTable:
    row = row.getValue("OneField")
    valueTable = row
    for row in rowsFc:
        row = row.setValue("OneField", valueTable)
        rowsFc.updateRow(row)
        row = rowsFc.next()
    row = rowsTable.next()    

del row, rowsFc, rowsTable
2
  • 1
    You can edit your original question, to correct or to add info.
    – Brad Nesom
    Commented Dec 16, 2010 at 12:46
  • try taking out all the lines that have .next(), with arcpy the rows are iterable, you for "for" loops on them, you don't do .next()
    – gotchula
    Commented Dec 16, 2010 at 23:10

5 Answers 5

3

That's exactly what QGIS ftools "Join by Attribute" tool is doing. It takes a shapefile and a dbf file (or a second shapefile) and joins their attribute tables. The tool is written in python, so you should find everything you need here: http://trac.osgeo.org/qgis/browser/trunk/qgis/python/plugins/fTools/tools/doJoinAttributes.py

3

From the code point of view, looks like you're using "row" for a bunch (~4) different things, some of which appear to be unnecessary. I don't do much with arcpy, but I'd imagine something like this would work better. If it did, you'd end up with the whole OneField column in your FC populated with the last value of OneField from your table:

for rowT in rowsTable:
    valueTable = rowT.getValue("OneField")
    for rowF in rowsFc:
        rowF.setValue("OneField", valueTable)
        rowsFc.updateRow(rowF)
        rowF = rowsFc.next()
    rowT = rowsTable.next() 
1
  • neuhausr, thank you very much, it works well how you put it Commented Dec 17, 2010 at 10:02
3

I spent a good amount of time trying to this myself and I've got something that works. Refer to my code here to get it working.

How to update feature class by iterating through values with branching?.

You'll need to make a list that contains all the fields so you can iterate through them.

2

My method for moving data from a dbf to a shapefile. Is to (of course I prefer to do this inside a gui software) join the dbf to the shapefile.

At that point I normally select attributes that are not null (I do this on the id field of the second table). If they are not null then they did match.

Once this is done you can select the the values by shapefile.onefield <> table.onefield.

Then run your calculation in the shapefile.

-1

Sorry for not answering your question, but I would definitely do this in db-environment instead. Especially if it is a lot of data, then that will probably be a lot faster.

If you for instance load the shapefile and the dbf into a PostGIS db your query could look something like this:

CREATE TABLE new_table AS
SELECT a.the_geom, a.populated_field, b.populated_field2, b.populated_field3
FROM shape_table a LEFT JOIN dbf_table b ON a.populated_field=b.populated_field1;

If you put index on the joining fields, this should be very fast. You can of course update shape_table instead, but like to create a new table instead to not destroy the original data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.