作为从ArcGIS 10.0升级的一部分,从Python 2.6升级到2.7后,在ArcGIS 10.3中运行脚本时出现问题

时间:2015-12-16 17:59:31

标签: python python-2.7 maps arcgis python-2.6

我是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

1 个答案:

答案 0 :(得分:0)

感谢大家的投入。 CWebster和我发现 df.zoomToSelectedFeatures()命令没有选择要放大的功能(上面脚本中的第15行)。由于未选择任何功能,因此脚本将无法继续运行。

为了解决这个问题,我们用 df.extent = lyr.getSelectedExtent()替换了 df.zoomToSelectedFeatures()命令行,现在脚本再次运行完成。 / p>

我们不确定问题是否与ArcGIS 10.3更新或Python 2.7更新有关,但如果其他人遇到类似问题,这些信息可能有所帮助。