[gdal-dev] Updating shapefile "fields" name with ogr2ogr

Even Rouault even.rouault at mines-paris.org
Wed Jun 20 10:50:34 PDT 2012


Le mercredi 20 juin 2012 18:41:40, Emmanuel Séguin a écrit :
> Hi,
> 
> This may be very easy but I did not find a way to use ogr2ogr to change
> a shapefile field (attribute) name.
> I've tried something like this : ' ogr2ogr -f "ESRI Shapefile" -sql
> "ALTER TABLE CHEMIN RENAME COLUMN prec_alti TO foo" CHEMIN2.shp
> CHEMIN.shp ' but I get the following error "ERROR 6: Can't alter field
> definition on a read-only shapefile layer." (I've tried to use the
> "AlterFieldDefn=TRUE" layer option with no success whatsoever)

I know this is going to sound a bit counter-intutive at first...

When doing ogr2ogr destination.shp source.shp [anything], there's no reason to 
open source.shp in read/write mode, so it is opened in read-only mode. When 
you issue a SQL command, it is meant as an action on the source dataset, not 
the target dataset. When this SQL command is a ALTER one, you obviously need 
read/write permissions. When you combine all the statements, you understand 
the error message...

The good news is that you can solve it easily by using ogrinfo instead. 
ogrinfo opens datasources in read/write mode by default (unless you specify -
ro). So you can run :

ogrinfo CHEMIN.shp -sql "ALTER TABLE CHEMIN RENAME COLUMN prec_alti TO foo"

Best regards,

Even


More information about the gdal-dev mailing list