I have data in Scratch folder, how to zip and download it?

190
2
09-23-2019 03:28 AM
JamesHone1
New Contributor III

Hi I have made a simple tool to clip from a couple of rasters after a list of bool is passed in, it will eventually clip fromn more than just 2 rasters.

The clipped rasters are sat in %scratch data%

I want to zip them up and offer them as a download at the end of this tool.

I have looked at the clip and ship example but that seems to ue a tool to zip and email all in one go, I want to zip then offer a download.

How do I zip my clipped tiffs.

How do I offer the resulting zip as a download.

Should I be using "in_memory"?

import arcpy

# Set geoprocessing environments
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"\\dacspreprodstorage.file.core.windows.net\preprodcontentstore\Jim\BSelect.gdb"

boolList = arcpy.GetParameterAsText(0)
if boolList == '#' or not boolList:
boolList = [True, False] # provide a default value if unspecified

outname = arcpy.GetParameterAsText(1)
if outname == '#' or not boolList:
outname = 'download' # provide a default value if unspecified


#set params
poly = "clipit"

tiflist = ["Whole_UK_Mosaic", "Whole_UK_COMORG"]


def cliptif(clippee, clipper, output):

desc = arcpy.Describe(clipper)
extent = desc.extent
coord_string = "{} {} {} {}".format(extent.XMin, extent.YMin, extent.XMax, extent.YMax)
arcpy.Clip_management(clippee, coord_string, output, in_template_dataset=poly, clipping_geometry="ClippingGeometry")


for i in range(len(tiflist)):
# outTif = r"C:\Jim\BathySelect\output\output"+str(i)+".tif"
outTif = "%scratchFolder%\\output"+str(i)+".tif"
cliptif(tiflist[i], poly, outTif)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks

Tags (2)
0 Kudos
2 Replies
BrittneyWhite1
Esri Contributor

For zipping your clipped tiffs, you may be able to use the zipfile module (it is part of the Python standard library). Here are some resources:

zipfile — ZIP Archive Access — PyMOTW 3: https://pymotw.com/3/zipfile/

zipfile — Work with ZIP archives — Python 3.7.4 documentation: https://docs.python.org/3/library/zipfile.html

JamesHone1
New Contributor III

Thanks Brittney, I have got zipfile to work and I am creating a zip in the scratchfolder.

Is this the best method if I want to launch this from a widget and have the user download the zip? would in_memory be any better?

I still feel a bit confused about workspaces, scratch folders, in_memory. Its currently writing my zip to the default scratch folder, which is not the workspace i set but the default.gdb, will this give me issues?

What if multiple users use the tool at the same time, will they be able to both write to the default gdb?, will they overwrite each other? I just feel a bit lost with questions implementing this, I'm going to try and get it firing from my web app and see how it goes!

heres my code so far, poly will also become an input parameter.

import arcpy, zipfile, os

# Set geoprocessing environments
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"\\dacspreprodstorage.file.core.windows.net\preprodcontentstore\Jim\BSelect.gdb"

boolList = arcpy.GetParameterAsText(0)
if boolList == '#' or not boolList:
boolList = [True, False] # provide a default value if unspecified

outname = arcpy.GetParameterAsText(1)
if outname == '#' or not boolList:
outname = 'download.zip' # provide a default value if unspecified


#set params
poly = "clipit"

tiflist = ["Whole_UK_Mosaic", "Whole_UK_COMORG"]


def cliptif(clippee, clipper, output):
desc = arcpy.Describe(clipper)
extent = desc.extent
coord_string = "{} {} {} {}".format(extent.XMin, extent.YMin, extent.XMax, extent.YMax)
arcpy.Clip_management(clippee, str(extent), output, in_template_dataset=poly, clipping_geometry="ClippingGeometry")


# loop tiflist and clip tifs
for i in range(len(tiflist)):
# outTif = r"C:\Jim\BathySelect\output\output"+str(i)+".tif"
if boolList[i]:
outTif = os.path.join(arcpy.env.scratchFolder, 'output'+str(i)+'.tif')
cliptif(tiflist[i], poly, outTif)


# zip results
try:
zipper = zipfile.ZipFile(os.path.join(arcpy.env.scratchFolder, outname), 'w')
for i in range(len(tiflist)):
zipper.write(os.path.join(arcpy.env.scratchFolder, "output"+str(i)+".tif"), "output"+str(i)+".tif")
except RuntimeError:
print('there was an error trying to zip')
finally:
zipper.close()
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