1

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?

gallay
  • 361
  • 3
  • 15

1 Answers1

3
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:

http://qgis.org/api/classQgsGeometry.html

Regards

ramiroaznar
  • 4,419
  • 2
  • 14
  • 29
Luigi Pirelli
  • 2,013
  • 14
  • 19
  • 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 http://gis.stackexchange.com/questions/66767/pyqgis-crs-on-the-fly-reprojetion) 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 08:00
  • import processing

    processing.runalg("",... 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()
    2. look for your command parameters with: processing.alghelp()
    – Luigi Pirelli Sep 26 '14 at 08:12