Problem with If , For statements

5560
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
1 Solution

Accepted Solutions
KONPETROV
Occasional Contributor III

Problem solved

import arcpy, os
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
#arcpy.env.workspace = arcpy.GetParameterAsText(0)
arcpy.env.workspace = "C:/Results/ArcMap/Visibility/Calc_View"
ink = arcpy.env.workspace
fc = arcpy.ListFeatureClasses("Pn*", "Point")
ras = arcpy.ListRasters("cl*", "GRID")
i = 0


for seip in fc:
    i= i + 1
    demi = "clpol" + str(i)
    vect = "Pnclpol" + str(i) + ".shp"
    for rast in ras:
        if (seip == vect and rast == demi):
            outViewshed = Viewshed(rast, seip)
            outViewshed.save("C:/Results/ArcMap/Visibility/Calc_View/View" + str(i))

Error 010004 is very hard to get away and I confirm as in the post   Raster NoDATA Value Error 010004 Driving me crazy!  that renaming your files may be a solution to the problem also.

View solution in original post

27 Replies
DanPatterson_Retired
MVP Emeritus

can you throw in some print statements to record the names of the source files and destination files to make sure there is anything to generate...especially OutViewshed.  Also line 23 doesn't appear to be indented properly

0 Kudos
KONPETROV
Occasional Contributor III

You are right about 23 line I didn't cope paste it well.

This is the form of my files

env.workspace = "c:/A/Calc_view"

out = "c:/A/Calc_view/Results"

my points is in the form of Pntclip_pol1, Pointclip_pol2...

and rasters in the form of dempol1, dempol2...

0 Kudos
KONPETROV
Occasional Contributor III

Actually when  i want to print for example

print shp or

print raster I don't get anything back.

0 Kudos
DanPatterson_Retired
MVP Emeritus

That is because there is nothing... for example, the works...it just doesn't do what you want because the list of objects in the ras list is empty and it printed exactly that... for each 'nothing' print nothing

>>> ras = []
>>> for raster in ras:
...  print(ras)
... 
>>>
BillChappell
Occasional Contributor II

Dan is right, when I run lists I always check counts to make sure it has data and matches what I think it should be..

Basic run. put in print statements for debugging, you can watch the progress and later comment or remove them when it works. Add text to help follow it, use the AddMessage option if you want to see the message in the results window.

import arcpy

arcpy.env.workspace =  r"\\gis01\GISTemp\temp.gdb"

fcList = arcpy.ListFeatureClasses()

theCount = len(fcList)

print theCount

for fc in fcList:

    print fc

ordinary least squares USE

arcpy.AddMessage(" Features in my list "+ theCount)

KONPETROV
Occasional Contributor III

I did that but print works only after the first loop. If I put the second for loop I don't have any result. My task is to use for the viewsheds first the Pnt1 with Dem1 then Pnt2 with Dem2 etc. If you have any other suggestion that would be welcomed.

0 Kudos
KONPETROV
Occasional Contributor III

ok first of all I must have my data in a folder and not in a folder inside the other folder* of course

0 Kudos
BillChappell
Occasional Contributor II

Ok,

My thoughts were if either your FC or ras list were empty it would fail, so I check those first.. I’d put a print with a count first. This confirms the data is there.. If you put a print after

for shp in fc:

print shp you could watch for errors as you would know the file being processes.

A

for raster in ras:

print ras would tell you which raster is being processed.

Both of these help rule out data issues, as everyone always looks at the code when it might be a simple data issue. No use troubleshooting code if this is the case.

For grins, comment out lines 6 + 7, just write the variables in as text until you get it and running This takes out dumb input errors, like did they use the backslash or forwardslash, python is funny about that..

My next though is why are you checking : if (shp == point and raster == dem):

You already defined them in the above code. Your loops would stop if you ran out of shapes or rasters..

On line 22, dem = ____ should line up under point = ____

Ok, now for the logic error..

Why do you need lines 21 and 22? They are not part of your naming of the output and it will step though all the rasters and shapes in the list..

Bill

KONPETROV
Occasional Contributor III

I corrected the code as much as I could in that form. But unfortunately i cannot get the 1st pair for the 1st point (Pntclip_pol1) and 1st dem (dempol1) and then rest of them 2,3,4 for some reason. I get only one viewshed

import arcpy, os

from arcpy import env

from arcpy.sa import *

arcpy.env.overwriteOutput = True

#env.workspace = "c:/DEFENCE_DATA/Results/ArcMap/Visibility/Calc_View"

env.workspace = arcpy.GetParameterAsText(0)

out = arcpy.GetParameterAsText(1)

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

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

point = "Pntdempol1.shp"

dem = "dempol1"

i = 1

for seip in fc:

    for rakster in ras:

        if (seip == point and rakster == dem):

            inRaster = rakster

            inObserverFeatures = seip

            outViewshed = Viewshed(inRaster, inObserverFeatures, "")

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

            i = int(i) + 1

            point = "Pntdempol" + str(i)

       dem = "dempol" + str(i)

0 Kudos