QGIS模型协议集成

QGISMCP通过模型上下文协议(MCP)将QGIS与Claude AI连接,实现双向通信、项目与图层操作、处理算法执行及Python代码运行,适用于地理信息系统开发与自动化任务。

  • by jjsantos01
  • Python
  • 本地部署
  • 1138 星标
  • 更新于
在 GitHub 查看 jjsantos01/qgis_mcp

使用说明

QGISMCP - QGIS模型上下文协议集成

QGISMCP 连接 QGIS(读作“Q-Eye-ess”,但通常直接称为“QGIS”) 到;向;朝 克劳德AI 通过 模型上下文协议(MCP)这使得克劳德能够直接与QGIS进行交互并控制它。这种集成使得快速创建项目、加载图层、执行代码等操作变得更为便捷。

这个项目深深植根于 BlenderMCP 由……设计的项目 西达尔特·阿胡贾

特点/功能

  • 双向沟通通过基于套接字的服务器将Claude AI连接到QGIS。
  • 项目操纵在QGIS中创建、加载和保存项目。
  • 图层操作向项目中添加或移除矢量图层或栅格图层。
  • 执行处理执行处理算法(处理工具箱)。
  • 代码执行在QGIS中通过Claude运行任意Python代码。功能非常强大,但使用此工具时也需格外谨慎。

组件

该系统由两个主要组件构成:

  1. **QGIS插件**一个QGIS插件,用于在QGIS内创建一个套接字服务器以接收并执行命令。
  2. **MCP 服务器**一个实现了模型上下文协议并连接到QGIS插件的Python服务器。

安装

先决条件

  • QGIS 3.X(仅在3.22版本上测试过)
  • Claude 桌面版
  • Python 3.10 或更高版本
  • UV包管理器:

如果你使用的是Mac,请安装uv作为

brew install uv

在 Windows PowerShell 中

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

否则,安装说明可在其网站上找到: 安装uv

⚠️ 未安装UV前请勿继续

下载代码

将此仓库下载到您的电脑。您可以使用以下命令克隆它:

git clone git@github.com:jjsantos01/qgis_mcp.git

QGIS插件

你需要复制这个文件夹 qgis_mcp_plugin(可译为“QGIS MCP 插件”) 并将其内容放到您的QGIS配置文件插件文件夹中。

你可以在QGIS中通过菜单获取你的配置文件文件夹 Settings -> User profiles -> Open active profile folder 然后,前往 Python/plugins 并粘贴该文件夹 qgis_mcp_plugin。

在Windows机器上,插件文件夹通常位于: C:\Users\USER\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins

在MacOS上: ~/Library/Application\ Support/QGIS/QGIS3/profiles/default/python/plugins

然后关闭QGIS并重新打开它。前往菜单选项 Plugins > Installing and Managing Plugins,选择 All 按Tab键并搜索“QGIS MCP”,然后勾选QGIS MCP复选框。

桌面集成版的Claude

首选 Claude > Settings > Developer > Edit Config > claude_desktop_config.json 包括以下内容:

如果你找不到“开发者选项卡”或 claude_desktop_config.json 看看这个 文档资料。

{
    "mcpServers": {
        "qgis": {
            "command": "uv",
            "args": [
                "--directory",
                "/ABSOLUTE/PATH/TO/PARENT/REPO/FOLDER/qgis_mcp/src/qgis_mcp",
                "run",
                "qgis_mcp_server.py"
            ]
        }

    }
}

使用方法

开始连接

  1. 在QGIS中,前往 plugins > QGIS MCP > QGIS MCP
  2. 点击“启动服务器”

与Claude一起使用

一旦在Claude上设置了配置文件,并且服务器在QGIS上运行,您将看到一个带有QGIS MCP工具的锤子图标。

工具

  • ping - 使用简单的ping命令检查服务器连接性
  • get_qgis_info - 获取当前安装的QGIS信息
  • load_project - 从指定路径加载QGIS项目
  • create_new_project - 创建一个新项目并保存它
  • get_project_info - 获取当前项目信息
  • add_vector_layer - 将一个矢量图层添加到项目中
  • add_raster_layer - 向项目中添加一个栅格图层
  • get_layers - 获取当前项目中的所有图层
  • remove_layer - 通过ID从项目中移除一层
  • zoom_to_layer - 缩放到指定图层的范围
  • get_layer_features - 从矢量图层中检索特征,可选限制数量
  • execute_processing - 使用给定参数执行处理算法
  • save_project - 将当前项目保存到指定路径
  • render_map - 将当前地图视图渲染为图像文件
  • execute_code - 执行作为字符串提供的任意PyQGIS代码

示例命令

这是我用来举的例子 演示:

You have access to the tools to work with QGIS. You will do the following:
	1. Ping to check the connection. If it works, continue with the following steps.
	2. Create a new project and save it at: "C:/Users/USER/GitHub/qgis_mcp/data/cdmx.qgz"
	3. Load the vector layer: ""C:/Users/USER/GitHub/qgis_mcp/data/cdmx/mgpc_2019.shp" and name it "Colonias".
	4. Load the raster layer: "C:/Users/USER/GitHub/qgis_mcp/data/09014.tif" and name it "BJ"
	5. Zoom to the "BJ" layer.
	6. Execute the centroid algorithm on the "Colonias" layer. Skip the geometry check. Save the output to "colonias_centroids.geojson".
	7. Execute code to create a choropleth map using the "POB2010" field in the "Colonias" layer. Use the quantile classification method with 5 classes and the Spectral color ramp.
	8. Render the map to "C:/Users/USER/GitHub/qgis_mcp/data/cdmx.png"
	9. Save the project.