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.
Answer
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 )
No comments:
Post a Comment