mcpField
Q
QGISMCP - QGIS Model Context Protocol Integration

QGISMCP - QGIS Model Context Protocol Integration

github.com/kicker315桌面应用783 周前更新

关于 QGISMCP - QGIS Model Context Protocol Integration

QGISMCP 通过 MCP 连接 QGIS 与 Claude AI,使 Claude 能够直接控制 QGIS。支持项目操作、图层管理、处理算法执行和 Python 代码执行。

主要功能

  1. 01双向通信连接 QGIS 和 Claude
  2. 02项目创建、加载和保存
  3. 03矢量和栅格图层管理
  4. 04处理算法执行
  5. 05任意 Python 代码执行

适用场景

  1. 01AI 辅助的 GIS 项目创建
  2. 02通过自然语言控制地理空间操作
  3. 03自动化图层处理和分析
  4. 04使用 Claude 编写 PyQGIS 脚本

MCP 相关

QGISMCP 是 MCP 服务,通过套接字连接为 Claude 客户端暴露 QGIS 功能,包括项目管理、图层操作和处理算法工具,使 Claude 能够直接与 QGIS 交互。

详细介绍

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

QGISMCP 通过模型上下文协议 (MCP) 将 QGIS 连接到 Claude AI,允许 Claude 直接与 QGIS 交互和控制。这一集成实现了提示词辅助项目创建、图层加载、代码执行等功能。

本项目基于 Siddharth Ahuja 的 BlenderMCP 项目。

功能特性

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

组件

系统包含两个主要组件:

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

安装

前置条件
  • QGIS 3.X(仅在 3.22 版本上测试过)
  • Cloud desktop
  • 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 [email protected]:jjsantos01/qgis_mcp.git
QGIS 插件

需要将文件夹 qgis_mcp_plugin 及其内容复制到你的 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 标签并搜索"QGIS MCP",然后勾选 QGIS MCP 复选框。

Claude for Desktop 集成

前往 Claude > Settings > Developer > Edit Config > claude_desktop_config.json,包含以下内容:

如果你找不到"Developers"标签或 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. 点击"Start Server" 启动服务
与 Claude 配合使用

设置好 Claude 配置文件后,QGIS 上的服务运行时,你将看到一个带有 QGIS MCP 工具的锤子图标。

Claude 工具

工具
  • 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.

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。