0

I'm using arcgis 10.0. I red the ESRI doc to setting up script tools parameters, but I don't succeed at setting them up.

My parameters before (without variable)

#arcpy.env.workspace = r'Z:\Documents\SIG\tests\synthese_tocorrect.gdb'
#pochoir = r'Z:\Documents\SIG\tests\decoupe.shp' #the clip layer with multiple polygons
#outFolder = r'Z:\Documents\SIG\tests\sorties'

My parameters after, with issues :

import arcpy, os
from arcpy import env
inWorkspace = arcpy.GetParamterAsText(0)#Refers to the geodatabase
pochoir = arcpy.GetParamterAsText(1) #Refers to the existing clip layer
outFolder = arcpy.GetParamterAsText(2) #Output folder.
env.workspace = inWorkspace
arcpy.MakeFeatureLayer_management(pochoir, "pochoirlyr") #Create layer to allow for selection
fcList = arcpy.ListFeatureClasses() #the feature class to be clipped by each of the polygon of the clip layer
OIDField = arcpy.Describe("pochoirlyr").OIDFieldName #Get name of OID field

for fc in fcList:
    arcpy.MakeFeatureLayer_management(os.path.join(arcpy.env.workspace, fc), "lyr")
    pocursor = arcpy.SearchCursor("pochoirlyr")
    for porow in pocursor:
        sql = '"' + OIDField + '" = ' + str(porow.getValue(OIDField)) #SQL to select one feature
        arcpy.SelectLayerByAttribute_management("pochoirlyr", "", sql) #Select polygon feature by OID
        arcpy.SelectLayerByLocation_management("lyr","INTERSECT","pochoirlyr") #Select feature layer to be clipped by selected clip feature 
        if arcpy.Describe("lyr").FIDSet: #Check for selection
            out_poly = os.path.join(outFolder, fc + "_" + str(porow.getValue(OIDField))) #Output feature class
            arcpy.Clip_analysis(fc, "pochoirlyr", out_poly)
    del porow
    del pocursor
    arcpy.SelectLayerByAttribute_management("pochoirlyr", "CLEAR_SELECTION")
    arcpy.Delete_management("lyr")

And in the properties of the script tools :

  • geodatabase : Workspace or Feature Dataset, input, no multivalue...
  • pochoir : shapefile, input, no multivalue...
  • sorties : Folder, output...

But when I choose a different parameter when opening the script tool GUI, it does not work. It does the job with the IDE parameters.

Any help ?

  • Are you trying to write a FGDB feature class to a folder rather than a FGDB? I wonder if sorties should be a FGDB. – Aaron Dec 10 '14 at 15:58
  • Without script tool parameters, there are no problems to get shapefiles in "sorties" folder. – Samy-DT Dec 10 '14 at 16:11
  • What about the outFolder? What did you set that to? – Aaron Dec 10 '14 at 16:16
  • Do you realy want to overwrite outFolder? If yes, set env.overwriteoutput=1. If this is just an output space for geoprocessing results, set it type to input in parameters definition window – FelixIP Dec 10 '14 at 20:37
1

You need to use the

arcpy.GetParameterAsText(0)

method:

See examples from:

Understanding script tool parameters

  • Hi @artwork21, I tried this method but instead of recognizing the variable, it's says that the data already exist,and it is deleting my input data...For example, take the first variable, the geodatabase, the GUI sent an error message saying that the geodatabase already exist, and if I run the tool, it delete this geodatabase. The same for the clip shapefile and the output folder. I don't understand... – Samy-DT Dec 10 '14 at 14:39
  • 1
    Can you provide more of your python code, parameter references should not delete workspaces or files. – artwork21 Dec 10 '14 at 15:00
  • I edited the above as close as possible as the desktop help example. – Samy-DT Dec 10 '14 at 15:41
  • Do you have the correct .py file referenced in the script tool? – artwork21 Dec 10 '14 at 16:57
  • If it's complaining that it already exists then you need to set arcpy.env.overwriteoutput = True to allow overwrite. If you change the parameters but the tool is ignoring them then check further down the script that you aren't resetting the variables and that you're actually using them for their intended purpose. – Michael Stimson Dec 10 '14 at 21:23
0

I use sys.argv[n] but GetParameterAsText is the same thing, but be aware that argv[0] is the script, argv[1] is the first parameter as opposed to GetParameterAsText(0) being the first parameter.

On each iteration (after the clip) you need to clear the selection or remake the layers "lyr" and "pochoirlyr" otherwise it will only work on the first iteration then both layers will be empty after that, layer selection is funny like that.

I think this will do what you're after:

import arcpy, os, sys
from arcpy import env

inWorkspace = sys.argv[1]#Refers to the geodatabase
pochoir = sys.argv[2] #Refers to the existing clip layer
outFolder = sys.argv[3] #Output folder.
env.workspace = inWorkspace
fcList = arcpy.ListFeatureClasses() #the feature class to be clipped by each of the polygon of the clip layer
OIDField = arcpy.Describe(pochoir).OIDFieldName #Get name of OID field from the feature class

for fc in fcList:
    pocursor = arcpy.SearchCursor(pochoir) # cursor from the feature class
    for porow in pocursor:
        sql = '"' + OIDField + '" = ' + str(porow.getValue(OIDField)) #SQL to select one feature
        arcpy.MakeFeatureLayer_management(pochoir, "pochoirlyr",sql)  #Create layer with selection already
        arcpy.MakeFeatureLayer_management(os.path.join(arcpy.env.workspace, fc), "lyr")
        arcpy.SelectLayerByLocation_management("lyr","INTERSECT","pochoirlyr") #Select feature layer to be clipped by selected clip feature

        if arcpy.Describe("lyr").fidSet: #Check for selection in the lyr
            out_poly = os.path.join(outFolder, fc + "_" + str(porow.getValue(OIDField))) #Output
            if arcpy.Exists(out_poly) or arcpy.Exists(out_poly + ".shp"):# check it doesn't already exist'
                arcpy.AddWarning("Sorry, that feature class already exists")
            else:
                arcpy.Clip_analysis(fc, "pochoirlyr", out_poly)

         # delete the layers so you can use the names again
         arcpy.Delete_management("lyr")
         arcpy.Delete_management("pochoirlyr")

    del porow
    del pocursor

Instead of making the layer then selecting by attributes against it I have modified it so that it creates the layer "pochoirlyr" using the SQL as the whereclause. On each iteration the "lyr" and "pochoirlyr" are created and delteted which avoids confusion about what's in the layer/not in the layer, what's selected and what isn't.. a clean start every time.

  • the running of your code generates the following error : "module object has no attribute 'GetParamterAsText'". Hence, I really don't understand why you want modify the if part of the code...This part has been tested and tested and it works fine and if I don't do a mistake, has no effect on parameters...My problem is only the setting of parameters. I just can't understand why GetParameterAsText don't make my parameters variables. – Samy-DT Dec 12 '14 at 9:54
  • I use sys.argv[n], like I said in my answer. That portion of the code is a direct copy of your own. – Michael Stimson Dec 14 '14 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.