Using GetParameterAsText() with Cursor.

1950
8
11-11-2014 07:51 AM
anTonialcaraz
Occasional Contributor II

Hi, I'm trying to use the GetParameterAsText() for user input.

When I use a value instead, it works fine but not with GetParameterAsText()

Any help will be greatly appreciate.

               NOT WORKING                                                                 WORKING

Capture.JPGCapture2.JPG

AreaFilter = arcpy.GetParameterAsText(0)

cur = arcpy.UpdateCursor(outFeatureAREA)

for row in cur:

    if row.F_AREA <= AreaFilter:

        cur.deleteRow(row)

        cur.updateRow(row)

del row

del cur

0 Kudos
8 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Alcaraz,

Try using the following:

AreaFilter = float(arcpy.GetParameterAsText(0))

0 Kudos
anTonialcaraz
Occasional Contributor II

Thanks Jake. Yes, I had already try that. I don't quite understand why it's not working.

Cheers

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Are you running the tool as a script within ArcMap?  If so, what is the Data Type you have specified for this parameter?  Try specifying this as a 'Double'.  Ex:

screen1.png

0 Kudos
anTonialcaraz
Occasional Contributor II

Hi, I was being too optimistic.

Here is the whole code:

===============================================================================

import arcpy

import string

import math

from arcpy import env

arcpy.env.overwriteOutput=True

from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

# Set workspace location, extent and variables

INWorkspace = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL\00_LFP_GLOBAL_Python\WORK_TEST_space\ISLAND_Effect\INPUT.gdb"

arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)

inFeature = INWorkspace + "\\" + "G_SL_HiStand_20M_J_Tith_m2_0"

outFeature = INWorkspace + "\\" + "HiStand_polygon"

outFeatureAREA = INWorkspace + "\\" + "HiStand_polygonAREA"

# Convert polyline feature to polygon

arcpy.FeatureToPolygon_management(inFeature, outFeature, "","NO_ATTRIBUTES", "")

# Calculate area in KM2

arcpy.CalculateAreas_stats(outFeature, outFeatureAREA)

Expr = "(!F_AREA!) / 1000000"

arcpy.CalculateField_management(outFeatureAREA,"F_AREA",Expr,"PYTHON")

# Filter by Area

AreaFilter = float(arcpy.GetParameterAsText(0))

cur = arcpy.UpdateCursor(outFeatureAREA)

for row in cur:

    if row.F_AREA <= AreaFilter:

        cur.deleteRow(row)

        cur.updateRow(row)

del row

del cur

===============================================================================

If I run the script up to "# Filter by Area" it will create a Polygon Feature Class (HiStand_polygonAREA) with a field called "F_Area" holding all area values.

If I then run the script only from "# Filter by Area" with "HiStand_polygonAREA" as the input feature class (outFeatureAREA) it works.

If I run the whole script at once it still produces the output required (HiStand_polygonAREA) but it doesn't perform anything within the cursor part, for example, deleting those rows <= than 1000; and I get this:

Capture2.JPG

So, I'm lost now.

0 Kudos
Luis_ÁngelPascual_Camino
New Contributor III

Hi again.

It looks like the value entered in the script execution dialog window (where you have written the parameter declared into scripts parameters) is a literal like "HiStand_polygon" and it cannot be converted to float.Have you entered a number in this parameter at execution time?

Is it the only one or do you have more parameters? You know the order is important, the first parameter declared has position number 0, the second has 1, and so on...

I can't think anything else, well, only one comment: why don't you get the parameter at the beginning of the script?
Maybe you can isolate the problem. I mean that there are others function calls along the code and the script can override the "parameter" value with the last one executed. (I don't know exactly, it's only an intuition).

Good luck!

Luis

anTonialcaraz
Occasional Contributor II

Thanks a lot Luis.

I put the parameter at the beginning of the script (as I normally do actually) and now IT WORKS!

That "rule" seems obviously to be important and I'm sure there is an explanation for it...

Many thanks!

Luis_ÁngelPascual_Camino
New Contributor III

Hi Alcaraz.

What about this?

AreaFilter = arcpy.GetParameter(0)

#If you want to convert to float:

AreaFilter = float(arcpy.GetParameter(0))

I used this code in v9.3:

import sys

AreaFilter = sys.argv[1] # The first argument has index number 1

I hope it helps. Good luck!

Luis

0 Kudos
anTonialcaraz
Occasional Contributor II

Gracias Luis,

Just found that what was going on:

The "AreaFilter" field data type on the attribute table was "Double"; that matches the data type for the parameter within the tool properties dialogue box (see image) (there is no "Float" option).

Capture1.JPG

Well, apparently python doesn't like that. I created a new "Float" Area field and used it in the script instead of AreaFilter. Now it works.

I think that the fact that there is no "Float" option in the Data Type python tool may lead to confusion.

Cheers

0 Kudos