Answers:
我刚刚开始使用FME,并且正在使用关闭脚本将目标FGDB复制到另一个位置并保存日志文件:
import distutils.dir_util, shutil, os, time, locale
src = 'C:/Testing/FME/TPW/Third_Party_Wells.gdb'
dst = '//share/Data Services/GIS Data/Data/Third Party Wells/Third_Party_Wells.gdb'
distutils.dir_util.copy_tree(src, dst)
logfile = FME_LogFileName
shutil.copy(logfile, 'C:/temp/PRD_' + os.path.basename(logfile)[:-4] + '_' + time.strftime('%Y_%m_%d_%H_%M_%S', time.localtime()) + '.log')
# Get features written counts
shl_count = str(FME_FeaturesWritten['ThirdPartyWellsSurface'])
bhl_count = str(FME_FeaturesWritten['ThirdPartyWellsBottom'])
lat_count = str(FME_FeaturesWritten['ThirdPartyWellsLaterals'])
# Write out features written counts to log
fm_log = open('C:/temp/PRD_Counts.log','a')
fm_log.write(time.strftime('%m/%d/%Y %I:%M:%S', time.localtime()) + ',' + shl_count + ',' + bhl_count + ',' + lat_count + ',' + str(FME_TotalFeaturesWritten) + '\n')
这是很基本的,但我认为没有限制。这里有很多的想法在这里为好。
编辑:在代码中添加以获取写入的功能数量并将其推入CSV日志文件。
看看Oliver的Python Corner。在FME中使用Python可以做很多事情。
我经常使用PythonCaller在1个转换器内进行一些属性操作,而不是使用10个不同的转换器(如果使用elif elif else ..)
您可以拥有非常基本的PythonCallers,例如以下示例,它将所有属性转换为大写值:
def upperAll(feature):
for att in feature.getAttributeList():
feature.setAttribute(att,feature.gettAttribute(att).upper())
我也使用PythonCaller来发送电子邮件,以防出现故障或与FTP服务器交互等。实际上没有限制。
玩得开心,开心的FMEing
杰夫
上面的好例子:我目前正在为我们的知识库写一篇名为FMEPedia的文章: Python和FME基础。
这包括一些简单的示例,例如在使用启动脚本运行工作区之前删除文件,使用PythonCaller操纵功能等。还有一些指向更复杂示例的链接。
Ken Bragg安全软件
例子:
自定义日志
import os.path, time, os, datetime, __main__ , sys, pyfme,shutil
from pyfme import *
class expFeature(object):
def __init__(self):
self.logger = pyfme.FMELogfile()
pass
def close(self):
try:
#folders creation
os.makedirs(param_folder)
#Log creation
logFile = param_folder + timecreated +".log"
FILE = open(logFile,"w")
log=FMELogfile(logFile)
log.log("Bla bla bla")
并发送电子邮件:
message = MIMEMultipart()
message["From"] = email_from
message["To"] = email_to
message['Date'] = formatdate(localtime=True)
message["Subject"] = subject
message.attach( MIMEText(html, 'html') )
attachment = MIMEBase('application', "octet-stream")
attachment.set_payload( open(FileLog,"rb").read() )
Encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(FileLog))
message.attach(attachment)
smtp = smtplib.SMTP(smtpServer)
smtp.sendmail(email_from, email_to, message.as_string())
print "Successfully sent email"
smtp.close()
我最近一直在使用PythonCaller转换器,该转换器从CSV文件获取坐标并将其另存为属性。CSV是从另一个使用BoundsExtractor变形器的工作区写入的,该变形器从我感兴趣的区域的边界框中获取边界坐标。
然后,我将这些属性传递给其他WorkspaceRunner,这些WorkspaceRunner使用边界坐标作为进一步处理的搜索窗口。我拥有全州范围的数据,要对整个州进行处理将需要几个小时。因为我将处理限制为特定的窗口,所以整个过程需要一分钟。
pythonCaller代码在这里:
import fmeobjects
import csv
import re
# Template Function interface:
def getBounds(feature):
outputDirectory = FME_MacroValues['Output_Directory'] # Set outputDirectory
NativeTitle = FME_MacroValues['Native_Title'] # Set NativeTitle
NativeTitle = re.sub('\W','_',NativeTitle)
NativeTitle = re.sub(' ','_',NativeTitle)
csvPath = outputDirectory + '\\' + NativeTitle + '_boundingbox.csv' # Set csvPath
# open csv file containing bounding coordinates
with open(csvPath, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter = ',')
bounds = reader.next()
# Set bounding variables
XMIN = float(bounds[0])
XMAX = float(bounds[1])
YMIN = float(bounds[2])
YMAX = float(bounds[3])
# Set attributes to variable values
feature.setAttribute("_xmin", XMIN)
feature.setAttribute("_ymin", YMIN)
feature.setAttribute("_xmax", XMAX)
feature.setAttribute("_ymax", YMAX)
pass
我还使用了python启动脚本,该脚本将文件夹树复制到另一个位置(如果尚不存在)。
import os
import fmeobjects
import shutil
srcDir_project = r'W:\AlignmentSheets\PostInstall\Alignment Sheet Generator\ProjectData\ProjectNameFolder'
srcDir_settings = r'W:\AlignmentSheets\PostInstall\Alignment Sheet Generator\ProjectData\Settings'
destBaseDir = FME_MacroValues['Output_Directory']
destDir_project = destBaseDir + '\\' + FME_MacroValues['A_Sheet_Project_Name'] + '\\'
destDir_settings = destBaseDir + '\\Settings\\'
if not os.path.exists(destDir_project):
shutil.copytree(srcDir_project,destDir_project)
print 'Successfully created "%s"' % destDir_project
else:
print '"%s" Already Exists. Not Creating Folder.' % destDir_project
if not os.path.exists(destDir_settings):
shutil.copytree(srcDir_settings,destDir_settings)
print 'Successfully created "%s"' % destDir_settings
else:
print '"%s" Already Exists. Not Creating Folder.' % destDir_settings