2
rasterlayer = QgsRasterLayer(self.infile, layername)
raster_crs = rasterlayer.dataProvider().crs()

if raster_crs is None:  
     self.rasterlayer.setCrs(QgsCoordinateReferenceSystem(4326, 
 QgsCoordinateReferenceSystem.EpsgCrsId))

how to avoid the pop-up prompt interface for projection, the above code is useless, what should i do?

enter image description here

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
magicmelon
  • 67
  • 4

1 Answers1

0

The dialog comes from the creation of a new raster layer with QgsRasterLayer when the source has no CRS - the rest of your code is irrelevant.

To fix this, you can set an option in QGIS' settings to use a default CRS. Save the current value so we can set it back afterwards so the users' setting isn't changed:

s = QSettings()
oldValidation = s.value( "/Projections/defaultBehavior" )
s.setValue( "/Projections/defaultBehavior", "useGlobal" )

then create the raster layer. I'm using an image file which has no coordinate info - if I used a GeoTIFF with a CRS I wouldn't ever get the popup dialog anyway:

path="coords2.png"
rlayer = QgsRasterLayer(path, "image")
rlayer.isValid()
# True

I see no popups! There's a warning that the CRS was undefined.

Then reset the setting once you're done. If you want to guarantee this happens even if your code stops with an error then you need to use a try clause and put this in the finally part:

s.setValue( "/Projections/defaultBehavior", oldValidation )

Most of this can be found here:

How can I specify the CRS of a raster layer in pyqgis?

but note that the spelling of defaultBehavior there is the English spelling, defaultBehaviour, whereas in QGIS 3 it seems to be the American spelling without the u.

Spacedman
  • 63,755
  • 5
  • 81
  • 115