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?
|
|
|||||||
|
|
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)
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. Here's the code for that section
Part of the code I didn't include copies the original feature class into a geodatabase, ensuring that the Shape_Area field exists and is automatically calculated. |
|||||||
|
|
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. |
|||
|
|
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.
|
||||
|
|
