[gdal-dev] extra spaces when writing to AAIGRID format

Even Rouault even.rouault at mines-paris.org
Thu Dec 15 14:01:12 EST 2011


Le jeudi 15 décembre 2011 18:21:11, bhmcrae a écrit :
> Hi,
> 
> I'm having problems with GDAL inserting leading spaces when writing from a
> numpy array to ESRI ASCII Grid format.
> 
> The problem occurs whenever there are integers in the array (even though
> the array itself is floating point).
> 
> E.g., if I try to write a 3x3 grid that ArcGIS can read, it needs to look
> something like this:
> 
> ncols        3
> nrows        3
> xllcorner    100.000000000000
> yllcorner    100.000000000000
> cellsize     0.000010000000
> 1.00000000000000000 1.4809201955795288 1.4809201955795288
> 0.11773009598255157 0.15476560592651367 0.86710774898529053
> 0.17429724335670471 0.76037305593490601 10.00000000000000
> 
> Instead, the ascii file written by GDAL looks like this (which can't be
> read by ArcGIS because of extra leading spaces in the first line of the
> data):
> 
> 
> ncols        5
> nrows        5
> xllcorner    100.000000000000
> yllcorner    100.000000000000
> cellsize     0.000010000000
>       1 1.4809201955795288 1.4809201955795288
>  0.25947147607803345 0.71342569589614868 2.8101832866668701
>  0.11773009598255157 0.15476560592651367 0.86710774898529053
>  0.17429724335670471 0.76037305593490601     10 0.74458205699920654
> 
> Here is the code I have been trying to use:
> 
> format = "MEM"
> driver = gdal.GetDriverByName( format )
> dst_ds = driver.Create( file, len(data[0]), len(data),1,gdal.GDT_Float32)
> dst_ds.SetGeoTransform([xllcorner, cellsize, 0, ull, 0, cellsize])
> dst_ds.GetRasterBand(1).WriteArray(data)
> format = 'AAIGrid'
> driver = gdal.GetDriverByName(format)
> dst_ds_new = driver.CreateCopy(file, dst_ds)
> dst_ds = None
> 
> 
> I've seen this problem on various versions of python going back at least 3
> years, so have relied on the much slower numpy.savetxt command instead of
> using GDAL.
> 
> Any thoughts on how to make this work?

Brad,

What iI can say for sure is that it is not related at all with which Python 
version you are using. This is specific to the AAIGRD driver. You would get the 
same results with gdal_translate.

It would be great if you could identify more precisely the cause for the 
failed opening. Is it :
1) the presence of spaces at the beginning of lines (the first number of the 
line being an integer or a float),
2) the presence of spaces at the beginning of lines starting with an integer,
3) integer values not being written as floating point numbers

For 1), 2) and 3), I've attached test1.asc, test2.asc and test3.asc that 
should test the hypothesis. I've also attached test0.asc for reference, which 
is the example that was generated by ArcGIS, and that I've altered to generate 
the other files. Could you open those files with ArcGIS and report which ones 
work ?

And what is the ArcGIS version you are using ?

Do other ArcGIS users that happen to read that thread have met similar issues 
?

Best regards,

Even
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test0.asc
Type: application/pgp-keys
Size: 294 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/gdal-dev/attachments/20111215/e821eb4d/test0.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test1.asc
Type: application/pgp-keys
Size: 297 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/gdal-dev/attachments/20111215/e821eb4d/test1.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test2.asc
Type: application/pgp-keys
Size: 279 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/gdal-dev/attachments/20111215/e821eb4d/test2.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test3.asc
Type: application/pgp-keys
Size: 276 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/gdal-dev/attachments/20111215/e821eb4d/test3.bin


More information about the gdal-dev mailing list