记录一下汇总代码,方便以后用:
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)