38

I can display a shapefile in openlayers but I need to add some attribute data.

I can open the dbf file in excel but there is no save as option in excel to the updated file as a dbf.

What is the best way (or software to use) to add attribute data to my shapefile?

3
  • Go back to an earlier version of Excel: MS removed the ability to write .dbf files with its latest version! :-(
    – whuber
    Commented Mar 6, 2011 at 21:30
  • 4
    Open office and Quattro Pro can edit and save dbf files, just don't delete rows or move stuff around otherwise the index file won't allow things to match up. Use a commercial or open source gis for these tasks, editing cell values should cause no problem
    – user681
    Commented Mar 6, 2011 at 23:06
  • Well!!! And I thought my question was too simple for this site!! I have not had a chance to try any of the suggestions because I somehow trashed my geoserver and postgres softwares ... :-( So once I get my linux box back to 100% I'll get check out all the suggestions Thanks for taking the time to reply chris
    – ChrisJ
    Commented Mar 11, 2011 at 6:11

10 Answers 10

30

Using QGIS you can edit your shapefile adding new columns and values. Just open the shapefile, go to Properties>Attributes and add new columns.

In newer versions of QGIS (2.x), 'Attributes' is called 'Fields'

5
  • I didn't think of using QGIS because on my Linux box it (Copiapo) crashes when I click on "Fetch python plugin". Anyone knows if there's a fix for this? In the meantime I'll try adding the attributes with a widows version.
    – ChrisJ
    Commented Mar 6, 2011 at 21:18
  • You could try to debug the problem the way Richard describes here: osgeo-org.1803224.n2.nabble.com/…
    – underdark
    Commented Mar 8, 2011 at 18:10
  • QGIS allows you to add new columns, but populating the new column with data appears to be point-click-enter -- really inefficient! I suggest using R as in mdsummer's answer below.
    – baha-kev
    Commented Feb 23, 2012 at 5:12
  • @baha-kev Why would it just be click-point-enter? Couldn't you just do something like this tutorial?
    – ocean800
    Commented Feb 4, 2016 at 20:10
  • 1
    In QGIS 3.2.1 it is located in layer context menu > 'Properties...' > 'Source Fields' category in dialog window.
    – apdevelop
    Commented Jul 27, 2018 at 14:41
25

Use R with the foreign package to modify the DBF file:

library(foreign)
dbfdata <- read.dbf("file.dbf", as.is = TRUE)
## add new attribute data (just the numbers 1 to the number of objects)
dbfdata$new.att <- 1:nrow(dbfdata)

## overwrite the file with this new copy
write.dbf(dbfdata, "file.dbf")

Or read the geometry and attribute data with the rgdal package (so you could modify the relationships as well and create a completely new shapefile):

library(rgdal)
## read "/path/to/files/filename.shp"
shp <- readOGR("/path/to/files/", "filename")  

## add new attribute data (just the numbers 1 to the number of objects)
shp$new.att <- 1:nrow(shp)

## write out to a new shapefile
writeOGR(shp, "/path/to/files/", "filename2")  
4

I would not recommend using OpenOffice - or a similar application - at all! Darren Cope commenting an answer to the question "DBF creation and manipulation without excel 2003" said:

it's that shapefiles get quite upset if you go and edit the .dbf in an 'outside' program

3
  • 3
    this isn't necessarily true: I've edited many shapefiles in OpenOffice and Excel without problems: you just need to be aware of the limitations of the format (column name lengths, data types, et cetera).
    – scw
    Commented Mar 7, 2011 at 2:34
  • @scw: can you give any links to information about the format limitations?
    – LarsH
    Commented Aug 27, 2014 at 16:18
  • 2
    The specific constraints vary by the particular software's use of the format, but the simplest restrictions are: 11 character field names, no special characters or spaces in field names, and stick to the core datatypes of DBF files (not esoteric things like 'memo' fields). For more details, see shapefile.py, the shapelib DBF API or this tome on xbase.
    – scw
    Commented Aug 27, 2014 at 18:39
4

I've merged several shapefiles using MS Access. I needed to left join some data from other shapefile and it worked quite well. Also it was quick. However I guess not everyone has this software

1
  • 1
    I use either Access or some other form of SQL to manipulate data, usually. Just seems much easier for me to run a few queries. As mentioned before, you just have to watch the changing of actual database format, this is something that is best handled in the actual GIS software.
    – MaryBeth
    Commented Mar 7, 2011 at 13:23
2

Using QGIS is nice, and provides a solid interface for interacting with the DBF, but if you need to do something programmatically or just want tools to inspect shapefiles, I thought I'd mention a few other tools: I often use the basic features of shapelib for examining DBF files: it can add, create and modify both geometries and attributes, I often use dbfdump myshape.dbf to get a quick overview of the attribute values.

Another option if you're interested in programmatic control of a DBF is dbfpy, a python library (an an alternative to the nice foreign library mdsummer mentions). An example script for adding a column:

import dbfpy

db = dbf.Dbf("myshape.dbf", new=False)
# add a new character field named 'myfield'
db.addField(("myfield", "C", 15))

db.close()
2

as andy pointed out in the link DARIAPRA refered you to, there is a Excel 2007 ADD-IN you might find useful - It allows you to save in the dbf format.you can find it on this website:

http://savedbf.blogspot.com/

(As I have Excel 2003, I've never tried it, but I heard it was useful.

As for DBF manipulation outside ArcGIS - I've done it a few times. Sometimes it works, sometimes it doesn't. My two cents: If you can avoid the outside manipulation, avoid it. If you're dealing with a non-english language avoid it altogether - It tends to get all fumbled up when editing outside of ArcGIS).

2

Alternatively:

  1. open the dbf in MS Excel 2007/10
  2. Make your changes (adding columns, populating data etc) and save as xls/xlsx
  3. Navigate to xls/xlsx location in ArcCatalog, right-click the worksheet and choose Export > To dBase (single)
  4. Rename the output dbf file as required
2

uDig has a nice tool called reshape, through which you can for example add attributes to a table: http://udig.github.io/docs/user/reference/Transform%20operation.html

Supported functions are those of the common query language: http://udig.github.io/docs/user/concepts/Constraint%20Query%20Language.html

2

I usually use R (check mdsomners answer), but I would not recommend learning that if that is the only thing you want to do. I think the best approach would be using a GIS program, to create the fields, and then try editing them using openoffice.

More importantly, you should think whether you need to edit all the fields the attribute table directly, or if you can join other tables to it. The latter can be done in most GIS programs, eg SAGA GIS

http://www.saga-gis.org/saga_modules_doc/shapes_tools/index.html

1
  • Agreed. Every time I add/change data, I wonder if there is an easier way.
    – MaryBeth
    Commented Mar 7, 2011 at 13:22
1

Open Office Calc allows you to read and write dbf files. But Lcasagrande's solution using QGIS should be safer.

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.