【发布时间】:2016-03-22 23:59:45
【问题描述】:
我是 python 脚本的新手,但一直在尝试监督一个前同事创建的脚本,以帮助我们在我的办公室自动化地图制作。我们最近从 ArcGIS 10.0 切换到 10.3,从 Python 2.6 切换到 Python 2.7。现在一些脚本不再工作。有没有人有任何提示可以帮助我开始了解导致此问题的可能原因?提前谢谢!
更新:不起作用的脚本不会产生错误。它只是达到 100%,但从未完成。似乎 100% 实际上并不是 100%,因为它停在“确定区域的最佳规模......”的位置。
##select by attributes, zoom to layer and copy scale
arcpy.AddMessage("Identifying optimal scale for tracts...")
mxd.activeView = "PAGE_LAYOUT"
arcpy.MakeFeatureLayer_management(consCopy, "ZoomLayer")
addLyr = arcpy.mapping.Layer("ZoomLayer")
arcpy.mapping.AddLayer(df, addLyr)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
urows = arcpy.UpdateCursor(consCopy)
for row in urows:
id = row.getValue("TRACT_UID")
strID = str(id)
expression = '"TRACT_UID" =' + "'" + strID + "'"
selection = arcpy.SelectLayerByAttribute_management("ZoomLayer", "NEW_SELECTION", expression)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
rScale = df.scale
if rScale >= 48000:
scale = 96000
elif rScale >= 24000:
scale = 48000
elif rScale >= 22800:
scale = 24000
elif rScale >= 21600:
scale = 22800
elif rScale >= 20400:
scale = 21600
elif rScale >= 19200:
scale = 20400
elif rScale >= 18000:
scale = 19200
elif rScale >= 16800:
scale = 18000
elif rScale >= 15600:
scale = 16800
elif rScale >= 14400:
scale = 15600
elif rScale >= 13200:
scale = 14400
elif rScale >= 12000:
scale = 13200
elif rScale >= 10800:
scale = 12000
elif rScale >= 9600:
scale = 10800
elif rScale >= 8400:
scale = 9600
elif rScale >= 7200:
scale = 8400
elif rScale >= 6000:
scale = 7200
elif rScale >= 4800:
scale = 6000
elif rScale >= 3600:
scale = 4800
elif rScale >= 2400:
scale = 3600
elif rScale >= 1500:
scale = 2400
elif rScale >= 1200:
scale = 1500
elif rScale >= 600:
scale = 1200
else:
scale = 600
row.setValue("Scale", scale)
urows.updateRow(row)
row.setValue("rawScale", rScale)
urows.updateRow(row)
del urows
del row
# add a scale for the topographic context maps
urows = arcpy.UpdateCursor(consCopy)
for row in urows:
case_id = row.getValue("Scale")
value = case_id*2
row.setValue("TopoScale", value)
urows.updateRow(row)
#set new consplan as data driven pages index
arcpy.AddMessage("Setting new results as data driven pages index...")
for keepers in arcpy.mapping.ListLayers(mxd):
if keepers.name == "Tract":
keepers.replaceDataSource(workSpace, "FILEGDB_WORKSPACE", consCopy)
elif keepers.name == "Other tracts used by producer":
keepers.replaceDataSource(workSpace, "FILEGDB_WORKSPACE", consCopy)
elif keepers.name == "TractIndex":
keepers.replaceDataSource(workSpace, "FILEGDB_WORKSPACE", consCopy)
del keepers
# Turn off toolKit Layers
arcpy.AddMessage("Turning off ToolKit layers...")
offList = arcpy.mapping.ListLayers(mxd)
for off in offList:
if off.name == "ZoomLayer":
arcpy.mapping.RemoveLayer(df, off)
elif off.name == "Practices (points)":
off.visible = False
elif off.name == consplanTitle:
off.visible = False
elif off.name == "Land Unit Topology":
off.visible = False
elif off.name == "Active PLUs":
off.visible = False
elif off.name == "AOI":
off.visible = False
elif off.name == "Base Layer":
off.visible = False
elif off.name == "Practices (lines)":
off.visible = False
elif off.name == "Practices (polygons)":
off.visible = False
elif off.name == "Legacy PLUs":
off.visible = False
elif off.name == "Case PLUs":
off.visible = False
elif off.name == "History PLUs":
off.visible = False
#clean up and clock out
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
mxd.dataDrivenPages.refresh()
del offList
del off
del mxd
del urows
del df
del id
del strID
del scale
del selection
del expression
【问题讨论】:
-
您可以从提供 CMVE 开始(即与 2.6 一起使用但在 2.7 中失败的最小可能脚本,更重要的是,什么不起作用,因为您希望它返回一组点你得到 [] 或一个例外)。我的猜测是 ArcGIS API 的变化不仅仅是 python 2.6 vs 2.7 的东西。
-
谢谢你,Foon。我在上面添加了更多细节。这有帮助吗?
-
不幸的是,正如我从经验中所知道的那样乏味和令人沮丧,解决这种升级不兼容问题的最佳方法之一是在每一行脚本之后包含
AddMessage。您知道它在您的第一行之后的某个地方,但在“将新结果设置为数据驱动页面索引...”之前(因为您没有看到),并且脚本中没有什么明显的地方我可以看到哪个会破坏在 10.3 中。除非他们已经淘汰UpdateCursor以支持da.UpdateCursor?
标签: python python-2.7 maps arcgis python-2.6