POST
|
Reread my post. After you posted this I made some edits that should be clearer. In the case of ShapeLength, did you want it to add even when ArcLength is greater than '0' or just when arcLength is less than or equal to '0'? I wrote it to do the latter. If you wanted it to do the former the code within the cursor rotuine would be if arcLength > '0':
arcLengthTot += float(arcLength)
shapeLengthTot += shapeLength
arcpy.AddMessage("ArcLength = " + arcLength)
else:
shapeLengthTot += shapeLength
arcpy.AddMessage("ShapeLength = " + str(shapeLength)) Edit: Fixed another indent issue. Only add ArcLength when it is greater than '0'. Otherwise, the script should return shapeLength values and add those together.
... View more
09-04-2013
11:41 AM
|
0
|
0
|
214
|
POST
|
Rereading my last post, I realize my statement maybe clear as mud. I need to add arcLength + arcLength and shapeLength + shapeLength, as two separate expressions.
... View more
09-04-2013
11:29 AM
|
0
|
0
|
214
|
POST
|
This is quickly becoming the script that would not go away. I have read through the help on arithmetic operators. I need to add the selected ArcLength values together and the selected Shape_Length values together. What is the syntax difference in using + for an arithmetic operator and using the + to concatenate fields?
... View more
09-04-2013
11:16 AM
|
0
|
0
|
440
|
POST
|
Richard, Thanks for all of your help. I really appreciate the explanations to go along with the examples. I learned a lot during the process.
... View more
09-04-2013
06:29 AM
|
0
|
0
|
440
|
POST
|
It is another case of the indent being at the wrong level. By indenting as I have above the loop includes the message as each record is processed. Before it only processed once after all rows had been read, so only the last row data was left. Also, I believe the line that reads if len(arcLength) > 0: should be replaced by the line I have shown above (if arcLength > '0': ) to ensure than even single digit above 0 arclength's are included in the output as an ArcLength. Why does changing the number 0 to a string '0', change the selection from arcLength to shapeLength? For instance, if len(arcLength) > 0: returns arcLength and if len(arcLength) > '0': returns shapeLength? The ARCLENGTH field is a string and the Shape_Length field is a double.
... View more
09-04-2013
06:08 AM
|
0
|
0
|
440
|
POST
|
It is another case of the indent being at the wrong level. By indenting as I have above the loop includes the message as each record is processed. Before it only processed once after all rows had been read, so only the last row data was left. Also, I believe the line that reads if len(arcLength) > 0: should be replaced by the line I have shown above (if arcLength > ' ':) to ensure than even single digit arclength's are included in the output as an ArcLength. Thanks Richard. This script is really kicking my butt! If I leave if len(arcLength) > '': shapeLength is returned if an arcLength is present or not and if len(arcLength) > 0: either an arcLength value is returned or arcLenth = . I am going to try and add a second if statement with if len(arcLength) > '': and see if that works.
... View more
09-03-2013
11:38 AM
|
0
|
0
|
440
|
POST
|
Using Jake's example, I was unable to get any results. Using, Rich's example, I was able to return either ARCLENGTH or Shape_Length. But, it only returns one record at a time and the record with the largest OID. For instance, if I have 3 Shape_Length records selected, Shape_Length = 302.5 is returned. The script returns only the record with the highest OID value. Any ideas on how to return all three values? Something along the lines of: Shape_Length = 244.5 Shape_Length = 692.9 Shape_Length = 302.5 for lyr in arcpy.mapping.ListLayers(mxd):
dsc = arcpy.Describe(lyr)
sel_set = dsc.fidSet.replace(";",",")
if dsc.shapeType == "Polyline" and dsc.fidSet != "":
cursor = arcpy.da.SearchCursor(lyr, ["OID@", "ARCLENGTH", "Shape_Length"], "OBJECTID IN (" + sel_set + ")")
for row in cursor:
arcLength = row[1]
shapeLength = row[2]
if len(arcLength) > 0:
arcpy.AddMessage("ArcLength = " + arcLength)
else:
arcpy.AddMessage("ShapeLength = " + str(shapeLength))
del row
del cursor
... View more
09-03-2013
10:40 AM
|
0
|
0
|
440
|
POST
|
Just indent the del row, rows one more time and that will fix that line. It is at the wrong indent level. I would have expected an indentation error, but not the name error. Thanks.
... View more
09-03-2013
06:39 AM
|
0
|
0
|
322
|
POST
|
ArcLength is a string field with spaces, so the test will fail unless it converts the string to a number and tests for a space. IN works even if there are no listed object (nothing is selected but no error occurs), a single item, or a list of items, so the else clause is not necessary. = fails and produces an error if there is no listed objectID value or a list, so IN is all you need. I am receiving an error in line 66, del row, rows. The error code is an NameError: name row and rows are not defined. How are they not defined? If I comment the del row, rows statement out the script runs, but without errors or results. import arcpy
mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames (mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]
arcpy.AddMessage(lyr.name)
for lyr in arcpy.mapping.ListLayers(mxd):
dsc = arcpy.Describe(lyr)
sel_set = dsc.fidSet
if dsc.shapeType == "Polyline":
if len(sel_set) > 0:
sel_set = sel_set.replace(";", ",")
arcLengthList = []
shapeLengthList = []
rows = arcpy.SearchCursor(lyr, "OBJECTID IN (" + sel_set + ")")
for row in rows:
arcLengthList.append(row.ARCLENGTH)
shapeLengthList.append(row.Shape_Length)
i = 0
while i < len(arcLengthList):
if len(arcLengthList) > 0:
print arcLengthList
else:
print shapeLengthList
i += 1
arcpy.AddMessage("Good")
else:
rows = arcpy.SearchCursor(lyr, "OBJECTID = " + sel_set)
for row in rows:
arcLength = row.ARCLENGTH
shapeLength = row.Shape_Length
if len(arcLength) > ' ':
arcpy.AddMessage("ArcLength = " + arcLength)
else:
arcpy.AddMessage("ShapeLength = " + str(shapeLength))
del row, rows
... View more
09-03-2013
06:15 AM
|
0
|
0
|
322
|
POST
|
ArcLength is a string field with spaces, so the test will fail unless it converts the string to a number and tests for a space. IN works even if there are no listed object (nothing is selected but no error occurs), a single item, or a list of items, so the else clause is not necessary. = fails and produces an error if there is no listed objectID value or a list, so IN is all you need. Richard, Which statement accounts for the ARCLENGTH field being a string with spaces? Because, if I use your example, I run into a RuntimeError: A column was specified that does not exist, in line 15. for row in rows:, but the error is probably in line 14, the SearchCursor. import arcpy
mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames (mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]
arcpy.AddMessage(lyr.name)
for lyr in arcpy.mapping.ListLayers(mxd):
dsc = arcpy.Describe(lyr)
sel_set = dsc.fidSet
myString = ",".join(sel_set)
rows = arcpy.da.SearchCursor(lyr, ["OID@", "ARCLENGTH", "Shape_Length"], "OBJECTID IN (" + myString + ")")
for row in rows:
arcLength = row.ARCLENGTH
shapeLength = row.Shape_Length
if len(arcLength) > 0:
arcpy.AddMessage("ArcLength = " + arcLength)
else:
arcpy.AddMessage("ShapeLength = " + str(shapeLength))
del row, rows
... View more
09-03-2013
05:27 AM
|
0
|
0
|
322
|
POST
|
I was hoping ESRI or the forums contained attitional information. I also have a book that covers Python and the OBJECTID IN operator is not mentioned.
... View more
08-30-2013
11:49 AM
|
0
|
0
|
322
|
POST
|
Richard, Can you point me in the direction to read up on the OBJECTID IN operator? It looks like I may need to select more than one Polyline at a time. Thanks
... View more
08-30-2013
11:33 AM
|
0
|
0
|
322
|
POST
|
Dan, Using your suggestion, would you suggest replacing print arcLength with the following: def printmessage():
print 'ARCLENGTH'
if __name__ == '__main__':
printmessage() Then repeat for Shape_Length? Thanks
... View more
08-29-2013
06:54 AM
|
0
|
0
|
348
|
POST
|
Why would a Search Cursor not work to return the ARCLENGTH or Shape_Length value? cursor = arcpy.da.SearchCursor("Lot_Lines", ["ARCLENGTH", "Shape_Length"])
for row in cursor:
row [0] = arcpy.AddMessage(str(row[0]))
del row
del cursor
... View more
08-29-2013
05:07 AM
|
0
|
0
|
348
|
POST
|
The problem is that the field is a String. The empty value for the ARCLENGTH field contains a space. The below code should work: mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
dsc = arcpy.Describe(lyr)
sel_set = dsc.fidSet
if dsc.shapeType == "Polyline":
rows = arcpy.SearchCursor(lyr, "OBJECTID = " + sel_set)
for row in rows:
arcLength = row.ARCLENGTH
shapeLength = row.shape.length
if len(arcLength) > 1:
print arcLength
else:
print shapeLength
del row, rows Jake, The script works using the Python Window within ArcMap. But, not as a Script within a Toolbox. The script runs, but I probably need to replace the print statements with AddMessages.
... View more
08-28-2013
11:34 AM
|
0
|
0
|
348
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|