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.
for lyr in arcpy.mapping.ListLayers(mxd): tlyr = lyr dsc = arcpy.Describe(tlyr) sel_set = dsc.fidSet if dsc.shapeType == "Polyline": rows = arcpy.SearchCursor(tlyr, "OBJECTID = " + sel_set) for row in rows: arcLength = row.ARCLENGTH shapeLength = row.shape.length if arcLength > 0: print arcLength else: print shapeLength del row, rows
Hi James,If this is a feature class, try the following below. If this is a shapefile, you will have to update the SearchCursor to use FID instead of OBJECTID.for lyr in arcpy.mapping.ListLayers(mxd): tlyr = lyr dsc = arcpy.Describe(tlyr) sel_set = dsc.fidSet if dsc.shapeType == "Polyline": rows = arcpy.SearchCursor(tlyr, "OBJECTID = " + sel_set) for row in rows: arcLength = row.ARCLENGTH shapeLength = row.shape.length if arcLength > 0: print arcLength else: print 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] for lyr in arcpy.mapping.ListLayers(mxd): tlyr = lyr dsc = arcpy.Describe(tlyr) sel_set = dsc.fidSet if dsc.shapeType == "Polyline": rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set) for row in rows: arcLength = row.ARCLENGTH shapeLength = row.Shape_Length if arcLength > 0: print arcLength else: print shapeLength del row del rows
Check the name of your OBJECTID field (right-click on layer > Properties > Fields tab). It may be OID, or OBJECTID1.
Have you checked to see if some of your earlier statements are getting the desired results such as the Describe?Can you get a length of your sel_set to see if that is returning values?
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 == "Polyline": rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set) for row in rows: arcLength = row.ARCLENGTH shapeLength = row.Shape_Length if arcLength > 0: print arcLength else: print shapeLength del row del rowsThe error is in line 32, for row in rows: RuntimeError: An invalid SQL statement was used. I know the select Object ID works, because it changes values depending on the feature selected.
rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set + ")") for row in rows: arcLength = row.[1] shapeLength = row.[2] if arcLength > 0: print arcLength else: print shapeLength del row del row
When you switched back to the original cursor type you got no records because your where clause needs to use the IN operator not the = operator.The error using the new arcpy.da cursor is due to the cursor declaration syntax being different. With the arcpy data access (arcpy.da) cursor you have to supply a mandatory field name list prior to defining an optional where clause. Your code has no field list. At a minimum the field list would have to include OBJECTID, ARCLENGTH and Shape_Length. So this code: rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)should be changed to this code: rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set +")")OID@ makes sure you get the correct ObjectID field name reference, regardless of the database. Possibly SHAPE@LENGTH should be Shape_Length, depending on whether you want to access a geodatabase length field or the actual shape geometry length. You also do not have to use field names again after getting the cursor. You can use numbers to represent the fields. So that means the code beyond that line could also change to: rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set + ")") for row in rows: arcLength = row.[1] shapeLength = row.[2] if arcLength > 0: print arcLength else: print shapeLength del row del row
How many features do you have in your target feature class?If you print out the fidset, are all of these features included in the semicolon-delimited string?Can you post the results of printing the fidset from your dsc object?
The feature class has over a million records. Printing that many records will take quite a while.The arcpy.AddMessage("Continue") value was returned.
James:Can you create a feature class with a small subset of features (say 10) and then print out the fidset?It appears from Richard's last post that the semi-colon delimited string will need to be converted to a comma separated list. You shouldbe able to find python methods to easily perform that process.
Python is case sensitive. Try dsc.fidSet
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 arcLength > 0: print arcLength else: print shapeLength del row, rows
The result is the same whether I use dsc.fidSet, dsc.FidSet, or dsc.FIDSet.
Attached is some data you can test with. Add the feature class to ArcMap, select a single line, then execute the following: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 arcLength > 0: print arcLength else: print shapeLength del row, rows
Are you positive your layer has a feature selection already applied? If nothing is selected, no FID values should be returned.
Jake, the script works with the feature class you provided, using the Python window within ArcMap.
Yes, but it only worked because you selected one feature. If you select 2 features you will get nothing. That is why you have to use the IN operator to work with a list of 0 or more items.
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.