arcpy批量合并融合矢量数据

数据下载

全国地理信息资源目录服务系统。
全国1:100万公众版基础地理信息数据(2021)覆盖全国陆地范围和包括台湾岛、海南岛、钓鱼岛、南海诸岛在内的主要岛屿及其临近海域,共77幅1:100万图幅,该数据集整体现势性为2019年。数据采用2000国家大地坐标系,1985国家高程基准,经纬度坐标。

为满足广大社会群众对地理信息数据资源的需求,经自然资源部授权,全国地理信息资源目录服务系统提供全国1:100万全图层要素免费下载的服务。下载数据采用1:100万标准图幅分发,内容包括水系、居民地及设施、交通、管线、境界与政区、地貌与土质、植被、地名及注记9个数据集,且保存要素间空间关系和相关属性信息。
arcpy批量合并融合矢量数据_第1张图片
下载完数据后,将所以有个人空间地理数据库解压至同一目录下。
arcpy批量合并融合矢量数据_第2张图片

批量合并融合代码

import os
import sys
import arcpy
# 源数据存储位置
arcpy.env.workspace = "//192.168.1.1/Data/Raw/China/shapefile/china_100w_2021_test"
# list all file geodatabases in the current workspace
# 获取workspace下所有 .gdb
workspaces = arcpy.ListWorkspaces("*","FileGDB")
# 合并后的文件输出位置
output_merged = "//192.168.1.1/Data/Raw/China/shapefile/china_100w_2021_test_merge"
# 融合后的文件输出位置
output_dissolved = "//192.168.1.1/Data/Raw/China/shapefile/china_100w_2021_test_dissolve"
# 创建目录
if not os.path.exists(output_merged):
	os.makedirs(output_merged)
if not os.path.exists(output_dissolved):
	os.makedirs(output_dissolved)
# step1:merge
for workspace in workspaces:
	print(workspace)
	arcpy.env.workspace = workspace
	featureClasses = arcpy.ListFeatureClasses()
	for fc in featureClasses:
		# 为避免程序闪退,建议每次只处理某一数据集中的数据,如 境界与政区(B)
		if(str(fc).startswith( 'B' ,0,1)):
			if not os.path.exists(output_merged+"/"+fc+".shp"):
				print("copying: " + workspace + "/" + fc)
				outFeatureClass = os.path.join(output_merged, fc.strip(".shp"))
				print("copying to: " + str(outFeatureClass))
				arcpy.CopyFeatures_management(fc, outFeatureClass)
				print("copied successfully: " + outFeatureClass)
			else:
				print("processing: " + workspace + "/" + fc)
				# arcpy.Append_management(workspace+"/"+fc, output_merged+"/"+fc,"NO_TEST")
				arcpy.Append_management(workspace+"/"+fc+".shp", output_merged+"/"+fc+".shp","NO_TEST")
				print("processed successfully:" + workspace + "/" + fc)
# step2:dissolve
arcpy.env.workspace = output_merged
featureClasses = arcpy.ListFeatureClasses()
for fc in featureClasses:
	print("dissolving: " + output_merged + "/" + fc)
	outFeatureClass = os.path.join(output_dissolved, fc.strip(".shp")+"_dissolve")
	# 规定融合的字段,默认不对FID/Shape/SHAPE_Leng/SHAPE_Area等系统字段进行融合
	fields = arcpy.ListFields(fc)
	dissolveFields = []
	# Iterate through the list of fields
	for field in fields:
		if(field.name == 'FID'):
			continue
		if(field.name == 'Shape'):
			continue
		if(field.name == 'SHAPE_Leng'):
			continue
		if(field.name == 'SHAPE_Area'):
			continue
		dissolveFields.append(str(field.name))
	print "dissolveFields : ", dissolveFields
	# 合并为Point/Polyline/Polygon
	# arcpy.Dissolve_management (fc, outFeatureClass,dissolveFields,"","SINGLE_PART", "DISSOLVE_LINES")
	# 合并为MultiPoint/MultiPolyline/MultiPolygon
	arcpy.Dissolve_management (fc, outFeatureClass,dissolveFields,"","MULTI_PART", "DISSOLVE_LINES")
	print("dissolved successfully: " + outFeatureClass)

arcpy批量合并融合矢量数据_第3张图片

注1:融合Dissolve后的文件仍存在少量拓扑检查错误,需自行解决。
注2:行政边界已被简化,不建议用作土地确权、林地确权等用途。
注3:默认不对系统字段进行融合Dissolve,融合结果或许会不符合预期,建议根据表字段修改注释中的方法。

你可能感兴趣的:(ArcGIS,Arcpy,行政区划,python)