0

I have created a model in ArcGIS ModelBuilder and in a point of the process I insert a python tool (New Field) to create new field in a shapefile.

After that I want to use this new field in other tool in the same model. But I cannot use it because it is not created, it is not selectable in the available fields of the shapefile.

This image shows a part of the model where I create the new field and after I want to use this new field as a input in Pivot Table tool.

enter image description here

The New Field toll python code is:

def getParameterInfo(self):
    """Define parameter definitions"""

# First parameter

    param0 = arcpy.Parameter(
        displayName="Shape entrada",
        name="in_features",
        datatype="GPTableView",
            parameterType="Required",
        direction="Input")


    parameters = [param0]
    return parameters

def isLicensed(self):
    """Set whether tool is licensed to execute."""
    return True
def updateParameters(self, parameters):return
def updateMessages(self, parameters):return

def execute(self, parameters, messages):
    """The source code of the tool."""

#-----------------------------
camp= "GROUP_"
val= "Pix_Value"

    a= parameters[0].valueAsText

arcpy.AddField_management(a, camp, "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(a, val, "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#-----------------------------


with arcpy.da.UpdateCursor(a, ["CODE2012",camp, val]) as cursor:
    for row in cursor:
        a= int(row[0])
        if a < 12100:
                    row[1] = "Residential"
            row[2]= 5
            elif a==12100 or a==12300 or a==12400:
                    row[1] = "Industrial"
            row[2]= 4
            elif a>12100 and a<12300:
                    row[1] = "Highway"
            row[2]= 6
            elif a == 21000:
                row[1] = "AnnualCrop"
            row[2]= 1
        elif a == 22000 or a== 25000:
                row[1] = "PermanentCrop"
            row[2]=2
        elif a == 23000:
                row[1] = "Pasture"
            row[2]=3
        elif a == 31000:
                row[1] = "Forest"
            row[2]=8
        elif a == 32000:
                row[1] = "HerbaceousVegetation"
            row[2]=9
        elif a == 50000:
                row[1] = "Water"
            row[2]=7
        else:
            row[1]= "No_clasified"
            row[2]=0


        cursor.updateRow(row)
    del a
    del cursor`
  • 1
    Why can you use Add Field tool instead of python? Then this will be just another step in the model. – BERA Sep 20 '18 at 12:57
  • Can you post the content of New Field script? – fatih_dur Sep 20 '18 at 13:36
  • @BERA, I use python because I also fill the new field. – MikV89 Sep 24 '18 at 12:22
  • 1
    @MikV89, there's also a Calculate Field tool available in Model Builder. See for example: How to Calculate fields for attribute table using ModelBuilder? – Andy Sep 24 '18 at 12:58
2

I seldom use ModelBuilder but one workaround I did use in similar case was to use dummy data to run the model up to the point where the field is added. This way you get the output from the add field tool (with the new field) to work with for the next steep.

When the model is complete you could change you dummy data to become a model parameter.

I only did that with tool from toolbox not with custom script so I don't know if it will work in your case

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.