geoprocessing service using in_memory produces no results

762
2
10-14-2013 04:08 PM
KevinJohnson
Occasional Contributor
Hi, I have a really basic script that I want to run as a geoprocessing service but from server it yields no results.   It works and gives the expected result as a script tool in desktop. It works as expected when wrapped in a model and run from desktop.  But when I publish it to server (10.0 sp3 .net) it doesn't fail, it just doesn't give any results.  I think I've followed the best practices for the folder structure and data paths etc.  Im using in_memory workspace for some of the outputs (hoping to speed up the process) and I think the server isn't actually able to write to in_memory (is that even possible?). 
Any suggestions are welcome, I've been trying to get this to work for a couple days now.
Thanks.  Here's the code:


import arcpy
from arcpy import env
import os
import sys

env.workspace = "in_memory"


APN = arcpy.GetParameterAsText(0)




scriptPath = sys.path[0]
thisFolder = os.path.dirname(scriptPath)
ToolDataPath = os.path.join(thisFolder, "ToolData")
ScratchPath = os.path.join(thisFolder, "Scratch")
ScratchDataPath = os.path.join(ScratchPath, "scratch.gdb")
parcels = os.path.join(ToolDataPath,"County_Parcels.lyr")
ServiceArea = os.path.join(ToolDataPath, "Service_Area.lyr" )

whereclause = '"APN" =  ' + str(APN)

try:
    arcpy.MakeFeatureLayer_management(parcels, "PARCELtobeQueried", whereclause, "", "OBJECTID OBJECTID HIDDEN NONE;SHAPE SHAPE HIDDEN NONE;APN APN VISIBLE NONE;PIN PIN HIDDEN NONE;STREETNUM STREETNUM HIDDEN NONE;STREETDIR STREETDIR HIDDEN NONE;STREET STREET HIDDEN NONE;CITY CITY HIDDEN NONE;FIRSTNAME FIRSTNAME HIDDEN NONE;LASTNAME LASTNAME HIDDEN NONE;MAILING1 MAILING1 HIDDEN NONE;MAILING2 MAILING2 HIDDEN NONE;MAILCITY MAILCITY HIDDEN NONE;MAILSTATE MAILSTATE HIDDEN NONE;MAILZIP MAILZIP HIDDEN NONE;LAND_USE LAND_USE HIDDEN NONE;ZONING ZONING HIDDEN NONE;WATER WATER HIDDEN NONE;SEWER SEWER HIDDEN NONE;ACREAGE ACREAGE HIDDEN NONE;TAXDIST TAXDIST HIDDEN NONE;BEDROOMS BEDROOMS HIDDEN NONE;BATHS BATHS HIDDEN NONE;YEARBLT YEARBLT HIDDEN NONE;LANDASS LANDASS HIDDEN NONE;BUILDASS BUILDASS HIDDEN NONE;TOTALASS TOTALASS HIDDEN NONE;LANDAPR LANDAPR HIDDEN NONE;BUILDAPR BUILDAPR HIDDEN NONE;TOTALAPR TOTALAPR HIDDEN NONE;PROPCODE PROPCODE HIDDEN NONE;STORIES STORIES HIDDEN NONE;TOWNHOUSE TOWNHOUSE HIDDEN NONE;UNITS UNITS HIDDEN NONE;BUILDINGTYPE BUILDINGTYPE HIDDEN NONE;NAME NAME HIDDEN NONE;NUMBER_ NUMBER_ HIDDEN NONE;TOWNSHIP TOWNSHIP HIDDEN NONE;RANGE RANGE HIDDEN NONE;SECT SECT HIDDEN NONE;NBHD NBHD HIDDEN NONE;FACTORDIST FACTORDIST HIDDEN NONE;SALEDATE SALEDATE HIDDEN NONE;SALEPRICE SALEPRICE HIDDEN NONE;SPECPROPCODE SPECPROPCODE HIDDEN NONE;QC QC HIDDEN NONE;ORIG_FID ORIG_FID HIDDEN NONE")
    #below is writing to in memory
    ParcelCentroid = env.workspace + "\\" + "ParcelCentroidFeature"
    arcpy.FeatureToPoint_management("PARCELtobeQueried",ParcelCentroid )
    arcpy.Delete_management("PARCELtobeQueried")
    #below is writing to in memory
    SpatialJoinOUT = env.workspace + "\\" + "ParcelCentroidSJBound"
    arcpy.SpatialJoin_analysis(ParcelCentroid,ServiceArea, SpatialJoinOUT, 'JOIN_ONE_TO_ONE','KEEP_ALL', "APN \"APN\" true true false 4 Long 0 0 ,First,#,{0},APN,-1,-1;DESCRIPTIO \"DESCRIPTIO\" true true false 254 Text 0 0 ,First,#,{1},DESCRIPTIO,-1,-1;NAME \"NAME\" true true false 254 Text 0 0 ,First,#,{2},NAME,-1,-1".format(ParcelCentroid,  ServiceArea,  ServiceArea),   "INTERSECT", "", "")
    arcpy.Delete_management(ParcelCentroid)
    rows = arcpy.SearchCursor(SpatialJoinOUT)
    row = rows.next()
    TestText = ''
    while row:
        print row.DESCRIPTIO
        TestText = str(row.DESCRIPTIO)
        break
    OUTPUT = arcpy.SetParameterAsText(1, TestText)
    print TestText
    del row, rows
    arcpy.Delete_management(SpatialJoinOUT)

