你在这里: 亚搏在线工作流程 > PythonCaller

PythonCaller

Executes a Python script to manipulate the feature.

当需要专用任务时,例如对属性的自定义统计分析,但工作台不提供适合该任务的变形式,Python脚本可以在要素的几何,属性和坐标系上执行专业和复杂的操作。

Note: Using Python to perform arbitrary operations on features is a powerful aspect of Workbench. However, the logic introduced into a workspace is less visible and can therefore be more difficult to maintain than logic built using Workbench’s built-in transformers. It is recommended that other transformers be used when possible instead of Python scripts.

通过访问功能的属性,几何和坐标系信息通过FME对象Python API

接口范式

PythonCaller可以用两种不同的方式与Python脚本进行界面:按函数或课程:

  • 打算一次处理单个功能时使用函数接口。
  • 使用类接口以获取更多的灵活性。

当您希望在一组功能上运行时,类接口很有用,例如收集收到的所有功能,然后以特定的排序顺序输出它们。另一个常见用例是累积所有功能,在整个集合上执行操作,然后将计算值输出为新属性的计算值。

功能界面示例

Pythoncaller将用恰好一个参数调用python函数:fmemeature对象。

The function will be called with each FMEFeature that comes into the input port. This feature will then continue through the workspace pipeline via the output port.

The function’s return value will be ignored by the PythonCaller. Any raised exception will terminate the translation. Any raised FMEException will be logged as an ERROR and will terminate the translation.

下面的示例将字符串属性添加到每个功能,并将其设置为当前时间:

导入fmeobjects.
import time

          
def时间戳(特征):
curTime = time.ctime(time.time())
feature.setattribute(“时间戳”,curtime)

类接口示例

PythonCaller将在类上调用两种方法:输入()和close()。将为输入端口中的每个FMeature调用INPUT()方法。当没有更多的FMeatures仍然存在时,将调用close()方法。必须使用pyoutput()方法明确地写出需要继续通过工作区进行进一步处理的功能。

类接口可以在一组功能上运行,而不是一次处理传入功能。这是通过将传入功能存储在列表中,然后在输出之前一次处理它们。

The example below calculates the total area of all the features processed and then outputs all the features with a new attribute containing the total area:

导入fmeobjects.

          
class FeatureProcessor(object):
def __init__(self):
self.featureList = []
self.totalarea = 0.0

          
def输入(self,feature):
self.featureList.append(feature)
self.totalArea += feature.getGeometry().getArea()

          
def close(self):
for feature in self.featureList:
feature.setAttribute(“total_area”,self.totalarea)
self.pyoutput(功能)

脚本编辑

A PythonCaller transformer can call scripts that are stored in the transformer itself or scripts that are stored globally for the entire workspace:

  • To store a Python script with a specific PythonCaller transformer, use the Python Script parameter in the transformer.
  • 要在全局全局存储Python脚本,请单击导航器中的高级工作区参数,然后双击启动python脚本。存储脚本在全球范围内具有保持Python逻辑集中的优势,这使得编辑和维护更容易。当在使用相同脚本的工作空间中有多个PythonCaller变压器时,这很有用。有关更多信息,请参阅启动和关闭Python脚本在FME工作台帮助。

FME可以访问.py.存储在文件系统上的模块,包括外部Python库中的模块。使用python“import”命令加载这些模块。FME将搜索标准Python模块位置和工作区位置以查找要导入的模块。

参数

依赖性

指定Python解释器

FME安装包括Python版本2.7和Python版本3.5解释器。用于Python处理的默认Python解释器是Python 2.7解释器。FME对象Python API支持Python 2.7,Python 3.4和Python 3.5。

FME使用的Python解释器执行Python脚本是由Python Compatibility工作区参数和首选Python翻译setting under Tools > FME Options > Translation.

Python Compatibilityspecifies the version of Python with which Python scripts are compatible. FME loads the首选Python翻译如果它与之兼容Python Compatibility。如果没有,FME加载适当的Python解释器匹配Python Compatibility

有关更多信息,请参阅FME Workbench帮助。

安装Python软件包

If you would like to install a third-party package for use by Python in FME, see将Python软件包安装到FME桌面在FME工作台帮助。

编辑变压器参数

Using a set of menu options, transformer parameters can be assigned by referencing other elements in the workspace. More advanced functions, such as an advanced editor and an arithmetic editor, are also available in some transformers. To access a menu of these options, clickbeside the applicable parameter. For more information, see变压器参数菜单选项

Transformer Categories

亚搏在线工作流程

FME许可级别

FME专业版及以上

搜索FME知识中心

搜索有关此变压器的样本和信息FME知识中心