tif批量提取至值

因为要将多种环境因子对齐,所以用到提取到点。

代码如下:

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值