Using ogr2ogr to rename fields in shapefiles

ogr2ogr is a commandline application packaged with GDAL. It can be used to rename the fields of a shapefile.

Like so:

ogr2ogr outputfile.shp inputfile.shp -sql "SELECT oldfield1 AS newfield1, oldfield2 AS newfield2 from inputfile"

The result only contains the fields in the SELECT query, with the field in the order they were mentioned in the SELECT query.

A shapefield named original.shp with the fields field1, field2 and field3(in that order), will change into result.shp with the fields field3 and newfield1(in that order) using:

ogr2ogr result.shp original.shp -sql "SELECT field3 AS field1, field1 AS newfield1 from original"

Sources

comments powered by Disqus