Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm rather new to python so my code might be horrid but I had a question about executing scripts from ArcMap and performance involving several if statements.

The main objective is to create a toolbox with several different possible executions depending on the users input.

Essentially I have two options that I see!?! --- note there might be errors as I am simply writing an example code that represents what I am doing. At the moment, iterating through only 5 (6000x6000cells) rasters my code runs in hours that in my mind should not take more than one.

import arcpy
arcpy.checkoutextension('spatial')

def main(inputfolder,outputfolder,x,y):
    inputfolder = arcpy.env.workspace
    RasterList = arcpy.ListRasters()

    for RasterImage in RasterList:
         try:
             if 'True' in x:
                  sa.Slope(.....)
             else:
                  if 'True' in y:
                       sa.Reclassify(....)
                  else: 
                       RasterImage * 2
         except Exception e:
              arcpy.GetMessages(e)

or is defining separate functions better? i.e.

def main(inputfolder,outputfolder,x,y):
    inputfolder = arcpy.env.workspace
    arcpy.ListRasters()

    if 'True' in x:
        x(inputfolder, outputfolder, RasterList)
    elif 'True' in y:
        y(inputfolder, outputfolder, RasterList)
    else:
         for RasterImage in RasterList:
             try:
                 RasterImage *2

def x(inputfolder, outputfolder, RasterList)
    for RasterImage in RasterList: 
         ....

def y(inputfolder, outputfolder, RasterList)
     .....

Any help would be most appreciated!

UPDATE

Solved my problem. Thanks for all the responses and help.

The problem was not with my code but rather that a few of the datasets that I had been given were corrupt. Using a try: except: I was able to pass through those corrupt files (write an error log of those files) and later delete them.

share|improve this question
    
What is the code testing for btw? What does x and y denote? –  R.K. Feb 25 '12 at 11:18
    
Sorry perhaps that was not clear. X and Y simple denotes 'True' and 'False' statements from the user input signifying whether a certain action should be performed. So in this example, if the user enters e.g. 'False' it is presumed that the raster dataset is already a slope and therefore would not need to be calculated - hence only reclassification is performed. –  BJEBN Feb 25 '12 at 12:53
    
Ah, it's more of a choice of operations then? Get slope if X is true and reclass if Y is true? Are they mutually exclusive? I mean if you reclass, you don't get the slope and vice versa? –  R.K. Feb 25 '12 at 14:01
    
Yes... precisely. If I did a standalone script perhaps I would use a checkbox instead of a 'True' 'False' string but Id like to use the toolbox within ArcMap. –  BJEBN Feb 25 '12 at 15:23
    
Got it. What is RasterImage * 2 meant to achieve btw? Also, I'm confused about your question as the scenario you're showing only needs one for loop to iterate over the RasterList. –  R.K. Feb 25 '12 at 15:49

2 Answers 2

up vote 2 down vote accepted

There is no problem running it from ArcMap or running it as a standalone script. Both of them will do the same.

The perfomance of if statements is neglectable. If your tests are arcpy function calls, they will need some time to be executed and return True or False. Otherwise, if you are just testing Python objects, they will be fast, at least fast enough.

Some people mention that ArcPy is riddled with memory leaks, that's why usually it takes so long to get things done. You can confirm that by processing a batch with one image and measure time, then do another batch with 10 images, and then another with 100. If the time taken to execute is much bigger on the bigger batches, you probably found a memory leak (this explanation is simplistic, but somewhere along these lines).

Otherwise I don't see anything in your example code that might be too time consuming. ArcPy is taking the time to execute each of your functions and outputting.

Do you see a lot of difference between executing this process inside ArcGIS (point n' click) and the executing via Python?

share|improve this answer
    
Yes... I just realized that the function almost appears to be stuck on the slope calculation in the script version. Takes over an hour(quit after that) compared to around 5min in ArcMap. –  BJEBN Feb 25 '12 at 22:54

Here is one trick i learned over the years. If you don't need a map while the script is running, run the python code standalone. It cuts down on the overhead of having ArcMap and a bunch of layers in memory.

share|improve this answer
    
Yah I figured as much... the reason i'd like to keep it in ArcMap is in order to design a toolbox that can later be distributed to others. I suppose I could always design my own GUI but it does sound like a lot of extra work... –  BJEBN Feb 25 '12 at 15:20
    
@BJEBN Designing your own GUI (via Python, I assume) isn't going to work in AG 10.0 and prior according to this. 10.1 may allow it, though. –  Michael Todd Feb 25 '12 at 17:50
    
@MichaelTodd Not even a standalone GUI calling parameters with sys.argv[]? Sounds strange although I think that discussion might have been designing the UI within ArcMap –  BJEBN Feb 25 '12 at 21:00
    
@BJEBN That was my understanding as well, that the discussion was related to developing the GUI within ArcMap. I thought that was what you were doing so I thought I'd mention it. Not sure about developing a GUI outside ArcMap, though. –  Michael Todd Feb 27 '12 at 15:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.