Esri ASCII GRID can be converted to esri .bil format by following way in a desktop From QGIS desktop menu-->Raster-->conversion-->Translate(convert format) How can I write a python code because I have 864 esri ascii grid data?
-
Personally I'd use GDAL_Translate -of EHDR and use the hdr.adf file in the grid as the input. If that sounds like something you'd be into I can expand that into an answer. Why .bil? ERDAS Imagine or GeoTiff works just as well, if not better.– Michael StimsonAug 5, 2014 at 22:31
-
I am using R to plot grid images and I am very new to R so I depends on an example.– NahmAug 5, 2014 at 23:00
1 Answer
Sorry, you asked for PyQGIS but this is just standard Python.
Here's a script I put together:
import os, sys, subprocess
# in folder and out folder arguments, if you want you can hard code
# these paths instead of using arguments.
InFolder = sys.argv[1]
OutFolder = sys.argv[2]
# GDAL_Translate = "C:\\YOUR\PATH\TO\GDAL_Translate.exe"
# this is my path for an example
GDAL_Translate = r"C:\Program Files\QGIS Dufour\bin\gdal_translate.exe"
GDAL_Options = "EHDR" # output format EHDR = .bil, HFA = .img, GTIFF = GeoTiff
for (pPath, pFolder, pFiles) in os.walk(InFolder):
for pThisFile in pFiles:
if pThisFile.lower() == "hdr.adf":
GridName = os.path.basename(pPath)
# set the paths for in and out files
ConvertFromFile = pPath + "\\" + pThisFile
ConvertToFile = OutFolder + "\\" + GridName + ".bil" # don't forget to change if you change the output format
# create a blank list and then put all the words in it
# in order to feed to the subprocess.call
Command = list()
Command.append(GDAL_Translate)
Command.append("-of")
Command.append(GDAL_Options)
Command.append(ConvertFromFile)
Command.append(ConvertToFile)
# run the command to do it and wait until it finishes
subprocess.call(Command)
This uses os.walk()
to go down the folder tree looking for all the files matching "hdr.adf" and executes GDAL_Translate
for each one. Note that no checking for if the file is going to be overwritten, depending on your circumstances you might want to address that.
To keep things simple I've not included any error checking so it's just the functional minimum. I tested this script on a folder with subfolders full of Esri GRIDs (3365 GRIDs) and it worked just fine converting to 3365 Esri BIL files.
-
Thank you very much !! My coding level is so low it takes time to digest.– NahmAug 6, 2014 at 0:35
-
You're welcome. Can you please accept the answer so future users can see that it helped: Click on the little tick on the left top of the answer. Aug 6, 2014 at 0:36
-
It's sort-of mentioned in the tour gis.stackexchange.com/tour, underneath the upvote/downvote there's a little tick that can be a bit hard to see (it will be grey) but only the person who asked the question can see it, if you click on that it will mark this question as "answered". Aug 6, 2014 at 0:48
-
-
Code is working because I had one result after I modified file extension part because I am dealing with ASC file instead of ESRI GRID ,hdr.adf. Anyway I can do that I plot that one converted .bil file in R environment. Everything is good. I need to work a little bit more on a file extension part. Thank you very much specially gdal_translate.exe part which opens my eye.– NahmAug 6, 2014 at 4:49