1

I have a raster file (.jp2), I use pyQgis to set layer CRS (step1) and then save as a raster .tif with other CRS (step2). The result is not correct. It seems to be save as from original raster to CRS (step2) but I need the raster file save as from (step1) to CRS (step2). Could you tell me how to resolve it?

Here is my code:

def getCustom(self, htd_output, f):
    htd_105 = "+proj=tmerc +lat_0=0 +lon_0=105 +k=0.9996 +x_0=500000 +y_0=0 +ellps=WGS84 +towgs84=191.90441429,39.30318279,111.45032835,0.00928836,-0.01975479,0.00427372,0.252906278 +units=m +no_defs"
    htd_106_nb = "+proj=tmerc +lat_0=0 +lon_0=106 +k=0.9999 +x_0=500000 +y_0=0 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
    if htd_output == "UTM Zone 48":
        custom = htd_105
    elif htd_output == "VN-2000 Noi bo mui 3 KTT 106":
        custom = htd_106_nb
    res = QgsCoordinateReferenceSystem()
    res.createFromProj4(custom)
    return res
def save_file(self, type):
    layers = self.iface.legendInterface().layers()
    if type == 'tif':
        output_dir = self.dlg.lineEdit.text() + "/"  

    htd = self.dlg.comboBox.currentText()
    htd_output = self.dlg.comboBox_output.currentText()
    # create directory if it doesn't exist
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    for f in layers:
        if f.type() != 1:
            return False
        temp = self.getCustom(htd, f)
        if htd! = "System":
            f.setCrs(temp)
        if type == 'tif':
            extent = f.extent()
            width, height = f.width(), f.height()
            renderer = f.renderer()
            provider = f.dataProvider()

            pipe = QgsRasterPipe()
            pipe.set(provider.clone())
            pipe.set(renderer.clone())
            # pipe.set(f.crs())

            file_writer = QgsRasterFileWriter(output_dir + f.name() + ".tif")
            file_writer.writeRaster(pipe,
                                    width,
                                    height,
                                    extent, self.getCustom(htd_output, f))

And this's my UI in QGIS:

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.