Error when using CalculateField_Management

1122
3
04-07-2011 03:25 AM
FernandoMons
New Contributor
Hello,

I am trying to do an update of two fields of a Feature Class (Point), based on a Spatial Join with another Feature Class (Polygon). The spatial type IS_WITHIN (Point is within Polygon)

The steps I'm trying to do are the following

1. Create the Spatial Join Feature class

gp.SpatialJoin_analysis(PORTALES, SECCIONES, PORTALES_SPATIAL, "JOIN_ONE_TO_ONE", "KEEP_ALL", "NUMERO 'NUMERO' true true false 3 Text 0 0 ...........)


2. Transform PORTALES_SPATIAL (Spatial_Join) and PORTALES (Points)  to table_view, feature layer

 
gp.MakeTableView_management(PORTALES_SPATIAL, SPATIAL_VIEW, "", "", "Join_Count Join_Count VISIBLE NONE;NUMERO NUMERO VISIBLE NONE;....)


  gp.MakeFeatureLayer_management(PORTALES, F_PORTALES, "", wsTEMP, "NUMERO NUMERO VISIBLE NONE ..............)    




3. Join F_PORTALES  (Point Feature Classes) and SPATIAL_VIEW (Spatial Join Table_View)

gp.AddJoin_management(F_PORTALES, "OBJECTID", SPATIAL_VIEW, "OBJECTID", "KEEP_ALL")
    ExistsJoin = 1


4. Finally Update field of F_PORTALES with value of SPATIAL_VIEW


   gp.CalculateField_management(PORTALES, "DIS", "[GISTOPOGRAFIA.Portales_SpatialJoin.CODDC]","VB")
    gp.CalculateField_management(PORTALES, "SEC", "[GISTOPOGRAFIA.Portales_SpatialJoin_View.CODSC]","VB")


Ok, in this point I'm receiving error 999999 when using VB syntaxis and error ERROR 000539: Invalid field 'GISTOPOGRAFIA.Portales_SpatialJoin.CODDC', when using PYTHON syntaxis.

I have searched the forum for responses but all Found, doesnt works for me. First I try to change the syntax from VB to Python, second I listed the field to put the exact field name in the CalculateField, but nothing works.

Also I have the doubt, if with this code, I am updating the original Featureclass or if I am updating an copy of them, because I am being forced to use MakeFeauture, so the AddJoin Work.

We also want to know if exists a more simplified way to achieve this goal.

Can you give me some help? Thanks.

P.D.: Here is all my code:

 

# ---------------------------------------------------------------------------
# CalculoSeccionesMB2.py
# Created on: vie mar 25 2011 10:56:03 
#   (generated by ArcGIS/ModelBuilder)
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, time

# Importar modulos ArcGIS
import arcgisscripting

# Importar modulos propios
import ASTools
reload(ASTools)

try:

    # Leemos parametros de Configuracion
    Tools = ASTools.DameParametro("GENERAL","ToolsPath")
    ws = ASTools.DameParametro("GENERAL","Workspace")
    

    ASTools.EscribeLog("Debug","Hora Inicio: " + time.ctime())
    

    # Create the Geoprocessor object
    gp = arcgisscripting.create(9.3)
    gp.SetProduct("ArcEditor")
    #gp.workspace = ws

    # Load required toolboxes...
    gp.AddToolbox(Tools + "Data Management Tools.tbx")
    gp.AddToolbox(Tools + "Analysis Tools.tbx")


    # FeatureClasses que se van a necesitar para el proceso
    PORTALES = ws +  "GISTOPOGRAFIA.Cartografia\\GISTOPOGRAFIA.Portales"
    SECCIONES = ws + "GISTOPOGRAFIA.Cartografia\\GISTOPOGRAFIA.Secciones_Censales"
    PORTALES_SPATIAL = ws + "GISTOPOGRAFIA.Portales_SpatialJoin"
    SPATIAL_VIEW = ws + "GISTOPOGRAFIA.Portales_SpatialJoin_View"
    wsTEMP = ws +  "GISTOPOGRAFIA.Temp"
    F_PORTALES = "Gistopografia.F_Portales"

    ExistsSpatial = 0
    ExistsJoin = 0


    # Borramos los objetos temporales
    #gp.Delete_management(PORTALES_SPATIAL, "")

    # Process: Spatial Join...
    gp.SpatialJoin_analysis(PORTALES, SECCIONES, PORTALES_SPATIAL, "JOIN_ONE_TO_ONE", "KEEP_ALL", "NUMERO 'NUMERO' true true false 3 Text 0 0 ,First,#," + PORTALES + ",NUMERO,-1,-1;LETRA 'LETRA' true true false 12 Text 0 0 ,First,#," + PORTALES + ",LETRA,-1,-1;INCIDENCIA 'INCIDENCIA' true true false 100 Text 0 0 ,First,#," + PORTALES + ",INCIDENCIA,-1,-1;TEXT 'TEXT' true true false 10 Text 0 0 ,First,#,"  + PORTALES +  ",TEXT,-1,-1;NUM 'NUM' true true false 10 Text 0 0 ,First,#," + PORTALES + ",NUM,-1,-1;BIS 'BIS' true true false 10 Text 0 0 ,First,#," + PORTALES + ",BIS,-1,-1;BLOQ 'BLOQ' true true false 10 Text 0 0 ,First,#," + PORTALES + ",BLOQ,-1,-1;PORTAL 'PORTAL' true true false 10 Text 0 0 ,First,#," + PORTALES + ",PORTAL,-1,-1;DIS 'DIS' true true false 10 Text 0 0 ,First,#," + PORTALES + ",DIS,-1,-1;SEC 'SEC' true true false 10 Text 0 0 ,First,#," + PORTALES + ",SEC,-1,-1;CODPOSTAL 'CODPOSTAL' true true false 5 Text 0 0 ,First,#," + PORTALES + ",CODPOSTAL,-1,-1;CUN 'CUN' true true false 10 Text 0 0 ,First,#," + PORTALES + ",CUN,-1,-1;CODIGOTRAM 'CODIGOTRAM' true true false 4 Long 0 9 ,First,#," + PORTALES + ",CODIGOTRAM,-1,-1;CROQUIS 'CROQUIS' true true false 255 Text 0 0 ,First,#," + PORTALES + ",CROQUIS,-1,-1;CODMUNI 'CODMUNI' true true false 5 Text 0 0 ,First,#," + SECCIONES + ",CODMUNI,-1,-1;CODDC 'CODDC' true true false 2 Text 0 0 ,First,#," + SECCIONES + ",CODDC,-1,-1;CODSC 'CODSC' true true false 3 Text 0 0 ,First,#," + SECCIONES + ",CODSC,-1,-1;CODIGO 'CODIGO' true true false 10 Text 0 0 ,First,#," + SECCIONES + ",CODIGO,-1,-1;TOTAL 'TOTAL' true true false 8 Double 8 38 ,First,#," + SECCIONES + ",TOTAL,-1,-1;CODDCSC 'CODDCSC' true true false 5 Text 0 0 ,First,#," + SECCIONES + ",CODDCSC,-1,-1;AREA 'AREA' false true true 8 Double 8 38 ,First,#," + SECCIONES + ",GISTOPOGRAFIA.Secciones_Censales.AREA,-1,-1;LEN 'LEN' false true true 8 Double 8 38 ,First,#," + SECCIONES + ",GISTOPOGRAFIA.Secciones_Censales.LEN,-1,-1;SHAPE_AREA 'SHAPE_AREA' false false true 0 Double 0 0 ,First,#," + SECCIONES + ",SHAPE.AREA,-1,-1;SHAPE_LEN 'SHAPE_LEN' false false true 0 Double 0 0 ,First,#," + SECCIONES + ",SHAPE.LEN,-1,-1", "IS_WITHIN", "0 Meters", "")
    ExistsSpatial = 1

    ASTools.EscribeLog("Debug","1. Relacion espacial Creada")


    # Process: Make Table View...
    gp.MakeTableView_management(PORTALES_SPATIAL, SPATIAL_VIEW, "", "", "Join_Count Join_Count VISIBLE NONE;NUMERO NUMERO VISIBLE NONE;LETRA LETRA VISIBLE NONE;INCIDENCIA INCIDENCIA VISIBLE NONE;TEXT TEXT VISIBLE NONE;NUM NUM VISIBLE NONE;BIS BIS VISIBLE NONE;BLOQ BLOQ VISIBLE NONE;PORTAL PORTAL VISIBLE NONE;DIS DIS VISIBLE NONE;SEC SEC VISIBLE NONE;CODPOSTAL CODPOSTAL VISIBLE NONE;CUN CUN VISIBLE NONE;CODIGOTRAM CODIGOTRAM VISIBLE NONE;CROQUIS CROQUIS VISIBLE NONE;CODMUNI CODMUNI VISIBLE NONE;CODDC CODDC VISIBLE NONE;CODSC CODSC VISIBLE NONE;CODIGO CODIGO VISIBLE NONE;TOTAL TOTAL VISIBLE NONE;CODDCSC CODDCSC VISIBLE NONE;AREA AREA VISIBLE NONE;LEN LEN VISIBLE NONE;SHAPE_AREA SHAPE_AREA VISIBLE NONE;SHAPE_LEN SHAPE_LEN VISIBLE NONE")
    ASTools.EscribeLog("Debug","2. Vista Spatial Creada")

    gp.MakeFeatureLayer_management(PORTALES, F_PORTALES, "", wsTEMP, "NUMERO NUMERO VISIBLE NONE;LETRA LETRA VISIBLE NONE;INCIDENCIA INCIDENCIA VISIBLE NONE;TEXT TEXT VISIBLE NONE;NUM NUM VISIBLE NONE;BIS BIS VISIBLE NONE;BLOQ BLOQ VISIBLE NONE;PORTAL PORTAL VISIBLE NONE;DIS DIS VISIBLE NONE;SEC SEC VISIBLE NONE;CODPOSTAL CODPOSTAL VISIBLE NONE;CUN CUN VISIBLE NONE;CODIGOTRAM CODIGOTRAM VISIBLE NONE;CROQUIS CROQUIS VISIBLE NONE")    
    ASTools.EscribeLog("Debug","2.1 Vista Portales Creada")


    # Process: Add Join...
    gp.AddJoin_management(F_PORTALES, "OBJECTID", SPATIAL_VIEW, "OBJECTID", "KEEP_ALL")
    ExistsJoin = 1
    ASTools.EscribeLog("Debug","3. Relacion BD Creada")

    fields = gp.listfields(F_PORTALES)
    print [ (f.name + " " + f.type + " " + str(f.length))  for f in fields]

    # Process: Calculate Field..                    GISTOPOGRAFIA.Portales_SpatialJoin.CODDC
    gp.CalculateField_management(PORTALES, "DIS", "!GISTOPOGRAFIA.Portales_SpatialJoin.CODDC!","Python")
    gp.CalculateField_management(PORTALES, "SEC", "[GISTOPOGRAFIA.Portales_SpatialJoin_View.CODSC]","VB")
    
    #gp.CalculateField_management(PORTALES, "DIS", "3")
    #gp.CalculateField_management(PORTALES, "SEC", "4")
    ASTools.EscribeLog("Debug","4. Campos Actualizados")
    

    # Borramos los objetos temporales
    #gp.RemoveJoin_management(SPATIAL_VIEW, PORTALES)
    gp.Delete_management(PORTALES_SPATIAL, "")
    del gp
    ASTools.EscribeLog("Debug","5. Proceso finalizado Correctamente")

    ASTools.EscribeLog("Debug","Hora Fin:" + time.ctime())

except:
    ASTools.EscribeLog("Critical","ERROR EN EL PROCESO.")
    gp.AddMessage(gp.GetMessages(2))
    ASTools.EscribeLog("Critical",gp.GetMessages(2))
    if (ExistsJoin == 1):
        gp.RemoveJoin_management(SPATIAL_VIEW,PORTALES)
        gp.Delete_management(PORTALES_SPATIAL, "")            
    else:
        if (ExistsSpatial == 1):
            gp.Delete_management(PORTALES_SPATIAL, "")
    del gp
    ASTools.EscribeLog("Debug","Hora Fin:" + time.ctime())


Tags (2)
0 Kudos
3 Replies
ZoeZaloudek
Occasional Contributor
Try using your feature layer (F_PORTALES) as the input for the CalculateField tool, instead of the original feature class (PORTALES)?
0 Kudos
FernandoMons
New Contributor
Hello,

Efectively the Calculatefield_management code was wrong. I was using PORTALES when te correct is F_PORTALES.  I supose the amount of tries i did, I changed this and not rollbacked it, before post to the forum.

But  using the correct code, the same error raises:

>>> Unhandled exception while debugging...
Traceback (most recent call last):
  File "\\SAytofich\carpetasgrupos\...\Python Scripts\CalculoSecciones.py", line 76, in <module>
    gp.CalculateField_management(F_PORTALES, "DIS", "!GISTOPOGRAFIA.Portales_SpatialJoin.CODDC!","Python")
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (CalculateField).
0 Kudos
DanaDiotte1
New Contributor II
Hello fmonsn,

I was getting the same error message with the CalculateField tool and with the help of a co-worker I found a solution. This may or may not help because i'm not too sure if your fields are area or length based. It turns out it's best to use Describe on feature class properties in the GDB before trying to calculate with an expression to convert the value to a different unit of measure. Here is some code inside a function that helped my script stop throwing ERROR 000539.

def CalcAreaHa(dataIn,haField):
    if not arcpy.ListFields(dataIn, haField):
        arcpy.AddField_management(dataIn, haField, "DOUBLE","","4") 
    desc = arcpy.Describe(dataIn)
    ftype = desc.ShapeType
    if ftype == 'Polygon':
        areaItem = desc.areaFieldName       
    if areaItem == 'GEOMETRY_Area':
        arcpy.CalculateField_management(dataIn,haField,"!geometry.area@HECTARES!", "PYTHON_9.3")
    else:
        arcpy.CalculateField_management(dataIn,haField,"!shape.area@HECTARES!", "PYTHON_9.3")
0 Kudos