Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Arcpy: Processing multiple files subsequently

$
0
0

I have an indexed dictionary containing files and want to “mosaic” (arcpy.MosaicToNewRaster_management) all files with the same index. The number of files per index range from 1 to about 30.

dic_of_files = {"index1":["file11", "file12","file13"], "index2":["file21", "file22"]}

for key in dic_of_files.iterkeys():
    arcpy.MosaicToNewRaster_management(dic_of_files[key], otherarcpyoptions)

Mosaicing 30 files in one go doesnt seem to work, I guess number is just to high. I had the idea of writing a script where python counts the number of files per index via and the loops through processing first file 1 and 2, then processing the result with file 3 and so on till it reaches the last file.

for key in dic_of_files.iterkeys():
    key_len = len(dic_of_files[key])
    if key_len < 2:
        arcpy.MosaicToNewRaster_management(dic_of_files[key], otherarcpyoptions)
    else:
        yes.. else what?

Can anybody tell me if this is a reasonable way to solve the problem? And maybe give me a hint how to script this solution?

EDIT:
ok I’ve gotten one step farther. I’ve nested a couple of loops so that python pairs up the first two items in the list, then the next two and so on (in case of an uneven item number it pairs up the last three).

The code now looks as follows (I’m omitting the arcpy function to keep it as simple as possible):

dic_of_files = {"index1": ["file10", "file11", "file12", "file13", "file14", "file15", "file16", "file17"], "index2": ["file20", "file21", "file22"]}
dic_of_pairs = {}

def addstuff(i="", v="", mydict=""):
    if i not in mydict:
        mydict[i] = []
    mydict[i].append(v)

for key, val in dic_of_files.items():
    key_len = len(val)
    if key_len < 2:
        print "do nothing"
    else:
        if key_len % 2 == 0:
            for j in range(0, (key_len - 1), 2):
                # Even number of files, process pairs all through
                output = str(str(j) + str(j + 1))
                addstuff(key, output, dic_of_pairs)
        else:
            for k in range(0, (key_len - 2), 2):
                if k < (key_len - 3):
                    # Uneven number of files. Process pairs at first..
                    output = str(str(k) + str(k + 1))
                    addstuff(key, output, dic_of_pairs)
                else:
                    # ... and process the last three files as triplets
                    output = str(str(k) + str(k + 1) + str(k + 2))
                    addstuff(key, output, dic_of_pairs)

Sadly, one round doesn’t do the trick, I have to repeat this block of code on the new dictionary (here: “dic_of_pairs”). It would be very elegant if I could use this same piece of code for the second (and all succeeding) rounds, until the every index contains just one file.

Is there a way to do this? Is overwriting the original dictionary (“dic_of_files”) an option, or better to pack this code into a function?


Viewing all articles
Browse latest Browse all 767

Trending Articles