Select to view content in your preferred language

No resource could be found at that address. Failed to execute (UploadServiceDefinition).

517
4
Jump to solution
07-15-2024 01:15 AM
muhammadidrees
New Contributor

ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds. ERROR: code:404, Could not find resource or operation 'ResultMCDA20240715121146590735.ImageServer' on the system., No resource could be found at that address. Failed to execute (UploadServiceDefinition). Failed to execute (MCDA Sohar Publish). Failed to execute (MCDA Sohar Publish). Failed.

0 Kudos
2 Solutions

Accepted Solutions
HaydenWelch
Frequent Contributor

Alright @muhammadidrees, I went ahead and wrote some code revies for you. It's still not 100% obvious where you're getting the 404 error, but I notated a few points where the error could be occurring. All you need to do is review my notes and your code side by side and see if you can spot the point that your code is actually raising the 404. My bet is on using hardcoded local filepaths and never actually doing anything with the passed parameters.

import sys, os
import traceback
import arcpy

#from arcpy.sa import *

# HW: Avoid * imports in the context of a script or tool
#     * imports should only really be used in terminal sessions
#     or when you are sure of the namespace you are working in
#     e.g. when you're building a package and need to elevate
#     submodule code into a module's __init__.py namespace
#     Use named imports instead:
 
arcpy.CheckOutExtension("Spatial")
import arcpy.sa as sa

# HW: All of these paths are hardcoded to the C drive.
#     Will the tool have access to these on the geoproccessing server?
#     This could be the source of the 404 error you're seeing.
service_name = "ResultMCDA"
base_folder = r"C:\Temp\MCDA"
sddraft_file = os.path.join(base_folder, f"{service_name}.sddraft")
sd_file = os.path.join(base_folder, f"{service_name}.sd")

arcpy.env.workspace = os.path.join(base_folder, "IPS.gdb")
arcpy.env.overwriteOutput = True
arcpy.env.extent = "CargoTerminal"
conn_file = os.path.join(base_folder, "connection.ags")

analysis_for = arcpy.GetParameterAsText(0)

# HW: This is a more extensible way to build your parameters dictionary
#     Now you can add or remove rasters from the list without changing
#     the rest of the code

# HW: You are pulling parameters from the tool dialog, but you are not
#     actually using them anywhere in the script. You're only using the
#     string literals that are defined in the list below.
rasters = \
    ["CargoTerminal", "Food", "Governmental", "HeavyIndustrial", 
    "IronAndSteel", "LightManufacturing", "Logistics", "Petrochemicals", 
    "ServiceProvider", "Solar", "Textiles", "MajorRoads", "MinorRoads", 
    "Railway", "StormWater"]
params = [arcpy.GetParameterAsText(i) for i in range(1, len(rasters))]

parameters = dict(zip(rasters, params))


# HW: Try to avoid single variable names like 'x, y' when unpacking
#     tuples or dictionaries. It's hard to tell what they represent
#     Unless it's something like 'x, y = point'.
#weighted_rasters = []
#for raster_name, raster in parameters.items():
#    weighted_rasters.append(raster_name)
    
# Even better, use a comprehension to build the list
weighted_rasters = [raster_name for raster_name, _ in parameters.items()]

arcpy.AddMessage("Rasters collected")
raster_count = len(weighted_rasters)
each_raster_weight = 1.0 / float(raster_count)

# HW: Never used?
#weighted_sum_rasters = []

wstable = [[w_raster, "Value", each_raster_weight] for w_raster in weighted_rasters]

# HW: This was generating a string, the above line generates a 2D list/table as shown in the 
#     sa.WeightedSum documentation

# wstable = ""
# for i in range(raster_count):
#     ws = weighted_rasters[i] + " VALUE " + str(each_raster_weight)
#     wstable = wstable + ";" + ws

