基于Python的地理遥感图像批量处理----读取数据

用于读取批量结构重复的数据

  • 读取shp文件指定属性的数据到excel
  • 常用于 按年/月/日的shp文件数据

代码:

import shapefile, os, openpyxl, time

if __name__ == '__main__':
    # shp文件夹路径
    shpMapFolder = r'Z:\1992-2018shape'
    excelFileMap = r'Z:\地类面积统计 - shape.xlsx'
    
	sheep = wb[wb.sheetnames[0]]
	wb = openpyxl.load_workbook(excelFileMap)
    # 行应该一致,而列要减1
    sheepRow, sheepColumn = 3, 2-1
   
    for folderName, subFolders, subFileNames in os.walk(shpMapFolder):
        for fileName in subFileNames:
            # 如果文件命为shp
            if os.path.splitext(fileName)[1] == '.shp':
                # 创建一个空字典
                storageDictionary = {
     }
                # shp文件路径
                baseShpFileMap = fr'{folderName}\{fileName}'
                print(baseShpFileMap)
                # 创建读取shp对象
                readShpFileObject = shapefile.Reader(baseShpFileMap)
                # 查看属性
                print(readShpFileObject.fields)
                # 读取shp文件数据
                for i in range(readShpFileObject.numRecords):
                    # 查看gridCode编号,在第3位
                    # print(readShpFileObject.record(i)[2])
                    gridCode = readShpFileObject.record(i)[2]
                    # 查看面积,在第5位
                    areaSize = readShpFileObject.record(i)[4] / 1000000
                    storageDictionary[gridCode] = storageDictionary.setdefault(gridCode, 0) + areaSize
                # 统计总面积
                sumAll = 0
                for j in storageDictionary.values():
                    sumAll += j
                
                # 将数据写入excel表格里
                for key, value in storageDictionary.items():
                    sheep.cell(row=sheepRow, column=sheepColumn+key).value = value
                sheep.cell(row=sheepRow, column=10).value = sumAll
                # 用户交互
                print(sumAll, '\t', end='')
                print(storageDictionary)
                time.sleep(1)
                # 找到下一个shp文件
                sheepRow += 1
                ##
    print('End...')
    wb.save(excelFileMap)

效果:
Input
基于Python的地理遥感图像批量处理----读取数据_第1张图片

Output

基于Python的地理遥感图像批量处理----读取数据_第2张图片

你可能感兴趣的:(python,ArcGis)