I want to make a simple if/else function inside my Model and I wanted to know if it's possible or not.
I want this thing to do that if an Input for 'Segment Layer' is selected that it doesn't have to execute the 'Segmentation(meanshift)' and uses 'Segment Layer' instead as an Input for the next Process.
the Inputs for the 'Segmentation(meanshift)'are as follows
Update after trying to implement Joseph's answer: I used the following code and got this error enter image description here
Update 2, after replacing the parameters I get an error in line 28 this is were 'else:' stands
##Example=name
##BGREN=optional raster
##Segment_Layer=optional vector
##output=output vector
if BGREN is not None:
processing.runalg('otb:segmentationmeanshift', \
-BGREN\
-0\
-70\
-70\
-0.1\
-100\
-100\
-0\
-0\
-0\
-'True'\
-'False'\
-1\
-0.1\
-'Layer'\
-'DN'\
-1024\
-1\
-0\
-output)
else:
output=Segment_Layer
Update it works :) after a few try's and with Joseph help this code now works like a charm, the final trick was to replace two optional values with 'None'.
##Example=name
##BGREN=optional raster
##Segment_Layer=optional vector
##output=output vector
if BGREN is not None:
processing.runalg('otb:segmentationmeanshift', \
BGREN, \
0, \
70, \
70, \
0.1, \
100, \
100, \
0, \
0, \
None, \
True, \
False, \
1, \
0.1, \
'Layer', \
'DN', \
1024, \
1, \
None, \
output)
else:
output=Segment_Layer
-
3I think you will need to use a custom script inside your modeler in order to use if/else statements correctly.Joseph– Joseph2017年02月13日 14:25:07 +00:00Commented Feb 13, 2017 at 14:25
-
1@Joseph that's what I thought too, the Problem is I'm not as good in scripting as i would like to be, I pretty much have no expirience at all...Andreas– Andreas2017年02月13日 14:32:42 +00:00Commented Feb 13, 2017 at 14:32
-
1@Andreas I confirm what Joseph wrote in its comment. However, I think you may get more help from the community if you edit your question and give the possibility to get some hints with Python (adding also a proper tag)...mgri– mgri2017年02月13日 16:01:09 +00:00Commented Feb 13, 2017 at 16:01
-
1@Andreas - Edited my post. Also, you should accept answers after they have solved your problem ;)Joseph– Joseph2017年02月14日 10:22:00 +00:00Commented Feb 14, 2017 at 10:22
-
1@Joseph I edited my post with the now working code, your help was much needed and I'm really glad you took some of your precious time to help me with this :) as of right now it's executing the algorithm and i have to wait for the results but it looks really promising, thank you again :)Andreas– Andreas2017年02月14日 10:40:26 +00:00Commented Feb 14, 2017 at 10:40
1 Answer 1
As mentioned in my comment, a custom script may be the way to go in this situation. You can create one from:
Processing Toolbox > Scripts > Tools > Create new script
The following script sets the input parameters as optional along with an if/else statement depending on which parameter contains a layer. So if a layer was selected for BGREN
, it will perform the segmentation tool and sets the output; otherwise the output will be the layer selected from Segment_Layer
.
Here is a possible script:
##Example=name
##BGREN=optional raster
##Segment_Layer=optional vector
##output=output vector
if BGREN is not None:
processing.runalg('otb:segmentationmeanshift', \
-BGREN, \
-filter, \
-filter.meanshift.spatialr, \
-filter.meanshift.ranger, \
-filter.meanshift.thres, \
-filter.meanshift.maxiter, \
-filter.meanshift.minsize, \
-mode, \
-mode.vector.outmode, \
-mode.vector.inmask, \
-mode.vector.neighbor, \
-mode.vector.stitch, \
-mode.vector.minsize, \
-mode.vector.simplify, \
-mode.vector.layername, \
-mode.vector.fieldname, \
-mode.vector.tilesize, \
-mode.vector.startlabel, \
-mode.vector.ogroptions, \
-output)
else:
output=Segment_Layer
The parameters for the otb:segmentationmeanshift
look scary! Unfortunately, I do not have Orfeo installed so cannot test this tool. However, the parameters are shown here, you just need to type them in instead of using the GUI. Once done, add the script into your modeler using BGREN
and Segment_Layer
as input layers:
Also, in your modeler, you may need to change the Required parameter definition of BGREN
and Segment_Layer
:
Then connect the output of the script to your NDVI tool.
I tested this to some extent using other tools which worked depending on which input layer was selected so hopefully it will work for your modeler!
EDIT:
Here is the code you used slightly modified (you need the commas to separate the parameters, True
and False
statements don't require quotes around them, the backward slash is just for readability but will include it as a one-liner):
##Example=name
##BGREN=optional raster
##Segment_Layer=optional vector
##output=output vector
if BGREN is not None:
processing.runalg('otb:segmentationmeanshift', \
BGREN, \
0, \
70, \
70, \
0.1, \
100, \
100, \
0, \
0, \
0, \
True, \
False, \
1, \
0.1, \
'Layer', \
'DN', \
1024, \
1, \
0, \
output)
else:
output=Segment_Layer
Or as a one-liner:
##Example=name
##BGREN=optional raster
##Segment_Layer=optional vector
##output=output vector
if BGREN is not None:
processing.runalg('otb:segmentationmeanshift',BGREN,0,70,70,0.1,100,100,0,0,0,True,False,1,0.1,'Layer','DN',1024,1,0,output)
else:
output=Segment_Layer
-
1I was adding a comment for encouraging @Andreas to edit its question and give the possibility to get some hints with Python for receiving more help, but you were faster!mgri– mgri2017年02月13日 15:41:24 +00:00Commented Feb 13, 2017 at 15:41
-
1@mgri - I would encourage you to post that comment as the OP might want to edit some details in and hopefully encourage others to post a possible solution :)Joseph– Joseph2017年02月13日 15:45:43 +00:00Commented Feb 13, 2017 at 15:45
-
1@Joseph damn that's a lot more than i thought it would be :O anyway I'll try to recreate this and leave a feedback if i got it to work, thanks in advance :)Andreas– Andreas2017年02月13日 15:57:16 +00:00Commented Feb 13, 2017 at 15:57
-
1Most welcome! I probably did overthink this so it may be a good idea to edit your question as @mgri suggested so that a more suitable python solution or such can be posted =)Joseph– Joseph2017年02月13日 16:00:01 +00:00Commented Feb 13, 2017 at 16:00
-
1@Andreas - Not stupid at all, everyone has to learn from the beginning =)Joseph– Joseph2017年02月14日 09:51:04 +00:00Commented Feb 14, 2017 at 9:51
Explore related questions
See similar questions with these tags.