1

Can I get India map extent in one click from OSM tile on canvas using PyQgis?

This is my code snippet.

canvas = iface.mapCanvas()
urlWithParams = 'type=xyz&url=https://a.tile.openstreetmap.org/{z}/{x}/{y}.png&crs=EPSG3857'
rlayer = QgsRasterLayer(urlWithParams, 'MapMyIndia', 'wms')
QgsProject.instance().addMapLayer(rlayer)
rect = QgsRectangle(7589389.42, 889589.56, 10842803.55, 4231219.37)
canvas.setExtent(rect)
canvas.update()
2
  • 1
    where are you stuck with the code snippet?
    – nmtoken
    Commented Aug 6, 2019 at 11:07
  • sir i need to plot india map on a single button click , so this code is written in a function which get triggered on click of that button. Commented Aug 6, 2019 at 11:20

1 Answer 1

2

You could use the following example which puts your code in a function and then a button is created on a toolbar which is connected to the function. You can test this using the Python Console:

def loadMap():
    if not 'MapMyIndia' in [layer.name() for layer in QgsProject.instance().mapLayers().values()]:
        urlWithParams = 'type=xyz&url=https://a.tile.openstreetmap.org/{z}/{x}/{y}.png&crs=EPSG3857'
        rlayer = QgsRasterLayer(urlWithParams, 'MapMyIndia', 'wms')
        QgsProject.instance().addMapLayer(rlayer)
    rlayer = QgsProject.instance().mapLayersByName('MapMyIndia')[0]
    canvas = iface.mapCanvas()
    rect = QgsRectangle(7589389.42, 889589.56, 10842803.55, 4231219.37)
    canvas.setExtent(rect)
    canvas.update()
    rlayer.triggerRepaint()

action = QAction(QIcon(""), "Load India", iface.mainWindow())
iface.addToolBarIcon(action)
action.triggered.connect(loadMap)
# Or to connect to a button on your plugin
#self.dlg.pushButton.clicked.connect(self.loadMap)
17
  • actually sir i need my map on canvas when i click my plugin button Commented Aug 6, 2019 at 13:12
  • Include self when you insert this function into your plugin script (i.e. def loadMap(self):). Then instead of using action = QAction..., connect it to your button using something like: self.dlg.pushButton.clicked.connect(self.loadMap)
    – Joseph
    Commented Aug 6, 2019 at 13:26
  • Sir i had done this but unable to plot in one click i need to click that two time and two layer get added and second time it appears on canvas. Commented Aug 6, 2019 at 13:34
  • 1
    Did you use PluginBuilder? If so, where did you put the line self.dlg.pushButton.clicked.connect(self.loadMap)? If it's under the def run() function then that would explain why you would need to click two times. Instead you should put it in the initGui() function so that the button is connected when the plugin is loaded.
    – Joseph
    Commented Aug 6, 2019 at 13:44
  • 1
    @abhinavgaurav - Unfortunately I cannot help you further. I tested the above code on QGIS 3.8. If you do not have this then I would suggest you download the latest version of QGIS and test the code.
    – Joseph
    Commented Aug 7, 2019 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.