Skip to main content
Question

PYTHON Cross and Dot product function with logic with Directional Vector??


vimva679
Supporter
Forum|alt.badge.img+9

Hello 

 

It says Translation is Successful, but i dont't see the any out , am i doing something horribly wrong or missing some setup ? 

 

imagePython Caller 

 

import fme
import fmeobjects
import numpy as np
 
class FeatureProcessor(object):
 
def __init__(self):
pass
 
  
 
  def input(self, feature):
 
     
 
    def myfunction(L, F, DX, DY, DZ):
 
      DLF = [0,0,-1]
 
      ULF = [0,0,1]
 
      NLF = [0,1,0]
 
      SLF = [0,-1,0]
 
      ELF = [1,0,0]
 
      WLF = [-1,0,0]
 
     
 
      if L == "D":
 
        UAP = DLF
 
      elif L == "U":
 
        UAP = ULF
 
      elif L == "N":
 
        UAP = NLF
 
      elif L == "S":
 
        UAP = SLF
 
      elif L == "E":
 
        UAP = ELF
 
      elif L == "W":
 
        UAP = WLF
 
       
 
      if F == "D":
 
        FAP = DLF
 
      elif F == "U":
 
        FAP = ULF
 
      elif F == "N":
 
        FAP = NLF
 
      elif F == "S":
 
        FAP = SLF
 
      elif F == "E":
 
        FAP = ELF
 
      elif F == "W":
 
        FAP = WLF
 
         
 
      VAP = np.cross(FAP, UAP)
 
      DU,DV,DN = np.linalg.inv(np.column_stack((UAP, VAP, FAP))).dot(np.array(DX,DY,DZ))
 
      return DU,DV,DN
 
       
 
      self.pyoutput(feature)
 
 
 
  def close(self):
 
    pass
import fme
import fmeobjects
import numpy as np
 
class FeatureProcessor(object):
 
    def __init__(self):
        pass
 
    def input(self, feature):
        feature = myfunction(L, F, DX, DY, DZ)
	    
        self.pyoutput(feature)

    def close(self):
        pass
 
    def myfunction(L, F, DX, DY, DZ):
        DLF = [0,0,-1]
        ULF = [0,0,1]
        NLF = [0,1,0]
        SLF = [0,-1,0]
        ELF = [1,0,0]
        WLF = [-1,0,0]
        
        if  L == "D":
            UAP = DLF	
        elif L == "U":
            UAP = ULF	
        elif L == "N":
            UAP = NLF
        elif L == "S":
            UAP = SLF
        elif L == "E":
            UAP = ELF
        elif L == "W":
            UAP = WLF
        
        if F == "D":
            FAP = DLF
        elif F == "U":
            FAP = ULF
        elif F == "N":
            FAP = NLF
        elif F == "S":
            FAP = SLF
        elif F == "E":
            FAP = ELF
        elif F == "W":
            FAP = WLF
        
        VAP = np.cross(FAP, UAP)
        DU,DV,DN = np.linalg.inv(np.column_stack((UAP, VAP, FAP))).dot(np.array(DX,DY,DZ))
        return L,F,DX,DY,DZ,DU,DV,DN
 
        self.pyoutput(feature)

 

     

     

     

 

7 replies

david_r
Evangelist
  • December 8, 2023

When posting Python code, can you please apply the Code Snippet formatting to make it more readable.

That said, why are you declaring a function "my_function" inside the input method, and then never calling it?


vimva679
Supporter
Forum|alt.badge.img+9
  • Author
  • Supporter
  • December 8, 2023

@david_r​ tnx for your reply, this is my first python code and am learning so not so much sure, how to get the code snippet formatting. I just copied and pasted from PythonCaller. However i have attached the FME workbench above.


redgeographics
Celebrity
Forum|alt.badge.img+48
vimva679 wrote:

@david_r​ tnx for your reply, this is my first python code and am learning so not so much sure, how to get the code snippet formatting. I just copied and pasted from PythonCaller. However i have attached the FME workbench above. 

imageYou can use that button on a selected piece of text 

To create a code snippet

(and on a general note, a little bit more info in the title of your post would make it easier for people browsing the list of topics to see what it's about)

 


vimva679
Supporter
Forum|alt.badge.img+9
  • Author
  • Supporter
  • December 8, 2023

@Hans van der Maarel​ :) thank you some new learning today


david_r
Evangelist
  • December 8, 2023

Try the following (untested). Be careful to preserve indents. I'm assuming that the incoming attributes DX, DY and DZ are simple numbers (int or float), the code needs modification if that is not the case.

import fme
import fmeobjects
import numpy as np
 
