zoom_to_layer
Focus the map view on a specific layer's extent in QGIS to quickly center your analysis area.
Instructions
Zoom to the extent of a specified layer.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| layer_id | Yes |
Implementation Reference
- Core handler function that executes the zoom to layer logic by setting the layer as active and calling zoomToActiveLayer on the QGIS interface.def zoom_to_layer(self, layer_id, **kwargs): """Zoom to a layer's extent""" project = QgsProject.instance() if layer_id in project.mapLayers(): layer = project.mapLayer(layer_id) self.iface.setActiveLayer(layer) self.iface.zoomToActiveLayer() return {"zoomed_to": layer_id} else: raise Exception(f"Layer not found: {layer_id}")
- src/qgis_mcp/qgis_mcp_server.py:216-220 (registration)MCP tool registration and proxy handler that forwards the zoom_to_layer command to the QGIS socket server.def zoom_to_layer(ctx: Context, layer_id: str) -> str: """Zoom to the extent of a specified layer.""" qgis = get_qgis_connection() result = qgis.send_command("zoom_to_layer", {"layer_id": layer_id}) return json.dumps(result, indent=2)
- qgis_mcp_plugin/qgis_mcp_plugin.py:133-149 (registration)Socket server command handler registration dictionary that maps 'zoom_to_layer' command to the zoom_to_layer handler method.handlers = { "ping": self.ping, "get_qgis_info": self.get_qgis_info, "load_project": self.load_project, "get_project_info": self.get_project_info, "execute_code": self.execute_code, "add_vector_layer": self.add_vector_layer, "add_raster_layer": self.add_raster_layer, "get_layers": self.get_layers, "remove_layer": self.remove_layer, "zoom_to_layer": self.zoom_to_layer, "get_layer_features": self.get_layer_features, "execute_processing": self.execute_processing, "save_project": self.save_project, "render_map": self.render_map, "create_new_project": self.create_new_project, }
- Client-side wrapper method for sending the zoom_to_layer command over the socket.def zoom_to_layer(self, layer_id): """Zoom to a layer's extent""" return self.send_command("zoom_to_layer", {"layer_id": layer_id})