Select to view content in your preferred language

Publishing a Python script as a geoprocessing service. Needs help.

1706
2
05-02-2019 06:18 AM
AnthonyTwesigye1
Emerging Contributor

Hi, I am tryng to publish a relatively simple python script as a geoprocessing service. The tool works well when executed locally.  It also publishes correctly, but I get the following error when the geoprocessing service executes:

I would like someone to help identify what errors I may have my code, which is attached to this message.

Also, here are the parameters of my script-

Thanks.

# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------
 import arcpy
# input feature for querying the statistics(feature set)
userInPutFc = arcpy.GetParameterAsText(0)
if userInPutFc == '#' or not userInPutFc:
    userInPutFc = "\\\\xxx\\Server Objects\\ArcGISGeoprocessing\\Script_ZonalStatistic_Solar\\Data\\Temp\\sotemp.lyr" # provide a default value if unspecified
# final = arcpy.GetParameterAsText(1)
final = "\\\\xxx\\Server Objects\\ArcGISGeoprocessing\\Script_ZonalStatistic_Solar\\Data\\Temp.gdb\\ZonalSolarOutput"  # provide a default value if unspecified

# work space
arcpy.env.workspace = "\\\\xxx\\Server Objects\\Data\\Raster\\Solar"
arcpy.env.overwriteOutput=True
 
# Input rasters array
input_data_array = []
# Exclude data from the analysis
exluded_data = ["PVGIS4_SARAH_YearlyAverageGlobalIrradianceHorizontalSurface", "PVGIS4_SARAH_YearlyAverageGlobalIrradianceOptimallyInclinedSurface", "PVGIS4_SARAH_YearlyAverageGlobalIrradianceOnTwoAxisSunTrackingSurface", "PVGIS4_SARAH_YearlyOptimalInclinationAngleForAnEquatorFacingPlane", "SolarGIS_OPTA_OptimumTiltToMaximizeYearlyYield"]
# List of geodatabases in the current workspace
workspaceGDBs = arcpy.ListWorkspaces("*GIS*", "FileGDB")
arcpy.AddMessage(workspaceGDBs)
# extract all the rasters into the input_data_array
for database in workspaceGDBs:
    arcpy.env.workspace = database
    databaseName = (database.split("\\")[-1]).split(".")[-2]
    if databaseName == "PVGIS":
        for fd in arcpy.ListDatasets("*_Yearly*","Raster"):
            if fd not in exluded_data:
                input_data_array.append(database + "\\" + fd)
    if databaseName == "SolarGIS":
        for fd in arcpy.ListDatasets("*","Raster"):
            if fd not in exluded_data:
                input_data_array.append(database + "\\" + fd)
arcpy.AddMessage("Finished obtaining data from the raster sources")
 
arcpy.env.overwriteOutput=True
 
# Create a new environment
 
out_c = "in_memory" + "\\" + "output_copy"
arcpy.CopyFeatures_management(userInPutFc, out_c)
 
arcpy.CalculateField_management(out_c, "Feature_no", "0", "PYTHON_9.3", "")
# Recalculate extent based on the feature
arcpy.RecalculateFeatureClassExtent_management(out_c)
# Feature extents
vectorExtent = arcpy.Describe(userInPutFc).extent
vectorExtentXMin = vectorExtent.XMin
vectorExtentYMin = vectorExtent.YMin
vectorExtentXMax = vectorExtent.XMax
vectorExtentYMin = vectorExtent.YMax
 
# Checkout the extension for using in statistics
arcpy.CheckOutExtension("Spatial")
 
arcpy.AddMessage("Started processing statistics")
 
table = []
count = 0
OI = arcpy.Describe(out_c)
field = OI.fields
zone_field = "Feature_no"
 
arcpy.AddMessage("The zone field is: " + zone_field)
 
