0

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()
  • 1
    where are you stuck with the code snippet? – nmtoken Aug 6 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. – abhinav gaurav Aug 6 at 11:20
1

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)
  • actually sir i need my map on canvas when i click my plugin button – abhinav gaurav Aug 6 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 Aug 6 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. – abhinav gaurav Aug 6 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 Aug 6 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 Aug 7 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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