6

I want to set the project CRS with Python in QGIS. If I set it to an EPSG code, it works as expected:

canvas = iface.mapCanvas()
selectedcrs = "EPSG:4326"
target_crs = QgsCoordinateReferenceSystem()
target_crs.createFromUserInput(selectedcrs)
canvas.setDestinationCrs(target_crs)

But when I want to set it to a User Defined Coordinate System, the project CRS is made empty, because its code is not in the predefined CRS list in QGIS, but it's in the User Defined Coordinate Systems list in QGIS.

canvas = iface.mapCanvas()
selectedcrs = "USER:100001"
target_crs = QgsCoordinateReferenceSystem()
target_crs.createFromUserInput(selectedcrs)
canvas.setDestinationCrs(target_crs)

Is there a way to set the project CRS to an existing User Defined Coordinate System? Or is there a way to get the definition of that existing User Defined Coordinate System with Python?

To be clear: I don't want to change the CRS of a layer. I want to change the CRS of the project.

I can select "USER:100001" and set it from the Project Properties, but I want to do that with Python.

In my full script the "USER:100001" comes from the Coordinate Reference System Selector (which also lists the User Defined Coordinate Systems) and use it with this code:

projSelector = QgsGenericProjectionSelector()
projSelector.exec_()
projSelector.selectedCrsId()
selectedcrs = projSelector.selectedAuthId()

The selectedcrs variable is stored as a setting and later used by the script I originally posted above.

5
  • Possible duplicate of How to define layer's CRS and avoid the CRS dialog in PyQGIS?
    – mgri
    Commented Feb 10, 2017 at 13:18
  • 2
    That other question is unrelated, because I don't want to set the CRS for a layer, but the CRS of the project. Commented Feb 10, 2017 at 13:34
  • 1
    Michel, you are right!
    – mgri
    Commented Feb 10, 2017 at 13:39
  • Do you know what your custom CRS is in WKT format?
    – Joseph
    Commented Feb 10, 2017 at 14:43
  • No, I don't know it in WKT. I only have it's name like "USER:100001". If I could find the definition of that custom CRS, that would help too. Commented Feb 10, 2017 at 14:53

3 Answers 3

7

In QGIS 3.0 it is very easy. For instance our EPSG is 2320:

QgsProject.instance().setCrs(QgsCoordinateReferenceSystem(2320))
6

Instead of AuthId, you could use the CrsId in this way:

from qgis.gui import QgsGenericProjectionSelector

projSelector = QgsGenericProjectionSelector()
projSelector.exec_()
crsId = projSelector.selectedCrsId()

target_crs = QgsCoordinateReferenceSystem()
target_crs.createFromId( crsId, QgsCoordinateReferenceSystem.InternalCrsId )
iface.mapCanvas().setDestinationCrs( target_crs )
0
3

This is more of a workaround but if you know what the well-known text is for your custom CRS, you could use something like the following in the Python console:

crs_wkt = 'PROJCS["OSGB_1936_British_National_Grid",GEOGCS["GCS_OSGB 1936",DATUM["OSGB_1936",SPHEROID["Airy_1830",6377563.396,299.3249646]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",49],PARAMETER["central_meridian",-2],PARAMETER["scale_factor",0.9996012717],PARAMETER["false_easting",400000],PARAMETER["false_northing",-100000],UNIT["Meter",1]]'
iface.mapCanvas().mapRenderer().setDestinationCrs(QgsCoordinateReferenceSystem(crs_wkt))

To find out what your CRS is in WKT, assign a layer with your custom CRS then run the following in the Python console:

qgis.utils.iface.activeLayer().crs().toWkt()

Or run the following tool:

Processing Toolbox > GDAL/OGR > [GDAL] > Miscellaneous > Information

It will show you some information including the Layer SRS WKT.


A one-liner method is to assign a layer with your custom CRS and use:

iface.mapCanvas().mapRenderer().setDestinationCrs(QgsCoordinateReferenceSystem(qgis.utils.iface.activeLayer().crs().toWkt()))
5
  • 1
    Since I get the CRS name (like "USER:100001") from the Coordinate Reference System Selector (which also lists the User Defined Coordinate Systems), I don't have a layer to get the CRS definition from. So I don't think your solution will help me. Do you know a way to get the CRS definition from the Coordinate Reference System Selector instead of the CRS name? Commented Feb 10, 2017 at 15:44
  • @MichelStuyts - Just to confirm, when you right-click any layer and choose Set Layer CRS, can you choose "USER:100001"?
    – Joseph
    Commented Feb 10, 2017 at 15:46
  • 1
    Yes, I can, but I don't want to set it on a layer, but set it for the project. I can select and set it from the Project Properties, but I want to do that with Python. Commented Feb 10, 2017 at 15:52
  • 1
    @MichelStuyts - What I meant was you can hardcode the WKT of your custom CRS if you know what the WKT is. In order to find out, you can assign it to a layer, read the WKT, insert it as a string into your code then use this to assign the project CRS. Then in the future, you do not need to assign it again to a layer as it's already been hardcoded.
    – Joseph
    Commented Feb 10, 2017 at 16:02
  • 1
    The script I write, will be used on different QGIS installations, so "USER:100001" will have a different definition on different installations. Hardcoding won't help me. Commented Feb 10, 2017 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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