# HW: Modified the try/except block to only wrap the code that could potentially raise an error
#     All above code is basically unable to raise an error as it returns the exact same result
#     regardless of the input. Because the parameters are not used in the script.
try:
    weighted_sum_result = sa.WeightedSum(wstable)
    arcpy.AddMessage("WeightedSum result generated successfully...")
    weighted_sum_result = sa.Int(weighted_sum_result)
    weighted_sum_result.save(service_name)
    arcpy.AddMessage("Raster saved at:" + service_name)

    arcpy.AddMessage("Creating SD draft image...")
    arcpy.CreateImageSDDraft(
        raster_or_mosaic_layer=service_name, 
        out_sddraft=sddraft_file, 
        service_type=service_name, 
        server_type='FROM_CONNECTION_FILE',
        connection_file_path=conn_file,
        copy_data_to_server=True,
        folder_name="MCDA",
        summary="Multi criteria decision analysis", 
        tags="ANALYSIS,MCDA")

    # Stage the service
    arcpy.AddMessage("Uploading SD draft as service...")
    arcpy.StageService_server(
        in_service_definition_draft=sddraft_file, 
        out_service_definition=sd_file)

    # Upload the service definition
    arcpy.AddMessage("Publishing service...")
    arcpy.UploadServiceDefinition_server(
        in_sd_file=sd_file, 
        in_server=conn_file, 
        in_folder_type="FROM_SERVICE_DEFINITION", 
        in_folder="MCDA",
        in_public=True, 
        in_organization=False, 
        in_override=True)
    arcpy.AddMessage("Service published successfully.")

# HW: You are currently printing the msgs variable, which is not defined 
#     in the except block.
except arcpy.ExecuteError:
    zmsgs = arcpy.GetMessages(2)
    arcpy.AddError(zmsgs) #arcpy.AddError(msgs)
    print(zmsgs) #print(msgs)

# HW: When catching a bare exception, you should always at least print the exception
except Exception as e:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = f"PYTHON ERRORS:{e}\nTraceback info:\n{tbinfo}\nError Info:\n{str(sys.exc_info()[1])}"
    msgs = f"ArcPy ERRORS:\n{arcpy.GetMessages(2)}\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
    print(pymsg)
    print(msgs)
    
finally:
    arcpy.CheckInExtension("Spatial")

View solution in original post

TonyAlmeida
Frequent Contributor

I would add some debugging, maybe something like this...

import sys
import os
import xml.dom.minidom as DOM
import traceback
import datetime
import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

service_name = "ResultMCDA"
base_folder = r"C:\Temp\MCDA"
sddraft_file = os.path.join(base_folder, f"{service_name}.sddraft")
sd_file = os.path.join(base_folder, f"{service_name}.sd")

arcpy.env.workspace = os.path.join(base_folder, "IPS.gdb")
arcpy.env.overwriteOutput = True
arcpy.env.extent = "CargoTerminal"
conn_file = os.path.join(base_folder, "connection.ags")

