I have a button in a QGIS (3.4.1) plugin that should: create a new project; set it's CRS to EPSG:4326'
, then import various maps.
However, when I run it for the first time after QGIS has loaded, the maps are show with their own CRS of EPSG:3857
(confirmed in the bottom right of the GUI). If I click the button a second time/run the code again, the maps are shown in the intended CRS of EPSG:4326
(again, confirmed in the bottom right).
The minimum code I can find to replicate the behavior is below:
# Create new project
iface.newProject(promptToSaveFlag=False)
project = QgsProject.instance()
# Create map tile layer (which by default uses EPSG:3857)
map_uri = 'type=xyz&url=http://a.tile.openstreetmap.org/{z}/{x}/{y}.png'
raster_layer = QgsRasterLayer(map_uri, 'openstreetmap.org', 'wms')
if raster_layer.isValid():
project.addMapLayer(raster_layer)
# Set CRS to EPSG:4326
project.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
Copy/pasting into the Python console seems to have the same results (i.e. first time after QGIS has loaded it doesn't work, second time it does).
Is this a bug in QGIS?
Can anyone propose a workaround?