I have lots of lines that I need to merge IF and only if they share the same polygon(s). All lines are made from the polygons (copy-paste into line) so they all snap.
In this little example line 499, 451, 447, 452 and 448 could be merged since they all share polygon 136 and none polygon on the outer side of the line.
Similarly line 442, 443 and 427 could be merged since they all share polygon 133 on the inner side and polygon 136 on the outer side.
My guess so far is to make points on the middle of each line (Features->Feature Vertices to Point and point type [MID]). And then do a Spatial Join where I use this new point layer as Target and my polygonlayer as Join [Join-One-To-Many]. Then I get a new feature that tells me the line-to-polygon relation; line = ORIG_FID and polygon JOIN_FID.
Line 427 is adjacent to polygon 133 and 136.
Line 443 is adjacent to polygon 133 and 136.
Line 447 is adjacent only to polygon 136.
And this is where I stop.
I now need a script that checks which polygon(s) each line is adjacent to and if two or more lines share exactly the same polygon or polygons then merge.
UPDATE:
I figured out that if I could make an unique ID on each link based on adjacent polygon(s) [Pol_ID], I could use this new link-ID to merge links with the same new_ID value. Here I made it simple and added the Pol_ID value(s) from adjacent polygon(s); see black values in figure.
New_ID 136 = Pol_ID value 136.
New_ID 269 = Pol_ID values 136+133.
So far so well, and I easily could merge all links with the same new_ID.
But then a new problem arose: the lines also have to be connected. Therefore the two lines with value 133 (blue arrows) shall not be merged since they are not connected to each other.
The missing part is now to: check if links with same new_ID value is connected, then merge the connected ones.
The red values represent the unique value [X-coordinate + Y-coordinate] to the start-/endpoint of the line. Thought I could use this value to check if lines were connected (share start/endpoint), but haven't figured it out yet..