try:
    analysis_for = arcpy.GetParameterAsText(0)
    parameters = {}
    parameters["CargoTerminal"] = arcpy.GetParameterAsText(1)
    parameters["Food"] = arcpy.GetParameterAsText(2)
    parameters["Governmental"] = arcpy.GetParameterAsText(3)
    parameters["HeavyIndustrial"] = arcpy.GetParameterAsText(4)
    parameters["IronAndSteel"] = arcpy.GetParameterAsText(5)
    parameters["LightManufacturing"] = arcpy.GetParameterAsText(6)
    parameters["Logistics"] = arcpy.GetParameterAsText(7)
    parameters["Petrochemicals"] = arcpy.GetParameterAsText(8)
    parameters["ServiceProvider"] = arcpy.GetParameterAsText(9)
    parameters["Solar"] = arcpy.GetParameterAsText(10)
    parameters["Textiles"] = arcpy.GetParameterAsText(11)
    parameters["MajorRoads"] = arcpy.GetParameterAsText(12)
    parameters["MinorRoads"] = arcpy.GetParameterAsText(13)
    parameters["Railway"] = arcpy.GetParameterAsText(14)
    parameters["StormWater"] = arcpy.GetParameterAsText(15)

    weighted_rasters = []
    for x, y in parameters.items():
        if y:  # Check if the parameter is not empty
            weighted_rasters.append(x)

    arcpy.AddMessage("Rasters collected")
    raster_count = len(weighted_rasters)
    if raster_count == 0:
        arcpy.AddError("No valid rasters provided.")
        raise ValueError("No valid rasters provided.")

    each_raster_weight = 1 / float(raster_count)
    weighted_sum_rasters = []

    wstable = ""
    for i in range(raster_count):
        ws = weighted_rasters[i] + " VALUE " + str(each_raster_weight)
        wstable = wstable + ";" + ws

    weighted_sum_result = WeightedSum(wstable)
    arcpy.AddMessage("WeightedSum result generated successfully...")

    weighted_sum_result = Int(weighted_sum_result)
    weighted_sum_result.save(service_name)
    arcpy.AddMessage("Raster saved at:" + service_name)

    arcpy.AddMessage("Creating SD draft image...")
    arcpy.CreateImageSDDraft(service_name, sddraft_file, service_name, 'FROM_CONNECTION_FILE', conn_file,
                             True, "MCDA", "Multi criteria decision analysis", "ANALYSIS,MCDA")

    # Stage the service
    arcpy.AddMessage("Uploading SD draft as service...")
    arcpy.StageService_server(sddraft_file, sd_file)

    # Upload the service definition
    arcpy.AddMessage("Publishing service...")
    arcpy.UploadServiceDefinition_server(sd_file, conn_file, in_folder_type="FROM_SERVICE_DEFINITION", in_folder="MCDA",
                                         in_public=True, in_organization=False, in_override=True)
    arcpy.AddMessage("Service published successfully.")

except arcpy.ExecuteError:
    msgs = arcpy.GetMessages(2)
    arcpy.AddError(msgs)
    print(msgs)

except Exception as e:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
    print(pymsg)
    print(msgs)

finally:
    arcpy.CheckInExtension("Spatial")

View solution in original post

4 Replies
HaydenWelch
Frequent Contributor

Will definitely need more information. Try sharing a code snippet that caused the error or giving any other information at all besides just the raw error code.

With this all I can tell you is that the code is correct and you're telling it to look for something that returns a 404 http error. If that image server isn't real then you'll get that error.

0 Kudos
muhammadidrees
New Contributor
Hi @HaydenWelch Thanks for your support. Your suggestion urges me to think in a different angle, that would really help me. 
 
Here is my complete code. This code is working fine when I execute it as a stand alone script or as a tool. But when I publish this tool as a geoprocessing service then it produces error on the last step as I mentioned earlier.
 
 
 
import sys, os
import xml.dom.minidom as DOM
import traceback
import datetime
import arcpy
from arcpy.sa import *
 
arcpy.CheckOutExtension("Spatial")
 
service_name = "ResultMCDA"
 
base_folder = r"C:\Temp\MCDA"
sddraft_file = os.path.join(base_folder, f"{service_name}.sddraft")
sd_file = os.path.join(base_folder, f"{service_name}.sd")
 
arcpy.env.workspace = os.path.join(base_folder, "IPS.gdb")
arcpy.env.overwriteOutput = True
arcpy.env.extent = "CargoTerminal"
conn_file = os.path.join(base_folder, "connection.ags")
 
