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.

Is it possible to update an attribute field in a shapefile using pyshp ?

I went through their documentation and all they show is how to create new attribute rather than editing or updating an already existing one.

If updating is not possible, is there any other recommended Python library for doing the same ?

share|improve this question
    
Do you have a sample shape file? –  R.K. Apr 10 '13 at 13:49
add comment

3 Answers

up vote 3 down vote accepted

You can use dbfpy to directly access and edit the attributes in the shape file's dbf file.

from dbfpy import dbf

db = dbf.Dbf("your_file.dbf")

#Editing a value, assuming you want to edit the first field of the first record
rec = db[0]
rec["FIRST_FIELD"] = "New value"
rec.store()
del rec
db.close()
share|improve this answer
add comment

It is the same thing with pyshp, except that you cannot update directly the dbf file. When you read a shapefile, the data are stored in Python lists

import shapefile
input = shapefile.Reader("yourfile.shp")
shapes = input.shapes() # -> the geometries in a list
fields = input.fields[1:] -> the fields definition in a list
fields_name = = [field[0] for field in fields] -> the fields names in a list
attributes = input.records() -> the attributes in a list

# now you can modify an attribute value in the list:
attributes[0][1] = "what you want" # second attribute of the first record
# if you want to use a field name for the attribute, like in dbfpy, you must use a dictionary
list = [(i,dict(zip(fields, attri))) for i,attri in enumerate(attributes)]
dict = dict((key, value) for (key, value) in list)
# now you can modify an attribute value in the dictionary:
dict[0]['field']='whatyouwant' # attribute of "field" in the first record
# and return to the original attributes list
attributes_cor = [i.values() for i in dict.values()]

but this changes the value in the list or in the dictionary, not in the dbf file. To do this, rather than affecting the original shapefile, it is better to create a copy with the new attribute list (same as Add a Field to an Existing Shapefile or Subsetting a Shapefile by Attributes ).

You can also use other Python libraries like ogr or Fiona (see Using the API of QSpatiaLite plugin for the principles, the data are stored as Python dictionaries)

from shapely.geometry import mapping, shape
import fiona
# Read the original Shapefile
with fiona.collection('yourfile.shp', 'r') as input:
# The output has the same schema
schema = input.schema.copy()
# write a new shapefile
with fiona.collection('yourcopyfile.shp', 'w', 'ESRI Shapefile', schema) as output:
    for elem in input:
         elem['properties']['field'] = 'whatyouwant' # or a function, or...
         output.write({'properties': elem['properties'],'geometry': mapping(shape(elem['geometry']))})
share|improve this answer
    
+1 I was gonna write about using Fiona/OGR too but I don't know enough about it so I went with dbfpy. Thanks for your detailed answer :) –  R.K. Apr 12 '13 at 8:25
add comment

I'm the author of PyShp. Both R.K. and Gene are correct. Editing a file like a dbf is just an illusion. What you are really doing is reading in the old one into a Python object, changing some variable values, and writing over the old file.

If you are going to do a bunch of dbf editing definitely use dbfpy. Like PyShp it is pure python but has more robust dbf capabilities than PyShp and a richer API for "editing".

The blog posts that Gene references shows how to read, change, and save a shapefile in PyShp which is what dbfpy is doing under the hood.

share|improve this answer
add comment

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.