I am parsing an NMEA file to be able to import it into an ArcGIS with arcpy. I finalised the parsing part and also created a list with the appropriate form. But if I'm trying to insert the List into the Attribute Table via an InsertCursor, I continously get the following error:
line 127, in <module>
cursor.insertRow(a) TypeError: sequence size must match size of the row
I already figured out that there are quotation marks in the list that causes the problem, but I really don't know how to create a workaround here. I already tried to make an array, but unfortunately I to stick to float values for the Longitude and Latitude.
This is how my code looks like:
import arcpy
#arcpy.env.workspace = env_path = arcpy.GetParameterAsText(0)
arcpy.env.overwriteOutput = 1
#nmea = arcpy.GetParameterAsText(1)
data = open('C:/temp/NMEA.txt', 'r')
input = data.read()
input2=input.split('\n')
GGA = list();
for lines in input2:
if '$GPGGA' in lines:
GGA.append(lines)
GGA2 = ''.join(GGA)
GGA3 = GGA2.split(",")
UTC = list();
Latitude = list();
Comp1 = list();
Longitude = list();
Comp2 = list();
GPSQual = list();
SatVis = list();
HDOP = list();
AntAlt = list();
AntHeight = list();
GeoidalSep1 = list();
SeaLevel = list();
GeoidalSep2 = list();
Age = list();
DifRef = list();
Check = list();
for item in GGA3:
GGA3.pop(0)
UTC.append(GGA3.pop(0))
Latitude.append(GGA3.pop(0))
Comp1.append(GGA3.pop(0))
Longitude.append(GGA3.pop(0))
Comp2.append(GGA3.pop(0))
GPSQual.append(GGA3.pop(0))
SatVis.append(GGA3.pop(0))
HDOP.append(GGA3.pop(0))
AntAlt.append(GGA3.pop(0))
AntHeight.append(GGA3.pop(0))
GeoidalSep1.append(GGA3.pop(0))
SeaLevel.append(GGA3.pop(0))
DifRef.append(GGA3.pop(0))
# NMEA Coordinate Format
#(d)ddmm.mmmm
#(d)dd + (mm.mmmm/60) (* -1 for W and S)
# Calculation Longitude
minLong = list();
degLong = list();
for x in Longitude:
minLong.append((x[-10:]))
degLong.append((x[0:3]))
minLongInt = map(float,minLong)
degLongInt = map(int,degLong)
minLongCalc = list();
for x in minLongInt:
minLongCalc.append(x/60)
LongCalc = [x +y for x,y in zip(degLongInt, minLongCalc)]
# Calculation Latitude
minLat = list();
degLat = list();
for x in Latitude:
minLat.append((x[-10:]))
degLat.append((x[0:2]))
minLatInt = map(float,minLat)
degLatInt = map(float,degLat)
minLatCalc = list();
for x in minLatInt:
minLatCalc.append(x/60)
LatCalc = [x +y for x,y in zip(degLatInt, minLatCalc)]
GPSQaulInt = map(float,GPSQual)
SatVisInt = map(float,SatVis)
HDOPInt = map(float,HDOP)
AntAltInt = map(float,AntAlt)
a = list();
for i in range(len(LatCalc)):
a.append("('{}','{}','{}','{}','{}','{}',({},{}),)".format(LatCalc[i],LongCalc[i],GPSQual[i],SatVis[i],HDOP[i],AntAlt[i],LatCalc[i],LongCalc[i]))
#Create NMEA Shape
arcpy.CreateFeatureclass_management("C:/tmp/","ParsedNMEA.shp","POINT","","DISABLED","DISABLED",4326,"","","","")
NMEAFile = "C:/tmp/ParsedNMEA.shp"
arcpy.AddField_management(NMEAFile,"X","DOUBLE",23,15,"","","NON_NULLABLE","REQUIRED")
arcpy.AddField_management(NMEAFile,"Y","DOUBLE", 23, 15, "", "", "NON_NULLABLE", "REQUIRED")
arcpy.AddField_management(NMEAFile,"GPSQuality","SHORT", 15, "", 10, "", "NULLABLE", "NON_REQUIRED")
arcpy.AddField_management(NMEAFile,"SatVis","TEXT", 15, "", 10, "", "NULLABLE", "NON_REQUIRED")
arcpy.AddField_management(NMEAFile,"HDOP","FLOAT", 5, 15, "", "", "NULLABLE", "NON_REQUIRED")
arcpy.AddField_management(NMEAFile,"AntHeight","DOUBLE", 20, 15, "", "", "NULLABLE", "NON_REQUIRED")
cursor = arcpy.da.InsertCursor(NMEAFile,("X","Y","GPSQuality","SatVis","HDOP","AntHeight","SHAPE@XY"))
for i in range(len(a)):
cursor.insertRow(a)
My approach is based on the example in ESRIs arcpy documentation:
import arcpy
# A list of values that will be used to construct new rows
row_values = [('Anderson', (1409934.4442000017, 1076766.8192000017)),
('Andrews', (752000.2489000037, 1128929.8114))]
# Open an InsertCursor
cursor = arcpy.da.InsertCursor('C:/data/texas.gdb/counties',
['NAME', 'SHAPE@XY'])
# Insert new rows that include the county name and a x,y coordinate
# pair that represents the county center
for row in row_values:
cursor.insertRow(row)
I am working with ArcGIS 10.4 and PythonWin (Python 2.7).
Output of my list looks like this:
["('47.78892519','13.0611603033','1','06','1.0','435.838',(47.78892519,13.0611603033),)", "('47.7889243533','13.061157485','1','06','1.0','436.367',(47.7889243533,13.061157485),)"]
Can anyone help me out with an workaround?
row _ values
isn't formatted as an array of arrays or list of arrays, it's an array of lists,.