2

I´m a beginner at python programming and wanted to start with a litte and hopefully simple project. I tried to read float values out of a textfile and write them into a shapefile.

The program is working fine until the writing in the shapefile. Following error occurs:

TypeError: value #0 - unsupported type: list
TypeError: sequence size must match size of the row

I tried different styles (last lines in the code) but i could´t work it out. So my question is: What is my error and what could i do to solve this Problem?

Example of the Textfile ("test.txt"):

2500000.00 5600000.00 10
2500001.00 5600010.00 20

Script

import fileinput
import string
import os

# Declaration
env.overwriteOutput=True
env.workspace= outpath

x = []
y = []
z = []

newfc="newfc.shp" 
outpath="c:/tmp/"


# Create new Shapefile and add FIELDS
arcpy.CreateFeatureclass_management(outpath, newfc, "Point")
arcpy.AddField_management(newfc, "X", "FLOAT", field_length = 50)
arcpy.AddField_management(newfc, "Y", "FLOAT", field_length = 50)
arcpy.AddField_management(newfc, "Z", "FLOAT", field_length = 50)

# Reference Cursors
cursorX=arcpy.da.InsertCursor(newfc,"X")
cursorY=arcpy.da.InsertCursor(newfc,"Y")
cursorZ=arcpy.da.InsertCursor(newfc,"Z")

# Read File 
a = open("c:/tmp/test.txt","r")
inputF = a.readlines()

for line in inputF:
        xCoordinate, yCoordinate, zValue = line.split()
        x.append(float(xCoordinate))
        y.append(float(yCoodrinate))
        z.append(float(zValue))

print([x]) # [[2500000.0, 2500001.0]]
print([y]) # [[5600000.0, 5600010.0]]
print([z]) # [[10.0, 20.0]]

# Copy Arrays into the Field
cursorX.insertRow(x) # TypeError: value #0 - unsupported type: list
cursorY.insertRow((y)) # TypeError: sequence size must match size of the row
cursorZ.insertRow(z)

1 Answer 1

5

Looks like you're trying to add data to the insert cursor one column at a time. Cursors think about the world one row at time. I recommend the following code. A few other notes... Python code runs in order, so make sure you declare your outpath variable before you set your env.workspace to outpath. Also, this code is going to create a shapefile with the attributes that you want, but it's not actually going to define a shape. (The fields you designated could have anything in them. The shapefile doesn't know that you're defining coordinates). So it will have an attribute table but no points will appear when you look at it in ArcMap. If that's what you're looking to do, check out this article. There are code examples at the bottom for writing point geometries.

import arcpy

newfcName ="newfc.shp" 
outpath = r"c:/GIS"

# Declaration
arcpy.env.overwriteOutput=True
arcpy.env.workspace= outpath


# Create new Shapefile and add FIELDS
newfc = arcpy.CreateFeatureclass_management(outpath, newfcName, "Point")
arcpy.AddField_management(newfc, "X", "FLOAT", field_length = 50)
arcpy.AddField_management(newfc, "Y", "FLOAT", field_length = 50)
arcpy.AddField_management(newfc, "Z", "FLOAT", field_length = 50)

# Reference Cursors
cursor=arcpy.da.InsertCursor(newfc, ["X", "Y", "Z"])

# Read File 
a = open("c:/GIS/test.txt","r")
inputF = a.readlines()

for line in inputF:
    xCoordinate, yCoordinate, zValue = line.split(" ")
    newRow = (float(xCoordinate), float(yCoordinate), float(zValue))
    cursor.insertRow(newRow)

a.close()

If you want to write the geometry too, substitute this for the last few lines:

cursor=arcpy.da.InsertCursor(newfc, ["X", "Y", "Z", "SHAPE@XY"])

# Read File 
a = open("c:/GIS/test.txt","r")
inputF = a.readlines()

for line in inputF:
    mySplit = line.split(" ")
    xCoordinate = float(mySplit[0])
    yCoordinate = float(mySplit[1])
    zValue = float(mySplit[2])
    newRow = (xCoordinate, yCoordinate, zValue, (xCoordinate, yCoordinate))
    cursor.insertRow(newRow)

a.close()
9
  • you really helped me out, thanks for you extensive Explanation =)
    – Andriel
    Commented Apr 28, 2016 at 16:59
  • nicely done. This can be applied to a csv as well correct?
    – ziggy
    Commented Apr 28, 2016 at 17:02
  • 1
    No problem, Andriel. Welcome to python. It's a fun language! Ziggy, sure, it think it should work with a csv. But if your data is comma separated, be sure you change the split to accomodate that. So something like this: xCoordinate, yCoordinate, zValue = line.split(",")
    – geobug
    Commented Apr 28, 2016 at 17:11
  • 1
    Whoops. Just noticed there was an identation error in the code above under the "for line in inputF" section. I corrected it above.
    – geobug
    Commented Apr 28, 2016 at 17:20
  • 1
    Yep, empty quotes will do. Like this: 'newfc = arcpy.CreateFeatureclass_management(outpath, newfcName, "Point", "", "", "", 26910)'
    – geobug
    Commented Apr 28, 2016 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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