Select to view content in your preferred language

Linear Events and Select By Location

772
3
Jump to solution
05-29-2013 06:59 AM
WesleyMarcell
Occasional Contributor
Greetings,
I am working on a script to check some linear events at my current job.  I have been having some issues, and it is frustrating because the process I am attempting would work for regular features, and works when you manually run it through GIS, but it does not work programatically in python.  FYI I am using ArcGIS 10.0 for this process.

Basically I am attempting to check two fields "From_Milepost" and "To_Milepost" to ensure that the events are going from low to high (M values should work also).  My problem is that for our workflow, we are only checking events that fall withing a certian division of the data.  Our SDE data often includes several divisions, so I am attempting a SBL before checking the Milepost values.

Here is a list of the different things I have attempted:

1. Select By Location (Linear event that falls within division) > Check that Milepost order is correct
    Result: Count on linear event is 140 (expected) when run through the check MP order section only four features are processed.

2. Select By Location > Create Feature Layer > Check MP order is correct
    Result: Count on linear event is 140 (expected) when run through the check MP order section only four features are processed.

3. Select By Location > Create Table View > Check MP order is correct
    Result: Count (6612) does not match, selection ignored when creating Table view
    All events are processed through MP loop (6612 records)

4. Select By Location > Create Feature Layer > Create Table View > Check MP order loop
    Results: Count of Feature layer 140, count of Table view 6612, all 6612 records looped through

And finally this workflow works, but not as I would like it to (OID's not retained, my data does not have unique identifiers)
5. Select by location > Copy Features management > Loop through MP check section
    Results: Count matches, only 140 features looped through.  OIDs renumbered 1-140

I have attached code below with the above numbered and commented out.  If anyone can help it would be appreciated.  What I am hoping is to get the 140 selected events to go through the loop.  I would like to retain the OID's if at all possible.  If there is a way to do this without creating Table views/feature layers that is great.  If there is a way to retain the original OID's when you run the CopyFeatures_management that would also be great.  My last idea is to create a FGDB and only write the errors to a feature class.  This would allow the user to zoom to the area and find the feature.  Retaining OID's however would be the best solution.  Any feedback is greatly appreciated.  I do not have much experience using Event Layers, and have even less experience attempting to program python using events.

Here is some delicious code goodness!!!
import arcpy from arcpy import mapping from arcpy import env  arcpy.env.overwriteOutput = True SUBDIVISION = "NCS_SUB_CODE_EVT Events" OPCODE = "NCS_OP_CODE_EVT Events"  ##Check MP Order NCS_OP_CODE_EVT arcpy.AddMessage("***Checking MP order - NCS_OP_CODE_EVT***") arcpy.SelectLayerByLocation_management(OPCODE, "INTERSECT", SUBDIVISION, "", "NEW_SELECTION")  ######1. This will run a loop on the data.  Count matches, but only four results are looped through#### ##arcpy.AddMessage("OPCODE = " + str(arcpy.GetCount_management(OPCODE))) ##rows = arcpy.SearchCursor(OPCODE)  ######2. This creates Feature Layer and then loops through.  Counts match, only four records looped#### ##arcpy.MakeFeatureLayer_management(OPCODE, "OPCODE_F") ##arcpy.AddMessage("OPCODE_FeatureLyr Count = " + str(arcpy.GetCount_management("OPCODE_F"))) ##rows = arcpy.SearchCursor("OPCODE_F")  ####3. This creates a Table View and then loops.  Counts match only four records looped#### ##arcpy.MakeTableView_management(OPCODE, "OPCODE_T") ##arcpy.AddMessage("OPCODE_TblView Count = " + str(arcpy.GetCount_management("OPCODE_T"))) ##rows = arcpy.SearchCursor("OPCODE_T")  ####4. This will combine above.  Create Feature Layer then send to Table view, then loop#### ##arcpy.MakeFeatureLayer_management(OPCODE, "OPCODE_F") ##arcpy.MakeTableView_management("OPCODE_F", "OPCODE_T") ##arcpy.AddMessage("OPCODE_FeatureLyr Count = " + str(arcpy.GetCount_management("OPCODE_F"))) ##arcpy.AddMessage("OPCODE_TblView Count = " + str(arcpy.GetCount_management("OPCODE_T"))) ##rows = arcpy.SearchCursor("OPCODE_T")  ####5. Copy features out.  Does not retain OID's arcpy.CopyFeatures_management(OPCODE, "OPCODE_SEL") arcpy.AddMessage(arcpy.GetCount_management("OPCODE_SEL")) rows = arcpy.SearchCursor("OPCODE_SEL")  for row in rows:         MP = (row.To_Milepost - row.From_Milepost)         arcpy.AddMessage("OID = " + str(row.OBJECTID) + " MP = " + str(MP))         if MP < 0:             arcpy.AddMessage("ObjectID: " + str(row.OBJECTID)+ " has decreasing Mileposts")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Alum
OID values are not under a user's control and cannot be retained within an OID field of any exported feature class or table (it will always renumber).  Geoprocessing operations that output OID values for backward links (such as Intersect, Spatial Join, and many others) create a Long field with a name like TARGET_FID, JOIN_FID or FID_OPCODE (FID plus source name).  Once these fields are converted to a Long field they are no longer ensured to be unique in the output table.  These tools usually combine two inputs.  Copy Features does not retain OIDs in a field like this as you have seen.

So you could create a polygon of your area and do a Spatial join with your features making the lines the TARGET_FID (primary source).  They would retain their original shape even if they were not contained completely by the area boundary.  Spatial Join should also work with all feature class type combinations (polygon on polygon, line or point, line on polygon, line or point, and point on polygon, line or point).  In effect Spatial Join is the process of Select by Location and output to a new feature class in one operation with backward OID support to your original source data.

View solution in original post

0 Kudos
3 Replies
WesleyMarcell
Occasional Contributor
bump :confused:
0 Kudos
RichardFairhurst
MVP Alum
OID values are not under a user's control and cannot be retained within an OID field of any exported feature class or table (it will always renumber).  Geoprocessing operations that output OID values for backward links (such as Intersect, Spatial Join, and many others) create a Long field with a name like TARGET_FID, JOIN_FID or FID_OPCODE (FID plus source name).  Once these fields are converted to a Long field they are no longer ensured to be unique in the output table.  These tools usually combine two inputs.  Copy Features does not retain OIDs in a field like this as you have seen.

So you could create a polygon of your area and do a Spatial join with your features making the lines the TARGET_FID (primary source).  They would retain their original shape even if they were not contained completely by the area boundary.  Spatial Join should also work with all feature class type combinations (polygon on polygon, line or point, line on polygon, line or point, and point on polygon, line or point).  In effect Spatial Join is the process of Select by Location and output to a new feature class in one operation with backward OID support to your original source data.
0 Kudos
WesleyMarcell
Occasional Contributor
Thanks for the reply! I think that a spatial join will do the trick.  I actually came up with the same solution this morning, and have tested and gotten it to work as I would like.   Thanks a bunch for the feedback.  i will try to put the code up when I have finished to help anyone else with this problem!!!
0 Kudos