Skip to main content
deleted 32 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43
from pathlib import Path
import requests
import math

service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")

image="/media/patrice/DataDrive/GORiM/ManualFineTuning/GEE/S2_S025_Z12R.tif"

Cname = Path(image).stem
Zone = Cname.split('_')[-1]

rlayer = iface.addRasterLayer(image, Cname, "gdal")
rlayer.loadNamedStyle("/media/patrice/DataDrive/GORiM/ManualFineTuning/ImageNIRStyle.qml")
rlayer.triggerRepaint()

def transform(rect):
    rlayer_crs = rlayer.crs()
    canvas_crs = QgsProject.instance().crs()
    if rlayer_crs != canvas_crs:
        xform = QgsCoordinateTransform(rlayer_crs, canvas_crs, QgsProject.instance())
        rect = xform.transform(rect)
        enlargement = math.sqrt(min(rect.width()**2 +, rect.height()**2))*0.05
    return rect.buffered(enlargement)
    
iface.mapCanvas().setExtent(transform(rlayer.extent()))
from pathlib import Path
import requests
import math

service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")

image="/media/patrice/DataDrive/GORiM/ManualFineTuning/GEE/S2_S025_Z12R.tif"

Cname = Path(image).stem
Zone = Cname.split('_')[-1]

rlayer = iface.addRasterLayer(image, Cname, "gdal")
rlayer.loadNamedStyle("/media/patrice/DataDrive/GORiM/ManualFineTuning/ImageNIRStyle.qml")
rlayer.triggerRepaint()

def transform(rect):
    rlayer_crs = rlayer.crs()
    canvas_crs = QgsProject.instance().crs()
    if rlayer_crs != canvas_crs:
        xform = QgsCoordinateTransform(rlayer_crs, canvas_crs, QgsProject.instance())
        rect = xform.transform(rect)
        enlargement = math.sqrt((rect.width()**2 + rect.height()**2))*0.05
    return rect.buffered(enlargement)
    
iface.mapCanvas().setExtent(transform(rlayer.extent()))
from pathlib import Path
import requests

service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")

image="/media/patrice/DataDrive/GORiM/ManualFineTuning/GEE/S2_S025_Z12R.tif"

Cname = Path(image).stem
Zone = Cname.split('_')[-1]

rlayer = iface.addRasterLayer(image, Cname, "gdal")
rlayer.loadNamedStyle("/media/patrice/DataDrive/GORiM/ManualFineTuning/ImageNIRStyle.qml")
rlayer.triggerRepaint()

def transform(rect):
    rlayer_crs = rlayer.crs()
    canvas_crs = QgsProject.instance().crs()
    if rlayer_crs != canvas_crs:
        xform = QgsCoordinateTransform(rlayer_crs, canvas_crs, QgsProject.instance())
        rect = xform.transform(rect)
        enlargement = min(rect.width(), rect.height())*0.05
    return rect.buffered(enlargement)
    
iface.mapCanvas().setExtent(transform(rlayer.extent()))
added 100 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43
from pathlib import Path
from PyQt5.QtCore import QTimer
import requests


def load_raster():
    image="/media/patrice/DataDrive/GORiM/ManualFineTuning/GEE/S2_S025_Z12R.tif"
    Cname = Path(image).stem
    Zone = Cname.split('_')[-1]
    rlayer = iface.addRasterLayer(image, Cname, "gdal")
    iface.setActiveLayer(rlayer)
    iface.zoomToActiveLayer()
    
def load_basemap():
    service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
    service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
    tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")
    if len(QgsProject.instance().mapLayers())<2:
        QTimer.singleShot(100, load_raster)
    else:
        load_raster()

load_basemap()
from pathlib import Path
from PyQt5.QtCore import QTimer
import requests


def load_raster():
    image="/media/patrice/DataDrive/GORiM/ManualFineTuning/GEE/S2_S025_Z12R.tif"
    Cname = Path(image).stem
    Zone = Cname.split('_')[-1]
    rlayer = iface.addRasterLayer(image, Cname, "gdal")
    iface.setActiveLayer(rlayer)
    iface.zoomToActiveLayer()
    
def load_basemap():
    service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
    service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
    tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")
    QTimer.singleShot(100, load_raster)

load_basemap()
from pathlib import Path
from PyQt5.QtCore import QTimer
import requests


def load_raster():
    image="/media/patrice/DataDrive/GORiM/ManualFineTuning/GEE/S2_S025_Z12R.tif"
    Cname = Path(image).stem
    Zone = Cname.split('_')[-1]
    rlayer = iface.addRasterLayer(image, Cname, "gdal")
    iface.setActiveLayer(rlayer)
    iface.zoomToActiveLayer()
    
def load_basemap():
    service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
    service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
    tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")
    if len(QgsProject.instance().mapLayers())<2:
        QTimer.singleShot(100, load_raster)
    else:
        load_raster()

load_basemap()
added 119 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

EDIT- After further testing: While both code snippets above worked in 3.28 on Ubuntu 22.04, I found a similar behaviour to that described by the OP in 3.22 on Windows though only when running the code in a new, empty project.

A workaround is to load the layers in two separate functions and call the second function from the first, using QTimer.singleShot to apply a small delay.

from pathlib import Path
from PyQt5.QtCore import QTimer
import requests


def load_raster():
    image="/media/patrice/DataDrive/GORiM/ManualFineTuning/GEE/S2_S025_Z12R.tif"
    Cname = Path(image).stem
    Zone = Cname.split('_')[-1]
    rlayer = iface.addRasterLayer(image, Cname, "gdal")
    iface.setActiveLayer(rlayer)
    iface.zoomToActiveLayer()
    
def load_basemap():
    service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
    service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
    tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")
    QTimer.singleShot(100, load_raster)

load_basemap()

EDIT- After further testing: While both code snippets above worked in 3.28 on Ubuntu 22.04, I found a similar behaviour to that described by the OP in 3.22 on Windows though only when running the code in a new, empty project.

A workaround is to load the layers in two separate functions and call the second function from the first, using QTimer.singleShot to apply a small delay.

from pathlib import Path
from PyQt5.QtCore import QTimer
import requests


def load_raster():
    image="/media/patrice/DataDrive/GORiM/ManualFineTuning/GEE/S2_S025_Z12R.tif"
    Cname = Path(image).stem
    Zone = Cname.split('_')[-1]
    rlayer = iface.addRasterLayer(image, Cname, "gdal")
    iface.setActiveLayer(rlayer)
    iface.zoomToActiveLayer()
    
def load_basemap():
    service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
    service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
    tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")
    QTimer.singleShot(100, load_raster)

load_basemap()
added 119 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43
Loading
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43
Loading