for raster in input_data_array:
    rasterLayerName = arcpy.Describe(raster).name
    arcpy.AddMessage("Processing statistics with: " + rasterLayerName)
    # Raster extents
    rasterExtent = arcpy.Describe(raster).extent
    rasterExtentXMin = rasterExtent.XMin
    rasterExtentYMin = rasterExtent.YMin
    rasterExtentXMax = rasterExtent.XMax
    rasterExtentYMax = rasterExtent.YMax
    # Compare vector and raster extents
    DXMin = rasterExtentXMin - vectorExtentXMin
    DYMin = rasterExtentYMin - vectorExtentYMin
    DXMax = rasterExtentXMax - vectorExtentXMax
    DYMax = rasterExtentYMax - vectorExtentYMin
 
    # Check if the features share extents
    if DXMin < 0 and DYMin < 0 and DXMax > 0 and DYMax > 0:
        out_t = "in_memory" + "\\" + "output_table" + str(count)
        arcpy.sa.ZonalStatisticsAsTable(out_c, zone_field, raster, out_t, "DATA", "SUM")
        arcpy.AddField_management(out_t, "SourceLayer", "TEXT")
        arcpy.CalculateField_management(out_t, "SourceLayer", "'" + rasterLayerName + "'", "PYTHON_9.3", "")
        table.append(out_t)
        count = count + 1
 
# final = "\\\\xxx\\Server Objects\\ArcGISGeoprocessing\\Script_ZonalStatistic_Solar\\Data\\Temp.gdb\\ZonalOutput"
arcpy.Merge_management(table, final)
 
arcpy.AddMessage("Merged the output table")
 
fieldList = arcpy.ListFields(final)
for field_of_interest in fieldList:
    if field_of_interest.name == "SUM":
        arcpy.AlterField_management(final, field_of_interest.name, 'SUM_kWh_kWp', 'SUM_kWh_kWp')
    if field_of_interest.name == "AREA":
        arcpy.AlterField_management(final, field_of_interest.name, 'AREA_SquareMeters', 'AREA_SquareMeters')
arcpy.AddMessage("Updated the table fields")
 
arcpy.SetParameterAsText(1, final)
 
arcpy.AddMessage(final)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
2 Replies
DarinaTchountcheva
Frequent Contributor

Anthony,

I haven't looked at your code, but I would suggest that you publish the service with the "Show Messages" parameter set to "Info", so you can get some more info about the error and find exactly where it happens. 

Here is the link explaining the setting:

https://enterprise.arcgis.com/en/server/10.6/publish-services/windows/geoprocessing-service-settings...

Also, for your output parameter I would make "final" to be in the scratch workspace:

final = os.path.join(arcpy.env.scratchGDB, "ZonalOutput")

And to make my service return the data I set the parameter like this: 

arcpy.SetParameter(1, final)

This discussion helped me resolve my geoprocessing service issues:

https://gis.stackexchange.com/questions/183188/arcgis-geoprocessing-service-output-location

Good Luck!

 

AnthonyTwesigye1
Emerging Contributor

It has been a while since I worked on this. I have been a bit engaged on different projects.

I am back on it without success.

I have enabled the info from the geoprocessing tool  and used the Scratch GDB for the final output.

After republishing the tool and trying to run it, I realise that my problem is on  setting  environment workspace.

The current work space is ,\\xxx\arcgisserver\directories\arcgissystem\arcgisinput\test\ZonalStatisticsSolarDataAccessTestSec.GPServer\extracted\v101


The considered work spaces GDBs are ,[]

I expected to get the environment I set as \\xxx\Server Objects\Data\Raster\Solar

and then the GDBs inside this workspace

The errors are related to the tool messages on line 20 and line 28 on the attached updated script

# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------


import arcpy
import os

# input feature for querying the statistics(feature set)
userInPutFc = arcpy.GetParameterAsText(0)
if userInPutFc == '#' or not userInPutFc:
    userInPutFc = "\\\\xxx\\Server Objects\\ArcGISGeoprocessing\\Script_ZonalStatistic_Solar\\Data\\Temp\\sotemp.lyr" # provide a default value if unspecified

# final result on the scratchGDB
final = os.path.join(arcpy.env.scratchGDB, "ZonalOutput")

# work space
arcpy.env.workspace = r"\\xxx\Server Objects\Data\Raster\Solar"
arcpy.env.overwriteOutput=True

arcpy.AddMessage("The current work space is ,{0}" .format(arcpy.env.workspace))

