import arcpy mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (mxd)[0] lyr = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0] for lyr in arcpy.mapping.ListLayers(mxd): tlyr = lyr dsc = arcpy.Describe(tlyr) sel_set = dsc.FIDSet if dsc.shapeType == "Line": if len(sel_set) > 0: #If ARCLENGTH value > 0 arcpy.AddMessage(str(ARCLENGTH)) #Return ARCLENGTH value else: arcpy.AddMessage(str(Shape_Length)) #Return Shape_Length value
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.
Only add ArcLength when it is greater than '0'. Otherwise, the script should return shapeLength values and add those together.
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 + ")") arcLengthTot = 0 shapeLengthTot = 0 for row in cursor: arcLength = row[1] shapeLength = row[2] if arcLength > '0': arcLengthTot += float(arcLength) arcpy.AddMessage("ArcLength = " + arcLength) else: shapeLengthTot += shapeLength arcpy.AddMessage("ShapeLength = " + str(shapeLength)) if arcLengthTot > 0: arcpy.AddMessage("ArcLengthTot = " + str(arcLengthTot)) if shapeLengthTot > 0: arcpy.AddMessage("ShapeLengthTot = " + str(shapeLengthTot)) del row del cursor
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.
if arcLength > '0': arcLengthTot += float(arcLength) shapeLengthTot += shapeLength arcpy.AddMessage("ArcLength = " + arcLength) else: shapeLengthTot += shapeLength arcpy.AddMessage("ShapeLength = " + str(shapeLength))
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.
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?
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 + ")") arcLengthTot = 0 shapeLengthTot = 0 for row in cursor: arcLength = row[1] shapeLength = row[2] if arcLength > '0': arcLengthTot += float(arcLength) arcpy.AddMessage("ArcLength = " + arcLength) else: shapeLengthTot += shapeLength arcpy.AddMessage("ShapeLength = " + str(shapeLength)) arcpy.AddMessage("ArcLengthTot = " + str(arcLengthTot)) arcpy.AddMessage("ShapeLengthTot = " + str(shapeLengthTot)) del row del cursor
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.
Thanks Richard. This script is really kicking my butt!
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.
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.5Shape_Length = 692.9Shape_Length = 302.5for 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 arcLength > '0': arcpy.AddMessage("ArcLength = " + arcLength) else: arcpy.AddMessage("ShapeLength = " + str(shapeLength)) del row del cursor
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 arcLength > '0': arcpy.AddMessage("ArcLength = " + arcLength) else: arcpy.AddMessage("ShapeLength = " + str(shapeLength)) del row del cursor
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
I would have expected an indentation error, but not the name error. Thanks.
Just indent the del row, rows one more time and that will fix that line. It is at the wrong indent level.
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.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
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
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.
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
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.
James,Here is tech article that discusses selecting multiple values:http://support.esri.com/en/knowledgebase/techarticles/detail/32024Try the below. It should work whether you select one line or multiple lines:mxd = arcpy.mapping.MapDocument("CURRENT") for lyr in arcpy.mapping.ListLayers(mxd): dsc = arcpy.Describe(lyr) sel_set = dsc.fidSet if dsc.shapeType == "Polyline": if len(sel_set) > 1: 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) > 1: print arcLengthList else: print shapeLengthList i += 1 else: rows = arcpy.SearchCursor(lyr, "OBJECTID = " + sel_set) for row in rows: arcLength = row.ARCLENGTH shapeLength = row.shape.length if len(arcLength) > ' ': print arcLength else: print shapeLength del row, rows
mxd = arcpy.mapping.MapDocument("CURRENT") for lyr in arcpy.mapping.ListLayers(mxd): dsc = arcpy.Describe(lyr) sel_set = dsc.fidSet if dsc.shapeType == "Polyline": if len(sel_set) > 1: 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) > 1: print arcLengthList else: print shapeLengthList i += 1 else: rows = arcpy.SearchCursor(lyr, "OBJECTID = " + sel_set) for row in rows: arcLength = row.ARCLENGTH shapeLength = row.shape.length if len(arcLength) > ' ': print arcLength else: print shapeLength del row, rows
mxd = arcpy.mapping.MapDocument("CURRENT") for lyr in arcpy.mapping.ListLayers(mxd): dsc = arcpy.Describe(lyr) sel_set = dsc.fidSet if dsc.shapeType == "Polyline": if len(sel_set) > 1: 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) > 1: print arcLengthList else: print shapeLengthList i += 1 else: 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
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
mxd = arcpy.mapping.MapDocument("CURRENT") for lyr in arcpy.mapping.ListLayers(mxd): dsc = arcpy.Describe(lyr) sel_set = dsc.fidSet.replace(";",",") if dsc.shapeType == "Polyline": rows = arcpy.da.SearchCursor(lyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set + ")") for row in rows: arcLength = row.[1] shapeLength = row.[2] if arcLength > ' ': print arcLength else: print shapeLength del row, rows
def printmessage(): print 'ARCLENGTH' if __name__ == '__main__': printmessage()
cursor = arcpy.da.SearchCursor("Lot_Lines", ["ARCLENGTH", "Shape_Length"]) for row in cursor: row [0] = arcpy.AddMessage(str(row[0])) del row del cursor
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
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
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.