批量合并全国地理信息资源目录服务系统下载的全国基础地理数据

下载地址:https://www.webmap.cn/commres.do?method=dataDownload

成果数据1:100万下载得到的是gdb格式的空间数据库。将所有空间数据库放到同一个文件夹下,脚本合并空间数据库内的同名图层,保存到一个新的空间数据库中。可根据自己的文件放置位置修改路径。

import os
import arcpy

# Set environment and paths
input_folder = r"D:\merge_tianditu_100"
output_gdb = r"D:\merge_tianditu_100\m.gdb"

# Ensure output GDB exists, create if not
if not arcpy.Exists(output_gdb):
    arcpy.management.CreateFileGDB(os.path.dirname(output_gdb), os.path.basename(output_gdb))

# Get all GDB file paths
gdb_list = []
for item in os.listdir(input_folder):
    if item.endswith(".gdb") and item != "m.gdb":  # Exclude output GDB
        gdb_path = os.path.join(input_folder, item)
        gdb_list.append(gdb_path)

if not gdb_list:
    print("No GDB files found!")
    exit()

# Get all layer names from the first GDB
first_gdb = gdb_list[0]
fc_names = []
arcpy.env.workspace = first_gdb
for dataset in arcpy.ListDatasets("", "Feature"):
    arcpy.env.workspace = os.path.join(first_gdb, dataset)
    for fc in arcpy.ListFeatureClasses():
        fc_names.append(os.path.join(dataset, fc))

arcpy.env.workspace = first_gdb
for fc in arcpy.ListFeatureClasses():
    fc_names.append(fc)

# For each layer name, merge all same-named layers from all GDBs
for fc_name in fc_names:
    print(fc_name)
    
    # Collect paths of same-named layers from all GDBs
    fc_paths = []
    for gdb in gdb_list:
        fc_path = os.path.join(gdb, fc_name)
        if arcpy.Exists(fc_path):
            fc_paths.append(fc_path)
    
    if not fc_paths:
        print("  No data sources found for layer ", fc_paths)
        continue
    
    # Determine output path
    output_path = os.path.join(output_gdb, fc_name)
    
    # Create necessary datasets (if layer is in a dataset)
    if os.path.dirname(fc_name):
        dataset_name = os.path.dirname(fc_name)
        dataset_path = os.path.join(output_gdb, dataset_name)
        if not arcpy.Exists(dataset_path):
            print("  Creating dataset: ", dataset_name)
            arcpy.management.CreateFeatureDataset(output_gdb, dataset_name)
    
    # Execute merge operation
    print("  Merging ",len(fc_paths)," layers to: ",output_path)
    arcpy.management.Merge(fc_paths, output_path)
    print("  Completed merging layer: ", fc_name)

print("All layers merged successfully!")

成果数据1:25万下载得到的是shp文件,将所有解压后的文件夹放到同一个文件夹下,同样遍历合并保存。

import os
import arcpy
import collections

# Set environment and paths
input_folder = r"D:\merge_tianditu_100"
output_folder = r"D:\merge_tianditu_100\shp25"

# Ensure output folder exists, create if not
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# Dictionary to store paths to shapefiles with the same name
shp_dict = collections.defaultdict(list)

# Traverse all folders and collect shapefile paths
print("Scanning for shapefiles...")
for root, dirs, files in os.walk(input_folder):
    # Skip the output folder if encountered
    if os.path.normpath(root) == os.path.normpath(output_folder):
        continue
    
    # Process each file
    for file in files:
        if file.endswith(".shp"):
            shp_path = os.path.join(root, file)
            shp_name = file
            shp_dict[shp_name].append(shp_path)

# Check if any shapefiles were found
if not shp_dict:
    print("No shapefiles found in the directories!")
    exit()

# Process each set of shapefiles
print("Found ",len(shp_dict)," unique shapefile names")
for shp_name, shp_paths in shp_dict.items():
    # Skip if only one shapefile found with this name
    if len(shp_paths) <= 1:
        print("Skipping ",shp_name," - only one instance found")
        continue
    
    output_path = os.path.join(output_folder, shp_name)
    print("Merging ",len(shp_paths)," instances of ",shp_name,"...")
    
    try:
        # Execute merge operation
        arcpy.management.Merge(shp_paths, output_path)
        print("  Successfully merged to: ",output_path)
    except arcpy.ExecuteError:
        print("  Error merging ",shp_name,": ",arcpy.GetMessages(2))
    except Exception as e:
        print("  Error merging ",shp_name,": ",str(e))

print("Merging process completed!")

对了,代码都是AI生成的,亲测可用。

提示词:

遍历D:\merge_tianditu_100文件夹里所有文件夹,将每个文件夹中的同名shp合并为一个新shp保存到D:\merge_tianditu_100\shp25中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值