# Input rasters array
input_data_array = []
# Exclude data from the analysis
exluded_data = ["PVGIS4_SARAH_YearlyAverageGlobalIrradianceHorizontalSurface", "PVGIS4_SARAH_YearlyAverageGlobalIrradianceOptimallyInclinedSurface", "PVGIS4_SARAH_YearlyAverageGlobalIrradianceOnTwoAxisSunTrackingSurface", "PVGIS4_SARAH_YearlyOptimalInclinationAngleForAnEquatorFacingPlane", "SolarGIS_OPTA_OptimumTiltToMaximizeYearlyYield"]
# List of geodatabases in the current workspace
workspaceGDBs = arcpy.ListWorkspaces("*GIS*", "FileGDB")
arcpy.AddMessage("The considered work spaces GDBs are ,{0}" .format(workspaceGDBs))

# extract all the rasters into the input_data_array
for database in workspaceGDBs:
    arcpy.env.workspace = database
    databaseName = (database.split("\\")[-1]).split(".")[-2]
    if databaseName == "PVGIS":
        for fd in arcpy.ListDatasets("*_Yearly*","Raster"):
            if fd not in exluded_data:
                input_data_array.append(database + "\\" + fd)
    if databaseName == "SolarGIS":
        for fd in arcpy.ListDatasets("*","Raster"):
            if fd not in exluded_data:
                input_data_array.append(database + "\\" + fd)
arcpy.AddMessage("Finished obtaining data from the raster sources")

arcpy.env.overwriteOutput=True

# Create a new environment

out_c = "in_memory" + "\\" + "output_copy"
arcpy.CopyFeatures_management(userInPutFc, out_c)


# Recalculate extent based on the feature
arcpy.RecalculateFeatureClassExtent_management(out_c)
# Feature extents
vectorExtent = arcpy.Describe(userInPutFc).extent
vectorExtentXMin = vectorExtent.XMin
vectorExtentYMin = vectorExtent.YMin
vectorExtentXMax = vectorExtent.XMax
vectorExtentYMin = vectorExtent.YMax

# Checkout the extension for using in statistics
arcpy.CheckOutExtension("Spatial")

arcpy.AddMessage("Started processing statistics")

table = []
count = 0
OI = arcpy.Describe(out_c)
field = OI.fields
zone_field = "Feature_no"

arcpy.AddMessage("The zone field is: " + zone_field)

for raster in input_data_array:
    rasterLayerName = arcpy.Describe(raster).name
    arcpy.AddMessage("Processing statistics with: " + rasterLayerName)
    # Raster extents
    rasterExtent = arcpy.Describe(raster).extent
    rasterExtentXMin = rasterExtent.XMin
    rasterExtentYMin = rasterExtent.YMin
    rasterExtentXMax = rasterExtent.XMax
    rasterExtentYMax = rasterExtent.YMax
    # Compare vector and raster extents
    DXMin = rasterExtentXMin - vectorExtentXMin
    DYMin = rasterExtentYMin - vectorExtentYMin
    DXMax = rasterExtentXMax - vectorExtentXMax
    DYMax = rasterExtentYMax - vectorExtentYMin

    # Check if the features share extents
    if DXMin < 0 and DYMin < 0 and DXMax > 0 and DYMax > 0:
        out_t = "in_memory" + "\\" + "output_table" + str(count)
        arcpy.sa.ZonalStatisticsAsTable(out_c, zone_field, raster, out_t, "DATA", "SUM")
        arcpy.AddField_management(out_t, "SourceLayer", "TEXT")

        arcpy.CalculateField_management(out_t, "SourceLayer", "'" + rasterLayerName + "'", "PYTHON", "")
        
        table.append(out_t)
        count = count + 1

# Merge the data
arcpy.Merge_management(table, final)

arcpy.AddMessage("Merged the output table")
# 
fieldList = arcpy.ListFields(final)
for field_of_interest in fieldList:
    if field_of_interest.name == "SUM":
        arcpy.AlterField_management(final, field_of_interest.name, 'SUM_kWh_kWp', 'SUM_kWh_kWp')
    if field_of_interest.name == "AREA":
        arcpy.AlterField_management(final, field_of_interest.name, 'AREA_SquareMeters', 'AREA_SquareMeters')
arcpy.AddMessage("Updated the table fields")

arcpy.SetParameter(1, final)

arcpy.AddMessage(final)


‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Any idea how I can handle workspace settings.

0 Kudos