使用 ArcGIS Pro 在 Field Calculator 中定位子字符串
Locating a substring in Field Calculator using ArcGIS Pro
我在 ArcGIS Pro 2.4 中遇到了一个应该相当简单的操作失败的问题,但我终究无法找出原因。
如果字段 "assettype" 包含搜索字符串的一部分,则将 assettype_groupup 的值设置为值 I return。
例如,如果 "assetttype" 包含字符串 "Building |Residential |Large ",并且我测试它是否包含术语 "Residential",并且计算结果为真,那么 return 字符串"Residential"。
目前该代码似乎没有 return 任何结果/没有任何效果,并且似乎 运行 太快了(3,000,000 行需要 2-3 秒)。
如果我尝试三元语句,这意味着一次使用一个术语,它似乎工作得很好。我宁愿不采用这种方法,因为 if/elif 可能性最终会变得广泛,我只想 运行 该例程一次。
你能看出以下设置有任何明显的问题吗
#Target Field Name
assettype_groupup
#Expression
func(!assettype!)
# Code block
def func(input):
if 'Residential' in input:
return 'Residential'
elif 'Industrial/Utilities' in input:
return 'Industrial/Utilities'
elif 'Transport/Infrastructure' in input:
return 'Transport/Infrastructure'
elif 'Conservation/National Park' in input:
return 'Conservation/National Park'
elif 'Recreational/Open Space' in input:
return 'Recreational/Open Space'
elif 'Mixed Use' in input:
return 'Mixed Use'
elif 'Community Use' in input:
return 'Community Use'
elif 'Rural/Primary Production' in input:
return 'Rural/Primary Production'
elif 'Special Use' in input:
return 'Special Use'
elif 'Unknown' in input:
return 'Unknown'
else:
''
我不熟悉 ArcGIS Pro,但Python 相当了解。
在Python中你应该避免命名变量或参数与任何相同
内置函数,input()
是内置函数的名称。另外,之后的行
最后的else:
,应该是return ''
。既然不是这样,你的
当没有匹配项时,函数将有效 return None
, 而不是
空字符串 ''
(假设这就是您的意图)。
考虑到这一点,以下将是编写可避免上述两个问题的函数的更好方法:
TARGETS = (
'Residential',
'Industrial/Utilities',
'Transport/Infrastructure',
'Conservation/National Park',
'Recreational/Open Space',
'Mixed Use',
'Community Use',
'Rural/Primary Production',
'Special Use',
'Unknown',
)
# Code block
def func(info):
for target in TARGETS:
if target in info:
return target
else: # Nothing matched.
return ''
一种更高级、更短且可能更快的方法是使用 Python 的 re
正则表达式模块,该模块是其标准库的一部分:
import re
regex = re.compile('|'.join(map(re.escape, TARGETS)))
# Code block
def func(info):
mo = regex.search(info)
return mo.group(0) if mo else ''
在 Python 3.8.0+ 中,可以通过使用 :=
(又名 "walrus" 运算符)利用 PEP 572's new syntax for assignment expressions 来缩短一点从该版本开始添加:
import re
regex = re.compile('|'.join(map(re.escape, TARGETS)))
def func(info):
return mo.group(0) if (mo := regex.search(info)) else '' # Py 3.8.0+
我在 ArcGIS Pro 2.4 中遇到了一个应该相当简单的操作失败的问题,但我终究无法找出原因。
如果字段 "assettype" 包含搜索字符串的一部分,则将 assettype_groupup 的值设置为值 I return。
例如,如果 "assetttype" 包含字符串 "Building |Residential |Large ",并且我测试它是否包含术语 "Residential",并且计算结果为真,那么 return 字符串"Residential"。
目前该代码似乎没有 return 任何结果/没有任何效果,并且似乎 运行 太快了(3,000,000 行需要 2-3 秒)。
如果我尝试三元语句,这意味着一次使用一个术语,它似乎工作得很好。我宁愿不采用这种方法,因为 if/elif 可能性最终会变得广泛,我只想 运行 该例程一次。
你能看出以下设置有任何明显的问题吗
#Target Field Name
assettype_groupup
#Expression
func(!assettype!)
# Code block
def func(input):
if 'Residential' in input:
return 'Residential'
elif 'Industrial/Utilities' in input:
return 'Industrial/Utilities'
elif 'Transport/Infrastructure' in input:
return 'Transport/Infrastructure'
elif 'Conservation/National Park' in input:
return 'Conservation/National Park'
elif 'Recreational/Open Space' in input:
return 'Recreational/Open Space'
elif 'Mixed Use' in input:
return 'Mixed Use'
elif 'Community Use' in input:
return 'Community Use'
elif 'Rural/Primary Production' in input:
return 'Rural/Primary Production'
elif 'Special Use' in input:
return 'Special Use'
elif 'Unknown' in input:
return 'Unknown'
else:
''
我不熟悉 ArcGIS Pro,但Python 相当了解。
在Python中你应该避免命名变量或参数与任何相同
内置函数,input()
是内置函数的名称。另外,之后的行
最后的else:
,应该是return ''
。既然不是这样,你的
当没有匹配项时,函数将有效 return None
, 而不是
空字符串 ''
(假设这就是您的意图)。
考虑到这一点,以下将是编写可避免上述两个问题的函数的更好方法:
TARGETS = (
'Residential',
'Industrial/Utilities',
'Transport/Infrastructure',
'Conservation/National Park',
'Recreational/Open Space',
'Mixed Use',
'Community Use',
'Rural/Primary Production',
'Special Use',
'Unknown',
)
# Code block
def func(info):
for target in TARGETS:
if target in info:
return target
else: # Nothing matched.
return ''
一种更高级、更短且可能更快的方法是使用 Python 的 re
正则表达式模块,该模块是其标准库的一部分:
import re
regex = re.compile('|'.join(map(re.escape, TARGETS)))
# Code block
def func(info):
mo = regex.search(info)
return mo.group(0) if mo else ''
在 Python 3.8.0+ 中,可以通过使用 :=
(又名 "walrus" 运算符)利用 PEP 572's new syntax for assignment expressions 来缩短一点从该版本开始添加:
import re
regex = re.compile('|'.join(map(re.escape, TARGETS)))
def func(info):
return mo.group(0) if (mo := regex.search(info)) else '' # Py 3.8.0+