2

I have exported "Con Tool" script from ModelBuilder. The model has two input parameters one is the DEM and the second is "Long Variable". When I run the model it executes perfectly fine but as I import the script to tool box and assign the variables and type and run them the Python log shows errors. I believe the script is taking the variable name instead of its value.

How do I fix that?

This is the script:

# ---------------------------------------------------------------------------
# conmodelscript.py
# Created on: 2020年07月24日 11:51:39.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: conmodelscript <DEM> <Input_Value> 
# Description: 
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
# Script arguments
DEM = arcpy.GetParameterAsText(0)
Input_Value = arcpy.GetParameterAsText(1)
# Local variables:
Long = "1"
Output = "C:\\Users\92347円\\Desktop\\test\\output.tif"
# Process: Con
arcpy.gp.Con_sa(DEM, Long, Output, "", "Value<=%Input Value%")

This is the model

enter image description here

enter image description here

enter image description here

enter image description here

This is how I add and run the script

enter image description here

enter image description here

enter image description here

enter image description here

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 24, 2020 at 7:07
1
  • 2
    Please Edit the question to contain the error message as text, so that is searchable by others. Commented Jul 24, 2020 at 7:23

1 Answer 1

3

Your code has inherited the ModelBuilder inline substitution syntax, which you need to fix to make the code work.

Change this line:

arcpy.gp.Con_sa(DEM, Long, Output, "", "Value<=%Input Value%")

to:

arcpy.gp.Con_sa(DEM, Long, Output, "", "Value <= " + Input_Value)

As you set the variable Input_Value using GetParameterAsText() it is converting the number into a string so a simple concatenation is all that is required for the where clause in Con().

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Jul 24, 2020 at 11:01
2
  • 1
    Thank you so so much. You're a life savior <3 God Bless Commented Jul 24, 2020 at 12:16
  • 2
    @MudassarShah If this has answered your question please consider up-voting and marking as the answer. See What should I do when someone answers my question? Commented Jul 24, 2020 at 21:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.