Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

Thank you very much

share|improve this question
1  
You can edit your original question, to correct or to add info. –  Brad Nesom Dec 16 '10 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 Dec 16 '10 at 23:10

5 Answers 5

Sorry for not answering your question, but I would definitly 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.

Regards

Nicklas

share|improve this answer

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

share|improve this answer

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() 
share|improve this answer
    
neuhausr, thank you very much, it works well how you put it –  Bogdan Palade Dec 17 '10 at 10:02

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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