In QGIS (2.18), I have several raster files (.asc-format) that have no projection information. The images display well at EPSG:5677. My aim is to store all the files in a new folder, using the GeoTIFF format and assigning EPSG:5677.

I have no python experience, however some python coding could help me here, I guess. I read something similar but shapefile-related here: Exporting several files at same time in QGIS?

Of course, this doesn't work with my raster files. So here goes my question: how can I export a bunch of raster files, using the original file names and assigning the desired coordinate reference system without having to export every single file manually?

  • Do you have ArcGIS? Also, if you have experience in R, I would suggest a very simple workaround. – Vijay Ramesh Mar 8 '17 at 16:30
  • 2
    No, I don't have ArcGIS. I wouldn't call myself experienced in R, but I do use it from time to time. However, I've never worked with geodata in R. – yenats Mar 8 '17 at 16:32
  • I have provided a solution in R, since you mentioned that you use it from time to time. Let me know if that helps. It's much quicker than doing it manually in QGIS. – Vijay Ramesh Mar 8 '17 at 16:52
  • Possible duplicate of gis.stackexchange.com/questions/208617/… (which was closed as off-topic, but has answers). – AndreJ Mar 9 '17 at 14:38
up vote 3 down vote accepted

Based on the User's comment that they would be open to trying this in R, here's the solution:

#Load the three packages below 
library(sp)
library(raster)
library(rgdal)

#List the files that you want to export from the source folder
files <- list.files(path="C:\\Users\\.", pattern="asc$", full.names=TRUE) #selects all the asc files in the directory
s <- stack(files) # stack all of them using r raster library
proj4string(s) <- CRS("+init=epsg:5677") #Sets it to the projection you wanted
newname<-paste("C:\\Users\\",names(s)) #Saves the previous names
writeRaster(s,newname, bylayer=T, format="GTiff")#Edited based on Fumy's comment    
  • Thank you, this worked for me. However, I preferred to keep the original file names, so I specified the location by > newname<-paste("C:\\Users\\",names(s)) and then stored the files > writeRaster(s,newname, bylayer=T, format="GTiff") – yenats Mar 9 '17 at 11:36
  • Thanks @F.Fumy. That's a great suggestion. Updated it to reflect your edits. – Vijay Ramesh Mar 9 '17 at 14:15

Given that you have QGIS, you therefore also have GDAL installed on your machine. So I suggest you adapt the example in this post over on Stackexchange and create a batch file.

In your case, you will also want to set the gdal_translate a_srs switch to 5677 and change the -of switch (see documentation). So the relevant line in the batch file will be something like this:

gdal_translate -of "GTiff" -a_srs 5677 %mypath%!infile! %mypath%!outfile!

If you leave out the -of switch altogether, it will default to GeoTiff anyway. I just wanted to differentiate from the example given in the link. If you do not have an environment variable set for gdal_translate (which in a standard osgeo4w installation probably won't be the case) then you will have to run this command from the osgeo4w bin folder (in my case c:\osgeo4w64\bin).

  • I guess it should be GTiff instead of GTif, should not it? (on edit version May 23'17, accessed June 15'18) – Santosa Sandy Jun 15 at 11:45
  • Indeed. On this point it is worth noting a small change since I gave this answer originally and that is that GTiff is no longer the default here and GDAL will guess the format based on the extension. – MappaGnosis Jun 19 at 7:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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