1

I have >2.5k .asc grid files which I need to merge into a single overarching dataset. I would like to avoid having to convert them into esri grids first so was looking to use QGIS (v.2.0).

I am aware of the command line length limitation with the QGIS merge tool so was looking to use the OSGeo4W shell to automate the process. Can someone help me with some code to cycle through the files and merge them?

I was looking at this thread but the suggested solution doesn't work - it results in an "index out of range error"

The files are all of the same format, in a single folder and have the name convention SDXXX.asc - where X is a single digit.

  • Did you want a .asc file as the output or a geotiff or a hillshade? – tjmgis Jan 28 '14 at 16:25
  • I need a single dataset in either .asc or geotiff. No need for hillshading. – Ed Rollason Jan 29 '14 at 10:44
  • Very helpful. Thanks. I just wanted to point out a typo on the last line; a missing underscore: gdaltranslate -> gdal_translate – Ruben Valbuena Mar 19 '18 at 13:56
3

You could do something like the following

SET DATADIR="C:\Temp\Data"

FOR %%A IN (*.asc) DO (gdal_translate -of GTiff -a_srs "EPSG:27700" -co "COMPRESS=LZW" "%%A" "%%~NA.tif")
pause

This would create a geotiff for each of your .asc files and then you could

gdalbuildvrt geotiff.vrt *.tif

This would create a VRT file for each of the tif files and then you could create master geotiff by:

gdaltranslate -of GTiff -a_srs "EPSG:27700" -co "COMPRESS=LZW" geotiff.vrt master.tif

You will need to add in your own prejction and maybe compression

You then add overviews using gdaladdo

You might be able to create a VRT from the original asc files and then create your master geotiff, but I have not tried that.

Hope that works

2

You can use "Build virtual raster" for this which you find in QGIS 2 under

Menu "Raster" >> "Miscellaneous" >> "Build virtual raster"

It accepts multiple files and input (all of them should have the same projection). Like this you then treat the thousands of ASCII tiles as a single VRT map.

See also: http://qgis.org/en/docs/user_manual/plugins/plugins_gdaltools.html

  • Thanks Markus - while that would work I could also do with the dataset being usable across a range of platforms, including ArcGIS, so a merging approach would still be preferable for me. – Ed Rollason Jan 28 '14 at 14:42
  • Then simply use the "Merge" feature from the same submenu. – markusN Jan 28 '14 at 23:32
  • The merge tool on the toolbar has a character limit on the command line. I have over 2 and a half thousand files which massively exceeds this limit. I'm therefore looking for a programmatic method around this. – Ed Rollason Jan 29 '14 at 10:45
  • It might be an idea to load your rasters into a Rasterlite database. This will give you one file that can be opened in QGIS. – nhopton Jan 29 '14 at 11:48
0

The following approach worked for me (using the command line):

name=${PWD##*/}
find ./ -name "*.asc" > file_list.txt
gdalbuildvrt -input_file_list file_list.txt "$name.vrt"
gdal_translate -of GTiff "$name.vrt" "$name.tif"

Behaviour: creates a .vrt virtual raster and a .tif file named after the directory basename.

If you want to choose your own file name, such as 'master', substitute the first line with name=master

Caveat: all my .asc files have a corresponding .prj projection file. If your .asc files don't have a projection definition, you might need add a projection to the gdal_translate command with the option -a_srs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.