Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is it possible to set the project CRS of an project in QGIS via a Python command?

In my case the user should open QGIS, start my written plugin and this plugin will load spatial data which has the epsg code 31467 (Gauss Krueger Zone 3). Unfortunately QGIS default CRS is WGS84 an so I get into trouble with the units and the scale.

My first approach was to use this code:

    my_crs = core.QgsCoordinateReferenceSystem(31467, core.QgsCoordinateReferenceSystem.EpsgCrsId)
    self.iface.mapCanvas().mapRenderer().setDestinationCrs(my_crs)

This changes (after the data is already visualized) the CRS to Gauss Krueger Zone 3. At least the espg code in the lower right corner has changed. The units are still in degrees and the scale is totally wrong. 1:5000 in Gauss Krüger is 1:528822376 in WGS 84 (in my case in QGIS). After setting the project CRS manually in the properties the project crs is really GK3 and the scale and the units are right.

So my question is, can I set the project CRS in another way than I have done it? I want to avoid the manual way.


I solved the problem by adding the following code:

qgis.utils.iface.mapCanvas().setMapUnits(0)
qgis.utils.iface.mapCanvas().refresh()

This sets the units to meter and the scale is updated automatically. The 0 stands for meters, 1 is for foot, 2 is for degree and 3 for unknown.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

Yes, its possible. The Openlayers plugin by Sourcepole automatically sets CRS to EPSG:3857.

Have a look at openlayers_layers.py and openlayers_plugin.py.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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