Solved

Double Transposing Attributes to Test for Attribute Names

  • 13 December 2016
  • 7 replies
  • 1 view

Badge +2

Hello everyone!

Long time user of FME and a big lurker here!

I am currently designing a FME Server Workbench where I expect a user to upload spreadsheets that does not have any sort of schema requirements aside from Lat/Long and a prefix of 'h_' for any hyperlinks in their spreadsheet.

I currently have two branches in my workbench

BRANCH 1

With Dynamic Reader and Writers the process is relatively simple, but my issue is with the hyperlinks. My hyperlink URLs need to be wrapped with <a href> </a> tags in order to work on web app builder mapping services. This branch creates the points out of the provided Lat/Long and accepts schemas for an increase in character width for larger hyperlinks

BRANCH 2

In order to test attribute names that contain 'h_', I used an attributeexploder and was able to determine which fields have a h_ thus needing the <a href> tags. Once I have the <a href> tag in place I want to transpose it again to return it to the original state with the attribute names being the fields with 'h_' and to reconnect it to branch 1

My issue:

In branch 2 my transposition does the job of identifying my 'h_' fields, but afterwards my attempts to transpose it again is failing me! Does anyone know a solution to rebuilding attributeexploded/aggregated lists?

Hopefully I have conveyed my issue clearly otherwise I will be checking over this post for more questions.

Thanks,

Patrick

icon

Best answer by takashi 14 December 2016, 09:49

View original

7 replies

Userlevel 2
Badge +17

The easiest way to do this would be to use a PythonCaller to loop through all the attributes and replace the 'h_' with the tags:

import fme
import fmeobjects
import re

def formatURLs(feature):
    # get names of feature attributes
    attNames = feature.getAllAttributeNames()
    # loop through attributes
    for att in attNames:
        # don't process generic attributes
        if not att.startswith('fme'):
            # get attribute value
            value = feature.getAttribute(att)
            # does attribute start with h_?
            if value.startswith('h_'):
                # replace h_ with tags
                newval = re.sub('^h_','<a href>',value) + '</a>'
                # write updated attribute back to feature
                feature.setAttribute(att,newval) 
Badge +2

The easiest way to do this would be to use a PythonCaller to loop through all the attributes and replace the 'h_' with the tags:

import fme
import fmeobjects
import re

def formatURLs(feature):
    # get names of feature attributes
    attNames = feature.getAllAttributeNames()
    # loop through attributes
    for att in attNames:
        # don't process generic attributes
        if not att.startswith('fme'):
            # get attribute value
            value = feature.getAttribute(att)
            # does attribute start with h_?
            if value.startswith('h_'):
                # replace h_ with tags
                newval = re.sub('^h_','<a href>',value) + '</a>'
                # write updated attribute back to feature
                feature.setAttribute(att,newval) 
Hey Dave,

 

 

Using Python totally slipped my mind! I'm just having a bit of an issue using the pythoncaller transformer.

 

 

I get this error:

 

PythonFactory failed to load python symbol `FeatureProcessor'

 

Factory proxy not initialized

 


f_21(PythonFactory): PythonFactory failed to process feature

 

 

I am on 

 

FME 2016.1 Build 16492

 

and using a 

 

a custom python interpreter from C:\Windows\SysWOW64\python27.dll for importing arcobjects

 

 

I had no issues using startup and shutdown scripts!

 

 

 

 

Badge +22
Hey Dave,

 

 

Using Python totally slipped my mind! I'm just having a bit of an issue using the pythoncaller transformer.

 

 

I get this error:

 

PythonFactory failed to load python symbol `FeatureProcessor'

 

Factory proxy not initialized

 

f_21(PythonFactory): PythonFactory failed to process feature

 

 

I am on

 

FME 2016.1 Build 16492

 

and using a

 

a custom python interpreter from C:\\Windows\\SysWOW64\\python27.dll for importing arcobjects

 

 

I had no issues using startup and shutdown scripts!

 

 

 

 

in the PythonCaller parameter window, set the Class or Function to Process Features to formatURLS instead of FeatureProcessor

 

Badge +2
in the PythonCaller parameter window, set the Class or Function to Process Features to formatURLS instead of FeatureProcessor

 

Thanks! That allows me to run this python script.

 

 

But I get an error:

 

Python Exception <AttributeError>: 'int' object has no attribute 'startswith' Error encountered while calling function `formatURLs'

 

 

 

I've looked at attNames itself when I used these lines:

 

attNames = ','.join(feature.getAllAttributeNames())

 

feature.setAttribute('attNames', attNames)

 

 

It is a list but it's missing its prefixes that I expect like fme_ for object types and what not and I cannot expose it's elements neither!

 

 

I am getting closer though!

 

 

 

 

 

Badge +2
No luck with Dave's code snippet. I got to a point where I was able to wrap one of the list elements from the getAttributeNames function with the regular expression. However with multiple 'h_' elements I cannot see through with this solution!

 

 

I'm looking using python to transpose the row to columns as my attributeexploded + aggregated list is extremely close to what I want!

 

 

Userlevel 2
Badge +17

Hi @pcheng, if I understand the requirement correctly, the workflow in the attached workspace might help you: format-urls-example.fmw (FME 2016.1.3.1)

format-urls-example 

Python Edition:

def formatURLs(feature):
    for attr in feature.getAllAttributeNames():
        if attr.startswith('h_'):
            v = feature.getAttribute(attr)
            feature.setAttribute(attr, '<a href="%s">%s</a>' % (v, v))

Tcl Edition:

proc formatURLs {} {
    foreach attr [FME_AttributeNames] {
        if {[regexp ^h_ $attr]} {
            set v [FME_GetAttribute $attr]
            FME_SetAttribute $attr "<a href=\"$v\">$v</a>"
        }
    }
}

 

Badge +2

Hi @pcheng, if I understand the requirement correctly, the workflow in the attached workspace might help you: format-urls-example.fmw (FME 2016.1.3.1)

format-urls-example 

Python Edition:

def formatURLs(feature):
    for attr in feature.getAllAttributeNames():
        if attr.startswith('h_'):
            v = feature.getAttribute(attr)
            feature.setAttribute(attr, '<a href="%s">%s</a>' % (v, v))

Tcl Edition:

proc formatURLs {} {
    foreach attr [FME_AttributeNames] {
        if {[regexp ^h_ $attr]} {
            set v [FME_GetAttribute $attr]
            FME_SetAttribute $attr "<a href=\"$v\">$v</a>"
        }
    }
}

 

Thank you so much @takashi, all three of your solutions provides the results that I wanted to integrate into my workbench!

 

Lots to learn from these solutions as well! I appreciate the learning opportunity.

 

Reply