Problem with If , For statements

6092
27
Jump to solution
11-23-2015 03:15 AM
KONPETROV
Occasional Contributor III

Hello I have created this code to run a number of viewsheds for a group of points shp Pnt1, Pnt2... and a group of  Dems, dem1, dem2 etc. For some reason I don't have any results but I don't know why. This is the code:

import arcpy, os
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True 

env.workspace = arcpy.GetParameterAsText(0)
out = arcpy.GetParameterAsText(1)
fc = arcpy.ListFeatureClasses("Pnt*", "Point")
ras = arcpy.ListRasters("dem*", "GRID")
point = "Pntclip_pol1"
dem = "dempol1"
i = 1
for shp in fc:
    for raster in ras:
        if (shp == point and raster == dem):
            inRaster = raster
            inObserverFeatures = shp
            outViewshed = Viewshed(inRaster, inObserverFeatures, "")
            outViewshed.save(out + "\\" + "view" + str(i))
            i = int(i) + 1
            point = "Pntclip_pol" + str(i)
       dem = "clippol" + str(i)
0 Kudos
27 Replies
KONPETROV
Occasional Contributor III

Because after the first pair of Point (Pntclip_pol1) and Dem (dempol1) I want to do the viewshed for Pntclip_pol2 and dempol2 etc 3,4,5..

0 Kudos
BillChappell
Occasional Contributor II

Ok,

To break it down:

fclist( PntA, pntB, pntC, pntD) <<< an example list of point features

demlist (dema, demb, demc, demd) <<example list of dems…

count = 1

for i in fclist #PntA first shape in list

for rak in demlist #dema first dem in list

if i == Pnta and rak == dema

outViewshed =Viewshed(rak, i)

outViewshed.save(mypath
"view" str(count)) #view1

count = count + 1

point = Pnta + 1 # Pnta1 #last indented code, last line of if statement

dem = dema+1 #dema1

NOTE: in green Pnta1, if not found your code will bomb here, after first view is run.

Did you want to run everything in the fclist against your first dem?

Then run it all over again against your second dem?

For i in fclist

For x in demlist

Outview =viewshed(I,x)

Save it here

  1. running again will use the first i and the second x and so on

  1. next i will run through (second i) and first x in xlist, then second x in xlist, etc..

Until you finish the I list..

.

0 Kudos
BillChappell
Occasional Contributor II

Forgot in a list (pnta, pntb, pntc)

To get the second item list[1] , you would have to remember the index or make count start at zero.

In your example you could have it just process one right after the other and not worry about the list index number..

0 Kudos
KONPETROV
Occasional Contributor III

I don't think I fully understand you,

I am seeing many errors in  the code, it would be better if you could put it in Python context.

for example

outViewshed.save(mypath"view" str(count)) here it should be (mypath + "view" + str(count))

count = count + int(1)

point = Pnta + str(1)

Unfortunately my code isn't right as well I can get only one Viewshed

0 Kudos
BillChappell
Occasional Contributor II

It’s your logic that’s tripping you.

• import arcpy, os

• from arcpy import env

• from arcpy.sa import *

• arcpy.env.overwriteOutput = True

• env.workspace = arcpy.GetParameterAsText(0)

• out = arcpy.GetParameterAsText(1)

• fc = arcpy.ListFeatureClasses("Pnt*", "Point")

• ras = arcpy.ListRasters("dem*", "GRID")

• point = "Pntclip_pol1"

• dem = "dempol1"

• i = 1

• for shp in fc: <<< First time in loop this is Pntclip_pol1???? 2nd time it’s Pntclip_pol2 ??

If it’s Pntclip_pol1 why did you define it?? It’s the first item in your fc list.

• for raster in ras: first time it’s dempol1?? Is that also the first item in your dem list?

• # if (shp == point and raster == dem): ???WHY??? Not needed..

• #inRaster = raster # not needed

• # inObserverFeatures = shp #not needed

• #outViewshed = Viewshed(inRaster, inObserverFeatures, "")

outViewshed = Viewshed(raster, shp)

• outViewshed.save(out + "
" + "view" + str(i))

• i = i + 1 #already an int as declared above..

• #point = "Pntclip_pol" + str(i)

• # dem = "clippol" + str(i)

Just say you have 3 points and 5 dems

For i in pointlist:

For dem in demlist:

theViewshed = viewshed(dem, i)

the Viewshed.save (xxxxxx+ str(1))

Here the loop would use the same i and run through the next dem

Once finished it would loop the next i and toss you back into the dem loop

1 pt

1 dem

Viewshed (1 dem, 1 pt)

Now loop

2 dem

Viewshed( 2 dem, 1 pnt)

Now loop

3 dem

Viewshed( 3 dem, 1 pt)

Now loop

Etc.. until finished with dems then kick to 2nd point

2 pt

1 dem

Viewshed (1 dem, 2 pt)

Now loop

2 dem

Viewshed( 2 dem, 2 pnt)

Now loop

3 dem

Viewshed( 3 dem, 2 pt)

Now loop

You don’t need to name the items in each list as they will always advance to the next item in each list.

Leave i =1 and have I = i+1 inside the dem loop so your new views are numbered..

0 Kudos
KONPETROV
Occasional Contributor III

That's a very analytic answer Mr Chappell, thank you very much.  I will study the code and I will reply you.

0 Kudos
KONPETROV
Occasional Contributor III

Ok I understand you now. Well at your question,  I define point and dem because each dem has only one specific point. Dem 1 has only point 1, dem2 has only point 2 etc..

0 Kudos
BillChappell
Occasional Contributor II

Oops, I screwed up , change my working script to:

fcList = arcpy.ListFeatureClasses("Pnt*", "Point")

#rasList = ras = arcpy.ListRasters("*", "All")

ras = “dem”

i = 1

for fc in fcList:

ras = ras + str(i)

print "Point is: " + fc + " the DEM is: " + ras

outViewshed = Viewshed(ras, fc)

outViewshed.save("C:/test/out/outvwshd"+ str(i))

ras = “dem”

i=i+1

print "Done"

Sorry I messed up I do a lot of point intersects with polygons, and I often step through 2 lists..

0 Kudos
KONPETROV
Occasional Contributor III

I will check it out also but the previous code returned me only Pointclippol1 & Pnt1,  Pointclippol1 & Pnt2 and then again the same error.

0 Kudos
BillChappell
Occasional Contributor II

This might help..

0 Kudos