class FeatureProcessor(object):
 
    def __init__(self):
        pass
 
    def input(self, feature):
        # Retrieve FME feature attributes into Python attributes
        L = feature.getAttribute('L')
        F = feature.getAttribute('F')
        DX = float(feature.getAttribute('DX'))
        DY = float(feature.getAttribute('DY'))
        DZ = float(feature.getAttribute('DZ'))
        
        # Call our function
        L, F, DX, DY, DZ, DU, DV, DN = myfunction(L, F, DX, DY, DZ)
        
        # Set FME feature attributes based on return values
        feature.setAttribute('L', L)
        feature.setAttribute('F', F)
        feature.setAttribute('DX', DX)
        feature.setAttribute('DY', DY)
        feature.setAttribute('DZ', DZ)
        feature.setAttribute('DU', DU)
        feature.setAttribute('DV', DV)
        feature.setAttribute('DN', DN)
        
        self.pyoutput(feature)
    
    def close(self):
        pass
 
def myfunction(L, F, DX, DY, DZ):
    DLF = [0,0,-1]
    ULF = [0,0,1]
    NLF = [0,1,0]
    SLF = [0,-1,0]
    ELF = [1,0,0]
    WLF = [-1,0,0]
    
    if  L == "D":
        UAP = DLF	
    elif L == "U":
        UAP = ULF	
    elif L == "N":
        UAP = NLF
    elif L == "S":
        UAP = SLF
    elif L == "E":
        UAP = ELF
    elif L == "W":
        UAP = WLF
    
    if F == "D":
        FAP = DLF
    elif F == "U":
        FAP = ULF
    elif F == "N":
        FAP = NLF
    elif F == "S":
        FAP = SLF
    elif F == "E":
        FAP = ELF
    elif F == "W":
        FAP = WLF
    
    VAP = np.cross(FAP, UAP)
    DU,DV,DN = np.linalg.inv(np.column_stack((UAP, VAP, FAP))).dot(np.array(DX,DY,DZ))
    return L,F,DX,DY,DZ,DU,DV,DN

 


vimva679
Supporter
Forum|alt.badge.img+9
  • Author
  • Supporter
  • December 8, 2023
david_r wrote:

Try the following (untested). Be careful to preserve indents. I'm assuming that the incoming attributes DX, DY and DZ are simple numbers (int or float), the code needs modification if that is not the case.

import fme
import fmeobjects
import numpy as np
 
class FeatureProcessor(object):
 
    def __init__(self):
        pass
 
    def input(self, feature):
        # Retrieve FME feature attributes into Python attributes
        L = feature.getAttribute('L')
        F = feature.getAttribute('F')
        DX = float(feature.getAttribute('DX'))
        DY = float(feature.getAttribute('DY'))
        DZ = float(feature.getAttribute('DZ'))
        
        # Call our function
        L, F, DX, DY, DZ, DU, DV, DN = myfunction(L, F, DX, DY, DZ)
        
        # Set FME feature attributes based on return values
        feature.setAttribute('L', L)
        feature.setAttribute('F', F)
        feature.setAttribute('DX', DX)
        feature.setAttribute('DY', DY)
        feature.setAttribute('DZ', DZ)
        feature.setAttribute('DU', DU)
        feature.setAttribute('DV', DV)
        feature.setAttribute('DN', DN)
        
        self.pyoutput(feature)
    
    def close(self):
        pass
 
def myfunction(L, F, DX, DY, DZ):
    DLF = [0,0,-1]
    ULF = [0,0,1]
    NLF = [0,1,0]
    SLF = [0,-1,0]
    ELF = [1,0,0]
    WLF = [-1,0,0]
    
    if  L == "D":
        UAP = DLF	
    elif L == "U":
        UAP = ULF	
    elif L == "N":
        UAP = NLF
    elif L == "S":
        UAP = SLF
    elif L == "E":
        UAP = ELF
    elif L == "W":
        UAP = WLF
    
    if F == "D":
        FAP = DLF
    elif F == "U":
        FAP = ULF
    elif F == "N":
        FAP = NLF
    elif F == "S":
        FAP = SLF
    elif F == "E":
        FAP = ELF
    elif F == "W":
        FAP = WLF
    
    VAP = np.cross(FAP, UAP)
    DU,DV,DN = np.linalg.inv(np.column_stack((UAP, VAP, FAP))).dot(np.array(DX,DY,DZ))
    return L,F,DX,DY,DZ,DU,DV,DN

 

image


vimva679
Supporter
Forum|alt.badge.img+9
  • Author
  • Supporter
  • December 8, 2023
vimva679 wrote:

image

@david_r​  got it

 

image 

YAHOO>>>>>>first FME Python code ...now working thank you David image


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings