(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(\"# 计算“志远县 [2024-06-30 01:03:59 | AI写代码神器 | 2048点数解答]