数据汇总代码

记录一下汇总代码,方便以后用:

import arcpy
from arcpy import env
from arcpy.sa import *
import os
from netCDF4 import Dataset
import csv
import math
import re
import arcpy.da
import pandas as pd
import time as ti

def get_path(filenames_in):
    path_list = []
    for root, dirs, files in os.walk(filenames_in):
        for file in files:
            # print(dir)
            if file[-3:] == "shp":
                filePath = os.path.join(filenames_in, file)
                # print(pathDir)
                path_list.append(filePath)
    return path_list

def CsvWriter(data, name, cloums):
    with open(name + ".csv", 'a', newline='') as fp:
        writer = csv.writer(fp, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(data)

def shpToCsv(path, fields, Csvname):
    path = str(path)
    data = pd.DataFrame()
    data["Lat"] = 0
    data["Lon"] = 0
    data["pre"] = 0
    with arcpy.da.SearchCursor(path, fields) as cursor:
        for row in cursor:
            data.loc[row[2],"Lat"] = float(row[0][0])
            data.loc[row[2],"Lon"] = float(row[0][1])
            data.loc[row[2],"pre"] = float(row[1])
        print(path + 'have been writed')

def addcolumn(column,shpPath,csvPath,fields,):
    data = pd.read_csv(csvPath)
    data[f"{column}"] = 0
    #env.workspace = workpath
    path = str(shpPath)
    print(path)
    with arcpy.da.SearchCursor(path,fields) as cursor:
        for row in cursor:
            data.loc[row[0],f"{column}"] = float(row[1])
        data.to_csv(csvPath, mode='w', index=False)

def getName(path):
    name = path.split('\\')[-1]
    timelist = re.findall("\d+", name)
    time = f'{timelist[0]}y{timelist[1]}m.shp'
    return time
def pathTranslation(type,FVCpath):
    outpathlist = []
    for path in FVCpath:
        time = re.findall("\d+",path)[0]
        year = time[:4]
        month = time[-2:]
        if month[0]== "0":
            month = month[1]
        name = f"{year}y{month}m.shp"
        outpath = f"D:\datasum\pointShp\{type}\{name}"
        outpathlist.append(outpath)
    return outpathlist
DEMpath = r"D:\datasum\DEM\\dem.shp"
FVCpath = r"D:\datasum\pointShp\FVC"
prePath = r"D:\datasum\pointShp\pre"
tmpPath = r"D:\datasum\pointShp\tmp"
ssdPath = r"D:\dataset\SSDshp"
sumCsvPath = "D:\\datasum\\csvData\\"
csvPath = "D:\datasum\csvData"
poxiang = "D:\datasum\DEM\\1aspect.shp"
podu = "D:\datasum\DEM\\200006slope.shp"
fileds1 = ["grid_code"]
fields2 = ['SHAPE@XY', "RASTERVALU","pointid"]
fields3 = ["pointid","RASTERVALU"]

# 此代码段是处理汇总数据,将各shp文件转换成CSV
FVCpathList = get_path(FVCpath)
prePathList = pathTranslation("pre",FVCpathList)
tmpPathList = pathTranslation("tmp",FVCpathList)
ndviPath = r"D:\datasum\pointShp\ndvi"
rootZip = zip(FVCpathList,prePathList,tmpPathList)
geodata = pd.read_csv("200006.csv")
demSeries = geodata["DEM"]
aspectSeries = geodata["aspect"]
slopeSeries = geodata["slope"]
for FVC,pre,tmp in rootZip:
    csvName =sumCsvPath + FVC.split("\\")[-1][:-4]+".csv"
    if not os.path.exists(csvName):
        time = FVC.split("\\")[-1][-10:-4]
        ssdShpPath = f"{ssdPath}\\{time[:4]}y{time[4:8]}m.shp"
        ndviPath = f"{ndviPath}\\ndvi{time}.shp"
        t0 = ti.time()
        shpToCsv(pre,fields2,csvName)
        addcolumn("tmp",tmp,csvName,fields3,)
        addcolumn("SSD",ssdShpPath,csvName,fields3,)
        data = pd.read_csv(csvName)
        data["DEM"] = demSeries
        data["aspect"] = aspectSeries
        data["slope"] = slopeSeries
        data.to_csv(csvName)
        addcolumn("ndvi", ndviPath, csvName, fields3)
        addcolumn("FVC", FVC, csvName, fields3)
        print(csvName,"have done")
        print(ti.time() - t0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值