因为要将多种环境因子对齐,所以用到提取到点。
代码如下:
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 glob
def get_path2(filenames_in):
folder = glob.glob(os.path.join(filenames_in, '*'))
pathlist = [glob.glob(os.path.join(folderFile, '*.tif'))[0] for folderFile in folder]
return pathlist
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:] == "tif":
filePath = os.path.join(filenames_in, file)
# print(pathDir)
path_list.append(filePath)
return path_list
def tifToShp(pointRoot, path, outFilie):
out_path = os.path.split(path)[1]
out_path = os.path.splitext(out_path)[0]
out_path = os.path.join(outFilie, f'{out_path}.shp')
print(out_path)
if os.path.exists(out_path):
pass
else:
nc_Ra = ExtractValuesToPoints(pointRoot, path, out_path, "NONE","ALL")
print(out_path + '已转换成shp文件')
return nc_Ra
def shpToCsv(path, fields, Csvname):
path = str(path)
time = getTime(path)
with open(Csvname + ".csv", 'a', newline='') as fp:
writer = csv.writer(fp, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
with arcpy.da.SearchCursor(path, fields) as cursor:
for row in cursor:
writer.writerow([time, row[0][0], row[0][1], row[1]])
print(path + 'have been writed')
def tifToCsv(pointRoot, rootPath, outPath, fields):
tifpathlist = get_path(rootPath)
name = outPath.split('\\')[-1]
for path in tifpathlist:
print(path)
shp = tifToShp(pointRoot, path, outPath)
print(shp)
#shpToCsv(shp, fields, name)
def getTime(path):
name = path.split('\\')[-1]
timelist = re.findall("\d+", name)
time = f'{timelist[0]}-{timelist[1]}'
return time
tmp_root = r"D:\datasum\tmp"
tmpOut = r"D:\datasum\pointShp\tmp"
pre_root = r"D:\datasum\pre"
preOut = r"D:\datasum\pointShp\pre"
ndviRoot = r"D:\datasum\\ndvi"
ndviOut = r"D:\datasum\pointShp\ndvi"
aspect = "D:\datasum\DEM\\1aspect.shp"
Fvcout = r"D:\datasum\pointShp\FVC"
fvc_root = r'D:\datasum\FVC'
dem = "D:\datasum\DEM\\slope1.shp"
for path in get_path(tmp_root):
tifToShp(dem, path, tmpOut)
for path in get_path2(fvc_root):
tifToShp(dem, path, Fvcout)
for path in get_path(pre_root):
tifToShp(dem, path, preOut)
for path in get_path(ndviRoot):
tifToShp(dem, path, Fvcout)