当前位置: 首页 > backend >正文

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

下载地址: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 GDBgdb_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 GDBsfc_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 pathoutput_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 operationprint("  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 encounteredif os.path.normpath(root) == os.path.normpath(output_folder):continue# Process each filefor file in files:if file.endswith(".shp"):shp_path = os.path.join(root, file)shp_name = fileshp_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 nameif len(shp_paths) <= 1:print("Skipping ",shp_name," - only one instance found")continueoutput_path = os.path.join(output_folder, shp_name)print("Merging ",len(shp_paths)," instances of ",shp_name,"...")try:# Execute merge operationarcpy.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中

http://www.xdnf.cn/news/15147.html

相关文章:

  • JAVA JVM垃圾收集
  • JavaScript 异步编程指南:async/await 与 Promise 该怎么选?
  • 中国银联豪掷1亿采购海光C86架构服务器
  • 第十五章 STL(stack、queue、list、set、map容器使用)
  • 基于Selenium和FFmpeg的全平台短视频自动化发布系统
  • Linux小白学习基础内容
  • 反向传播notes
  • 重新配置电脑中的环境变量
  • NFS文件存储及部署论坛(小白的“升级打怪”成长之路)
  • GO启动一个视频下载接口 前端可以边下边放
  • 计算机网络实验——以太网安全实验
  • 图解LeetCode:79递归实现单词搜索
  • 【个人笔记】负载均衡
  • 04-ES6
  • 自动驾驶数据集综述:统计特征、标注质量与未来展望
  • AI+低代码双引擎驱动:重构智能业务系统的产品逻辑
  • 《PyQt6-3D应用开发技术文档》
  • MySQL常用命令
  • STM32F103C8T6驱动无源蜂鸣器详解:从硬件设计到音乐播放
  • hive小文件问题
  • requestIdleCallback:解锁浏览器空闲时段的性能优化艺术
  • MatrixOne Intelligence v3.3 正式发布:结构化、自动化、可视化三重进化
  • 二分查找篇——寻找旋转排序数组中的最小值【LeetCode】
  • Spring Boot项目中大文件上传的优化策略与实践
  • C++的类中的虚拟继承【底层剖析(配图解)】
  • Android 13----在framworks层映射一个物理按键
  • SQL的初步学习(一)(以MySQL为例)
  • wpf使用webview2显示网页内容(最低兼容.net framework4.5.2)
  • 相机:以鼠标点为中心缩放(使用OpenGL+QT开发三维CAD)
  • 关于在html页面利用js操作liMarquee以及解决使用过程中出现的问题,附全屏切换相关代码