0

In Aligning Raster and vector layer using PyQGIS when they do not match?, someone gave a solution for creating a point memory layer to correctly align a raster image with a vector.

I am wondering how this can be implemented for a .PNG raster image?

I think the method is the same, just the commands would be different?

I have tried:

def addRasterImage1(self):
    x = self.lon0    
    y = self.lat0
    self.layers = []
    fileName = 'Desktop' 

    dir = os.path.dirname(fileName) if fileName is not None else "." #path of the saved raster image
    formats = ["*.%s" % "PNG"]  

    file = myfile_list[k_zt]  #raster image to load onto vector image
    print file
    if file:
        layer = QgsRasterLayer(file, fileName)
        print "layer",layer

        if not layer.isValid():
            return

        # add layer to the registry
        QgsMapLayerRegistry.instance().addMapLayer(layer)

        # # set extent to the extent of layer
        self.canvas.setExtent(layer.extent())                       
        self.layers.insert(0, QgsMapCanvasLayer(layer))

        print "layers number:",len(self.layers)
        self.canvas.setLayerSet(self.layers)
        self.canvas.refresh()
        self.MyWorldLayer(ext = False)

Which produces a shifted raster png file on the vector map (the vector map is self.MyWorldLayer()).

I have then also tried changing the raster layer by resampling, similar to the code Changing Raster Layer Resampling with PyQGIS?.

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.