5

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 it's 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?

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

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

EDIT: 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.

Michel Stuyts
  • 2,095
  • 16
  • 16

3 Answers3

6

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

QgsProject.instance().setCrs(QgsCoordinateReferenceSystem(2320))
Mustafa Uçar
  • 1,581
  • 18
  • 29
5

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 )
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
2

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 either 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()))
Joseph
  • 75,746
  • 7
  • 171
  • 282
  • 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? – Michel Stuyts Feb 10 '17 at 15:44
  • @MichelStuyts - Just to confirm, when you right-click any layer and choose Set Layer CRS, can you choose "USER:100001"? – Joseph Feb 10 '17 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.

    – Michel Stuyts Feb 10 '17 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 Feb 10 '17 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. – Michel Stuyts Feb 10 '17 at 16:07