Python script null/no output in GP Results but is writing the file as expected.

692
6
Jump to solution
05-02-2017 11:21 AM
ZacharyHart
Regular Contributor

I've created a simple script to share via a web app so that folks without desktop can convert shapes to KML/KMZ. The script writes the output KMZ as expected in the scratch folder where it belongs but doesn't appear in the GP Results window and therefore is a null output when published as a GP Service.

I have a similar script which takes a zip of a multiple shapefiles, reprojects them and zips them back up; this script tool is configured identically and the output zip shows in the GP results and also is returned in the output when published as a GP service.

Script Tool:

Code (not really sure this will help):

import arcpy  
import zipfile
import sys
import os
from os import makedirs
from os.path import join
  
inZip = arcpy.GetParameterAsText(0)   
  
# Create a folder in the scratch directory to extract zip to  
outFolder = os.path.join(arcpy.env.scratchFolder, "zipContents")  
os.mkdir(outFolder)

# Create a folder in the scratch directory for the KML
KMLfolder = join(arcpy.env.scratchFolder, "KML")
makedirs(KMLfolder)
  
# Extract the zip contents  
zip2Extract = zipfile.ZipFile(inZip, 'r')  
zip2Extract.extractall(outFolder)  
zip2Extract.close()

# Set the workspace
arcpy.env.workspace = outFolder

# Create a list of shapefiles
featureclasses = arcpy.ListFeatureClasses()
print featureclasses

# Use ListFeatureClasses to generate a list of inputs 
for infc in arcpy.ListFeatureClasses():

    # Determine if the input has a defined coordinate system, can't project if it does not
    dsc = arcpy.Describe(infc)

    if dsc.spatialReference.Name == "Unknown":
        print('skipped this fc due to undefined coordinate system: ' + infc)
        
    else:
       
        # Make Feature Layer from FC
        tempLayer = arcpy.MakeFeatureLayer_management(infc, "tempLayer")
        # Define KML output
        baseName=os.path.basename(infc).split('.')[0]
        outKML = os.path.join(KMLfolder, baseName + '.kmz')        
        #Convert Layer to KML
        output = arcpy.LayerToKML_conversion(tempLayer, outKML)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Zachary,

I believe you will need to add the following line:

arcpy.SetParameterAsText(1, outKML)

View solution in original post

6 Replies
NeilAyres
MVP Frequent Contributor

Sorry, but...

The script writes the output KMZ as expected but fails to show up in the scratch folder where it belongs but

So where does the output go?

ZacharyHart
Regular Contributor

Hah! let me edit that....doh!

0 Kudos
NeilAyres
MVP Frequent Contributor

If this is a geo-processing service, aren't you supposed to return some output to it?

Like at the end SetParameter...()

ZacharyHart
Regular Contributor

I like your logic, and looked to see if I implemented in that my other script but I didn't and it works fine.

I'm thinking I could trick this into working by setting the output parameter as 'required' (but whatever the user enters for that, the tool creates the name of the output on its own anyway).

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Zachary,

I believe you will need to add the following line:

arcpy.SetParameterAsText(1, outKML)
ZacharyHart
Regular Contributor

Jeez...I feel dumb now. Um, both Jake & Neil are correct (and I marked as helpful). Totally works now (although I'm totally at a loss as to why the other script works as expected w/o setting a parameter for the output).

EDIT: in a twist of fate, i'm going to have to roll up the output into a zip anyway to account for multiple shapefiles in the input zip).

EDIT x2: so in modifying my script further I realized what the difference between the two script tools is. Even though I did not set a parameter for the output, because I supplied a default value in the script tool parameter properties, it will add that as a GP result...I'm guessing that is poor practice and will be implementing  set parameter instead.

0 Kudos