I saw a great interface to FME with Python
What you guys are doing with it? I want ideas.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
I'm just getting started with FME, and am using a shutdown script to copy my target FGDB to another location and to save out the log file:
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')
That's pretty basic, but there really is no limit I don't think. There are tons of ideas here as well.
EDIT: Added in code to get numbers of features written and push those out to CSV log file.
Have a look at Oliver's Python Corner. There's a ton of things you can do using Python in FME.
I often use the PythonCaller to make some attributemanipulations inside 1 transformer rather than using 10 different transformers (if elif elif else ..)
You can have very basic PythonCallers like this example which will convert all your attributes to uppercase values:
def upperAll(feature): for att in feature.getAttributeList(): feature.setAttribute(att,feature.gettAttribute(att).upper())
I also use the PythonCaller to send emails in case of failure or interact with an FTP Server etc .. There are really no limits
Have fun and happy FMEing
Good example above: I'm currently writing an article for our knowledgebase called FMEPedia here: Python and FME Basics.
This includes some simple examples such as deleting a file before running a workspace with a start up script, manipulating features with a PythonCaller etc. There are also links to more complex examples.
Ken Bragg Safe Software
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")
And Send Email:
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()
I've recently been using a PythonCaller transformer which gets coordinates from a CSV file and saves them as attributes. The CSV gets written from a another workspace which uses a BoundsExtractor Transformer which gets the bounding coordinates from a bounding box of my area of interest.
I then pass these attributes to other WorkspaceRunners which use the bounding coordinates as a search window for further processing. I have State-Wide data and to process on the whole state would take several hours. Because I limit my processing to a particular window, the whole thing takes one minute.
The pythonCaller code is here:
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) XMAX = float(bounds) YMIN = float(bounds) YMAX = float(bounds) # Set attributes to variable values feature.setAttribute("_xmin", XMIN) feature.setAttribute("_ymin", YMIN) feature.setAttribute("_xmax", XMAX) feature.setAttribute("_ymax", YMAX) pass
I also use a python start up script which copies a folder tree to another location if it doesn't exist already.
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