try:
analysis_for = arcpy.GetParameterAsText(0)
parameters = {}
parameters["CargoTerminal"] = arcpy.GetParameterAsText(1)
parameters["Food"] = arcpy.GetParameterAsText(2)
parameters["Governmental"] = arcpy.GetParameterAsText(3)
parameters["HeavyIndustrial"] = arcpy.GetParameterAsText(4)
parameters["IronAndSteel"] = arcpy.GetParameterAsText(5)
parameters["LightManufacturing"] = arcpy.GetParameterAsText(6)
parameters["Logistics"] = arcpy.GetParameterAsText(7)
parameters["Petrochemicals"] = arcpy.GetParameterAsText(8)
parameters["ServiceProvider"] = arcpy.GetParameterAsText(9)
parameters["Solar"] = arcpy.GetParameterAsText(10)
parameters["Textiles"] = arcpy.GetParameterAsText(11)
parameters["MajorRoads"] = arcpy.GetParameterAsText(12)
parameters["MinorRoads"] = arcpy.GetParameterAsText(13)
parameters["Railway"] = arcpy.GetParameterAsText(14)
parameters["StormWater"] = arcpy.GetParameterAsText(15)
 
weighted_rasters = []
for x, y in parameters.items():
weighted_rasters.append(x)
 
arcpy.AddMessage("Rasters collected")
raster_count = len(weighted_rasters)
each_raster_weight = 1 / float(raster_count)
weighted_sum_rasters = []
 
wstable = ""
for i in range(raster_count):
ws = weighted_rasters[i] + " VALUE " + str(each_raster_weight)
wstable = wstable + ";" + ws
 
weighted_sum_result = WeightedSum(wstable)
arcpy.AddMessage("WeightedSum result generated successfully...")
 
weighted_sum_result = Int(weighted_sum_result)
weighted_sum_result.save(service_name)
arcpy.AddMessage("Raster saved at:" + service_name)
 
arcpy.AddMessage("Creating SD draft image...")
arcpy.CreateImageSDDraft(service_name, sddraft_file, service_name, 'FROM_CONNECTION_FILE',conn_file,
True, "MCDA","Multi criteria decision analysis", "ANALYSIS,MCDA")
 
# Stage the service
arcpy.AddMessage("Uploading SD draft as service...")
arcpy.StageService_server(sddraft_file, sd_file)
 
# Upload the service definition
arcpy.AddMessage("Publishing service...")
arcpy.UploadServiceDefinition_server(sd_file, conn_file, in_folder_type="FROM_SERVICE_DEFINITION", in_folder="MCDA",
in_public=True, in_organization=False, in_override=True)
arcpy.AddMessage("Service published successfully.")
 
except arcpy.ExecuteError:
msgs = arcpy.GetMessages(2)
arcpy.AddError(msgs)
print(msgs)
 
except Exception as e:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(pymsg)
arcpy.AddError(msgs)
print(pymsg)
print(msgs)
 
finally:
arcpy.CheckInExtension("Spatial")
HaydenWelch
Frequent Contributor

Alright @muhammadidrees, I went ahead and wrote some code revies for you. It's still not 100% obvious where you're getting the 404 error, but I notated a few points where the error could be occurring. All you need to do is review my notes and your code side by side and see if you can spot the point that your code is actually raising the 404. My bet is on using hardcoded local filepaths and never actually doing anything with the passed parameters.

import sys, os
import traceback
import arcpy

#from arcpy.sa import *

# HW: Avoid * imports in the context of a script or tool
#     * imports should only really be used in terminal sessions
#     or when you are sure of the namespace you are working in
#     e.g. when you're building a package and need to elevate
#     submodule code into a module's __init__.py namespace
#     Use named imports instead:
 
arcpy.CheckOutExtension("Spatial")
import arcpy.sa as sa

# HW: All of these paths are hardcoded to the C drive.
#     Will the tool have access to these on the geoproccessing server?
#     This could be the source of the 404 error you're seeing.
service_name = "ResultMCDA"
base_folder = r"C:\Temp\MCDA"
sddraft_file = os.path.join(base_folder, f"{service_name}.sddraft")
sd_file = os.path.join(base_folder, f"{service_name}.sd")

arcpy.env.workspace = os.path.join(base_folder, "IPS.gdb")
arcpy.env.overwriteOutput = True
arcpy.env.extent = "CargoTerminal"
conn_file = os.path.join(base_folder, "connection.ags")

analysis_for = arcpy.GetParameterAsText(0)

