1

I am trying to create a memory layer:

tempLayer = QgsVectorLayer('LineString', 'my_temp_layer', 'memory')

But as soon as this line is excecuted (not when I add it to the map!) the CRS dialog pops up. How do I intercept that?

I know I can set the CRS, but that does not help, since the dialog pops up when the first line is executed...

tempLayer = QgsVectorLayer('LineString', 'trassen_tmp_bfp', 'memory')
tempLayer.setCrs(QgsCoordinateReferenceSystem(4326))
BritishSteel
  • 6,637
  • 4
  • 38
  • 64
  • 1
    Does it work if you specify the CRS while creating the memory layer? : https://gis.stackexchange.com/a/73981/67477 – Thomas B Jul 04 '18 at 14:54
  • I create a layer like this: layer = QgsVectorLayer("Polygon?crs=EPSG:4326&index=yes","isoLayer","memory") ... Same problem occurs here as well. – Riccardo Jan 23 '19 at 13:29

1 Answers1

3

You have to include the crs in the path argument of the QgsVectorLayer constructor

tempLayer = QgsVectorLayer('LineString?crs=EPSG:4326', 'my_temp_layer', 'memory')

Alternatively, you can also set up QGis so that newly added layers use the project's CRS, or a default CRS (in Settings/Options/CRS/Crs for new layers)

QGIS Options dialog

You can also set those properties via the pyQGIS API

QSettings().setValue('/Projections/defaultBehavior', 'useProject')  # Use project's crs

or

QSettings().setValue('/Projections/defaultBehavior', 'useGlobal')  # Use default crs
QSettings().setValue('Projections/layerDefaultCrs', 'EPSG:4326')

Note that the default value for '/Projections/defaultBehavior' is 'prompt'