使用Arcpy从字段获取变量

发布于 2025-02-09 13:11:01 字数 1425 浏览 1 评论 0原文

我正在使用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()

I am creating a toolbox tool using a python script that creates maps based on user input. I have created a map template that gets saved and altered with python. I am struggling on how to update some text in text boxes in the layout view using Arcpy. I was able to do it with dynamic text with data driven pages, but I couldn't find any python code to get data driven pages to refresh so I decided to try to update the text with python directly. With data driven pages, the dynamic text was pulling the text from an attribute table. I'm fairly new to python so am struggling with how to pull values from a table to use as part of the text. I am able to update text as long as I have the variable defined somewhere else (not from a table), but the only method I found to pull data from a table was with a search cursor but that returns a list rather than a value so I get an error. The feature classes with the text values I want only have one row in them so it is a list of one. How can I convert that list to a value. I am only including the applicable parts of the script. I also removed the actual paths from the code.

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢好甜 2025-02-16 13:11:03

从Arcgis Toobox脚本工具运行时,此代码将起作用。

# 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

This code will work when run from a arcgis toobox script tool.

# 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
甜警司 2025-02-16 13:11:03

我能够调整ArmedWithTheword的代码来提出这一点。

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

I was able to tweak armedwiththeword's code to come up with this.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文