I am working with polylines with hundreds of thousands of features, and would like to write a script that returns the OIDs of all lines adjacent (intersecting with) each line. I have been experimenting with the Geometry object and the shp.touches() method, but it seems as though a cursor with a nested cursor on the same feature is the only way to use it, and this takes much too long.
The main goal is to perform an "Eliminate" task on a polyline, where short lines are merged to adjacent longer lines that share some unique ID. Collecting information in a dictionary has always been my technique of choice because of its speed, but dealing with geometries complicates this significantly. Unfortunately, Eliminate only works on polygons, and is quite slow itself.
I have already tried to record shape geometries in a dictionary (dict[row.OBJECTID] = [row.Shape_Length, row.Shape, row.uniqueID]) and attempted a nested iteration, but the geometry objects don't seem to be interacting in the same way they do within a cursor iteration (shp.touches() doesn't work). Has anyone come across any techniques I could use for this problem?

Would Dissolve work for you or are you looking to merge lines that are only of a specific length?
–
kenbujaJan 11 '13 at 18:35

the latter, and not just that, but i want to merge only short lines to longer lines that share a unique ID, which makes things complex =P
–
ndimhypervolJan 11 '13 at 18:50

I think the information you need would be accessible from an arc-node list (part of topology) so an approach to this would be to try and get at that after building a topology.
–
PolyGeo♦Jan 12 '13 at 2:12

3 Answers
3

I'm working an a project that does something similar, but I'm using ArcObjects. Its goal is to aggregate polygons that are smaller than a user-designated area with their larger adjacent neighboring polygons.

My logic was to find the smallest polygon and merging it with a larger, adjacent polygon. I chose this method instead of just cycling straight through a feature cursor, since after a polygon is merged with another one, it could still be smaller than the minimum area. This ensures that the resultant polygon can still be returned by the search for the smallest polygon. The SQL code to find the smallest polygon is for the layer "layername" (assuming it's in a geodatabase)

"Shape_Area" = (Select Min("Shape_Area") from layername

The adjacent polygon that it gets merged with is not chosen randomly, but has the closest value for a selected numeric attribute. You could modify this to only merge with a feature that has the same unique attribute.

The only little bug I'm still working on is when there is an island polygon. Currently, the code will exit since otherwise it would just get stuck in an endless loop, with the query filter always returning that island polygon as the smallest feature.

nice solution to this problem. there is now an arcgis 10.0 tool called "Eliminate" which does a similar function, but takes a selected layer as input.
–
ndimhypervolJan 14 '13 at 18:15

My colleague initially looked at Eliminate to solve his problem, but we settled on this method instead, since he wanted to merge features based on attribute rather than longest shared border or largest area.
–
kenbujaJan 14 '13 at 18:29

i think this method is logically better than mine, but would take much longer in arcpy because there is no access to the shape method "MergeFeature". instead, one has to modify field values and then dissolve on them in a recursive process. i wish there was better access to these ArcObjects methods in arcpy.
–
ndimhypervolJan 16 '13 at 17:23

I worked in a project something similar of this, but I used ArcObjects. My goal was connecting to adjacent polylines if one of it's end point is another one's starting point to make two short polylines a single polyline. My process was:

By these processes I made two dictionaries. After creating dictionaries, I check for if both dictionaries contain same point and in both dictionaries, that key has only one feature in feature list, then I created a new polyline with those two polylines and delete two short polylines.

yes! this is very similar to the technique that i used in my solution. my function also checks for membership to a "streamID", which may not be applicable to some projects.
–
ndimhypervolJan 14 '13 at 18:10

I try to avoid answering my own questions, but I came up with an arcpy solution. Kudos to kenbuja, who provided a good ArcObjects solution, but the question asked for an arcpy solution. To address PolyGeo, this particular solution does not require creating a topology, but simply accessing the Shape object. A topology-based solution may also work, but I've had trouble automating the corrections within the topology framework. I would love to see a topology solution though. Here's my cleanLineGeom function, which assumes that adjacent lines share a startPoint / endPoint combination.

This function was written for stream networks, hence the params "streamID" and "segID", but can be used for any polyline. "StreamID" refers to the line ID, while "segID" refers to the ID for smaller lines that share a line ID. The function will only merge short (length < lineClusterTolerance) line segments to longer (length > lineClusterTolerance) line segments that share a "streamID". Lastly, the output is a new feature class, so the input is not altered.

Notice -- there will be some short lines left untouched in the process. These are lines that had no longer lines adjacent. These lines are exceptions, and can simply be dissolved normally after the function is run.