Site menu:

Cyclocross For Roadies: How and Why to Get Started - Buy Now!

Site search

Categories

Renaming Fields in a Shapefile

Here’s another quick post on the use of OGR for a simple yet often annoying task; renaming fields in a Shapefile.

We’ve all had a Shapefile with column names that make no sense (10 character limit anyone?) or with fields that were manually created by some geoprocessing tool and given a random name upon output. So how do you rename these columns? QGIS is getting close with its new ‘Fields’ options under the layer “Properties,” but can’t yet do the job. The “Table Manager” plugin will do the job fairly easily, but is not a built in function. Kosmo will do the task, but is often very slow and will choke on a large file. And please don’t go opening the .dbf and editing names directly–that’s just asking for trouble! (and Excel won’t even save back to a .dbf, so you’ll have to use LibreOffice!)  So, what’s the failproof solution? Once again, we turn to the amazing OGR. Using ogr2ogr we can slide in an SQL statement that does the trick nicely, and as with almost all OGR tasks, it’s quick, stable and effective. Here’s how simple it is:

ogr2ogr outdataset indataset -sql "SELECT src_field1 AS dst_field1, src_field2 AS dst_field2 FROM sourcelayer"

Of course you can rename any/all of the fields using this method all in one step! The other great thing is that you can enter virtually any SQL statement, so you can do other tasks at the same time as the rename if you need to. For example you could pick out select fields, rename them, and also enter a WHERE clause to subset your dataset at the same time. There are so many options!

Final word: Don’t be afraid of the command line! It’s fast, stable and powerful!

Comments

Comment from Mark Verschuur
Time April 26, 2011 at 11:29 am

Nice post. I have been looking for a solution for a long time. I finally ended up with a nice tool in arcgis called ‘feature class to feature class’. It does the same thing, but ofcourse it’s not open source …

Comment from kay Warrie
Time April 26, 2011 at 2:12 pm

mmh, you are creating a new feature class in both cases (ogr or gp), i usually create new fields , populate them with the field-calculator and then delete the old ones.

Comment from serena
Time April 27, 2011 at 12:52 pm

that is usually what I do

Comment from Christian
Time December 21, 2011 at 10:34 am

Very useful and easy, that’s what I needed this morning, thanks!

Comment from darrencope
Time December 22, 2011 at 11:48 am

Glad it helped you out Christian!

Comment from James
Time July 11, 2012 at 5:48 am

genius…..like that alot. Surely its time to change the shape file format. Or make a tool that does multiple delete fields, reorder your fields and rename your fields….

thanks

Comment from James
Time July 11, 2012 at 5:51 am

like you say Table Manager in Quantum GIS is brilliant

Pingback from DARREN COPE » 2012 – The Year in Review
Time February 3, 2013 at 4:17 pm

[…] Renaming Fields in a Shapefile […]

Comment from farahnak
Time January 21, 2015 at 3:23 am

very nice post, I can not execute the statement. It will be very much appreciated if you define all parameter of the statement in detail. I have a shape file by the name of test. The layer name is water. For example I want to change the field name “ph” to “ec”. What statement can I use.

Comment from darrencope
Time January 21, 2015 at 7:51 am

The syntax I presented above is as detailed as it gets. It’s not clear what you mean by ‘layer name’ but as far as I know, there is no such thing for a shapefile. For you, it should be something like “ogr2ogr outdataset.shp test.shp -sql “SELECT ph AS ec FROM test” – you will also need to add all of the other fields you want to the list, even if you are not renaming them, otherwise they will not get carried through.

Write a comment





Bookshelf 2.0 developed by revood.com

Read previous post:
Race Report – Paris to Ancaster 2011

Paris to Ancaster is over for another year. P-A is my favourite race of the year, and has been my...

Close