0

I have the following code in Python for a tool I want to build. For some reason I viewshed don't exist in the folder I have chose (out). Also when I am trying to define the output workspace every time the file I have created inside the Input folder is deleted so I created one more outside of the folder where my input data are. Why is that? I am using ArcGis 10.2.2

import arcpy, os
from arcpy import env
from arcpy.sa import *

env.workspace = arcpy.GetParameterAsText(0)
out = arcpy.GetParameterAsText(1)
fc = arcpy.ListFeatureClasses("Pnt*", "Point")
ras = arcpy.ListRasters("clip*", "GRID")
point = "Pntclip_pol1"
dem = "clip_pol1"
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)

1 Answer 1

1

I wouldn't worry too much about setting the env.workspace. Just build the full path yourself with the os module:

import arcpy, os

workspace = arcpy.GetParameterAsText(0) 
arcpy.env.workspace = workspace
out = arcpy.GetParameterAsText(1)
fc = arcpy.ListFeatureClasses("Pnt*", "Point")
ras = arcpy.ListRasters("clip*", "GRID")
point = "Pntclip_pol{}"
dem = "clip_pol{}"
i = 1
for shp in fc:
    for raster in ras:
        if (shp == point.format(i) and raster == point.format(i)):
            path = os.path.join(workspace, '{}_view_{}'.format(out, i))
            outViewshed = arcpy.sa.Viewshed(raster, shp, "")
            outViewshed.save(path)
            i += 1
11
  • The lines you threw are crucial for the tool, because I want to do some viewsheds for certain pairs of shp and dem. In any case it doesn't work either. Thanks for trying.
    – Konstantin
    Commented Nov 19, 2015 at 16:29
  • @por.bet It's not clear from the example code how dem and point are used. Unless your conditional statement was meant to compare the variables and not the strings. (i.e., shp == point not shp == 'point'. See the difference?
    – Paul H
    Commented Nov 19, 2015 at 16:33
  • I have some points in the form of Pntclip_pol1, Pntclip_pol2 and some rasters in the form of clip_pol1, clip_pol2 etc. The statement is to make some viewsheds only for these pairs that end at 1,2,3,4..
    – Konstantin
    Commented Nov 19, 2015 at 16:37
  • @por.bet I understand that, but evaluating shp == 'point' will always be False. You need shp == point. See my edits
    – Paul H
    Commented Nov 19, 2015 at 16:39
  • 1
    @por.bet Then try things like printing out the path variable inside the loop. Do some troubleshooting here. I don't have your data to test on.
    – Paul H
    Commented Nov 19, 2015 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.