except:
    print "cannot find apn"
    msg = arcpy.GetMessages()
    print msg
0 Kudos
2 Replies
KevinJohnson
Occasional Contributor
Hi Everyone,

Wanted to repost my code,  all the field mapping stuff was taking up a lot of space...
Does any one know if the arcgisserver account needs permissions to someplace in particular to write to in_memory workspace?

The script below is supposed to make a centroid of a selected parcel and then spatial join it to a service area fc and return an attribute from that spatial joined fc.   Its works fine in desktop but returns nothing when its published as a service.   BTW its published as a script tool within a model with two parameter variables, gplong (in) and GPString (out).   I've tried writing these intermediate outputs to scratch workspace with no luck either.  When Im using only select  cursors and not trying to write to anything it will return results as a service (but I need the centroid).    If anyone has any ideas (script or server permissions or otherwise) I really appreciate it.  Thanks,

import arcpy
import os
import sys

APN = arcpy.GetParameterAsText(0)
scriptPath = sys.path[0]
thisFolder = os.path.dirname(scriptPath)
ToolDataPath = os.path.join(thisFolder, "ToolData")
parcels = os.path.join(ToolDataPath,"County_Parcels.lyr")
ServiceArea = os.path.join(ToolDataPath, "Service_Area.lyr" )
whereclause = '"APN" =  ' + str(APN)

#Get the parcel centroid of the selected parcel and then spatial join the
#  parcel centroid with the Service Area it falls within.
#  Return the name of the Service Area.

try:
    #make feature layer of the parcel by the APN the user enters
    arcpy.MakeFeatureLayer_management(parcels, "PARCELtobeQueried", whereclause)
    ParcelCentroid = "in_memory" + "\\" + "ParcelCentroidFeature"
    #Create a centroid of the feature layer
    arcpy.FeatureToPoint_management("PARCELtobeQueried",ParcelCentroid )
    SpatialJoinOUT = "in_memory" + "\\" + "ParcelCentroidSJBound"
    #do a spatial join attaching the attributes of the Service Area to the Parcel Centroid feature
    arcpy.SpatialJoin_analysis(ParcelCentroid,ServiceArea, SpatialJoinOUT)
    #search cursor to get the attribute of the spatial join out feature
    rows = arcpy.SearchCursor(SpatialJoinOUT)
    row = rows.next()
    TestText = ''
    while row:
        print row.DESCRIPTIO
        TestText = str(row.DESCRIPTIO)
        break
    OUTPUT = arcpy.SetParameterAsText(1, TestText)
    print TestText
    del row, rows
    arcpy.Delete_management(SpatialJoinOUT)
    arcpy.Delete_management("PARCELtobeQueried")
    arcpy.Delete_management(ParcelCentroid)
except:
    msg = arcpy.GetMessages()
    print msg
0 Kudos
KevinJohnson
Occasional Contributor
Figured it out... it was using feature to point tool.  That tool requires an Server Advanced license.  the script tool worked on desktop because I had an Info license...   Went about getting the centroid using the geometry object tools.  

import arcpy
import os
import sys

APN = arcpy.GetParameterAsText(0)

scriptPath = sys.path[0]
thisFolder = os.path.dirname(scriptPath)
ToolDataPath = os.path.join(thisFolder, "ToolData")
parcels = os.path.join(ToolDataPath,"County_Parcels.lyr")
ServiceArea = os.path.join(ToolDataPath, "Service_Area.lyr" )
whereclause = '"APN" =  ' + str(APN)
print whereclause

#Get the parcel centroid of the selected parcel and then spatial join the
#  parcel centroid with the Service Area it falls within.
#  Return the name of the Service Area.

try:
    arcpy.MakeFeatureLayer_management(parcels, "PARCELtobeQueried", whereclause)
    arcpy.MakeFeatureLayer_management(ServiceArea, "ServiceLayer")
    layerdesc  = arcpy.Describe("ParceltobeQueried")
    shapefieldname = layerdesc.ShapeFieldName
    print str(shapefieldname)
    rows = arcpy.SearchCursor("ParceltobeQueried")
    for row in rows:
        feat = row.getValue(shapefieldname)
        cent = feat.centroid
        print cent.X, cent.Y
        myPt = arcpy.Point(cent.X, cent.Y)
        myPtGeometry = arcpy.PointGeometry(myPt)
        arcpy.SelectLayerByLocation_management("ServiceLayer", "INTERSECT", myPtGeometry)
        srows = arcpy.SearchCursor("ServiceLayer")
        for srow in srows:
            print srow.DESCRIPTIO
        OUTPUT = arcpy.SetParameterAsText(1, str(srow.DESCRIPTIO))
        #arcpy.Delete_management("ServiceLayer")
        del srow, srows

    del row, rows
    arcpy.Delete_management("PARCELtobeQueried")

except:
    msg = arcpy.GetMessages()
    print msg
0 Kudos