2

I made a plugin in QGIS using pyqgis and qt designer to plot OSM tile in which I have one button which connects me to function in which I wrote a program to plot raster layer using param and I am setting an extent but when I call function when button triggers once it gives me simple ODM world map and on clicking that button again it plots the given extent. How to handle this issue?

Here is code of function:

def maptileOSM(self):
        canvas=self.iface.mapCanvas()
        urlWithParams ='type=xyz&url=https://a.tile.openstreetmap.org/{z}/{x}/{y}.png&zmax=19&zmin=0&crs=EPSG3857'
        rlayer = QgsRasterLayer(urlWithParams, 'OSM', 'wms')
        x = float(8667633)
        y = float(3296664)
        scale=1000000
        rect = QgsRectangle(x-scale,y-scale,x+scale,y+scale)'''
        canvas.setExtent(self.rect)
        QgsProject.instance().addMapLayer(rlayer)

self.dlg.pushButton.clicked.connect(self.maptileOSM) 
  • It seems that the best way to go would be to seperate your code into two functions: 1) load the map 2) zoom to the rectangle you'd wish. Then you could easily tie each function to a separate interface in the UI, and then it works. – 15Step Aug 6 at 9:34
  • I had tried this already and same results sir. – abhinav gaurav Aug 6 at 9:39
  • The key here is not only seperating the function into two functions but also to two different buttons in the GUI. That should work, see here qgistutorials.com/tr/docs/3/building_a_python_plugin.html about tying different buttons to different functions. It worked for me. – 15Step Aug 6 at 10:03
  • sir i have to do this using one button only. it must be like this when we click that button we want India's map on canvas. – abhinav gaurav Aug 6 at 10:19

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.