for-loop doesn't work with parameters

480
5
05-09-2012 10:20 AM
MartinBurger
New Contributor
Hi folks,

I hope I can describe my problem in english, it's hard enough for me in german...

I wrote a Python-script which runs different tools for each row of the attribute-table of an layer by using a SearchCursor and a "for row in rows"-loop.

This worked great so far.

Then I rewrote the script, so that not only I can use it (with my set layers), but anyone else too. Therefore I set parameters for the script in the toolbox, so that anyone who wants to use the script can do this with his own layers.

Now here is the problem:

It works great, but only for the first row. The for-loop doesn't work anymore, it runs the different tools only one time and is finished after that.

Can you please tell me, how i can set the properties of the script, so it can run the for-loop, if I use the parameters?

I searched for long in the online-help, but I found nothing that could help me.

Please excuse my bad spilling, if i wrote a bit strange.

Thank you and greetings,

Martin
Tags (2)
0 Kudos
5 Replies
MikeMacRae
Occasional Contributor III
Martin,

Can you please post your script?

Thanks,
Mike
0 Kudos
MartinBurger
New Contributor
Hello Mike,

here it is:

# Check out any necessary licenses (lädt benötigte Lizenzen)
arcpy.CheckOutExtension("3D")
arcpy.CheckOutExtension("spatial")

# Local variables: (definiert Variablen)

jea1__2_ = arcpy.GetParameterAsText(0)
finras = arcpy.GetParameterAsText(1)
jea1_1 = arcpy.GetParameterAsText(2)
jea1 = arcpy.GetParameterAsText(0)
    
Speicherort_Sichtfeld = arcpy.GetParameterAsText(3)
Speicherort_Reclassify = arcpy.GetParameterAsText(4)
Speicherort_Raster_zu_Polygon = arcpy.GetParameterAsText(5)
Speicherort_Gefaehrdung = arcpy.GetParameterAsText(6)

value =  0


# Definieren des Search Corsor

rows = arcpy.SearchCursor(jea1, "", "", "", "FID A")

# Beginnt mit for-Schleife für row in rows

for row in rows:
    
    # Local variables:


    # value wird aus den Werten der verschiedenen Spalten der jea1-Attributtabelle zusammengesetzt 
    value = row.getValue("Typ") + str(row.getValue("Abteilung")) + "_" + str(row.getValue("lfdNr_Abt"))

    # Speicherorte und -namen der verschiedenen Berechnungen werden definiert
    sichtfeld = Speicherort_Sichtfeld + "\\" + "sife_" + value
    sf_rcl = Speicherort_Reclassify + "\\" + "sf_" + value + "_rcl"
    poly_shp = Speicherort_Raster_zu_Polygon + "\\" + "poly_" + value + ".shp"
    gefaehrdet_shp = Speicherort_Gefaehrdung + "\\" + "gefdt_" + value + ".shp"



    # Process: Layer nach Attributen auswählen
    arcpy.SelectLayerByAttribute_management(jea1__2_, "NEW_SELECTION", "\"FID\" = " + str(row.getValue("FID")))
    
    # Process: Sichtfeld
    arcpy.Viewshed_3d(finras, jea1, sichtfeld, "1", "FLAT_EARTH", "")
    
    # Process: Reclassify
    arcpy.gp.Reclassify_sa(sichtfeld, "VALUE", "0 NODATA;1 1", sf_rcl, "DATA")
    
    # Process: Raster zu Polygon
    arcpy.RasterToPolygon_conversion(sf_rcl, poly_shp, "SIMPLIFY", "VALUE")
    
    # Process: �?berschneiden (Intersect)
    arcpy.Intersect_analysis( [poly_shp, jea1_1] , gefaehrdet_shp, "ALL", "", "INPUT")


The "arcpy.GetParameterAsText()"-commands import the layers I specify in the parameters, which where set by me in the script-properties. I hope I explained it sufficiently.

Regards,

Martin
0 Kudos
MikeMacRae
Occasional Contributor III
Martin,

