(Arcgis)python geopandas库分割shp属性表特定内容,批量导出shp文件

一、两个文件。实现目标:从1个shp文件,根据属性表内容提取成200个shp文件,文件名取自txt文本内容

php 复制代码
shp文件(要素1-200.shp):打开属性表前14项相同,后200项不相同。
------------任务目标:需要输出200个shp(属性表前14项相同,循环后200项单独提取出1项)
python 复制代码
txt文本(numbered.txt)内容:
1 要素1
2 要素2
... ...(总共200行)
python 复制代码
import geopandas as gpd

# 文件路径
file_path = r"D:\要素\要素1-200.shp"
species_names_file = r"D:\numbered.txt"

# 读取要素名称
species_dict = {}
with open(species_names_file, 'r', encoding='utf-8') as file:
    for line in file:
        if line.strip():
            number, name = line.strip().split(maxsplit=1)
            species_dict[int(number)] = f"{number} {name}"  # 存储编号和名称,包括空格

# 读取shapefile
data = gpd.read_file(file_path)

# 确定几何列的名称
geometry_column = data.geometry.name

# 获取前14个基础要素的列名
base_columns = data.columns[:14].tolist()
if geometry_column not in base_columns:
    base_columns.append(geometry_column)  # 确保几何列被包括

# 循环处理每个要素
for i in range(1, 200):  # 物种编号从1到200
    species_column = f'要素{i}'  # 构建要素列名
    if species_column in data.columns:
        # 选择基础要素和当前要素的列,确保包括几何列
        selected_columns = base_columns + [species_column]
        selected_data = data[selected_columns]

        # 使用包括编号和名称的字符串作为文件名
        if i in species_dict:
            filename = species_dict[i]  # 从字典中获取包含编号和名称的字符串
            new_file_path = fr"D:\要素\{filename}.shp"
        else:
            filename = f"{i} 要素_selected"  # 如果编号不存在于字典中,使用默认名称
            new_file_path = fr"D:\要素\{filename}.shp"

        # 导出到新的shapefile,使用UTF-8编码
        selected_data.to_file(new_file_path, encoding='utf-8')
        print(f"{filename}的数据已成功导出到 {new_file_path}")

二、最后每个要素需要放在单独的文件夹中,文件夹名命名为文件名

复制代码
文件名                 文件夹名
1 要素1.shp            1 要素1
2 要素2.shp            2 要素2
......
python 复制代码
import os
import shutil

# 包含 shapefile 文件的目录
source_dir = r"D:\要素\要素1-200"

# 遍历源目录中的所有文件
for filename in os.listdir(source_dir):
    if filename.endswith(".shp"):  # 检查文件是否为 shapefile
        # 去掉文件扩展名得到基本名称
        base_name = os.path.splitext(filename)[0]

        # 使用基本名称创建新的目录路径
        new_dir = os.path.join(source_dir, base_name)

        # 如果目录不存在则创建目录
        if not os.path.exists(new_dir):
            os.makedirs(new_dir)

        # 移动 shapefile 及其关联文件
        for ext in ['.shp', '.shx', '.dbf', '.prj', '.cpg', '.sbn', '.sbx', '.qix', '.fbn', '.fbx', '.ain', '.aih', '.ixs', '.mxs', '.atx']:
            old_file = os.path.join(source_dir, f"{base_name}{ext}")
            if os.path.exists(old_file):
                shutil.move(old_file, new_dir)

        print(f"已将 {filename} 及其关联文件移动到 {new_dir}")
相关推荐
天天被压力24 分钟前
【别再到处找免费股票数据API了:官方204个接口,32篇一次讲透 #02】涨停跌停与特色股池:5类股池接口一次拉全
java·人工智能·python
Wx-bishekaifayuan25 分钟前
springboot甘肃特产服务平台50301-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
midelshuang29 分钟前
RAG 文档解析(三):bbox 实战——多栏排版与水印 PDF
python
FYKJ_201038 分钟前
springboot雅集社区养老管理系统04456-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
zhanghaha13141 小时前
Python进阶教程:35_PyQt (保姆级零基础教程)
开发语言·python·pyqt
信誓旦旦的程序猿1 小时前
【Python 量化取数指南 #09】Python 拿港股通数据:港股通成交与港股财报实测
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据api
FYKJ_20101 小时前
springboot羽毛球场地管理系统00626-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
Wx-bishekaifayuan1 小时前
django医院营收信息预测系统49414-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
Wx-bishekaifayuan1 小时前
springboot会议室预约管理系统42030-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
IT毕设梦工厂2 小时前
计算机毕业设计选题推荐:基于大数据的房地产交易数据分析与可视化|毕业设计选题|计算机毕设|选题推荐|毕设指导|项目定制|源码|高质量项目
大数据·hive·hadoop·python·数据分析·spark·课程设计