我正在使用基于用戶輸入創建地圖的 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()
uj5u.com熱心網友回復:
當從 arcgis 工具箱腳本工具運行時,此代碼將起作用。
# define the aprx file and the layout in the project
import arcpy
aprx = arcpy.mp.ArcGISProject(r'path\to\the\arcgis\aprxfile.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'path\to.gdb\featureclass', 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
uj5u.com熱心網友回復:
我能夠調整 armourwiththeword 的代碼來提出這個問題。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/496278.html
標籤:python-2.7 弧度 弧形图
上一篇:通過str.replace(obj_cls,"newstring")運行物件類時獲取字串
下一篇:從SQL中的時間戳獲取周數