Skip to main content
added 6 characters in body
Source Link
Taras
  • 35.8k
  • 6
  • 77
  • 152

I think that the numpynumpy route that Bera@Bera suggested is better, but you can do what you want also with a text editor and gdal_translategdal_translate.

First, find out how many rows and columns you have in the data and have a look at how ESRI AsciiGrid files are written https://github.com/OSGeo/gdal/blob/master/autotest/gdrivers/data/aaigrid/nodata_float.asc

Add header lines intoat the beginning of the text file with these values:

ncols        what-you-have-in-data
nrows        what-you-have-in-data
xllcorner    0
yllcorner    0
cellsize     1

Change the commas between the cell values into spaces. The linefeeds can stay as they are. These both small test files seem to work in the same way.

asciitest1.asc
==============
ncols        5
nrows        2
xllcorner    0
yllcorner    0
cellsize     1
1 2 3 4,5
6 7 8 9 10

asciitest2.asc
==============
ncols        5
nrows        2
xllcorner    0
yllcorner    0
cellsize     1
1 2 3 4 5 6 7 8 9 10

Test with gdalinfogdalinfo:

gdalinfo asciitest.asc
Driver: AAIGrid/Arc/Info ASCII Grid
Files: asciitest.asc
Size is 5, 2
Origin = (0.000000000000000,2.000000000000000)
Pixel Size = (1.000000000000000,-1.000000000000000)
Corner Coordinates:
Upper Left  (   0.0000000,   2.0000000)
Lower Left  (   0.0000000,   0.0000000)
Upper Right (   5.0000000,   2.0000000)
Lower Right (   5.0000000,   0.0000000)
Center      (   2.5000000,   1.0000000)
Band 1 Block=5x1 Type=Float32, ColorInterp=Undefined

Convert into TIFF:

gdal_translate asciitest2.asc asciitest2.tif
Input file size is 5, 2
0...10...20...30...40...50...60...70...80...90...100 - done.

I think that the numpy route that Bera suggested is better, but you can do what you want also with a text editor and gdal_translate.

First find out how many rows and columns you have in the data and have a look at how ESRI AsciiGrid files are written https://github.com/OSGeo/gdal/blob/master/autotest/gdrivers/data/aaigrid/nodata_float.asc

Add header lines into the beginning of the text file with these values:

ncols        what-you-have-in-data
nrows        what-you-have-in-data
xllcorner    0
yllcorner    0
cellsize     1

Change the commas between the cell values into spaces. The linefeeds can stay as they are. These both small test files seem to work in the same way.

asciitest1.asc
==============
ncols        5
nrows        2
xllcorner    0
yllcorner    0
cellsize     1
1 2 3 4,5
6 7 8 9 10

asciitest2.asc
==============
ncols        5
nrows        2
xllcorner    0
yllcorner    0
cellsize     1
1 2 3 4 5 6 7 8 9 10

Test with gdalinfo

gdalinfo asciitest.asc
Driver: AAIGrid/Arc/Info ASCII Grid
Files: asciitest.asc
Size is 5, 2
Origin = (0.000000000000000,2.000000000000000)
Pixel Size = (1.000000000000000,-1.000000000000000)
Corner Coordinates:
Upper Left  (   0.0000000,   2.0000000)
Lower Left  (   0.0000000,   0.0000000)
Upper Right (   5.0000000,   2.0000000)
Lower Right (   5.0000000,   0.0000000)
Center      (   2.5000000,   1.0000000)
Band 1 Block=5x1 Type=Float32, ColorInterp=Undefined

Convert into TIFF

gdal_translate asciitest2.asc asciitest2.tif
Input file size is 5, 2
0...10...20...30...40...50...60...70...80...90...100 - done.

I think that the numpy route that @Bera suggested is better, but you can do what you want also with a text editor and gdal_translate.

First, find out how many rows and columns you have in the data and have a look at how ESRI AsciiGrid files are written https://github.com/OSGeo/gdal/blob/master/autotest/gdrivers/data/aaigrid/nodata_float.asc

Add header lines at the beginning of the text file with these values:

ncols        what-you-have-in-data
nrows        what-you-have-in-data
xllcorner    0
yllcorner    0
cellsize     1

Change the commas between the cell values into spaces. The linefeeds can stay as they are. These both small test files seem to work in the same way.

asciitest1.asc
==============
ncols        5
nrows        2
xllcorner    0
yllcorner    0
cellsize     1
1 2 3 4,5
6 7 8 9 10

asciitest2.asc
==============
ncols        5
nrows        2
xllcorner    0
yllcorner    0
cellsize     1
1 2 3 4 5 6 7 8 9 10

Test with gdalinfo:

gdalinfo asciitest.asc
Driver: AAIGrid/Arc/Info ASCII Grid
Files: asciitest.asc
Size is 5, 2
Origin = (0.000000000000000,2.000000000000000)
Pixel Size = (1.000000000000000,-1.000000000000000)
Corner Coordinates:
Upper Left  (   0.0000000,   2.0000000)
Lower Left  (   0.0000000,   0.0000000)
Upper Right (   5.0000000,   2.0000000)
Lower Right (   5.0000000,   0.0000000)
Center      (   2.5000000,   1.0000000)
Band 1 Block=5x1 Type=Float32, ColorInterp=Undefined

Convert into TIFF:

gdal_translate asciitest2.asc asciitest2.tif
Input file size is 5, 2
0...10...20...30...40...50...60...70...80...90...100 - done.
Source Link
user30184
  • 71.6k
  • 4
  • 77
  • 130

I think that the numpy route that Bera suggested is better, but you can do what you want also with a text editor and gdal_translate.

First find out how many rows and columns you have in the data and have a look at how ESRI AsciiGrid files are written https://github.com/OSGeo/gdal/blob/master/autotest/gdrivers/data/aaigrid/nodata_float.asc

Add header lines into the beginning of the text file with these values:

ncols        what-you-have-in-data
nrows        what-you-have-in-data
xllcorner    0
yllcorner    0
cellsize     1

Change the commas between the cell values into spaces. The linefeeds can stay as they are. These both small test files seem to work in the same way.

asciitest1.asc
==============
ncols        5
nrows        2
xllcorner    0
yllcorner    0
cellsize     1
1 2 3 4,5
6 7 8 9 10

asciitest2.asc
==============
ncols        5
nrows        2
xllcorner    0
yllcorner    0
cellsize     1
1 2 3 4 5 6 7 8 9 10

Test with gdalinfo

gdalinfo asciitest.asc
Driver: AAIGrid/Arc/Info ASCII Grid
Files: asciitest.asc
Size is 5, 2
Origin = (0.000000000000000,2.000000000000000)
Pixel Size = (1.000000000000000,-1.000000000000000)
Corner Coordinates:
Upper Left  (   0.0000000,   2.0000000)
Lower Left  (   0.0000000,   0.0000000)
Upper Right (   5.0000000,   2.0000000)
Lower Right (   5.0000000,   0.0000000)
Center      (   2.5000000,   1.0000000)
Band 1 Block=5x1 Type=Float32, ColorInterp=Undefined

Convert into TIFF

gdal_translate asciitest2.asc asciitest2.tif
Input file size is 5, 2
0...10...20...30...40...50...60...70...80...90...100 - done.