1

I am trying to troubleshoot some issues I'm having with the Python map function that is part of a larger problem I'm having with trying to run my code concurrently. Why does map() here create an empty output? It runs with no error and creates a map object, but does not create any rasters.

Set-up code:

import arcpy
import os
import arcpy

wd = r'myPath'
os.chdir(wd)

def create_raster(input):
    string_out_name = 'EmptyTIFF_' + str(input) + '.tif'
    tif = arcpy.CreateRasterDataset_management(out_path = wd, out_name = string_out_name, pixel_type = '8_BIT_UNSIGNED', number_of_bands = "1")
    string_ascii_name = string_out_name.strip('.tif') + '.asc'
    arcpy.RasterToASCII_conversion(tif, os.path.join(wd, string_ascii_name))    

myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

A for loop works and creates ten GeoTIFF and ten ASCII rasters:

for i in myList:
    create_raster(i)

However, when I try to use map( ), the output is empty. How can I use the map( ) function here?

map(create_raster, myList)

The reason I'm trying to use map( ) is so that I can use the multiprocessing or threading packages with something like the following (which isn't producing anything):

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    executor.map(create_raster, tif_list)
2
  • Is this a GIS question? It appears to be more of a Python question, and may be better suited to Stack Overflow, than to GIS Stack Exchange. Eg, if you delete (comment-out) both of the arcpy lines from your create_raster() function then add print(string_out_name + " " + string_ascii_name) at the end of that function, do you get the same problem? Commented Dec 19, 2023 at 23:22
  • You actually actually post the code including the setup section AND the map() line to demonstrate how you are actually using the map() function. Commented Dec 19, 2023 at 23:29

1 Answer 1

1

map() in Python 3 is evaluated "lazily". That is, the code of the function that it is calling is not actually run until the result is required. For more details, see the accepted answer at: https://stackoverflow.com/questions/19342331/python-map-calling-a-function-not-working

So to make this work, you can assign the results of the the map() to a variable (eg, result = map(create_raster, myList)), and then do something with each item in the result iterable (eg, assign convert it to a list: result_list = list(map(create_raster, myList))).

So, that would give us:

import arcpy
import os
import arcpy

wd = r'myPath'
os.chdir(wd)

def create_raster(input):
    string_out_name = 'EmptyTIFF_' + str(input) + '.tif'
    tif = arcpy.CreateRasterDataset_management(out_path = wd, out_name = string_out_name, pixel_type = '8_BIT_UNSIGNED', number_of_bands = "1")
    string_ascii_name = string_out_name.strip('.tif') + '.asc'
    arcpy.RasterToASCII_conversion(tif, os.path.join(wd, string_ascii_name))    

myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

result_list = list(map(create_raster, myList))

You're not actually doing anything with result_list, but this will force it to actually run the create_raster() for each item.

NB: I can't comment on the concurrency of either the map() function or the list() function. But the following may be of use: https://stackoverflow.com/questions/67686563/run-same-function-parallelly-with-different-items-in-the-list-in-python

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.