使用arcpy从字段中获取变量



我正在使用python脚本创建一个工具箱工具,该脚本根据用户输入创建地图。我已经创建了一个地图模板,得到保存和修改与python。我正在努力如何使用Arcpy更新布局视图中的文本框中的一些文本。我能够使用数据驱动页面的动态文本来实现它,但是我找不到任何python代码来刷新数据驱动页面,所以我决定尝试直接使用python来更新文本。对于数据驱动的页面,动态文本是从属性表中提取文本。我对python相当陌生,所以我正在努力从表中拉值作为文本的一部分使用。只要我在其他地方(不是从表)定义了变量,我就可以更新文本,但是我发现从表中提取数据的唯一方法是使用搜索光标,但它返回列表而不是值,因此我得到了一个错误。具有文本值的功能类我只想在它们中有一行,所以它是一个列表。如何将该列表转换为值。我只包括脚本的适用部分。我还从代码中删除了实际路径。

import arcpy
import os
ID = arcpy.GetParameterAsText(1)
city = arcpy.GetParameterAsText(3)
WS = os.path.join('path to gdb', "WS")
dfield = 'name'
datefield = 'date'
cfield = "county"
#Use SearchCursor - these features only have one row, but these are my problem because they are lists
wsname = [row[0] for row in arcpy.da.SearchCursor(WS, dfield)]
wsdate = [row[0] for row in arcpy.da.SearchCursor(WS, datefield)]
county = [row[0] for row in arcpy.da.SearchCursor(overview, cfield)]

#update text
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
elm.text = elm.text.replace('WS',wsname) #this doesn't work because wsname is a list
elm.text = elm.text.replace('City',city) #this works
elm.text = elm.text.replace('text2',"words"+ ID +" -more words") #This works
elm.text = elm.text.replace('Name', county) #this doesn't work because county is a list
elm.text = elm.text.replace('Date',wsdate) #this doesn't work because wsdate is a list
arcpy.RefreshActiveView()
mxd.save()

这段代码可以在arcgis工具箱脚本工具中运行。

# define the aprx file and the layout in the project
import arcpy
aprx = arcpy.mp.ArcGISProject(r'pathtothearcgisaprxfile.aprx')
aprxLayout = aprx.listLayouts()[0] '''adding the zero index will return the first 
layout in the layout list, if there is more than one layout'''
# get the attribute value to use for the layout text element
fieldNames = ['FieldName1', 'FieldName2']
with arcpy.da.SearchCursor(r'pathto.gdbfeatureclass', fieldNames) as sc:
for row in sc:
if (row[0]) is not None:
field1Value = (row[0])
if (row[0]) is None:
field1Value = 'Null'
if (row[1]) is not None:
field2Value = (row[0])
if (row[1]) is None:
field2Value = 'Null'
# Assign the attribute value to the layout text element
for textElem in aprxLayout.listElements:
if textElem.name == 'name of layout text element in the element properties':
text.Elem.text = field1Value
if textElem.name == 'name of layout text element in the element properties':
text.Elem.text = field2Value
aprx.saveACopy(r'path/to/folder/projectname')
del aprx

我可以用theword的代码来调整这个。

import arcpy
mxd = arcpy.mapping.MapDocument(path_to_mxd)
fieldNames = ['name', 'date']
with arcpy.da.SearchCursor(WS, fieldNames) as sc:
for row in sc:
if(row[0]) is not None:
field1Value = (row[0])
if(row[0]) is None:
field1Value = 'Null'
if(row[1]) is not None:
field2Value = (row[1])
if(row[1]) is None:
field2Value = 'Null'
fieldName = ['CTY_NAME']
with arcpy.da.SearchCursor(overview, fieldName) as sc:
for row in sc:
if(row[0]) is not None:
field3Value = (row[0])
if(row[0]) is None:
field3Value = 'Null'

# Assign the attribute value to the layout text element
for textElem in arcpy.mapping.ListLayoutElements(mxd,'TEXT_ELEMENT'):
if textElem.name == 'title':
textElem.text = field1Value + " words"
if textElem.name == 'subtitle':
textElem.text = "WS -0"+ ID + " -more words"
if textElem.name == 'city':
textElem.text = city
if textElem.name == 'county':
textElem.text = field3Value
if textElem.name == 'date':
textElem.text = field2Value

相关内容

  • 没有找到相关文章