Two articles that you would likely be interested in, as they have similar motivations to yours:
Limits of Predictability in Human Mobility
by: Chaoming Song, Zehui Qu, Nicholas Blumm, Albert-László Barabási.
Science, Vol. 327, No. 5968. (19 February 2010), pp. 1018-1021.
Understanding individual human mobility patterns
by: Marta C. Gonzalez, Cesar A. Hidalgo, Albert-Laszlo Barabasi.
Nature, Vol. 453, No. 7196. (05 June 2008), pp. 779-782.
Note the two studies use the same data, which is similar to yours but not at the level of precision in space or time. I don't think what I would describe what you want to find as a trajectory, but I'm not sure what I would call it either. Why exactly do you want to cluster the beginning/end nodes of your "trajectories".
Shapely Polygon
object has attribute exterior
. Shapely MultiPolygon
object has Polygon
object sequence. You should iterate over those polygons. You can do that using attribute geoms
of MultiPolygon
.
Use this way:
import shapely.geometry as sg
import shapely.ops as so
import matplotlib.pyplot as plt
r1 = sg.Polygon([(0,0),(0,1),(1,1),(1,0),(0,0)])
r2 = sg.box(0.5,0.5,1.5,1.5)
r3 = sg.box(4,4,5,5)
new_shape = so.cascaded_union([r1, r2, r3])
fig, axs = plt.subplots()
axs.set_aspect('equal', 'datalim')
for geom in new_shape.geoms:
xs, ys = geom.exterior.xy
axs.fill(xs, ys, alpha=0.5, fc='r', ec='none')
plt.show()
Best Answer
If I use your first example matplotlib - extracting data from contour lines
The result is:
The number of elements (lines) is given by:
and the result you want is the area of one of the polygons (with
contourf()
: 7 polygons)In fact, the xy list determined by:
are the coordinates of the exterior LinearRings of the coloured Polygons. So
Plot of the Polygon 4
and the result is given by
poly.area
But there are other solutions as in matplotlib - users: pyplot: Extract contourset without plotting or stackoverflow : Python: find contour lines from matplotlib.pyplot.contour() with the undocumented module matplotlib._cntr without plotting anything.