I encounter a strange problem. I have a simple shapefile to which I added some fields and then populated them.

Now I want to access those fields from a script to run some buffer operations. I want to do this per line segment so I execute a Select_analysis. The tool runs without throwing an exception but not all fields are present in the newly generated shapefile, namely only those from before I added the fields.

For testing purposes I tried to access the fields also with a SearchCursor-method, which returns none. Strange enough when the shape is used within ArcGIS directly everything works fine (Select_analysis, calculating on the newly added fields,...).

Any idea what is going on? Every hint is very much appreciated!

EDIT: I'm using ArcGIS 9.3.1. on WindowsXP. I believe posting the script isn't of much help as the problem seems to be with the ESRI software. But I do it anyway, maybe someone does spot something strange.

 # ---------------------------------------------------------------------------
# zonering_leggerV2.py
# docu still to come...
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os
from GeoSql import ArcSql
import GIS_tools as gt

# Create the Geoprocessor object
# Geoprocessor-object (ArcMap 9.1 and older)
    import win32com.client
    gp = win32com.client.Dispatch('esriGeoprocessing.GpDispatch.1')

# Geoprocessor-object (ArcMap 9.2 and newer)
    import arcgisscripting
    gp = arcgisscripting.create(9.3)

    # global gp stettings
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
    gp.OverWriteOutput = 1
    msg = gp.getMessage(2)

    msg = gp.getmessages()
    print msg

def buff(source, dest, field_name_zone, side, type_end):

    # Process: Buffer (2)...
        gp.Buffer_analysis(source, dest, field_name_zone, side, type_end, "NONE", "")
        msg = gp.getmessage(2)
        print msg, '\n'
        msg = gp.getmessages()
        print msg, '\n'

def merge(zone_l, zone_r, out_data):
    # Process: Merge...
    merge_str = "%s;%s" % (zone_l, zone_r)
        gp.Merge_management(merge_str, out_data)
        msg = gp.getmessage(s)
        print msg, '\n'
        msg = gp.getmessages()
        print msg, '\n'

# --> programme parameters

type_end = 'FLAT'
field_name_zone = {'KR': 'KERN_R',
                  'BL': 'BINNEN_L',
                  'f_name_zone_new': 'code_zone',
                  'KERN': "_KZ",
                  'B_BINNENKANT': "_BBI",
                  'B_BUITENKANT': "_BBU"}

#--> file declarations and data gathering

dir_base = "D:\\opdrachten\\legger\\dijkvakken\\"    
dir_gp_tmp = dir_base + "gp_tmp\\"

vakken_shp = "%sbijlmerring_042012.shp" % dir_base

column = "PKM_NAME"
#vakken = gt.get_data_from_table(vakken_shp, fields = [column])
vakken = ["A2035-001"]
invalid =('-')

