Trouble writing floating point numbers to field of new shapefile

379
0
05-11-2011 12:25 PM
MarkWilson3
New Contributor
Hi all,

I am trying to:
- read data from a .dbf file
- caculate some statistics
- then read in another .dbf file with some point coordinates
- joint the two together
- then create a shapefile
- and finally create points in the shapefile with the statistical data in them

Everything is peachy except that things fail when i try to write a floating point value to a field in the newly created shapefile.  Records that contain zeros write to the file just fine.  I have tried everything i can think of to make it work including defining the field as Double, Float as well as trying to format the statistics numbers before writing them with the Decimal module that comes with Python. 

Below is my code.  Also included is some sample output that shows one record that worked and one that did not.  It is interesting that no output comes out of the GetMessages when the write fails.

Note: the code seems to fail at the following command:

          feature.setValue(tot_consumption, dmd_dict[key][1])


# create fields
    street_number = "Strt_Num"
    street_name = "Strt_Name"
    city = "City"
    APN = "APNNODASH"
    tot_days = "Total_days"
    tot_consumption = "Total_cons"
    ADD_fld = "ADD_gpm"
    MDD_fld = "MDD_gpm"
    try:
        arcpy.AddField_management(FC, street_number, "Text", "", "", 32)
        arcpy.AddField_management(FC, street_name, "Text", "", "", 32)
        arcpy.AddField_management(FC, city, "Text", "", "", 32)
        arcpy.AddField_management(FC, APN, "Text", "", "", 32)
        arcpy.AddField_management(FC, tot_days, "Long", 8,)
        arcpy.AddField_management(FC, tot_consumption, "Float", 12, 6)
        arcpy.AddField_management(FC, ADD_fld, "Float", 12, 6)
        arcpy.AddField_management(FC, MDD_fld,"Float", 12, 6)
    except:
        print arcpy.GetMessages()
   
    # create points
    linked_count = 0
    not_linked_count = 0
    cursor = arcpy.InsertCursor(FC)
    for key in dmd_dict:
        newkey = key[1], key[2], key[3]
        print "NEWKEY  ", newkey
       
        try:
            link_values = links[newkey]  # looks up the values in the link table for APN, X, Y
            linked_count += 1
            print "LINKS  ", link_values
            vertex = arcpy.CreateObject("Point")
            print "chkpt a"
            print "X = ", link_values[1]
            vertex.X = link_values[1]
            print "chkpt b"
            print "Y = ", link_values[2]
            vertex.Y = link_values[2]
            print "chkpt c"
            feature = cursor.newRow()
            print "chkpt d"
            feature.shape = vertex
            print "chkpt e"
            feature.setValue(street_number, key[1])
            print "chkpt f"
            feature.setValue(street_name, key[2])
            print "chkpt g"
            feature.setValue(city, key[3])
            print "chkpt h"
            feature.setValue(APN, link_values[0])
            print "chkpt i"
            print dmd_dict[key][0]
            feature.setValue(tot_days, int(dmd_dict[key][0]))
            print "chkpt j"
            print dmd_dict[key][1]
            feature.setValue(tot_consumption, dmd_dict[key][1])
            print "chkpt k"
            print dmd_dict[key][2]
            feature.setValue(ADD_fld, dmd_dict[key][2])
            print "chkpt l"
            print dmd_dict[key][3]
            feature.setValue(MDD_fld, dmd_dict[key][3])
            print "chkpt m"
            cursor.insertRow(feature)
        except:
            not_linked_count += 1
            print "NOT LINKED", key
            for item in key:
                f.write(str(item) + "\t")
            length = len(dmd_dict[key])
            for index in range(length):
                f.write(str(dmd_dict[key][index]))
                if index != length - 1:
                    f.write("\t")
            f.write("\n")
            print arcpy.GetMessages(2)



NEWKEY   ('1470', 'PROSPECT AVE', 'CAPITOLA')
LINKS   ('03404613', 6136687.2654799996, 1815503.14329)
chkpt a
X =  6136687.26548
chkpt b
Y =  1815503.14329
chkpt c
chkpt d
chkpt e
chkpt f
chkpt g
chkpt h
chkpt i
1094
chkpt j
701.00000
NOT LINKED ('10990.00000', '1470', 'PROSPECT AVE', 'CAPITOLA')

NEWKEY   ('1525', 'PROSPECT AVE', 'CAPITOLA')
LINKS   ('03404502', 6136595.11974, 1815685.3788000001)
chkpt a
X =  6136595.11974
chkpt b
Y =  1815685.3788
chkpt c
chkpt d
chkpt e
chkpt f
chkpt g
chkpt h
chkpt i
0
chkpt j
0
chkpt k
0.0
chkpt l
0.0
chkpt m
Tags (2)
0 Kudos
0 Replies