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?
1 Answer
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
-
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 ...– gallayCommented Sep 24, 2014 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.– gallayCommented Sep 25, 2014 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: Commented Sep 26, 2014 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>) Commented Sep 26, 2014 at 8:12