【问题标题】:Trouble with for loop for python process [closed]python进程的for循环问题[关闭]
【发布时间】:2014-05-20 17:53:28
【问题描述】:

我最近想出了如何对栅格文件进行一些 NumPy 计算,但现在我试图在 208 个栅格上迭代运行我提出的过程,所有这些都在 1 个文件夹中。

我认为这是一项非常简单的任务,但是在查阅了许多 stackexchange 线程和教程之后,我仍然没有弄清楚。 (我对 python 很陌生。)

我已包含以下代码。我得到的错误:

invalid syntax on "def cumulativecalculation()"

提前感谢您的帮助!

import os 
import sys 
import arcpy 
import numpy as np 
import glob


arcpy.env.overwriteOutput=True

def cumulativecalculation()

    #Set geoprocessing variables
    inRaster = filename
    des = arcpy.Describe(inRaster)
    sr = des.SpatialReference
    ext = des.Extent
    ll = arcpy.Point(ext.XMin,ext.YMin)

    #Convert GeoTIFF to numpy array
    a = arcpy.RasterToNumPyArray(inRaster)

    #Flatten for calculations
    a.flatten()

    #Find unique values, and record their indices to a separate object
    a_unq, a_inv = np.unique(a, return_inverse=True)

    #Count occurences of array indices
    a_cnt = np.bincount(a_inv)

    #Cumulatively sum the unique values multiplied by the number of
    #occurences, arrange sums as initial array
    b = np.cumsum(a_unq * a_cnt)[a_inv]

    #Divide all values by 10 (reverses earlier multiplication done to
    #facilitate accurate translation of ASCII scientific notation
    #values < 1 to array)
    b /= 10

    #Rescale values between 1 and 100
    maxval = np.amax(b)
    b /= maxval
    b *= 100

    #Restore flattened array to shape of initial array
    c = b.reshape(a.shape)

    #Convert the array back to raster format
    outRaster = arcpy.NumPyArrayToRaster(c,ll,des.meanCellWidth,des.meanCellHeight)

    #Set output projection to match input
    arcpy.DefineProjection_management(outRaster, sr)

    #Setting the OutName
    OutName = "filename" + "_cumulative" + ".tif"

    #Save the raster as a TIFF
    outRaster.save("E:\\NSF Project\\Salamander_Data\\New_Cumulative_Rasters\\OutName")

src = "E:\\NSF Project\\Salamander_Data\\NoDataToZero\\HadleyGCM\\*.tif"

for filename in glob.glob(src):
    cumulativecalculation(filename)

sys.exit()

【问题讨论】:

    标签: python for-loop numpy gis arcpy


    【解决方案1】:

    您需要在 () 之后添加一个冒号。

    def cumulativecalculation():
    

    【讨论】:

    • 如果通过在某处添加颜色或其他东西来解决 OP 的问题,最好标记为 offtopic->简单的印刷错误
    • 谢谢。抱歉,我猜这个问题太简单了。
    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 2021-08-14
    • 2018-08-12
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多