2

I use pyQGIS (Python 2. with QGIS 2.18) to assign the same reference system to some raster files in .asc format (that came out without any SRS). Each .asc file has 1m pixel dimension.

I use this pyqgis script to convert the raster in GeoTIFF and assigning the EPSG:3003.

It works but output files have a final resolution of 16.2 x 15.6. How to preserve the original pixel dimension?

import os
import processing
from qgis.core import *

raster_filepath = "/home/giacomo/Desktop/Livorno/QGIS/Raster/DEM_Lidar/new/prova"
outputDir = "/home/giacomo/Desktop/Livorno/QGIS/Raster/DEM_Lidar/new/"

for i in os.listdir(raster_filepath):

 if i.endswith (".asc"):

  #name fileOut
  ii = i[:-4]
  fileOut = outputDir + "/" + ii + "_rep.tif"

  layercount = os.path.join(raster_filepath, i)
  raster_layer = QgsRasterLayer(layercount, 'raster')

  # assessing the raster dimension
  raster_extent = raster_layer.extent()
  xmin = raster_extent.xMinimum()
  xmax = raster_extent.xMaximum()
  ymin = raster_extent.yMinimum()
  ymax = raster_extent.yMaximum()

  # processing
  processing.runalg("gdalogr:translate", \
  {"INPUT":raster_layer, \
  "OUTSIZE":100, \
  "OUTSIZE_PERC":False, \
  "EXPAND":0, \
  "PROJWIN": "%f,%f,%f,%f"%(xmin,xmax,ymin,ymax), \
  "SRS":"EPSG:3003", \
  "OUTPUT":fileOut})
  print(fileOut)
3

Normally I would do this on the command line but you should be able to do it in python too.

I would simply use gdal_translate -a_srs epsg:3003 file.asc file.tif - there should be no need to specify a projwin or outsize and I suspect that is where things are going wrong, it seems likely that the raster_extent is being interpreted wrongly as you have no projection set at that point.

  • You are right. I mean: I still used the gdalogr processing from the python shell in QGIS, but just deleting the OUTSIZE and OUTSIZE_PERC from my script was sufficient to preserve the raster resolution. Thanks – ilFonta Dec 31 '18 at 13:02

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.