import arcpy 
# Retrieve input parameters
inX = arcpy.GetParameterAsText(0)
inY = arcpy.GetParameterAsText(1)
inDescription = arcpy.GetParameterAsText(2)

incidentsFC = "C:/Data/Yakima/Incidents.shp"
descriptionField = "DESCR"

# Make a tuple of fields to update
fieldsToUpdate = ("SHAPE@XY", descriptionField)

# Create the insert cursor
with arcpy.da.InsertCursor(incidentsFC, fieldsToUpdate) as cursor:
    # Insert the row providing a tuple of affected attributes
    cursor.insertRow(((float(inX),float(inY)), inDescription))

I came across this script tool in an online course which shows an example for using the Insert Cursor where we insert a row in a point shapefile. I couldn't understand something here. Do we somehow specify what 'type' of input we will provide when we use GetParameterAsText()?

If yes, then why do we need to specify again in the last line that the coordinates given earlier are float type?

Can't we just write cursor.insertRow((inX, inY), inDescription) and if No, then how does Python understand the input in the variable inDescription is a string/text without using the double quotes anywhere?


As the name GetParameterAsText() indicates, or the documentation states, the value will be converted to text, or like we call it: a string.

Gets the specified parameter as a **text** string by its index position from the list of parameters. 

So, if the user enteres coordinates, such as 35.5432 then the tool would get understand those as '35.5432', a string. This is, however, not necessarily what we want, so, in this case, we would convert that string back to a number. As we are interested in the decimals we have to convert it to a float (not an integer, as it would chop off the decimals), and use float().

Note that you could also have converted right when getting the parameters:

inX = float(arcpy.GetParameterAsText(0))

In my opinion that makes the code easier to read, and less cluttered later on.

The important part with GetParameterAsText() is what you define in ArcGIS, when you build the tool. Here the more important, ArcGIS-internal data type, is defined. It can be daunting at first, and sometimes one has to try multiple times until finding the correct type. The interface looks like this (sorry, German interface!):

enter image description here

But the bottom line is, it would arrive in your Python as text, so sometimes conversions are necessary, such as the float() in this case!

  • Got it. But I have one question. Since GetParameterAsText() converts stuff to string then how does this above interface work? For e.g. if I set Double from this list then the input of the GetParameterAsText() function will be considered as Double and not a string this time? I think so...isn't it? And if I dont select anything from this list then it will remain a string by default? Also if I select float in this list then I dont need to use the float() function in my code right? Btw thanks for the handy tip for using float() early in the code with GetParameterAsText(). – Salman Feb 3 '15 at 12:13

To respect the type of input set on a Python script tool's dialog you would need to use arcpy.GetParameter().

By using arcpy.GetParameterAsText() my understanding is that whatever is ingested has its data type converted to a string.

It appears that float is being used here to convert those strings into floating point numbers.

  • GetParameterAsText returns a string... obviously! You may consider getting the parameter as text then doing some tests to ensure that the value is valid. Python has a nice try: statement, try: convert text to what you need except: arcpy.AddError("cannot convert") then consider sys.exit(0) to terminate the script. Of course you can use try/except on the getting of the parameters and use GetParameter which will go to the except if the parameter given doesn't match the type expected. – Michael Stimson Feb 1 '15 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.