# Why does polygon.intersection(complex_linestring) split the linestring and mess up Z?

I have a complex (self crossing) line with Z values which I'd like to truncate to the area inside a box. The problem is Shapely splits the line into simple (non-crossing) sections and the Z value at the intersection of the loop is incorrect.

My high-level goal is taking a pile of GPS tracks, trimming them to only include the area in a box and calculate speed within the box. I'm using Z to represent seconds. I suspect this problem is deep in geos. Can you suggest a better approach?

I made a pretty picture of this example at http://i.stack.imgur.com/pQxtH.png but it may not work since I'm new here.

from shapely.geometry import LineString
import shapely.geometry
box = shapely.geometry.box(1, 1, 4, 5)
line = LineString([(2, 0, 0), (2, 4, 4), (3, 4, 5), (3, 3, 6), (0, 3, 9)])
[[p for p in l.coords] for l in line.intersection(box)]

results in:

[[(2.0, 1.0, 1.0), (2.0, 3.0, 5.0)],
[(2.0, 3.0, 5.0), (2.0, 4.0, 4.0), (3.0, 4.0, 5.0), (3.0, 3.0, 6.0), (2.0, 3.0, 5.0)],
[(2.0, 3.0, 5.0), (1.0, 3.0, 8.0)]]

but I was expecting only this segment:

[(2,1,1), (2,4,4), (3,4,5), (3,3,6), (1,3,8)]

shapely.__version__
'1.2.17'
shapely.geos.geos_version_string
'3.2.2-CAPI-1.6.2'
-