这个python工具箱中的语法错误在哪里?

758
2
跳到解决方案
06-24-2021 12:58 PM
MattWilkie1
通过
临时贡献者II

当我运行这个工具箱中的单个工具时,ArcGIS Pro告诉我有一个语法错误。相反的是文件页错误00989表示它不报告错误是什么或在哪里。我无法在视觉上找到它,然后输入python -m compileall ImageRepo。Pyt说没问题。你们能看出哪里不对吗?

我使用的是ArcGIS Pro v2.8.1。

工具箱:https://gist.github.com/maphew/0bae6f3005ca129f2a9566e343cf37d7

mattwilkie1_0 - 1624564437194. - png

GdalToCog
=====================
参数

输入栅格Finlayson124_SP6_13Sep2017_150cm_pro_nd.tif
输出光栅D:\work\ImgRepo\ 20121-06-10 \ pro_pyt_to_co_tif
=====================
消息

Python语法错误:在脚本T:\ENV.558\ImageRepo.pyt中

标签(3)
0荣誉
1解决方案

接受的解决方案
Tim_McGinnes
通过
临时贡献者III

当我指定存储在磁盘上的输入tif文件时,它在Pro 2.8.1中工作正常。我可以使它错误时,选择从我的地图tif文件,但错误是不同的(没有这样的文件或目录)。我认为GDAL这样运行是无法解析路径的。在您的参数中,输入文件没有路径-也许可以尝试传递完整的路径和文件名。它在其他输入文件上出错吗?

另一种可能是在创建脚本的本地副本时出现了问题,或者可能是Python环境出了问题?您是否使用默认环境?

如此:

# -*- coding: utf-8 -*- import arcpy from osgeo import gdal gdal. useexceptions () class Toolbox(object): def __init__(self): """定义工具箱(工具箱的名称是.pyt文件的名称)。"”“自我。label = "工具箱" self。alias = "toolbox" #与此工具箱关联的工具类列表。tools = [Tool] class Tool(object): def __init__(self): """Gdal to Cloud Geotiff translator."”“自我。label = "GdalToCog" self.description = "将栅格压缩为云优化Geotiff,使用最适合我们在环境育空图像的选项" self。canRunInBackground = False def getParameterInfo(self): """定义参数定义""" params = [] params += [arcpy。参数(displayName ="Input Raster", name =" in_raster", datatype="GPRasterLayer", parameterType="Required", direction="Input")] params += [arcpy. net]。参数(displayName="Output Raster", name="out_raster", datatype="DERasterDataset", parameterType="Required", direction="Output")] return params def islicense (self): """设置工具是否被授权执行。"def updateParameters(self, parameters): """在执行内部验证之前修改参数的值和属性。这个方法在参数被改变时被调用。return def updateMessages(self, parameters):修改由每个工具参数的内部验证创建的消息。该方法在内部验证后调用。”"" return def execute(self, parameters, messages): """Source code (adapted from 'gdal-to-cog.py')""" infile = parameters[0].valueAsText outfile = parameters[1].valueAsText gdal.SetConfigOption('GDAL_CACHEMAX','30%') options = ["COMPRESS=ZSTD", "PREDICTOR=YES", "LEVEL=17", "BIGTIFF=YES", "NUM_THREADS=ALL_CPUS",] def progress_cb(complete, message, cb_data): '''Emit progress report in numbers for 10% intervals and dots for 3%''' if int(complete*100) % 10 == 0: print(f'{complete*100:.0f}', end='', flush=True) elif int(complete*100) % 3 == 0: print(f'{cb_data}', end='', flush=True) arcpy.AddMessage(f'Translating {infile} to {outfile}') gdal.Translate(outfile, infile, creationOptions=options, format="COG", callback=progress_cb, callback_data='.' ) return

在原帖子中查看解决方案

2回答
Tim_McGinnes
通过
临时贡献者III

当我指定存储在磁盘上的输入tif文件时,它在Pro 2.8.1中工作正常。我可以使它错误时,选择从我的地图tif文件,但错误是不同的(没有这样的文件或目录)。我认为GDAL这样运行是无法解析路径的。在您的参数中,输入文件没有路径-也许可以尝试传递完整的路径和文件名。它在其他输入文件上出错吗?

另一种可能是在创建脚本的本地副本时出现了问题,或者可能是Python环境出了问题?您是否使用默认环境?

如此:

# -*- coding: utf-8 -*- import arcpy from osgeo import gdal gdal. useexceptions () class Toolbox(object): def __init__(self): """定义工具箱(工具箱的名称是.pyt文件的名称)。"”“自我。label = "工具箱" self。alias = "toolbox" #与此工具箱关联的工具类列表。tools = [Tool] class Tool(object): def __init__(self): """Gdal to Cloud Geotiff translator."”“自我。label = "GdalToCog" self.description = "将栅格压缩为云优化Geotiff,使用最适合我们在环境育空图像的选项" self。canRunInBackground = False def getParameterInfo(self): """定义参数定义""" params = [] params += [arcpy。参数(displayName ="Input Raster", name =" in_raster", datatype="GPRasterLayer", parameterType="Required", direction="Input")] params += [arcpy. net]。参数(displayName="Output Raster", name="out_raster", datatype="DERasterDataset", parameterType="Required", direction="Output")] return params def islicense (self): """设置工具是否被授权执行。"def updateParameters(self, parameters): """在执行内部验证之前修改参数的值和属性。这个方法在参数被改变时被调用。return def updateMessages(self, parameters):修改由每个工具参数的内部验证创建的消息。该方法在内部验证后调用。”"" return def execute(self, parameters, messages): """Source code (adapted from 'gdal-to-cog.py')""" infile = parameters[0].valueAsText outfile = parameters[1].valueAsText gdal.SetConfigOption('GDAL_CACHEMAX','30%') options = ["COMPRESS=ZSTD", "PREDICTOR=YES", "LEVEL=17", "BIGTIFF=YES", "NUM_THREADS=ALL_CPUS",] def progress_cb(complete, message, cb_data): '''Emit progress report in numbers for 10% intervals and dots for 3%''' if int(complete*100) % 10 == 0: print(f'{complete*100:.0f}', end='', flush=True) elif int(complete*100) % 3 == 0: print(f'{cb_data}', end='', flush=True) arcpy.AddMessage(f'Translating {infile} to {outfile}') gdal.Translate(outfile, infile, creationOptions=options, format="COG", callback=progress_cb, callback_data='.' ) return

MattWilkie1
通过
临时贡献者II

有趣。在我的机器上,我需要a)使用完整路径而不是映射层,b)禁用“回调”选项。我尝试了默认的Pro python和克隆env(使用File >> python >> Manage创建)。

我认为我的安装可能有些不对劲,因为一个测试python工具箱里面什么都没有,但是演示设置进度器运行时间超过4分钟,但实际的工具执行部分仅为18秒。

0荣誉