# HW: This is a more extensible way to build your parameters dictionary
#     Now you can add or remove rasters from the list without changing
#     the rest of the code

# HW: You are pulling parameters from the tool dialog, but you are not
#     actually using them anywhere in the script. You're only using the
#     string literals that are defined in the list below.
rasters = \
    ["CargoTerminal", "Food", "Governmental", "HeavyIndustrial", 
    "IronAndSteel", "LightManufacturing", "Logistics", "Petrochemicals", 
    "ServiceProvider", "Solar", "Textiles", "MajorRoads", "MinorRoads", 
    "Railway", "StormWater"]
params = [arcpy.GetParameterAsText(i) for i in range(1, len(rasters))]

parameters = dict(zip(rasters, params))


# HW: Try to avoid single variable names like 'x, y' when unpacking
#     tuples or dictionaries. It's hard to tell what they represent
#     Unless it's something like 'x, y = point'.
#weighted_rasters = []
#for raster_name, raster in parameters.items():
#    weighted_rasters.append(raster_name)
    
# Even better, use a comprehension to build the list
weighted_rasters = [raster_name for raster_name, _ in parameters.items()]

arcpy.AddMessage("Rasters collected")
raster_count = len(weighted_rasters)
each_raster_weight = 1.0 / float(raster_count)

# HW: Never used?
#weighted_sum_rasters = []

wstable = [[w_raster, "Value", each_raster_weight] for w_raster in weighted_rasters]

# HW: This was generating a string, the above line generates a 2D list/table as shown in the 
#     sa.WeightedSum documentation

# wstable = ""
# for i in range(raster_count):
#     ws = weighted_rasters[i] + " VALUE " + str(each_raster_weight)
#     wstable = wstable + ";" + ws

# HW: Modified the try/except block to only wrap the code that could potentially raise an error
#     All above code is basically unable to raise an error as it returns the exact same result
#     regardless of the input. Because the parameters are not used in the script.
try:
    weighted_sum_result = sa.WeightedSum(wstable)
    arcpy.AddMessage("WeightedSum result generated successfully...")
    weighted_sum_result = sa.Int(weighted_sum_result)
    weighted_sum_result.save(service_name)
    arcpy.AddMessage("Raster saved at:" + service_name)

    arcpy.AddMessage("Creating SD draft image...")
    arcpy.CreateImageSDDraft(
        raster_or_mosaic_layer=service_name, 
        out_sddraft=sddraft_file, 
        service_type=service_name, 
        server_type='FROM_CONNECTION_FILE',
        connection_file_path=conn_file,
        copy_data_to_server=True,
        folder_name="MCDA",
        summary="Multi criteria decision analysis", 
        tags="ANALYSIS,MCDA")

    # Stage the service
    arcpy.AddMessage("Uploading SD draft as service...")
    arcpy.StageService_server(
        in_service_definition_draft=sddraft_file, 
        out_service_definition=sd_file)

    # Upload the service definition
    arcpy.AddMessage("Publishing service...")
    arcpy.UploadServiceDefinition_server(
        in_sd_file=sd_file, 
        in_server=conn_file, 
        in_folder_type="FROM_SERVICE_DEFINITION", 
        in_folder="MCDA",
        in_public=True, 
        in_organization=False, 
        in_override=True)
    arcpy.AddMessage("Service published successfully.")

# HW: You are currently printing the msgs variable, which is not defined 
#     in the except block.
except arcpy.ExecuteError:
    zmsgs = arcpy.GetMessages(2)
    arcpy.AddError(zmsgs) #arcpy.AddError(msgs)
    print(zmsgs) #print(msgs)

# HW: When catching a bare exception, you should always at least print the exception
except Exception as e:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = f"PYTHON ERRORS:{e}\nTraceback info:\n{tbinfo}\nError Info:\n{str(sys.exc_info()[1])}"
    msgs = f"ArcPy ERRORS:\n{arcpy.GetMessages(2)}\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
    print(pymsg)
    print(msgs)
    
