This question already has an answer here:

In a python script of qgis I load layers from different datasources using the QgsVectorLayer() and the QgsRasterLayer() commands. The coordinate reference systems of the data sources differs. Since I have for some sources only the permission to read, I cannot transfrom the CRS of the datasource. Thus, i have to change the CRS from old CRS into new CRS in the in memory layer. What is the way to do it, e.g. to get an extent() in the new coordinates?

marked as duplicate by jbchurchill, whyzar, lynxlynxlynx, Mapperz Aug 28 '17 at 15:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

sourceCrs = QgsCoordinateReferenceSystem(...source crs code...)

destCrs = QgsCoordinateReferenceSystem(...dest crs code...)

xform = QgsCoordinateTransform(sourceCrs, destCrs) #you can also do reverse convertion

if geom.transform(xform):
   ...your code...

where geom is the QgsGeometry you want to transform... in your case a geometry created from the extent coordinate using static method of QgsGeometry:



  • ok, i understand how to convert a single geometry. Is this also the approach to convert a complete layer, i.e. to iterate over each geometry and transform it as you described? I am looking for doing in python something similar as the "on-the-fly" reprojection process in the qgis qui ... – gallay Sep 24 '14 at 20:45
  • i just fount that "ont-the-fly" reprojection can be done with setProjectionsEnabled(True) (see gis.stackexchange.com/questions/66767/…) and setCrsTransformEnabled in qgis 2.4. – gallay Sep 25 '14 at 21:14
  • to use less code probably is better to use some command interface by Procssing toolbox. All staffs you have in Processing can be accessed using python with: – Luigi Pirelli Sep 26 '14 at 8:00
  • import processing processing.runalg("<alg name>",... parameters of the command) I whould use gdal_translate to convert geometries and to get all processign parameters: 1) look for your command with: processing.alglist(<patter that can match your command>) 2) look for your command parameters with: processing.alghelp(<your command>) – Luigi Pirelli Sep 26 '14 at 8:12

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