struct.error What causes this?

1,031 views
Skip to first unread message

jlha...@hotmail.com

unread,
Sep 17, 2013, 1:54:53 PM9/17/13
to geospati...@googlegroups.com
I am thouroughly confused by this one so if someone has some  insite that could put me back on track.
 

The error message
 headerLength, recordLength)
struct.error: ushort format requires 0 <= number <= USHRT_MAX


The code


def open_column(): #Works  with open lat lon column button in def edit
  
    latlong = askopenfilename()
    #print(latlong)
    lat_long=[]
    in_lat_long_file= open(latlong) 
    lat_long= in_lat_long_file.read()
    in_lat_long_file.close()
 
    slat1=-1
    cc=0
    dd=1
    shapefile2=[]
    outside=0
    myList = ''.join(map(str, lat_long))
    newstr1 = myList.replace("'", "")
    myList2=newstr1.strip("").split(',') 
    mystr1=''.join(myList2)
    myList4 = mystr1.split("\n")
    myList5 = [tuple(myList4[ip:ip+2]) for ip in range(0, len(myList4), 2)]
    myList5 = [(myList4[ip:ip+1]) for ip in range(0, len(myList4), 1)]
    myString = str(myList5)
     
    zot=0
    zob=1
    nop=0
    loopt=len(myList5)
    wg = shapefile.Writer(shapefile.POINT)
    print (loopt, "loopt")
    x=1
    
    for hk in myList5:
        if zob+2<=loopt:
             
            nope=myList5[zot]
            itemw = myList5[zot]
            itemw2 = str(itemw)
            itemw3 = itemw2.replace("'", "")
            itemw4 = itemw3.replace("[", "")
            itemw5 = itemw4.replace("]", "")
            itemw6 = float(itemw5)
            itemh = myList5[zob]
            itemh2 = str (itemh)
            itemh3 = itemh2.replace("'", "")
            itemh4 = itemh3.replace("[", "")
            itemh5 = itemh4.replace("]", "")
            itemh6 = float(itemh5)
              
            print(itemw6,itemh6)
            nop2=float(nop)
               
            boo= (nop2,nop2)
                
            if zot < loopt: 
      
                wg.point(itemw6,itemh6)
                wg.field('FIRST_FLD')
                wg.records.append(boo)
            wg.save("point")
            zot=zot+2
            zob=zob+2
            nop=nop+ 1

Joel Lawhead

unread,
Sep 17, 2013, 3:10:14 PM9/17/13
to geospati...@googlegroups.com
I think you need to move the following line:
wg.field('FIRST_FLD')

outside of this loop:
for hk in myList5

You only need to create fields once per shapefile and it appears you a creating the same field over and over for the same shapefile.

My suggestion would be to try and move the field command directly after the Writer initiation like this:
wg = shapefile.Writer(shapefile.POINT)
wg.field('FIRST_FLD')

My guess is that if you ran this code on a small loop during testing it might work.  But then if you turned this function loose on a large data set, the unintended field creation eventually exceeded the maximum allowable byte size for the dbf spec to store field information which is what the error suggests.  That fact would make it really confusing to track down.

Let me know if it works.

- Joel
Message has been deleted

jlha...@hotmail.com

unread,
Sep 17, 2013, 6:25:05 PM9/17/13
to geospati...@googlegroups.com

 Thanks, that worked.
Reply all
Reply to author
Forward
0 new messages