基于ArcPy批量线转面、面合并、与点空间挂接处理
需求:现有大量楼栋文件夹,每个文件夹中有很多楼层文件夹(如1F、2F……),每个楼层文件夹中有boundary.shp、door.shp、unit_poi.shp文件,需要批量将每个楼层中的boundary.shp、door.shp转面并合并,然后与unit_poi.shp挂接,存放在当前文件夹下。如下图所示:
图 数据组织结构
方法:基于ArcPy编写批处理脚本,有缺少三个shp中任何一个文件则提示,并统计处理楼层总数。代码如下:
1 # -*- coding: utf-8 -*- 2 # Import arcpy module 3 import arcpy 4 from arcpy import env 5 # 导入os模块 6 import os 7 import shutil 8 # 待遍历的路径 英文路径 9 path = r'H:\20200717' 10 env.workspace = path 11 # 待转面的两个线要素 12 doorShp = "door.shp" 13 boundaryShp = "boundary.shp" 14 # 待空间挂接的点要素 15 unit_poiShp = "unit_poi.shp" 16 # door和bodunary合并的面要素类名称 17 door_bound_polygon = "unit_area.shp" 18 # 面和poi空间挂接后的要素 19 polygon_SpatialJoin = "unit.shp" 20 # 统计已处理文件夹数量 21 dirCount = 0 22 # 调用walk方法递归遍历path目录 24 for dir in os.listdir(path): # 列出当前目录的子目录和文件,不递归 25 Folderdir = os.path.join(path, dir) 26 try: 27 Folderdir = unicode(Folderdir, 'GB2312') 28 print(Folderdir) 29 except Exception, e: 30 print 'unicode错误信息', e 31 if os.path.isdir(Folderdir): 32 floors = os.listdir(Folderdir) # 所有文件和文件夹 33 # print(floors) 34 for floor in floors: 35 if floor.endswith(('F', 'f')): 36 floorDir = os.path.join(Folderdir, floor) 37 doorShpPath = os.path.join(floorDir, doorShp) 38 boundaryShpPath = os.path.join(floorDir, boundaryShp) 39 unit_poiShpPath = os.path.join(floorDir, unit_poiShp) 40 if os.path.exists(doorShpPath) and os.path.exists(boundaryShpPath) and os.path.exists(unit_poiShpPath): 41 # Process: 要素转面 42 outputPolygonFeatClass = os.path.join(floorDir, door_bound_polygon) 43 # 若合并面文件存在则删除 44 if os.path.exists(outputPolygonFeatClass): 45 arcpy.Delete_management(outputPolygonFeatClass) 46 arcpy.FeatureToPolygon_management([doorShpPath, boundaryShpPath], outputPolygonFeatClass, "","ATTRIBUTES", "") 47 # Process: 空间连接 48 outputJoinFeatClass = os.path.join(floorDir, polygon_SpatialJoin) 49 # 若空间挂接文件存在则删除 50 if os.path.exists(outputJoinFeatClass): 51 arcpy.Delete_management(outputJoinFeatClass) 52 arcpy.SpatialJoin_analysis(outputPolygonFeatClass, unit_poiShpPath, outputJoinFeatClass) 53 else: 54 print floorDir, '有shp文件不存在' 55 dirCount += 1 56 print '已处理文件夹数量:',dirCount
-----欢迎转载,但请于明显处标明出处:http://www.cnblogs.com/pxtgis/
如果您觉得本文确实帮助了您,可小小打赏下,谢谢
![](https://files-cdn.cnblogs.com/files/pxtgis/%E5%BE%AE%E4%BF%A12.bmp)