render_map
Export the current QGIS map view to an image file with customizable dimensions for sharing or documentation purposes.
Instructions
Render the current map view to an image file with the specified dimensions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| width | No | ||
| height | No |
Implementation Reference
- src/qgis_mcp/qgis_mcp_server.py:248-253 (handler)Primary MCP tool handler for 'render_map'. Uses FastMCP decorator for registration and proxies the render command to the QGIS plugin via socket.@mcp.tool() def render_map(ctx: Context, path: str, width: int = 800, height: int = 600) -> str: """Render the current map view to an image file with the specified dimensions.""" qgis = get_qgis_connection() result = qgis.send_command("render_map", {"path": path, "width": width, "height": height}) return json.dumps(result, indent=2)
- qgis_mcp_plugin/qgis_mcp_plugin.py:133-149 (registration)QGIS plugin command dispatcher registration, mapping 'render_map' string to the 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, }
- QGIS-side handler that performs the actual map rendering using QgsMapSettings and QgsMapRendererParallelJob, saving to the specified path.def render_map(self, path, width=800, height=600, **kwargs): """Render the current map view to an image""" try: # Create map settings ms = QgsMapSettings() # Set layers to render layers = list(QgsProject.instance().mapLayers().values()) ms.setLayers(layers) # Set map canvas properties rect = self.iface.mapCanvas().extent() ms.setExtent(rect) ms.setOutputSize(QSize(width, height)) ms.setBackgroundColor(QColor(255, 255, 255)) ms.setOutputDpi(96) # Create the render render = QgsMapRendererParallelJob(ms) # Start rendering render.start() render.waitForFinished() # Get the image and save img = render.renderedImage() if img.save(path): return { "rendered": True, "path": path, "width": width, "height": height } else: raise Exception(f"Failed to save rendered image to {path}") except Exception as e: raise Exception(f"Render error: {str(e)}")