AnsweredAssumed Answered

arcpy geoprocessing service with sde database produces no output url

Question asked by mkosior on Sep 4, 2014

I'm trying to use a geoprocessing script to clip some data and return to the user (via url) the path to a zip file with the results of the clip. I also need to connect to an sde database. I can get a url to show up if I use shapefiles and but it seems to loose the plot when i use the sde connection (probably because i have to set the workspace to the sde connection and then the server doesn't have a place to write temp files to?!). It runs okay but doesn't produce any output on the server folders and i do not get a url. I have pasted the code below (hopefully it shows up ok). Thanks. ps: I copied a lot of the code from esri's ship and clip example. Also i registered "D:\arcgisserver\directories\service_output" on the server as a folder so it is able to see it.

 

import arcpy

import sys

import os

import traceback

import zipfile

 

arcpy.env.overwriteOutput = True

 

def zipUpFolder(folder, outZipFile):

    # zip the data

    try:

        zip = zipfile.ZipFile(outZipFile, 'w', zipfile.ZIP_DEFLATED)

        zipws(unicode(folder), zip, "CONTENTS_ONLY")

        zip.close()

    except RuntimeError:

        # Delete zip file if exists

        if os.path.exists(outZipFile):

            os.unlink(outZipFile)

        zip = zipfile.ZipFile(outZipFile, 'w', zipfile.ZIP_STORED)

        zipws(unicode(folder), zip, "CONTENTS_ONLY")

        zip.close()

        #Message"  Unable to compress zip file contents."

        gp.AddWarning(get_ID_message(86133))

 

def zipws(path, zip, keep):

    path = os.path.normpath(path)

    # os.walk visits every subdirectory, returning a 3-tuple

    #  of directory name, subdirectories in it, and filenames

    #  in it.

    for (dirpath, dirnames, filenames) in os.walk(path):

        # Iterate over every filename

        for file in filenames:

            # Ignore .lock files

            if not file.endswith('.lock'):

                #gp.AddMessage("Adding %s..." % os.path.join(path, dirpath, file))

                try:

                    if keep:

                        zip.write(os.path.join(dirpath, file),

                        os.path.join(os.path.basename(path), os.path.join(dirpath, file)[len(path)+len(os.sep):]))

                    else:

                        zip.write(os.path.join(dirpath, file),

                        os.path.join(dirpath[len(path):], file))

                except Exception as e:

                    #Message "    Error adding %s: %s"

                    arcpy.AddWarning(get_ID_message(86134) % (file, e[0]))

    return None

 

if __name__ == '__main__':

    try:

        arcpy.env.workspace = os.path.join(r"D:\arcgisserver\directories\service_scripts", u'PUBLISH_MERCATOR_GISPublish.sde')

        ws = "D:\\arcgisserver\\directories\\service_output"

        arcpy.env.scratchWorkspace = "D:\\arcgisserver\\directories\\service_output"

        outputZipFile = "D:\\arcgisserver\\directories\\service_output\\out.zip"

        zipFolderPath = "D:\\arcgisserver\\directories\\service_output\\output"

 

        aoi = "455719.332353, 6300257.134719, 499534.419983,6269598.547920"

       

        dsList = arcpy.ListDatasets("*","Feature")

        for ds in dsList:

            fcList = arcpy.ListFeatureClasses("*","",ds)

            for fc in fcList:

                featureclass = fc.split(".")[2]

                arcpy.AddMessage(fc)

 

                exts = []

                exts = aoi.split(",")

                extent = arcpy.Extent(exts[0],exts[1],exts[2],exts[3])

                pts = [arcpy.Point(extent.XMin, extent.YMin),

                arcpy.Point(extent.XMax, extent.YMin),

                arcpy.Point(extent.XMax, extent.YMax),

                arcpy.Point(extent.XMin, extent.YMax)]

                array = arcpy.Array(items=pts)

                poly = arcpy.Polygon(array)

 

                sr = arcpy.SpatialReference()

                sr.factoryCode = 26912

                sr.create()

                poly = arcpy.Polygon(array,sr) 

               

                tmp_poly = os.path.join(ws, "polygon.shp")

                tmp_orig = os.path.join(ws, "to_clip.shp")

                outputpath = os.path.join(zipFolderPath, featureclass+".shp")

 

                arcpy.CopyFeatures_management(poly, "D:\\arcgisserver\\directories\\service_output\\tmp_poly.shp")#tmp_poly)

                arcpy.Clip_analysis(fc, "D:\\arcgisserver\\directories\\service_output\\tmp_poly.shp", outputpath)

               

        zipUpFolder(zipFolderPath, outputZipFile)

 

        arcpy.SetParameterAsText(0, outputZipFile)

        arcpy.SetParameterAsText(1, arcpy.env.scratchWorkspace)

       

               

    except:

        tb = sys.exc_info()[2]

        tbinfo = traceback.format_tb(tb)[0]

        pymsg = "ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \

                unicode(sys.exc_type)+ ": " + unicode(sys.exc_value) + "\n"

Outcomes