I previously asked a question about my raster image being larger and shifted over from my vector image here, but now I realize that the raster image is only shifted to the left and up from the vector map (both image and map are defined in decimal degree coordinates). The vector layer is a world shapefile and the raster image is a .png image that is overlain on the shapefile.

Below I have two examples where the raster image is shifted when uploaded on the shapefile in PyQGIS: example1 example2

The blue and green are part of the vector shapefile (blue being the ocean and green being the land). The purple, white grid, and green contours are part of the raster image. The larger red 'X' represents the set of coordinates that were defined on the vector map. The smaller red '+' represents the same set of coordinates, but on the raster image. Here is where I can tell that the raster image is shifted compared to the vector map because the red 'X' and the '+' represent the same coordinates (i.e. 75.23694W, 39.86N for the second image).

I am wondering how to fix this? Is there anyway to easily correct the raster location to the correct area?

My PyQGIS code to upload the raster image is below:

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():

        # add layer to the registry

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

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

def MyWorldLayer(self, ext=bool):
    file = mypath + "world.shp"
    layer = QgsVectorLayer(file, fileName, "ogr")
    if not layer.isValid():

    symbols = layer.rendererV2().symbols()
    symbol = symbols[0]

    if ext == True:
    if ext == False:
        rect = QgsRectangle(xmin,ymin,xmax,ymax)

    self.layers.insert(0, QgsMapCanvasLayer(layer))

Where self.MyWorldLayer(ext = False) is the vector shapefile map. Please let me know if you have any suggestions on how to fix this!


I am trying to use this solution here, but this is for a raster of points. Can this be used for a .png raster?

** Here is a link that you can access to see the .png raster and the world shapefile used as the vector layer layers link**

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.