0

I want to write plugin to clip and export TMS raster layer with selected zoom level and world-file by map extent or vector mask. Wile I learn a possible ways, I found an old question: Export raster layer in python to specific folder

#from qgis.core import *
#from qgis.gui import *
import os
#import sys

rlayer = iface.activeLayer()
print(rlayer.name())
file_name =f"C:/tmp/{rlayer.name()}.tif"
print (file_name)
file_writer = QgsRasterFileWriter(file_name)
project = QgsProject().instance()
provider = rlayer.dataProvider()
pipe = QgsRasterPipe()
provider = rlayer.dataProvider()

if not pipe.set(provider.clone()):
    print ("Cannot set pipe provider")
    
file_writer.Mode(1)

file_writer.writeRaster(
    pipe,
    provider.xSize(),
    provider.ySize(),
    provider.extent(),
    provider.crs(),
    project.transformContext())
del file_writer

This code works for simple raster layer, but doesn't works for TMS layer. Either it does not throw an exception. How do I fix the code?

2
  • 1
    Maybe it's a typo but your path in file_name contain both " and '. When I change the ' into " your code works for me.
    – JULESG
    Nov 25 at 13:49
  • Yes, you are right! file_name =f"C:/tmp/{layer.name()}.tif" Nov 25 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.