使用数据驱动页面创建表的Python脚本


11

我正在尝试转换一些python脚本,以显示位于特定数据驱动页面内的功能表(基于dbf)。到目前为止,我已经使脚本成功地能够将地图刷新到特定的表,但是它不会更新表。

我将其设置为三个文本框,当用户从ArcToolbox运行脚本时,应使用三个特定字段进行更新。

关于为什么我的表没有更新的任何建议?

import arcpy, sys, os

#Reference current MXD
mxd = arcpy.mapping.MapDocument("current")

#Get input parameter
Name = arcpy.GetParameterAsText(0)

#Reference  data frames
mapatlasDF = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
locatorDF = arcpy.mapping.ListDataFrames(mxd, "Locator Map")[0]

#Reference appropriate layers
atlasLyr = arcpy.mapping.ListLayers(mxd, "PinalCreekMapAtlas_HalfMile", mapatlasDF)[0]
locatorLyr = arcpy.mapping.ListLayers(mxd, "Locator Map", locatorDF)[0]
atlasoutlineLyr = arcpy.mapping.ListLayers(mxd, "Map Atlas Outline", locatorDF)[0]

#Reference layout elements by calling ListLayoutElements 
for elm in arcpy.mapping.ListLayoutElements(mxd):
  if elm.name =="Table1Column1": tab1Col1Txt = elm
  if elm.name =="Table1Column2": tab1Col2Txt = elm
  if elm.name =="Table1Column3": tab1Col3Txt = elm

#Reference the Data Driven Page object
ddp = mxd.dataDrivenPages

#Set the current page to be the one selected in the script tool
arcpy.AddMessage(Name)

pageID = mxd.dataDrivenPages.getPageIDFromName(str(Name))
mxd.dataDrivenPages.currentPageID = pageID

#Set the appropriate definition queries
atlasLyr.definitionQuery = "Name = '" + Name +  "'"
locatorLyr.definitionQuery = "Name = '" + Name +  "'"
atlasoutlineLyr.definitionQuery = "Name <> '" + Name +  "'"

#Update Sheet Index data frame
arcpy.SelectLayerByAttribute_management(locatorLyr, "NEW_SELECTION", "\"Name\" = '" + Name + "'")
locatorDF.panToExtent(locatorLyr.getSelectedExtent())

#Reference Affected Parcels table and select appropriate records
parcelTable = arcpy.mapping.ListTableViews(mxd, "AffectedParcels")[0]

#Build query and create search cursor to loop through rows
parcelFieldValue = "Page " + Name
queryExp = "\"MapPage\" = '" + parcelFieldValue + "'"  #e.g., "MapPage" = 'Page 01'
parcelRows = arcpy.SearchCursor(parcelTable.dataSource, queryExp)

#Clear all table text values
tab1Col1Txt.text = " "; tab1Col2Txt.text = " "; tab1Col3Txt.text = " "

#iteate through each row, update appropiate text
count = 0
for row in parcelRows:
  if count < 30: #Table1 - static position
    tab1Col1Txt.text = tab1Col1Txt.text + row.getValue("OwnerName") +"\n"
    tab1Col2Txt.text = tab1Col2Txt.text + row.getValue("APN") + "\n"
    tab1Col3Txt.text = tab1Col3Txt.text + row.getValue("LengthTrail") + "\n"
  if count ==30:  
    arcpy.AddMessage("Table Overflow") #The code could be reworked to show the last 90 records
  count = count + 1

arcpy.RefreshActiveView()
arcpy.AddMessage("PROCESS COMPLETED")

我首先尝试将您的脚本缩减为一个更简单的示例。对象是否按ListLayoutElements类型返回TextElement?您是否可以在脚本中更新单个文本值,而无需其他任何代码?
scw 2013年

正如scw所说,元素实际上是否已返回?我将在每个if语句中添加arcpy.AddMessage(“ Found Table1Column1”),然后在if count <30区域中添加arcpy.AddMessage(tab1Col1Txt.text + tab1Col2Txt.text + tab1Col3Txt.text)。这样可以更好地了解问题出在哪里。
eseglem 2013年

从代码中不清楚,在哪里定义了tab1Col1Txt,tab1Col2Txt和tab1Col3Txt对象。首先尝试验证row.getValue部分返回的内容
Matej

Answers:


2

也许这些例子可以帮助您:

具有动态表和图形的DDP 10.1_v1

此示例演示了如何使用arcpy.mapping API扩展数据驱动页面(DDP)的功能以生成真正的地图系列动态表格和图形

arcpy.mapping具有动态图形表的地图册

该项目结合了数据驱动页面和arcpy.mapping来构建包含动态图形表的地图系列。


1
您可以在答案中提供更多内容吗?仅链接答案不是首选,因为链接可能会随着时间而变化。
艺术品
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.