Select to view content in your preferred language

Python Web Geoprocessing Tool (Data Extract) Creates Empty Feature Classes Until Restarted

406
0
05-27-2022 08:53 AM
MichaelMorisette
New Contributor III

This is a phenomenon that has been occurring for us since the beginning of web geoprocessing tools, and seems to have become been more frequent after migrating our tools to the ArcGIS Pro runtime.

The behavior is that the tool works as it should for awhile.  Then, after temporarily losing connection to our enterprise geodatabase, it begins creating empty feature classes until the service is restarted.  I've implemented a workaround where I've scripted the service to automatically restart after our nightly database compress.  However, this doesn't account for other instances where connection is unexpectedly lost.

Are there any more robust workarounds that anybody knows of?

I'm wondering if I could write something into the python code to wake up whatever fell asleep after the connection was lost.  Here is what I currently have.  This is basically just a variant of the "Clip And Ship" tool rewritten to work with ArcGIS Pro.

"""This tool powers the clip and ship tool for the data downloads"""

from datetime import datetime
from os import listdir, mkdir, walk
from os.path import basename, isdir, join
from shutil import rmtree
from zipfile import ZipFile

from arcpy import AddMessage, env, GetParameterAsText
from arcpy.analysis import Clip
from arcpy.conversion import ExportCAD
from arcpy.management import CreateFileGDB

# Constants
GDB_NAME = "FayettevilleGISData.gdb"
ZIP_FOLDER = f"{env.scratchFolder}/output"
if not isdir(ZIP_FOLDER):
    mkdir(ZIP_FOLDER)
AddMessage(env.scratchFolder)

# Clip layers to area of interest
env.overwriteOutput = True
inLayers = GetParameterAsText(0).split(";")
aoi = GetParameterAsText(1)
featureFormat = GetParameterAsText(2).split(" - ")[1]
outPath = "memory"
if featureFormat == "GDB":
    # Create the geodatabase
    CreateFileGDB(ZIP_FOLDER, GDB_NAME)
    outPath = join(ZIP_FOLDER, GDB_NAME)
elif featureFormat == "SHP":
    outPath = ZIP_FOLDER
for layer in inLayers:
    # Clip the layer
    AddMessage(layer)
    outName = layer.replace("Download_Data\\", "")
    if featureFormat == "SHP":
        outName = f"{outName}.shp"
    clippedFeatures = join(outPath, outName)
    Clip(layer, aoi, clippedFeatures)

# Export to CAD if necessary
if featureFormat not in ("GDB", "SHP"):
    cadLayers = []
    for layer in inLayers:
        cadLayers.append(join("memory", layer))
    extension = "dwg"
    if "DWG" in featureFormat:
        extension = ".dwg"
    if "DGN" in featureFormat:
        extension = ".dgn"
    elif "DXF" in featureFormat:
        extension = ".dxf"
    ExportCAD(cadLayers, featureFormat, (f"{ZIP_FOLDER}/"
                                         f"FayettevilleGISData{extension}"))

# Zip the clipped files
outFile = GetParameterAsText(3)
with ZipFile(outFile, "w") as zipObj:
    # Iterate over all the files in directory
    for folderName, subfolders, filenames in walk(ZIP_FOLDER):
        for filename in filenames:
            if not filename.lower().endswith(".zip"):
                # Create complete filepath of file in directory
                filePath = join(folderName, filename)
                # Add file to zip
                outPath = basename(filePath)
                if GDB_NAME in filePath:
                    outPath = join(GDB_NAME, basename(filePath))
                try:
                    zipObj.write(filePath, outPath)
                except:
                    pass

 

0 Kudos
0 Replies