1

I am building a QGIS plugin. The users will fill some parameters from UI and the response image will be displayed on QGIS map.

There is a back-end service that serves images (PNG, WebP, JPEG) of given geographical bounding box (EPSG:4326, EPSG:3857).

How can I display a picture on map canvas with bounding box using Python?

1
  • 2
    One question per Question, please, as detailed in the Tour.
    – Vince
    Commented Mar 21, 2023 at 14:36

1 Answer 1

1

Here is a solution using gdal:translate :

# variables to modify
picture_path ="/path/to/your/picture.png" 
extent = QgsRectangle(2.05287815000000018, 44.10549260999999888, 7.19556459999999998, 46.81428704999999724)
crs = QgsCoordinateReferenceSystem.fromEpsgId(4326)

# add picture to QGIS
picture =QgsRasterLayer(picture_path, 'picture')
# give picture the right coordinate reference system
picture.setCrs(QgsProject.instance().crs())
# use param algorithm with your extent to translate the picture
translate_param = {'INPUT': picture, 'TARGET_CRS': QgsProject.instance().crs(), 'NODATA': None,
                   'COPY_SUBDATASETS': False, 'OPTIONS': '',
                   'EXTRA': '-a_ullr '+str(extent.xMinimum())+' '+str(extent.yMaximum())+' '+str(extent.xMaximum())+' '+str(extent.yMinimum()),
                   'DATA_TYPE': 0, 'OUTPUT': 'TEMPORARY_OUTPUT'}
translate = processing.runAndLoadResults("gdal:translate",  translate_param)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.