0

Im new to Python and I need to transform some .iff Data to .shp, .shx, and .dbf data. I use the Code below in combination with the pyShape package from Joel Lawhead. And I get the Error:"... line 157, in if not w.shx.closed : w.shx.close() AttributeError: 'NoneType' object has no attribute 'closed'". This seems like a simple Error which might not be to complicated to solve but for me its hard. Maybe someone can help me.

Best regards Vestus

###Code####
from __future__ import print_function
import shapefile
import struct, sys

sys.path.insert(1,r"C:\Python27\ArcGIS10.6\Lib\pyshp-1.2.3")
#sys.path.insert(1,r"C:\Python2_7\Lib\pyshp-1.2.3")
#import shapefile

# Constants for shape types
#NULL = 0
#POINT = 1
#POLYLINE = 3
#POLYGON = 5
#MULTIPOINT = 8
#POINTZ = 11
#POLYLINEZ = 13
#POLYGONZ = 15
#MULTIPOINTZ = 18
#POINTM = 21
#POLYLINEM = 23
#POLYGONM = 25
#MULTIPOINTM = 28
#MULTIPATCH = 31

In = "VOR_1"

IFFin = In + ".IFF"
shpfile_out = In

try:
    print ("In: IFF-file:   " + IFFin)
    print ("Out: Shapefile: " + shpfile_out + ".shp")


# Calculate MaxTime
    
    prev_part = 0
    MaxTime = []
    MaxTime.append(1)
    MaxTime[0] = 0
    i = 0
    j = 0
    IFFfile = open(IFFin, "r")
    for line in IFFfile:
        i = i + 1
        if i == 1:
            t = line.split(" ")
            NrAtt = int(t[0])
            if NrAtt != 9:
                print ("Number of attributes in IFF-file <> 9")
                print ("Programm aborted...")
                break
        elif i > 10:
            while "  " in line:
                line = line.replace ("  "," ")
            t = line.strip(" ").split(" ")
            part = int(t[0])
            if prev_part != part:
                MaxTime.append(1)
                j = j + 1
            prev_part = part
            MaxTime[j] = float(t[5])
            
    IFFfile.close()



    Shape_Type = 5
    w = shapefile.Writer(shapefile.POLYLINE)
    w.autoBalance = 1
    w.field('Particle','N',6)
    w.field('ILAY_Start','N',6)
    w.field('Z_start','F',10,8)
    w.field('TIME_start','F',10,8)
    w.field('Veloc_start','F',10,8)
    w.field('IROW_start','N',6)
    w.field('ICOL_start','N',6)
    w.field('RTIME_start','F',10,8)
    w.field('ILAY_end','N',6)
    w.field('Z_end','F',10,8)
    w.field('TIME_end','F',10,8)
    w.field('Veloc_end','F',10,8)
    w.field('IROW_end','N',6)
    w.field('ICOL_end','N',6)
    w.field('RTIME_end','F',10,8)
    w.field('Z_Mean','F',10,8)
    w.field('TIME_Mean','F',10,8)
    w.field('Veloc_Mean','F',10,8)
    w.field('RTIME_Mean','F',10,8)
    
    
    i = 0
    j = 0
    prev_part = 0
    ilay_start = 0
    xcrd_start = 0
    ycrd_start = 0
    zcrd_start = 0
    time_start = 0
    veloc_start = 0
    irow_start = 0
    icol_start = 0       
    rtime_start = 0

    IFFfile = open(IFFin, "r")
    for line in IFFfile:
        i = i + 1
        if i == 1:
            t = line.split(" ")
            NrAtt = int(t[0])
            if NrAtt != 9:
                print ("Number of attributes in IFF-file <> 9")
                print ("Programm aborted...")
                break
        elif i > 10:
            while "  " in line:
                line = line.replace ("  "," ")
            t = line.strip(" ").split(" ")
            part = int(t[0])
            ilay_end = int(t[1])
            xcrd_end = float(t[2])
            ycrd_end = float(t[3])
            zcrd_end = float(t[4])
            time_end = float(t[5])
            veloc_end = float(t[6])
            irow_end = int(t[7])
            icol_end = int(t[8])            

            if prev_part == part:
                w.line(parts=[[[xcrd_start,ycrd_start],[xcrd_end,ycrd_end]]])
                w.record(part,
                         ilay_start, zcrd_start, time_start, veloc_start, irow_start, icol_start, (MaxTime[j] - time_start),
                         ilay_end, zcrd_end, time_end, veloc_end, irow_end, icol_end, (MaxTime[j] - time_end),
                         ((zcrd_start + zcrd_end) / 2), ((time_start + time_end) / 2),
                         ((veloc_start + veloc_end) / 2),
                         (((MaxTime[j] - time_start) + (MaxTime[j] - time_end)) / 2))
            else:
                j = j + 1
            prev_part = part
            ilay_start = ilay_end
            xcrd_start = xcrd_end
            ycrd_start = ycrd_end
            zcrd_start = zcrd_end
            time_start = time_end
            veloc_start = veloc_end
            irow_start = irow_end
            icol_start = icol_end

    IFFfile.close()
    w.save(shpfile_out)
  

finally:

    if not IFFfile.closed : IFFfile.close()
    if not w.shp.closed : w.shp.close() #if not w.shp.closed : w.shp.close()
    if not w.shx.closed : w.shx.close()
    if not w.dbf.closed : w.dbf.close()
    print('%s' % 'End')

###Code end###
Vestus
  • 1
  • 1
  • Your var its empty, your are not calling right function – Luis Bote Mar 18 '21 at 12:24
  • Ok. Can you please give me some more detailed information how to fix this? Im not sure why my var is empty. Thanks for the fast response. – Vestus Mar 18 '21 at 12:28
  • Ok, i see doc and problem isnt that, problem its this function dont exist, u just ve to close your var w, as i post on answer, i didnt try cause i cant reproduce code, but ir should work – Luis Bote Mar 18 '21 at 14:49

1 Answers1

0

try it:

finally:

    if not IFFfile.closed : IFFfile.close()
    w.close()

line edited cause getting error

https://pypi.org/project/pyshp/

Luis Bote
  • 169
  • 2
  • 13
  • now I get the Error: "line 160, in if not w.closed : w.close() AttributeError: Writer instance has no attribute 'closed'" – Vestus Mar 18 '21 at 13:03