it looks like you are not incrementing your parameters properly. You have already indexed your first parameter as (0) and then you did it again for 'Jea1' Try incrementing it after your last parameter so:

jea1 = arcpy.GetParameterAsText(7)


I would suggest reordering your indexed parameters so that it is easier to read as well.

Mike
0 Kudos
MartinBurger
New Contributor
Hello Mike,

ok, I changed that. I didn't need the "jea1__2_", so I erased it from the script. The code is now:

# Check out any necessary licenses (lädt benötigte Lizenzen)
arcpy.CheckOutExtension("3D")
arcpy.CheckOutExtension("spatial")

# Local variables: (definiert Variablen)

jea1 = arcpy.GetParameterAsText(0)
finras = arcpy.GetParameterAsText(1)
jea1_1 = arcpy.GetParameterAsText(2)

    
Speicherort_Sichtfeld = arcpy.GetParameterAsText(3)
Speicherort_Reclassify = arcpy.GetParameterAsText(4)
Speicherort_Raster_zu_Polygon = arcpy.GetParameterAsText(5)
Speicherort_Gefaehrdung = arcpy.GetParameterAsText(6)

value =  0


# Definieren des Search Corsor

rows = arcpy.SearchCursor(jea1, "", "", "", "FID A")

# Beginnt mit for-Schleife für row in rows

for row in rows:
    
    # Local variables:


    # value wird aus den Werten der verschiedenen Spalten der jea1-Attributtabelle zusammengesetzt 
    value = row.getValue("Typ") + str(row.getValue("Abteilung")) + "_" + str(row.getValue("lfdNr_Abt"))

    # Speicherorte und -namen der verschiedenen Berechnungen werden definiert
    sichtfeld = Speicherort_Sichtfeld + "\\" + "sife_" + value
    sf_rcl = Speicherort_Reclassify + "\\" + "sf_" + value + "_rcl"
    poly_shp = Speicherort_Raster_zu_Polygon + "\\" + "poly_" + value + ".shp"
    gefaehrdet_shp = Speicherort_Gefaehrdung + "\\" + "gefdt_" + value + ".shp"



    # Process: Layer nach Attributen auswählen
    arcpy.SelectLayerByAttribute_management(jea1, "NEW_SELECTION", "\"FID\" = " + str(row.getValue("FID")))
    
    # Process: Sichtfeld
    arcpy.Viewshed_3d(finras, jea1, sichtfeld, "1", "FLAT_EARTH", "")
    
    # Process: Reclassify
    arcpy.gp.Reclassify_sa(sichtfeld, "VALUE", "0 NODATA;1 1", sf_rcl, "DATA")
    
    # Process: Raster zu Polygon
    arcpy.RasterToPolygon_conversion(sf_rcl, poly_shp, "SIMPLIFY", "VALUE")
    
    # Process: �?berschneiden (Intersect)
    arcpy.Intersect_analysis( [poly_shp, jea1_1] , gefaehrdet_shp, "ALL", "", "INPUT")


As before, the different steps work and the results are ok. But it is still the problem, that it only runs once and is finished then.

When I ran the script without the parameters defined and just had the pathes of the layers instead of the "GetParameterAsText()" in it, it ran the fore-loop until all rows in the attribute-table where edited.


Martin
0 Kudos
MartinBurger
New Contributor
Hey guys,

it's pretty embrassing for me, but I've solved my problem and want to tell you, what was wrong:

As I rewrote the script, so that it could be used by setting parameters, I tested it several times, until it was faultless. While I was testing, I didn't notice, that the first row of my "Jea1"-Layer was selected all the time.

So when I ran the script again and "jea1" was (I don't know how tosay) imported (?), it only imported the first row of the layer. Due to this fact, the script could only run one time and end then, because in this moment there was only one row which could be edited.

Now I added a line to the script, which clears any selection, before the for-loop begins. So it doesn't matter, if anyone forgets to clear the selections, before running the script.

arcpy.SelectLayerByAttribute_management(jea1, "CLEAR_SELECTION", "")
   

Although I think now, how stupid I could be, I'm very happy, that everything works.

Greetings Martin
0 Kudos