第一章 FMEFeature类方法的介绍和使用
目录
6.FMEFeature.buildAggregateFeat
前言
在学习该专栏课程前,需要大家对python基础知识有一点了解,比如什么是类,什么是函数,什么是面向对象。FMEFeature类是fmeobj中最核心的类,任何格式的数据在fme中的最终呈现都是以FMEFeature类来表示。FMEObjcet python api中提供了不少类方法,可以让我们在pythoncaller中实现很多有意思的操作,学会该类也会让我们在处理数据的时候更加的得心应手。
一、FMEFeature是什么?
FMEFeature类其实就是FME的"F"也就是feature本身,该类包含了3类信息:几何信息,属性信息,坐标系信息。
二、FMEFeature初体验
1.pythoncaller转换器特性介绍
我们在fme中随意的调用一个pythoncaller转换器,我们会发现该转换器中给你提供了一个基本框架,如果对python比较熟悉的同学,不难发现,该代码中的FeatureProcessor类是继承了object基础类的属性,在input函数中的feature则为fmeobjects.FMEFeature本身,也就是该转换器的输入要素。
import fme
import fmeobjects
class FeatureProcessor(object):
def __init__(self):
pass
def input(self, feature):
self.pyoutput(feature)
def close(self):
pass
def process_group(self):
pass
如果还不能理解的话,我们可以用一个小操作来帮助理解,比如self.pyoutput(feature),意思就是抛出要素,那么如果我们将该代码注释则会发现,转换器出口没有任何数据输出。
这其实是一个很有意思的设计,input方法会接收上一个模块传递的每一条要素,并且该函数是动态执行的,一旦接收一条要素,则会马上触发一次input方法。 __init__方法主要负责初始化一些变量,close方法会在input函数执行完毕后,最后再执行一次,process_group方法我们后期讲解,主要是做分组计算的时候使用。
2.新建一个要素
新建一个空要素非常简单,在PythonCreator转换器用fmeobjects.FMEFeature()创建一个空要素
import fmeobjects
class FeatureCreator(object):
def __init__(self):
pass
def input(self, feature):
newFeature = fmeobjects.FMEFeature()
self.pyoutput(newFeature)
def close(self):
pass
然后用self.pyoutput()函数抛出要素。就可以创建一个什么都没有的空要素出来。
三、fmeobjects.FMEFeature类方法说明
1.FMEFeature.
addCoordinate
该方法可以给要素添加坐标点,但是只限于矢量要素,如果是栅格要素则会报错。输出参数为坐标点X,Y,Z。
使用案例:给一个空要素添加坐标点
import fme
import fmeobjects
class FeatureProcessor(object):
def __init__(self):
pass
def input(self, feature):
feature.addCoordinate(2,2,4)
self.pyoutput(feature)
def close(self):
pass
def process_group(self):
pass
2.FMEFeature.addCoordinates
给一个要素新增坐标串,需要注意的该方法的输入参数必须是列表格式,坐标串用元组表示。
比如[(1,2),(2,3),(4,5)]。
使用案例:给一个空要素添加坐标串
import fme
import fmeobjects
class FeatureProcessor(object):
def __init__(self):
pass
def input(self, feature):
feature.addCoordinate([(1,2),(2,3),(4,5)])
self.pyoutput(feature)
def close(self):
pass
def process_group(self):
pass
3.FMEFeature.boundingBox
该方法需要变量来接收返回值,返回值为坐标串元祖((xmin,ymin),(xmax,ymax))。
使用案例:获取一个要素的boundingBox
input函数中
box=feature.boundingBox()
print(box)
打印结果
4.FMEFeature.boundingCube
该方法和上一个方法一样,只是该方法是对三维的几何数据使用,返回((minx, miny, minz), (maxx, maxy, maxz))元祖。
5.FMEFeature.buffer
该方法有两个参数,第一个是参数是缓冲的宽度,第二个参数是缓冲的取样角度(如果我选择的角度是5度,那么每5度则会有一个顶点)
使用案例:buffer一个面
def input(self, feature):
feature.buffer(10,6)
self.pyoutput(feature)
6.FMEFeature.buildAggregateFeat
该方法可以将几个要素以列表传入,并生成一个聚合体要素。
使用案例:聚合几个要素
import fme
import fmeobjects
class FeatureProcessor(object):
def __init__(self):
self.features=[]#初始化一个列表变量,用于接收feature要素
pass
def input(self, feature):
self.features.append(feature)#将要素传入列表
def close(self):
newfeature=fmeobjects.FMEFeature()#新建一个空要素
newfeature.buildAggregateFeat(self.features)#将列表变量转换为聚合体
self.pyoutput(newfeature)#抛出要素
pass
def process_group(self):
pass
总结
如果要解锁全部api文档和使用示例,请联系作者qq1131698384