finally:
    arcpy.CheckInExtension("Spatial")
TonyAlmeida
Frequent Contributor

I would add some debugging, maybe something like this...

import sys
import os
import xml.dom.minidom as DOM
import traceback
import datetime
import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

service_name = "ResultMCDA"
base_folder = r"C:\Temp\MCDA"
sddraft_file = os.path.join(base_folder, f"{service_name}.sddraft")
sd_file = os.path.join(base_folder, f"{service_name}.sd")

arcpy.env.workspace = os.path.join(base_folder, "IPS.gdb")
arcpy.env.overwriteOutput = True
arcpy.env.extent = "CargoTerminal"
conn_file = os.path.join(base_folder, "connection.ags")

try:
    analysis_for = arcpy.GetParameterAsText(0)
    parameters = {}
    parameters["CargoTerminal"] = arcpy.GetParameterAsText(1)
    parameters["Food"] = arcpy.GetParameterAsText(2)
    parameters["Governmental"] = arcpy.GetParameterAsText(3)
    parameters["HeavyIndustrial"] = arcpy.GetParameterAsText(4)
    parameters["IronAndSteel"] = arcpy.GetParameterAsText(5)
    parameters["LightManufacturing"] = arcpy.GetParameterAsText(6)
    parameters["Logistics"] = arcpy.GetParameterAsText(7)
    parameters["Petrochemicals"] = arcpy.GetParameterAsText(8)
    parameters["ServiceProvider"] = arcpy.GetParameterAsText(9)
    parameters["Solar"] = arcpy.GetParameterAsText(10)
    parameters["Textiles"] = arcpy.GetParameterAsText(11)
    parameters["MajorRoads"] = arcpy.GetParameterAsText(12)
    parameters["MinorRoads"] = arcpy.GetParameterAsText(13)
    parameters["Railway"] = arcpy.GetParameterAsText(14)
    parameters["StormWater"] = arcpy.GetParameterAsText(15)

    weighted_rasters = []
    for x, y in parameters.items():
        if y:  # Check if the parameter is not empty
            weighted_rasters.append(x)

    arcpy.AddMessage("Rasters collected")
    raster_count = len(weighted_rasters)
    if raster_count == 0:
        arcpy.AddError("No valid rasters provided.")
        raise ValueError("No valid rasters provided.")

    each_raster_weight = 1 / float(raster_count)
    weighted_sum_rasters = []

    wstable = ""
    for i in range(raster_count):
        ws = weighted_rasters[i] + " VALUE " + str(each_raster_weight)
        wstable = wstable + ";" + ws

    weighted_sum_result = WeightedSum(wstable)
    arcpy.AddMessage("WeightedSum result generated successfully...")

    weighted_sum_result = Int(weighted_sum_result)
    weighted_sum_result.save(service_name)
    arcpy.AddMessage("Raster saved at:" + service_name)

    arcpy.AddMessage("Creating SD draft image...")
    arcpy.CreateImageSDDraft(service_name, sddraft_file, service_name, 'FROM_CONNECTION_FILE', conn_file,
                             True, "MCDA", "Multi criteria decision analysis", "ANALYSIS,MCDA")

    # Stage the service
    arcpy.AddMessage("Uploading SD draft as service...")
    arcpy.StageService_server(sddraft_file, sd_file)

    # Upload the service definition
    arcpy.AddMessage("Publishing service...")
    arcpy.UploadServiceDefinition_server(sd_file, conn_file, in_folder_type="FROM_SERVICE_DEFINITION", in_folder="MCDA",
                                         in_public=True, in_organization=False, in_override=True)
    arcpy.AddMessage("Service published successfully.")

except arcpy.ExecuteError:
    msgs = arcpy.GetMessages(2)
    arcpy.AddError(msgs)
    print(msgs)

except Exception as e:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
    print(pymsg)
    print(msgs)

finally:
    arcpy.CheckInExtension("Spatial")