for vak_name in vakken:
    if invalid in vak_name:
        vak_name_mutat = vak_name.replace(invalid, '_')

    selected_vak = "%s%s.shp" % (dir_gp_tmp, vak_name_mutat)

    # Local variables...
    buffer_KL = "%s%s_%s.shp" % (dir_gp_tmp, vak_name_mutat, field_name_zone['KL'])
    buffer_KR = "%s%s_%s.shp" % (dir_gp_tmp, vak_name_mutat, field_name_zone['KR'])
    buffer_BR = "%s%s_%s.shp" % (dir_gp_tmp, vak_name_mutat, field_name_zone['BR'])
    buffer_BL = "%s%s_%s.shp" % (dir_gp_tmp, vak_name_mutat, field_name_zone['BL'])
    #buffer_shapes = [buffer_KL, buffer_KR, buffer_BR, buffer_KL]

    dat_zones ={'kern_l': (buffer_KL, field_name_zone['KL'], "LEFT"),
               'kern_r': (buffer_KR, field_name_zone['KR'], "RIGHT"),
               'binnen_l': (buffer_BL, field_name_zone['BL'], "LEFT"),
               'binnen_r': (buffer_BR, field_name_zone['BR'], "RIGHT")}

    merge_kern = "%s%s_mergeKern.shp" % (dir_gp_tmp, vak_name_mutat)
    merge_binnen = "%s%s_mergeBinnen.shp" % (dir_gp_tmp, vak_name_mutat)

    dissolve_kern = "%s%s_dissolveKern.shp" % (dir_gp_tmp, vak_name_mutat)

    erase_out = "%s%s_erase.shp" % (dir_gp_tmp, vak_name_mutat)

    result_tmp = "%s%s_zones.shp" % (dir_gp_tmp, vak_name_mutat)

    sql = ArcSql(column, [vak_name]).statement()
    print sql

    # Process: Select...
        gp.Select_analysis(vakken_shp, selected_vak, sql)
        msg =gp.getmessage(2)
        print msg, '\n'
        msg =gp.getmessages()
        print msg

    for i in dat_zones.itervalues():
        buff(selected_vak, i[0], i[1], i[2], type_end)

    merge(dat_zones['kern_l'][0], dat_zones['kern_r'][0], merge_kern)

        gp.Dissolve_management (merge_kern, dissolve_kern, column)
        msg =gp.getmessage(2)
        print msg, '\n'
        msg = gp.getmessages()
        print msg, '\n'

    gt.add_field(dissolve_kern, field_name_zone['f_name_zone_new'], 'TEXT', length_txt='10')
    gt.update_fields(dissolve_kern, sql, [field_name_zone['f_name_zone_new']], [field_name_zone['KERN']])

    for f in [dat_zones['binnen_l'][0], dat_zones['binnen_r'][0]]:
        gt.add_field(f, field_name_zone['f_name_zone_new'], 'TEXT', length_txt='10')
        if '_L' in f: 
            gt.update_fields(f, sql, [field_name_zone['f_name_zone_new']], [field_name_zone['B_BINNENKANT']])
        elif '_R' in f:
            gt.update_fields(f, sql, [field_name_zone['f_name_zone_new']], [field_name_zone['B_BUITENKANT']])
            'error at update field binnenzone...!\n'

    merge(dat_zones['binnen_l'][0], dat_zones['binnen_r'][0], merge_binnen) 

        gp.erase(merge_binnen, dissolve_kern, erase_out)
        msg = gp.getmessage(2)
        print msg, '\n'
        msg = gp.getmessages()
        print msg, '\n'

    merge(erase_out, dissolve_kern, result_tmp) 
  • If you were to edit your answer and include the code from the script you wrote, that may help diagnose the problem. Also including details about computer platform, ArcGIS version, etc. The more detail you can provide in your question, the better your chance of someone giving you a good solution. FAQ - How Do I Ask Questions Here? – Get Spatial May 9 '12 at 8:09

Could you double check the shape file in arcmap, give "open attribute table". Are you seeing all the columns you have added and populated. Because, i have disabled the visibility of few columns through properties -> Fields tab. When I manually tried to create buffer, in the "field" selection, the invisible fields are not available..

So, i guess your problem may be because of this..

  • No, that is not the source of the problem. Too bad, but thanks for the input anyway. – LarsVegas May 9 '12 at 16:06

I am getting an exception for this line "ERROR 000313: The length of Field Name must not be larger than 10"

gt.add_field(f, field_name_zone['f_name_zone_new'], 'TEXT', length_txt='10')

I do not have spatial analyst so I used gp.add_field("Cal.shp", field_name_zone['f_name_zone_new'], 'TEXT', length_txt='10')

  • It's really nice you wanna help me on this. But you can't just use my code like this because its based on some modules of my own (see the import GIS_tools as gt line for example. This has nothing to do with spatial analyst which you don't need to add a field anyway. As you can see the value associated with the key ` field_name_zone['f_name_zone_new']` is 'code_zone' so the length is fine. length_txt='10' refers to the number of characters the field may hold. – LarsVegas May 10 '12 at 14:50
  • one more try.. the add_field is creating exception, but that exception is not captured.. so add print "exception" print gp.GetMessages(2) – vadivelan May 10 '12 at 15:13
  • @ vadivelan. You really don't get it, do you? First of all: the problem is with the SELECT statement! Secondly, as I said earlier, I use a module of my own called GIS_tools. So the exception handling is not visible in the script I posted. – LarsVegas May 11 '12 at 8:17

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.