Hi,
i have develop a python script that convert a MakeFeatureLayer to Shapefile into a specific folder with concatenation timestamp. This is the code write:
filename = r"C:\tmp"
data = datetime.datetime.now()
timestamp = str(data).replace("-","").replace(":","").replace(".","").replace(" ","_")events_l = arcpy.MakeFeatureLayer_management("event_lyr", "events_layer")
print "Inizio segmentazione dinamica eventi"
updateFC = arcpy.CopyFeatures_management(events_l,filename + "\\events_"+timestamp+".shp")
arcpy.SelectLayerByAttribute_management("event_lyr", "NEW_SELECTION", "LOC LIKE 'LO%'")
arcpy.AddMessage("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
print("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
layer = arcpy.MakeFeatureLayer_management(Lo_UTM, "loc_lyr")
sourceFC = layer
sourceField = ['OR_ID','SHAPE@']
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceField)}updateFieldsList = ["loc", "Shape"]
with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:
for updateRow in updateRows:
keyValue = updateRow[0]
print(keyValue)
if keyValue in valueDict:
updateRow[1] = valueDict[keyValue][0]
print(valueDict[keyValue][0])
updateRows.updateRow(updateRow)
arcpy.SelectLayerByAttribute_management('event_lyr', "CLEAR_SELECTION")
arcpy.SetParameter(1, arcpy.MakeFeatureLayer_management(filename + "\\events_"+timestamp+".shp","output_lyr",""))
when i run the script on ArcMap works very fine and so i have publish it on ArcGIS Server as a GP Service Async.
When i run this GP by REST Folder works very fine, but if i stop the GP Service and clear the folder with shapefile created after start if i run the GP this failed and on log of ArcGIS Server i have this error:
in MakeFeatureLayer raise e ExecuteError: ERROR 000622: Failed to execute (Make Feature Layer). Parameters are not valid. ERROR 000628: Cannot set input into parameter out_layer. Failed to execute
on the ultimate line of script.
But i f i republish the script the GP works.
Any help why this issues?
Thanks
Solved! Go to Solution.
You should be using the arcpy.env.scratchFolder to construct the path to output data. I'm also using the os.path.join function to construct the path to output data. Finally, you don't need to create a feature layer again to return the data, simply return the data the updateFCPath variable is pointing to.
import os
filePath = arcpy.env.scratchFolder
data = datetime.datetime.now()
timestamp = str(data).replace("-","").replace(":","").replace(".","").replace(" ","_")
events_l = arcpy.MakeFeatureLayer_management("event_lyr", "events_layer")
print "Inizio segmentazione dinamica eventi"
updateFCPath = os.path.join(filePath,"{0}.shp".format(timestamp))
updateFC = arcpy.CopyFeatures_management(events_l,updateFCPath)
arcpy.SelectLayerByAttribute_management("event_lyr", "NEW_SELECTION", "LOC LIKE 'LO%'")
arcpy.AddMessage("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
print("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
layer = arcpy.MakeFeatureLayer_management(Lo_UTM, "loc_lyr")
sourceFC = layer
sourceField = ['OR_ID','SHAPE@']
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceField)}
updateFieldsList = ["loc", "Shape"]
with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:
for updateRow in updateRows:
keyValue = updateRow[0]
print(keyValue)
if keyValue in valueDict:
updateRow[1] = valueDict[keyValue][0]
print(valueDict[keyValue][0])
updateRows.updateRow(updateRow)
arcpy.SelectLayerByAttribute_management('event_lyr', "CLEAR_SELECTION")
arcpy.SetParameter(1,updateFCPath)
What is "event_lyr", used initially on line 7? If that's already a feature layer, there's no need to run the Make Feature Layer tool again, and you don't really need to store it on disk. I've also added the arcpy.AddMessage line so you see the print statement on line 4.
data = datetime.datetime.now()
timestamp = str(data).replace("-","").replace(":","").replace(".","").replace(" ","_")
print "Inizio segmentazione dinamica eventi"
arcpy.AddMessage("Inizio segmentazione dinamica eventi")
arcpy.SelectLayerByAttribute_management("event_lyr", "NEW_SELECTION", "LOC LIKE 'LO%'")
arcpy.AddMessage("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
print("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
layer = arcpy.MakeFeatureLayer_management(Lo_UTM, "loc_lyr")
sourceFC = layer
sourceField = ['OR_ID','SHAPE@']
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceField)}
updateFieldsList = ["loc", "Shape"]
with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:
for updateRow in updateRows:
keyValue = updateRow[0]
print(keyValue)
if keyValue in valueDict:
updateRow[1] = valueDict[keyValue][0]
print(valueDict[keyValue][0])
updateRows.updateRow(updateRow)
arcpy.SelectLayerByAttribute_management('event_lyr', "CLEAR_SELECTION")
arcpy.SetParameter(1,"event_lyr")
You should be using the arcpy.env.scratchFolder to construct the path to output data. I'm also using the os.path.join function to construct the path to output data. Finally, you don't need to create a feature layer again to return the data, simply return the data the updateFCPath variable is pointing to.
import os
filePath = arcpy.env.scratchFolder
data = datetime.datetime.now()
timestamp = str(data).replace("-","").replace(":","").replace(".","").replace(" ","_")
events_l = arcpy.MakeFeatureLayer_management("event_lyr", "events_layer")
print "Inizio segmentazione dinamica eventi"
updateFCPath = os.path.join(filePath,"{0}.shp".format(timestamp))
updateFC = arcpy.CopyFeatures_management(events_l,updateFCPath)
arcpy.SelectLayerByAttribute_management("event_lyr", "NEW_SELECTION", "LOC LIKE 'LO%'")
arcpy.AddMessage("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
print("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
layer = arcpy.MakeFeatureLayer_management(Lo_UTM, "loc_lyr")
sourceFC = layer
sourceField = ['OR_ID','SHAPE@']
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceField)}
updateFieldsList = ["loc", "Shape"]
with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:
for updateRow in updateRows:
keyValue = updateRow[0]
print(keyValue)
if keyValue in valueDict:
updateRow[1] = valueDict[keyValue][0]
print(valueDict[keyValue][0])
updateRows.updateRow(updateRow)
arcpy.SelectLayerByAttribute_management('event_lyr', "CLEAR_SELECTION")
arcpy.SetParameter(1,updateFCPath)
What is "event_lyr", used initially on line 7? If that's already a feature layer, there's no need to run the Make Feature Layer tool again, and you don't really need to store it on disk. I've also added the arcpy.AddMessage line so you see the print statement on line 4.
data = datetime.datetime.now()
timestamp = str(data).replace("-","").replace(":","").replace(".","").replace(" ","_")
print "Inizio segmentazione dinamica eventi"
arcpy.AddMessage("Inizio segmentazione dinamica eventi")
arcpy.SelectLayerByAttribute_management("event_lyr", "NEW_SELECTION", "LOC LIKE 'LO%'")
arcpy.AddMessage("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
print("num_sel: " + str(arcpy.GetCount_management("event_lyr")))
layer = arcpy.MakeFeatureLayer_management(Lo_UTM, "loc_lyr")
sourceFC = layer
sourceField = ['OR_ID','SHAPE@']
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceField)}
updateFieldsList = ["loc", "Shape"]
with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:
for updateRow in updateRows:
keyValue = updateRow[0]
print(keyValue)
if keyValue in valueDict:
updateRow[1] = valueDict[keyValue][0]
print(valueDict[keyValue][0])
updateRows.updateRow(updateRow)
arcpy.SelectLayerByAttribute_management('event_lyr', "CLEAR_SELECTION")
arcpy.SetParameter(1,"event_lyr")
Hi Jonathan,
thanks for your reply and good help.
"Event_lyr" is a result of MakeFeatureLayer that i have create at the start of the script.
I have apply your change and now the script works fine also after stop service, but now i have seen that when i run the Geoprocessing Service on ArcGIS Server the result is not write into scratchFolder that i have set into script.
You're processing the data in memory, which will be faster than writing to disk. If you want to save the results on disk, use the CopyFeatures tool and construct the path using the arcpy.env.scratch<loc> environment variable.
outData = os.path.join(arcpy.env.scratchFolder,"outData.shp")
or
outData = os.path.join(arcpy.env.scratchGDB,"outData")
and then
arcpy.SetParameter(1,outData)
Thanks Jonathan