5

Trying to write the city name and point coords to a file. The following works with a polygon shapefile but not the point shapefile. I know I'm missing something basic.

import arcpy

# Set up the Environment
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\temp"

fc = r"C:\Utemp\cities.shp"
fields = ['SHAPE@XY']
output = open(r"C:\temp\cities.txt", 'w')
COFilePath = r"C:\temp\cities.csv"
COFile = open (COFilePath, "w")

# For each row write citiy name & coords
cursor = arcpy.da.SearchCursor(fc, "SHAPE@")
for row in cursor:
    for point in row[0].getPart(0):
        COFile.write(str(point.X) + ", " + str(point.Y) + "\n")
COFile.close()

Error: TypeError: 'Point' object is not iterable

3
  • 1
    Looks something like row[0].getPart(0) already returns a Point but you are trying to access it as an array of points.
    – ulrich
    Feb 10 '16 at 15:44
  • Little debug tip: Throw the code into arcmaps python window, run it and inspect the last value of row (should be the first though). Use type(row) and type type(row[0]) for example. Feb 10 '16 at 15:56
  • @Andreas - Very new to python. row = type(row[0]) = <class 'arcpy.arcobjects.geometries.PointGeometry'>
    – BillTheCat
    Feb 10 '16 at 16:23
4

row.getPart(0) is necessarily returning the first geometry in the row, so it won't be an iterable.

Thus your code becomes:

import arcpy

# Set up the Environment
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\temp"

fc = r"C:\Utemp\cities.shp"
fields = ['SHAPE@XY']
output = open(r"C:\temp\cities.txt", 'w')
COFilePath = r"C:\temp\cities.csv"
COFile = open (COFilePath, "w")

# For each row write citiy name & coords
cursor = arcpy.da.SearchCursor(fc, "SHAPE@")
for row in cursor:
    point = row[0].getPart(0)
    COFile.write(str(point.X) + ", " + str(point.Y) + "\n")

COFile.close()

Though I would recommend using with blocks in this case to avoid leaving things (cursors, files) open in case of an error. Also note that you open a file that you never use, and you don't close it, so I've cleaned that up.

import arcpy

# Set up the Environment
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\temp"

fc = r"C:\Utemp\cities.shp"
fields = ['SHAPE@XY']
output = open(r"C:\temp\cities.txt", 'w')

with open(r"C:\temp\cities.csv", 'w') as COFile:
    with arcpy.da.SearchCursor(fc, "SHAPE@") as cursor:
        for row in cursor:
            point = row[0].getPart(0)
            COFile.write("{}, {}\n".format(point.X, point.Y))

Note: the getPart(0) might not be necessary. point = row[0] may suffice.

So what's going on here? A cursor is a object that iterates through the shapefile only once (typically called a "generator", but arcpy is weird so I don't know if that's strictly true). So the for row in cursor line is conceptually the same as walking down the attribute table. Within each row, you're selecting the first column with row[0], which is the geometry in this case. Generally speaking, geometries can have multiple parts (e.g., boundaries that are attached to the mainland, but also have islands), but you're only looking an the first part with row[0].getPart(0), which returns a point geometry. Then you're writing the x- and y-coords to a file.

3
  • Thank you so much, not only for the code, but the explanation. The only difference I see is that you use with arcpy.da.SearchCursor(fc, "SHAPE@") as cursor: and I used cursor = arcpy.da.SearchCursor(fc, "SHAPE@"). Not sure how those are different other than format. Try to understand.
    – BillTheCat
    Feb 10 '16 at 17:12
  • @KenKirkeby yes that's the only difference between my two solutions. The main difference between my solutions and your code is that I'm not trying to loop the result of row[0].getPart(0)
    – Paul H
    Feb 10 '16 at 17:14
  • @KenKirkeby I also just demonstrated a different string formatting method, in case you're curious.
    – Paul H
    Feb 10 '16 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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