-- coding: utf-8 -- import arcpy import os import glob import re import datetime import…
- User
-- coding: utf-8 --
import arcpy import os import glob import re import datetime import pandas as pd from arcpy.sa import *
arcpy.CheckOutExtension('Spatial') arcpy.env.overwriteOutput = True
class BatchClip: def init(self, inws, outws, mask): self.inws = inws self.outws = outws self.mask = mask print(self.inws)
codedef clip(self): if not os.path.exists(self.outws): os.mkdir(self.outws) rasters = glob.glob(os.path.join(self.inws, "*.tif")) shps = glob.glob(os.path.join(self.mask, "*.shp")) inwsname = self.inws[-5:] for shp in shps: shp_name = os.path.basename(shp) # print(shp_name) for ras in rasters: ras_name = os.path.basename(ras) shpname = shp_name[-8:-4] rastername = ras_name[:8] nameT = inwsname + shpname+"_"+ rastername + ".tif" outname = os.path.join(self.outws, nameT) out_extract = arcpy.sa.ExtractByMask(ras, shp) out_extract.save(outname) print(" ----Batchclip down ----")class RasterReclassify: def init(self, workspace): # Set workspace self.workspace = workspace arcpy.env.workspace = self.workspace
code - Assistant
这个错误通常是由于比较不同类型的对象造成的。在这种情况下,很可能是因为
df['ID']和landid的类型不匹配。您可以尝试将
landid转换为与df['ID']相同的类型,然后再进行比较。根据您的数据,可以使用以下代码将landid转换为int类型:codelandid = int(landid)将此代码添加到
calculate_fields方法的适当位置后,重新运行程序,看看问题是否解决。 - User
Traceback (most recent call last): File "f:/GEE/wurenlongchang/3.py", line 273, in main() File "f:/GEE/wurenlongchang/3.py", line 268, in main field_calculator.calculate_fields() File "f:/GEE/wurenlongchang/3.py", line 211, in calculate_fields arcpy.CalculateField_management(shp, 'Area', crop, expression_type="python") File "C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy\arcpy\management.py", line 3647, in CalculateField raise e arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid. ERROR 000728: Field Area does not exist within table Failed to execute (CalculateField).
- Assistant
这个错误是由于计算字段操作中指定的字段
Area不存在于相关的表中导致的。在
calculate_fields方法中,您可以在计算字段之前添加一个检查,以确保字段存在。您可以使用arcpy.ListFields方法获取字段列表,并检查Area是否在列表中。您可以尝试以下代码:
pythonfield_names = [f.name for f in arcpy.ListFields(shp)] if 'Area' not in field_names: arcpy.AddField_management(shp, 'Area', 'Double') arcpy.CalculateField_management(shp, 'Area', crop, expression_type="python")将这段代码添加到
calculate_fields方法中适当的位置,然后重新运行程序,看看问题是否解决。 - User
(u'2024\u9879\u76ee\u5305\u5bb6\u5e97\u51ac\u9ea6\u5730\u738b\u52c7\u5730\u5757', u'\u68c9\u82b1')Traceback (most recent call last): File "f:/GEE/wurenlongchang/3.py", line 277, in main() File "f:/GEE/wurenlongchang/3.py", line 272, in main field_calculator.calculate_fields() File "f:/GEE/wurenlongchang/3.py", line 211, in calculate_fields arcpy.CalculateField_management(shp, 'landName', landName, expression_type="python") File "C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy\arcpy\management.py", line 3647, in CalculateField raise e arcgisscripting.ExecuteError: ERROR 000539: SyntaxError: invalid syntax (, line 1)
Failed to execute (CalculateField). - Assistant
这个错误是由于计算字段操作中使用的表达式中包含了无效的语法造成的。
根据您提供的错误信息,问题似乎出现在计算字段
landName时的表达式中。根据您的代码,错误可能是由于
landName变量的值包含了非法字符,例如特殊字符、空格或其他无效字符。在计算字段之前,您可以尝试使用re.sub函数将这些无效字符替换为空字符串。请尝试将以下代码添加到
calculate_fields方法中适当的位置:pythonimport re # 清除无效字符 landName = re.sub(r'[^\w\s]', '', landName) # 计算字段 arcpy.CalculateField_management(shp, 'landName', landName, expression_type="python")运行代码,看看问题是否解决。如果仍然存在问题,请检查
landName变量的值并确保它不包含任何无效字符。