Python 实现读取 shp 图层数据:统计志远县图斑个数与罗丰县面积并保存结果及代码
(1)读取name字段中“志远县”图斑的个数: ```python import geopandas as gpd # 读取图层数据 township = gpd.read_file("data1/township.shp") # 计算“志远县”图斑的个数 zhiyuan_count = township[township["name"] == "志远县"].shape[0] zhiyuan_count ``` (2)读取area字段(面积),并计算“罗丰县”的面积之和: ```python # 计算“罗丰县”的面积之和 luofeng_area_sum = township[township["name"] == "罗丰县"]["area"].sum() luofeng_area_sum ``` (3)将运行结果保存到目录\out_data中: ```python import os # 设置结果文件存放目录 result_dir = "out_data" if not os.path.exists(result_dir): os.makedirs(result_dir) # 保存结果到文件 with open(os.path.join(result_dir, "result.txt"), "w") as f: f.write(f"志远县图斑个数:{zhiyuan_count} ") f.write(f"罗丰县面积之和:{luofeng_area_sum} ") ``` (4)将源代码文件保存到目录\out_python中: ```python # 设置源代码文件存放目录 code_dir = "out_python" if not os.path.exists(code_dir): os.makedirs(code_dir) # 保存源代码到文件 with open(os.path.join(code_dir, "task.py"), "w") as f: f.write("import geopandas as gpd ") f.write(" ") f.write("# 读取图层数据 ") f.write("township = gpd.read_file(\"data1/township.shp\") ") f.write(" ") f.write("# 计算“志远县”图斑的个数 ") f.write("zhiyuan_count = township[township[\"name\"] == \"志远县\"].shape[0] ") f.write("zhiyuan_count ") f.write(" ") f.write("# 计算“罗丰县”的面积之和 ") f.write("luofeng_area_sum = township[township[\"name\"] == \"罗丰县\"][\"area\"].sum() ") f.write("luofeng_area_sum ") f.write(" ") f.write("import os ") f.write(" ") f.write("# 设置结果文件存放目录 ") f.write("result_dir = \"out_data\" ") f.write("if not os.path.exists(result_dir): ") f.write(" os.makedirs(result_dir) ") f.write(" ") f.write("# 保存结果到文件 ") f.write("with open(os.path.join(result_dir, \"result.txt\"), \"w\") as f: ") f.write(" f.write(f\"志远县图斑个数:{zhiyuan_count} \") ") f.write(" f.write(f\"罗丰县面积之和:{luofeng_area_sum} \") ") f.write(" ") f.write("# 设置源代码文件存放目录 ") f.write("code_dir = \"out_python\" ") f.write("if not os.path.exists(code_dir): ") f.write(" os.makedirs(code_dir) ") f.write(" ") f.write("# 保存源代码到文件 ") f.write("with open(os.path.join(code_dir, \"task.py\"), \"w\") as f: ") f.write(" f.write(\"import geopandas as gpd \") ") f.write(" f.write(\" \") ") f.write(" f.write(\"# 读取图层数据 \") ") f.write(" f.write(\"township = gpd.read_file(\\\"data1/township.shp\\\")\ \") ") f.write(" f.write(\" \") ") f.write(" f.write(\"# 计算“志远县”图斑的个数 \") ") f.write(" f.write(\"zhiyuan_count = township[township[\"name\"] == \\\"志远县\"].shape[0]\ \") ") f.write(" f.write(\"zhiyuan_count \") ") f.write(" f.write(\" \") ") f.write(" f.write(\"# 计算“罗丰县”的面积之和 \") ") f.write(" f.write(\"luofeng_area_sum = township[township[\"name\"] == \\\"罗丰县\"][\"area\"].sum()\ \") ") f.write(" f.write(\"luofeng_area_sum \") ") f.write(" f.write(\" \") ") f.write(" f.write(\"import os \") ") f.write(" f.write(\" \") ") f.write(" f.write(\"# 设置结果文件存放目录 \") ") f.write(" f.write(\"result_dir = \\\"out_data\\\"\ \") ") f.write(" f.write(\"if not os.path.exists(result_dir):\ \") ") f.write(" f.write(\" os.makedirs(result_dir)\ \") ") f.write(" f.write(\" \") ") f.write(" f.write(\"# 保存结果到文件\ \") ") f.write(" f.write(\"with open(os.path.join(result_dir, \\\"result.txt\\\"), \\\"w\\\") as f:\ \") ") f.write(" f.write(\" f.write(f\\\"志远县图斑个数:{zhiyuan_count} \\\")\ \") ") f.write(" f.write(\" f.write(f\\\"罗丰县面积之和:{luofeng_area_sum} \\\")\ \") ") f.write(" f.write(\" \") ") f.write(" f.write(\"# 设置源代码文件存放目录\ \") ") f.write(" f.write(\"code_dir = \\\"out_python\\\"\ \") ") f.write(" f.write(\"if not os.path.exists(code_dir):\ \") ") f.write(" f.write(\" os.makedirs(code_dir)\ \") ") f.write(" f.write(\" \") ") f.write(" f.write(\"# 保存源代码到文件\ \") ") f.write(" f.write(\"with open(os.path.join(code_dir, \\\"task.py\\\"), \\\"w\\\") as f:\ \") ") f.write(" f.write(\" f.write(\\\"import geopandas as gpd\\\")\ \") ") f.write(" f.write(\" f.write(\\\"\\\ \\\")\ \") ") f.write(" f.write(\"# 读取图层数据\ \") ") f.write(" f.write(\"township = gpd.read_file(\\\"data1/township.shp\\\")\\\")\ \") ") f.write(" f.write(\"# 计算“志远县 ######[AI写代码神器 | 2048点数解答 | 2024-06-30 01:03:59]
- 易语言子程序 TriggerCode 代码详解:通配符查找与结果运算(字节豆包 | 607点数解答 | 2026-02-09 11:59:34)38
- GIS软件操作:计算小学所属学区及各学区小学数量全攻略 (百度文心 | 767点数解答 | 2024-06-30 05:27:42)183
- 用 Python 处理学区与小学图层:计算小学所属学区并统计各学区小学数量(GPT | 642点数解答 | 2024-06-30 05:27:56)182
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)465
- 星际编码大赛终极对决:机械星AI与异星人激战“逆序对”统计难题(字节豆包 | 509点数解答 | 2025-04-19 17:33:00)281
- 解决Java调用Python接口中文乱码问题:设置UTF - 8编码全攻略(讯飞星火 | 160点数解答 | 2024-06-06 17:18:39)419
- Java调用Python接口中文乱码问题:字符编码统一解决方案(讯飞星火 | 344点数解答 | 2024-06-06 17:19:55)530
- 解决Java调用Python接口时中文值乱码问题:设置字符编码为UTF-8(讯飞星火 | 264点数解答 | 2024-06-06 17:27:09)394
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)346
- Java代码实现从JSON数据中提取ID,涉及“数据”与“项目 - 知识库”结构(字节豆包 | 603点数解答 | 2025-04-15 22:48:51)239
- C++ 实现:计算一排鹅与最高、最矮鹅的身高差及代码详解(字节豆包 | 549点数解答 | 2025-11-16 20:19:06)85
- 编程揭秘:计算 𝑛! 在 𝑃 进制下末尾零个数的方法与代码实现(DeepSeek | 549点数解答 | 2026-01-11 17:49:54)50