[gdal-dev] extra spaces when writing to AAIGRID format
Brad McRae
bmcrae at TNC.ORG
Thu Dec 15 14:46:00 EST 2011
Hi Even and Eli,
Thanks much for your responses. As soon as I sent that email I
realized I might eat my words on the inability to open the ASCII grids
in ArcMap. This problem did crop up when we released a version of our
code that used GDAL a few years back. A user reported intermittent
errors reading grids that our code exported. Since then, I have
assumed that we must fix the writing behavior of our GDAL code.
But, I was able to open the files you sent in ArcMap 10 (as well as
the grid example that I sent out). So it is not always a problem.
Moreover, it may not be a problem at all with the latest versions of
ArcGIS- the old problem may have even been on ArcView 3.x. We'll do
some further testing and decide whether it seems risky at all.
Thanks again,
Brad
On Thu, Dec 15, 2011 at 11:01 AM, Even Rouault
<even.rouault at mines-paris.org> wrote:
> 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
--
------------------------------------------------------------------------------
Brad McRae
The Nature Conservancy
1917 1st Avenue
Seattle, WA 98101
Cell: 541-223-1170
Office: 206-436-6206
Fax: 206-343-5608
email: bmcrae at tnc.org
http://www.nceas.ucsb.edu/~mcrae/
More information about the gdal-dev
mailing list