-1

I am trying to write the GP tool (Python script), To extract the data for selected location into KMZ file, Please check below code it's executing successfully but written the incorrect zip file location in Server whereas working fine in ArcMap, In server temporary file created in ArcGIS user location the zip file not returning to arcgis output directory Please check and correct my code.

Script tool - Parameters are given below

  • Layerlist - Layers - Input Multiple-yes
  • Clipshape -Shape - Input
  • OutputZip-file - Dervived Output

Code :

import arcpy 
import os  
import zipfile 
import datetime 
import string
import tempfile  
import shutil
import time



def makezip(filename,fileslist):
    try:
##        if os.path.exists(filename):
##            os.remove(filename)
        #os.chdir(zip_dir)
        outzip = zipfile.ZipFile(filename,"w",zipfile.ZIP_DEFLATED)
        for file in fileslist:
            outzip.write(file,os.path.basename(file))
        outzip.close()
        return outzip.filename
    except:
        print("error")
if __name__ == "__main__":
    try:
        arcpy.env.overwriteOutput = True
        temp_dir = tempfile.mkdtemp()
    #temp_dir = %scratchworkspace%
        #zip_dir = tempfile.mkdtemp()
        arcpy.env.overwriteOutput = True
        print "Workspace created"
        #arcpy.env.workspace =temp_dir
        lyrs = arcpy.GetParameterAsText(0)
        ClipShape = arcpy.GetParameterAsText(1)
        #outzip =  arcpy.GetParameterAsText(2)
        arcpy.env.workspace =temp_dir
        arcpy.env.scratchWorkspace= temp_dir
        print(arcpy.env.scratchGDB)
        lyrs = lyrs.replace("'", "")
        lyrs = lyrs.split(";")
        fileslist=[]
#layerslist=layers.split(";")
        for layer in lyrs:
            head,tail=os.path.split(layer)
            layername=arcpy.MakeFeatureLayer_management(layer,"output_layer")
            outfile=os.path.join(arcpy.env.workspace,tail+".kmz")
            #outfile=tail+".kmz"
            clippedshp= arcpy.Clip_analysis(layername,ClipShape,arcpy.env.scratchGDB+"\\"+tail)
            clippedlayer=arcpy.MakeFeatureLayer_management(clippedshp,layername)
            if os.path.exists(outfile):
                os.remove(outfile)
            arcpy.LayerToKML_conversion(clippedlayer,outfile,100,None,' ',None,None,"CLAMPED_TO_GROUND")
            fileslist.append(outfile)
        filename=os.path.join(temp_dir,"Kmz.zip")
        #filename=os.path.join("%scratchworkspace%","Kmz.zip")
        result =makezip (filename,fileslist)
    #arcpy.env.scratchWorkspace= zip_dir
        arcpy.SetParameter(2,result)
        #print(outzip)
    except:
        print(arcpy.GetMessages())

    finally:
        print("Completed")
        #shutil.rmtree(temp_dir, True)
        print("remove temportyfile")
  • For questions that involve code we ask that you show us where you are stuck with your own code by including just a code snippet in your question. We do not offer a service to debug full code. – PolyGeo Dec 27 '18 at 1:53
1

Plain and simple, you should use scratchGDB and scratchFolder variables when working with directories for geoprocessing service tools. Your code has a lot of confusing pieces as you're trying to set directories to things you should not be. I'll comment out some things you don't need

        # this is an unnecessary command with GP Services
        #arcpy.env.overwriteOutput = True
        # don't use python, thus user's temp space. Use the arcpy variable as I mentioned
        #temp_dir = tempfile.mkdtemp()
        #temp_dir = %scratchworkspace%
        #zip_dir = tempfile.mkdtemp()
        # You have this twice. And it's not necessary
        #arcpy.env.overwriteOutput = True
        # print wont do anything from a service. You should use arcpy.AddMessage()
        #print "Workspace created"
        #arcpy.env.workspace =temp_dir
        lyrs = arcpy.GetParameterAsText(0)
        ClipShape = arcpy.GetParameterAsText(1)
        #outzip =  arcpy.GetParameterAsText(2)
        arcpy.env.workspace =temp_dir
        # You can't override the scratchWorkspace
        #arcpy.env.scratchWorkspace= temp_dir
        # again, don't use print.
        #print(arcpy.env.scratchGDB)

You can see that only leaves you with 3 lines of code.

lyrs = arcpy.GetParameterAsText(0)
ClipShape = arcpy.GetParameterAsText(1)
arcpy.env.workspace =temp_dir

The last line of code should be updated to: arcpy.env.workspace =arcpy.env.scratchFolder - but to be honest, I don't see any place that you actually rely on the workspace.

So after cleaning up / removing all the unnecessary code, you just need to use the scratch variables properly. Take the following examples (your line then my line)

clippedshp= arcpy.Clip_analysis(layername,ClipShape,arcpy.env.scratchGDB+"\\"+tail)
clippedshp= arcpy.Clip_analysis(layername, ClipShape, os.path.join(arcpy.env.scratchGDB, tail))

outfile=os.path.join(arcpy.env.workspace,tail+".kmz")
outfile=os.path.join(arcpy.env.scratchFolder, tail +".kmz")

After making these changes, this will generate files in the geoprocessing jobs directory and the framework will send the file back to the client that made the request. You've mentioned your derived output is set to file, so that's good.

This would be what I think your final code should look like:

def makezip(filename,fileslist):
    try:
        outzip = zipfile.ZipFile(filename,"w",zipfile.ZIP_DEFLATED)
        for file in fileslist:
            outzip.write(file,os.path.basename(file))
        outzip.close()
        return outzip.filename
    except:
        arcpy.AddError("error")
if __name__ == "__main__":
    try:
        arcpy.AddMessage("Workspace created")
        lyrs = arcpy.GetParameterAsText(0)
        ClipShape = arcpy.GetParameterAsText(1)
        lyrs = lyrs.replace("'", "")
        lyrs = lyrs.split(";")
        fileslist=[]
        for layer in lyrs:
            head,tail=os.path.split(layer)
            layername=arcpy.MakeFeatureLayer_management(layer,"output_layer")
            outfile=os.path.join(arcpy.env.scratchFolder, tail +".kmz")
            clippedshp= arcpy.Clip_analysis(layername, ClipShape, os.path.join(arcpy.env.scratchGDB, tail))
            clippedlayer=arcpy.MakeFeatureLayer_management(clippedshp, layername)
            if os.path.exists(outfile):
                os.remove(outfile)
            arcpy.LayerToKML_conversion(clippedlayer, outfile, 100, None,' ',None,None,"CLAMPED_TO_GROUND")
            fileslist.append(outfile)
        filename=os.path.join(temp_dir,"Kmz.zip")
        result =makezip (filename,fileslist)
        arcpy.SetParameter(2,result)
     except:
        print(arcpy.GetMessages())

    finally:
        arcpy.AddMessage("Completed")
  • finally, I have updated the code as you mentioned, even though its working fine in ArcMap but after publishing the service, the zip file URL it's not in ArcGIS Server output directory but URL showing Outputdirecoty so it's giving the error. Error: Invalid URL Code: 400 - Please check give the final code – Venkata Ramarao Dec 20 '18 at 6:18
  • Waiting for the updated code. Could you please check and correct it – Venkata Ramarao Dec 24 '18 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.