AnsweredAssumed Answered

How let Geoprocessing service make a list of feature classes from a folder

Question asked by saidakif on Nov 27, 2017

Hi Python folks

I created an arctoolbox to make some surface calculation from specific layer (in a GIS server). this arctoolbox is converted to a Geoprocessing service (GPS) embedded after in a web application.

in bring this tool to be more flexible, I create a folder in GIS server where there are more feature classes. Th user, without any skills in GIS, could just add his feature classes in this folder and run the GPS.

The arctoolbox work perfectly in arcMAP. But after creating the GPS, it seems that the list creating is not working.

Is there any way to let a GPS do this list creating and after processing it?

 

Here after my code and the tool interface. 

Thanks a lot for your help

import arcpy
import os
from arcpy import env
env.outputZFlag = "DISABLED"
env.outputMFlag = "DISABLED"
# Set all the parameters
fClass1 = arcpy.GetParameterAsText(0)
fClass2 = arcpy.GetParameterAsText(1)
fClass3 = arcpy.GetParameterAsText(2)
fClass4 = arcpy.GetParameterAsText(3)
fClass5 = arcpy.GetParameterAsText(4)
fClass5 = "%scratchworkspace%\\Calcul"
workspace = arcpy.GetParameterAsText(5)
env.workspace = workspace

Union1 = "%scratchworkspace%\\Union1"
Union = "%scratchworkspace%\\Union"
UnionF = "%scratchworkspace%\\UnionF"
Union_Diss = "%scratchworkspace%\\Union_Diss"
Union_Diss_inter = "%scratchworkspace%\\Union_Diss_inter"
Union_Diss_inters = "%scratchworkspace%\\Union_Diss_inters"
env.workspace = "%scratchworkspace%"
for objFeatureClass in arcpy.ListFeatureClasses():
arcpy.Delete_management(objFeatureClass)
# Union
arcpy.Union_analysis ([fClass1, fClass2], Union1, "ONLY_FID")
arcpy.Union_analysis ([Union1, fClass3],Union, "ONLY_FID")
arcpy.Delete_management(Union1)
env.workspace = workspace
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference("Canada Albers Equal Area Conic")
listCH_fc = []
listCH_fc = arcpy.ListFeatureClasses()
if listCH_fc:
for i in range(0,len(listCH_fc)):
arcpy.CopyFeatures_management(listCH_fc[i],"%scratchworkspace%\\Out"+str(i))

env.workspace = "%scratchworkspace%"
listCH = []
listCH = arcpy.ListFeatureClasses("Out*")
if len(listCH)==1:
arcpy.Union_analysis ([Union, listCH[0]],UnionF, "ONLY_FID")
if len(listCH)==2:
arcpy.Union_analysis ([listCH[0], listCH[1]],"%scratchworkspace%\\FOut", "ONLY_FID")
arcpy.Union_analysis ([Union, "%scratchworkspace%\\FOut"],UnionF, "ONLY_FID")
if len(listCH)>2:
arcpy.Union_analysis ([listCH[0], listCH[1]],"%scratchworkspace%\\FOut1", "ONLY_FID")
for i in range(2,len(listCH)):
arcpy.Union_analysis ([listCH[i], "%scratchworkspace%\\FOut"+str(i-1)],"%scratchworkspace%\\FOut"+str(i), "ONLY_FID")
arcpy.Union_analysis ([Union, "%scratchworkspace%\\FOut"+str(len(listCH)-1)],UnionF, "ONLY_FID")
if listCH:
Union = UnionF

arcpy.AddField_management(Union,"Diss","LONG")
arcpy.CalculateField_management(Union,"Diss",1,"PYTHON")
# Generalization
arcpy.Dissolve_management (Union, Union_Diss, "Diss","","MULTI_PART")
# Intersect
arcpy.Intersect_analysis([Union_Diss, fClass4], Union_Diss_inter,"ALL")
arcpy.Dissolve_management (Union_Diss_inter, Union_Diss_inters, "Name","","MULTI_PART")
arcpy.JoinField_management(Union_Diss_inters, "Name", fClass4, "Name", "TAreaPerEEZ")
arcpy.AddField_management(Union_Diss_inters, "EEZTArea", "DOUBLE")
arcpy.AddField_management(Union_Diss_inters, "AreaProt", "DOUBLE")
arcpy.AddField_management(Union_Diss_inters, "TProtArea", "DOUBLE")
arcpy.AddField_management(Union_Diss_inters, "PercTArea", "DOUBLE")
arcpy.AddField_management(Union_Diss_inters, "TPercArea", "DOUBLE")

exp = "!SHAPE.AREA@SQUAREKILOMETERS!"
arcpy.CalculateField_management(Union_Diss_inters, "AreaProt", exp, "PYTHON_9.3")
EEZTArea = 0
TPArea = 0
urows = arcpy.UpdateCursor(Union_Diss_inters)
for row in urows:
TPArea += row.getValue("AreaProt")
EEZTArea += row.getValue("TAreaPerEEZ")
del urows
urows = arcpy.UpdateCursor(Union_Diss_inters)
for row in urows:
row.setValue("TProtArea", TPArea)
row.setValue("EEZTArea", EEZTArea)
if row.getValue("Name")=="Atlantic and Arctic":
row.setValue("PercTArea", (row.getValue("AreaProt")/row.getValue("TAreaPerEEZ"))*100)
row.setValue("TPercArea", (row.getValue("TProtArea")/row.getValue("EEZTArea"))*100)
if row.getValue("Name")=="Pacific":
row.setValue("PercTArea", (row.getValue("AreaProt")/row.getValue("TAreaPerEEZ"))*100)
row.setValue("TPercArea", (row.getValue("TProtArea")/row.getValue("EEZTArea"))*100)
urows.updateRow(row)
del urows
arcpy.CopyFeatures_management(Union_Diss_inters, fClass5)

Outcomes