My script geterates two lines in a temp shapefile.
I'm then using SearchCursor and Describe to get the lengths of these lines, like this:
rows = arcpy.SearchCursor(tempSHP)
len = 0
shapeName = arcpy.Describe(tempSHP).shapeFieldName
for row in rows:
feat = row.getValue(shapeName)
len = feat.length
print len
What I really want is to set the length of each line to a different variable - I tried various things like below but haven't found the right way to approach this little problem (the error for the code below said "row does not support indexing"):
for row in rows:
featX = row[0].getValue(shapeName)
lenX = featX.length
featY = row[1].getValue(shapeName)
lenY = featY.length
Solved! Go to Solution.
Use a dictionary to store the 'variables':
rows = arcpy.SearchCursor(shp)
shapeName= arcpy.Describe(tempSHP).shapeFieldName
count = 0
d={}
for row in rows:
count +=1
feat = row.getValue(shapeName)
d["string{0}".format(count)] = str(feat.area)
print d['string1']
print d['string2']
print d['string3']
#etc
Indexing is supported in arcpy.da, not plain arcpy.
Try
rows = arcpy.da.SearchCursor(tempSHP)
# or even better:
with arcpy.da.SearchCursor(tempSHP) as rows:
# do something
The second method will automatically clean up rows and row when you exit the with block, so you don't have to do so manually.
Use a dictionary to store the 'variables':
rows = arcpy.SearchCursor(shp)
shapeName= arcpy.Describe(tempSHP).shapeFieldName
count = 0
d={}
for row in rows:
count +=1
feat = row.getValue(shapeName)
d["string{0}".format(count)] = str(feat.area)
print d['string1']
print d['string2']
print d['string3']
#etc
The reason for the "row does not support indexing" error is tied to the data type of row. The code you posted is returning an arcpy Polygon object, and that object doesn't support indexing. Since I don't know whether you are working with points, lines, or polygons, I will assume polygons and use the centroids in the code snippet below:
for row in rows:
lenX = row.getValue(shapeName).centroid.X
lenY = row.getValue(shapeName).centroid.Y
And I agree with Greg Keith, the Data Access (arcpy.da) cursors are much more robust than the older/original arcpy cursors.
Read the original post a bit too quickly, I now see we are talking about lines. I guess I don't completely understand the intent or goal of the original code. Are featX and featY different features or the X and Y component of the same feature?
I'm trying to measure the width and height of a raster in metres. I'm working in WGS84 (degrees) so Raster Properties aren't much good - and I don't like the results when I re-project the raster. So I have constructed two lines based on TOP, BOTTOM, RIGHT, LEFT raster properties, then I reproject those and get their lengths. They are separate features and they will be deleted once I know their lengths.
Pieter GvdB's answer works for me. I'm pretty new to python, I tried Greg Keith's answer but my knowledge isn't good enough to know how to fit it in my code.
Using arcpy.da, you can retrieve the fields by index. Arcpy.da SearchCursors return tuples.
So, in the code below, you'd retrieve values by index; you don't use getValue.
# create list of field names you want returned. This is faster if you don't want all fields.
# optionally, you could skip this (and leave fldList out of SearchCursor call below)
# if you did want all fields. Use an asterisk instead of fldList in that case.
fldList = ['fieldone', 'fldFive']
with arcpy.da.SearchCursor(tempSHP, fldList) as rows:
for row in rows:
print row[0] # prints value of fieldone in first record, then second, etc
print row[1] # prints value of fieldfive in first record, then second, etc
Also note in the help link above that various shape field properties can be accessed by tokens instead of field names, and that none of this works on raster fields.
You may be able to achieve what you want just using raster extents and not getting into cursors at all. Does the following code work for you?
def rasterDim(raster, EPSG=None):
ras = arcpy.Raster(raster)
extent = arcpy.Describe(ras).Extent
if EPSG is not None:
sr = arcpy.SpatialReference(EPSG)
extent = extent.projectAs(sr)
return extent.width, extent.height
Pass the function above the text path to the raster and an optional EPSG code if you want to get the width and height based on a different projection than the raster's spatial reference.