The need is:
I have a set of shapefile and need to have another copy of them addind a new field and populating it with the filename (without extension it match a code identifier).
The solution was using a shell dos and the ogr2ogr and ogrinfo utility:
This is the code:
I use the gdal batch utility from osgeo4w package.
-----
mkdir destination-folder
for /R D:\temp\temp2\input-folder %%f IN (*.shp) do (
ogr2ogr D:\temp\temp2\destination-folder\zz_%%~nf_output.shp %%f
cd D:\temp\temp2\input-folder
ogrinfo zz_%%~nf_output.shp -sql "ALTER TABLE zz_%%~nf_output ADD COLUMN filename character(128)"
ogrinfo zz_%%~nf_output.shp -dialect SQLITE -sql "UPDATE zz_%%~nf_output SET filename = '%%~nf'"
cd ..
)
------
Just an explain of some details:
In the ogrinfo command: in the alter-table sql command the tablename inside the sql command should be equals to the filename of shapefile called externally.
In the ogrinfo command: in the update sql-command there is the same relation name-table with name shapefile of the alter-table.
Nessun commento:
Posta un commento