Вопрос

my task is to execute multiple buffers with the list-loop!

import arcpy

arcpy.env.overwriteOutput = 1  
arcpy.env.workspace="C:\\salzburg.gdb"  
sbgRivers="sbg_rivers"
buff_name=raw_input("Please insert a file name:")  
for buffer_size in [100,200,300,450]:  
    outfile="C:\\salzburg.gdb\\buffer_output"
    arcpy.Buffer_analysis(sbgRivers,buff_name+str(buffer_size),buffer_size)  
    print "Buffer complete"

So the next step is: If the file name already exists(raw_input), the user is asked to enter a new data set name until a name is found, which doesn´t exist yet!

I thought about a while loop, but i´m not sure how to integrate it in the code above

i ended up with this

import arcpy
arcpy.env.overwriteOutput = 1  
arcpy.env.workspace="C:\\salzburg.gdb"  
sbgRivers="sbg_rivers"
buff_name=raw_input("Please insert a new data set name:")
while arcpy.Exists(buff_name):    
    buff_name=raw_input("Please enter a new data set name")  
    for buffer_size in [100,200,300,450]:    
         arcpy.Buffer_analysis(sbgRivers,buff_name+str(buffer_size),buffer_size) 
         print "Buffer complete"

What do you think? Any help is welcome and appreciated!

Это было полезно?

Решение

You would want to first make sure that you have the correct filename and then proceed to the next step. The code should be: (assuming arcpy.Exists(buff_name) does what it is supposed to do, because I think it should be arcpy.Exists(buff_name+str(100)) or something like it.)

import arcpy
arcpy.env.overwriteOutput = 1  
arcpy.env.workspace="C:\\salzburg.gdb"  
sbgRivers="sbg_rivers"
buff_name=raw_input("Please insert a new data set name:")
while arcpy.Exists(buff_name):    
    buff_name=raw_input("Name already exists. Please enter a new data set name")  
for buffer_size in [100,200,300,450]:    
    arcpy.Buffer_analysis(sbgRivers,buff_name+str(buffer_size),buffer_size) 
    print "Buffer complete"

Your previous code would have run the for loop for all the inputs provided (even the wrong ones).

Другие советы

Ok If you Want to use input() In a while loop the code will be like this:

sett=True
while sett:
    buff_name=raw_input("Please insert a file name:")  
    if buff_name=="":
        continue 
    else: 
        break ##Or: sett=False
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top