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

760
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\2021-06-10\pro_pyt_to_cog.tif
=====================
消息

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

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

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

当我在Pro 2.8.1中指定存储在磁盘上的输入tif文件时,它对我来说工作得很好。我可以在从我的地图中选择tif文件时出错,但错误是不同的(没有这样的文件或目录)。我认为GDAL在以这种方式运行时无法解析路径。在你的参数中,输入文件没有路径——也许可以试着给它传递完整的路径和文件名。它在其他输入文件上出错吗?

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

如此:

# -*- coding: utf-8 -*- import arcpy from osgeo import gdal gdal. useexceptions () class Toolbox(object): def __init__(self): """定义工具箱(工具箱的名称是.pyt文件的名称)"”“自我。标签= "工具箱"自我。alias = "toolbox" #与工具箱自身相关联的工具类列表。tools = [Tool] class工具(对象):def __init__(self): """Gdal to Cloud Geotiff translator."”“自我。label = "GdalToCog" self.description = "压缩光栅到云优化Geotiff,使用最适合我们的环境育空图像的选项" self。canRunInBackground = False def getParameterInfo(self): """定义参数定义""" params = [] params += [arcpy.]Parameter(displayName ="Input Raster", name =" in_raster", datatype="GPRasterLayer", parameterType="Required", direction="Input")] params += [arcpy.]Parameter(displayName="Output Raster", name="out_raster", datatype="DERasterDataset", parameterType="Required", direction="Output")] return params def isLicensed(self): ""设置工具是否允许执行""" return True 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

当我在Pro 2.8.1中指定存储在磁盘上的输入tif文件时,它对我来说工作得很好。我可以在从我的地图中选择tif文件时出错,但错误是不同的(没有这样的文件或目录)。我认为GDAL在以这种方式运行时无法解析路径。在你的参数中,输入文件没有路径——也许可以试着给它传递完整的路径和文件名。它在其他输入文件上出错吗?

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

如此:

# -*- coding: utf-8 -*- import arcpy from osgeo import gdal gdal. useexceptions () class Toolbox(object): def __init__(self): """定义工具箱(工具箱的名称是.pyt文件的名称)"”“自我。标签= "工具箱"自我。alias = "toolbox" #与工具箱自身相关联的工具类列表。tools = [Tool] class工具(对象):def __init__(self): """Gdal to Cloud Geotiff translator."”“自我。label = "GdalToCog" self.description = "压缩光栅到云优化Geotiff,使用最适合我们的环境育空图像的选项" self。canRunInBackground = False def getParameterInfo(self): """定义参数定义""" params = [] params += [arcpy.]Parameter(displayName ="Input Raster", name =" in_raster", datatype="GPRasterLayer", parameterType="Required", direction="Input")] params += [arcpy.]Parameter(displayName="Output Raster", name="out_raster", datatype="DERasterDataset", parameterType="Required", direction="Output")] return params def isLicensed(self): ""设置工具是否允许执行""" return True 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(使用文件>> python >> Manage创建)。

我想我的安装可能有问题,因为一个测试python工具箱里什么都没有,只有设置进度运行需要超过4分钟,但实际的工具执行部分只有18秒。

0荣誉