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.