Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

Now I wanna access those fields from a script to run some buffer operations. I wanna 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 shape file, 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)
try:
    import win32com.client
    gp = win32com.client.Dispatch('esriGeoprocessing.GpDispatch.1')

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


try:
    # 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
    gp.CheckOutExtension("Spatial")
    msg = gp.getMessage(2)
    gp.addmessage(msg)

except:
    msg = gp.getmessages()
    print msg
    gp.addmessage(msg)



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

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



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



#------------------------------------------------------------------------------------------------------------
# --> programme parameters

type_end = 'FLAT'
field_name_zone = {'KR': 'KERN_R',
                  'KL':'KERN_L',
                  'BR':'BINNEN_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...
    try:
        gp.Select_analysis(vakken_shp, selected_vak, sql)
        msg =gp.getmessage(2)
        print msg, '\n'
    except:
        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)

    try:
        gp.Dissolve_management (merge_kern, dissolve_kern, column)
        msg =gp.getmessage(2)
        print msg, '\n'
    except:
        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']])
        else:
            'error at update field binnenzone...!\n'
            sys.exit()


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

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

    merge(erase_out, dissolve_kern, result_tmp) 
share|improve this question
    
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
add comment

2 Answers

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..

share|improve this answer
    
No, that is not the source of the problem. Too bad, but thanks for the input anyway. –  LarsVegas May 9 '12 at 16:06
add comment

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')

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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