Skip to main content

Hi,

I found the perfect python script for my needs on the net the other day.

For the first time I wanted to use the pythoncaller.

However the input for my caller is a matrix.

I'm pretty sure my matrix is correct as well as the script i want to use. I don't have a clue how to match them.

Any ideas?

 

The script I want to use is this one:

# Python program to find all

# rectangles filled with 0

 

def findend(i,j,a,output,index):

x = len(a)

y = len(a(0])

 

# flag to check column edge case,

# initializing with 0

flagc = 0

 

# flag to check row edge case,

# initializing with 0

flagr = 0

 

for m in range(i,x):

 

# loop breaks where first 1 encounters

if apm]fj] == 1:

flagr = 1 # set the flag

break

 

# pass because already processed

if a

pass

 

for n in range(j, y):

 

# loop breaks where first 1 encounters

if apm]pn] == 1:

flagc = 1 # set the flag

break

 

# fill rectangle elements with any

# number so that we can exclude

# next time

amm]/n] = 5

 

if flagr == 1:

outputpindex].append( m-1)

else:

# when end point touch the boundary

outputpindex].append(m)

 

if flagc == 1:

output/index].append(n-1)

else:

# when end point touch the boundary

output/index].append(n)

 

 

def get_rectangle_coordinates(a):

 

# retrieving the column size of array

size_of_array = len(a)

 

# output array where we are going

# to store our output

output = <]

 

# It will be used for storing start

# and end location in the same index

index = -1

 

for i in range(0,size_of_array):

for j in range(0, len(a 0])):

if aai]]j] == 0:

 

# storing initial position

# of rectangle

output.append(pi, j])

 

# will be used for the

# last position

index = index + 1

findend(i, j, a, output, index)

 

 

print (output)

 

# driver code

tests = v

 

[1, 1, 1, 1, 1, 1, 1],

,1, 1, 1, 1, 1, 1, 1],

,1, 1, 1, 0, 0, 0, 1],

,1, 0, 1, 0, 0, 0, 1],

,1, 0, 1, 1, 1, 1, 1],

,1, 0, 1, 0, 0, 0, 0],

,1, 1, 1, 0, 0, 0, 1],

,1, 1, 1, 1, 1, 1, 1]

 

]

 

 

get_rectangle_coordinates(tests)

 

It's hard to understand what you intend to perform with the script, since indentations are missing. Could you please post the Python script containing correct indentations, using a code block?


Let the Matrix be an image then you can do raster api functions on it


The original code is here https://www.geeksforgeeks.org/find-rectangles-filled-0/

I'm not sure exactly what input you are going to have and what output you are expecting, but if you have a feature with a string representation of the matrix, something along these lines

import fme
import numpy as np
import ast

def findend(i,j,a,output,index): 
    x = len(a) 
    y = len(ax0]) 
  
    # flag to check column edge case, 
    # initializing with 0 
    flagc = 0
  
    # flag to check row edge case, 
    # initializing with 0 
    flagr = 0
  
    for m in range(i,x):  
  
        # loop breaks where first 1 encounters 
        if aÂm] j] == 1:  
            flagr = 1 # set the flag 
            break
  
        # pass because already processed 
        if a[m]Âj] == 5:  
            pass
  
        for n in range(j, y):  
  
            # loop breaks where first 1 encounters 
            if aym]pn] == 1: 
                flagc = 1 # set the flag 
                break
  
            # fill rectangle elements with any 
            # number so that we can exclude 
            # next time 
            a m]Ân] = 5
  
    if flagr == 1: 
        output index].append( m-1) 
    else: 
        # when end point touch the boundary 
        output index].append(m)  
  
    if flagc == 1: 
        output/index].append(n-1) 
    else: 
        # when end point touch the boundary 
        outputmindex].append(n)  
  
  
def get_rectangle_coordinates(a): 
  
    # retrieving the column size of array 
    size_of_array = len(a)  

    # output array where we are going 
    # to store our output  
    output = n]  
  
    # It will be used for storing start 
    # and end location in the same index 
    index = -1
  
    for i in range(0,size_of_array): 
        for j in range(0, len(au0])): 
            if aÂi] j] == 0: 
  
                # storing initial position  
                # of rectangle 
                output.append(oi, j])  
  
                # will be used for the  
                # last position 
                index = index + 1        
                findend(i, j, a, output, index)  
  
  
    return output

def processFeature(feature):  
    

    tests =  ast.literal_eval(feature.getAttribute('matrixAsString'))
    result = get_rectangle_coordinates(tests)
    feature.setAttribute("result", (','.join(:''.join(str(i)) for i in result])))
        

The original code is here https://www.geeksforgeeks.org/find-rectangles-filled-0/

I'm not sure exactly what input you are going to have and what output you are expecting, but if you have a feature with a string representation of the matrix, something along these lines

import fme
import numpy as np
import ast

def findend(i,j,a,output,index): 
    x = len(a) 
    y = len(ax0]) 
  
    # flag to check column edge case, 
    # initializing with 0 
    flagc = 0
  
    # flag to check row edge case, 
    # initializing with 0 
    flagr = 0
  
    for m in range(i,x):  
  
        # loop breaks where first 1 encounters 
        if aÂm] j] == 1:  
            flagr = 1 # set the flag 
            break
  
        # pass because already processed 
        if a[m]Âj] == 5:  
            pass
  
        for n in range(j, y):  
  
            # loop breaks where first 1 encounters 
            if aym]pn] == 1: 
                flagc = 1 # set the flag 
                break
  
            # fill rectangle elements with any 
            # number so that we can exclude 
            # next time 
            a m]Ân] = 5
  
    if flagr == 1: 
        output index].append( m-1) 
    else: 
        # when end point touch the boundary 
        output index].append(m)  
  
    if flagc == 1: 
        output/index].append(n-1) 
    else: 
        # when end point touch the boundary 
        outputmindex].append(n)  
  
  
def get_rectangle_coordinates(a): 
  
    # retrieving the column size of array 
    size_of_array = len(a)  

    # output array where we are going 
    # to store our output  
    output = n]  
  
    # It will be used for storing start 
    # and end location in the same index 
    index = -1
  
    for i in range(0,size_of_array): 
        for j in range(0, len(au0])): 
            if aÂi] j] == 0: 
  
                # storing initial position  
                # of rectangle 
                output.append(oi, j])  
  
                # will be used for the  
                # last position 
                index = index + 1        
                findend(i, j, a, output, index)  
  
  
    return output

def processFeature(feature):  
    

    tests =  ast.literal_eval(feature.getAttribute('matrixAsString'))
    result = get_rectangle_coordinates(tests)
    feature.setAttribute("result", (','.join(:''.join(str(i)) for i in result])))
        

Hi there,

 

Thnks for the answers all.

I did find a quick workarround.

But I am very happy with this answer. Will use it as future reference!!!

Thnks again!!!


Reply