root/Shapely/branches/1.2/examples/intersect.py

Revision 1597, 2.6 KB (checked in by seang, 5 months ago)

Commit sphinx and matplotlib extensions, convert a figure over to the plot
directive.

Line 
1# intersect.py
2#
3# Demonstrate how Shapely can be used to analyze and plot the intersection of
4# a trajectory and regions in space.
5
6from functools import partial
7import random
8
9import pylab
10
11from shapely.geometry import LineString, Point
12from shapely.ops import cascaded_union
13
14# Build patches as in dissolved.py
15r = partial(random.uniform, -20.0, 20.0)
16points = [Point(r(), r()) for i in range(100)]
17spots = [p.buffer(2.5) for p in points]
18patches = cascaded_union(spots)
19
20# Represent the following geolocation parameters
21#
22# initial position: -25, -25
23# heading: 45.0
24# speed: 50*sqrt(2)
25#
26# as a line
27vector = LineString(((-25.0, -25.0), (25.0, 25.0)))
28
29# Find intercepted and missed patches. List the former so we can count them
30# later
31intercepts = [patch for patch in patches.geoms if vector.intersects(patch)]
32misses = (patch for patch in patches.geoms if not vector.intersects(patch))
33
34# Plot the intersection
35intersection = vector.intersection(patches)
36assert intersection.geom_type in ['MultiLineString']
37
38if __name__ == "__main__":
39    # Illustrate the results using matplotlib's pylab interface
40    pylab.figure(num=None, figsize=(4, 4), dpi=180)
41   
42    # Plot the misses
43    for spot in misses:
44        x, y = spot.exterior.xy
45        pylab.fill(x, y, color='#cccccc', aa=True) 
46        pylab.plot(x, y, color='#999999', aa=True, lw=1.0)
47   
48        # Do the same for the holes of the patch
49        for hole in spot.interiors:
50            x, y = hole.xy
51            pylab.fill(x, y, color='#ffffff', aa=True) 
52            pylab.plot(x, y, color='#999999', aa=True, lw=1.0)
53   
54    # Plot the intercepts
55    for spot in intercepts:
56        x, y = spot.exterior.xy
57        pylab.fill(x, y, color='red', alpha=0.25, aa=True) 
58        pylab.plot(x, y, color='red', alpha=0.5, aa=True, lw=1.0)
59   
60        # Do the same for the holes of the patch
61        for hole in spot.interiors:
62            x, y = hole.xy
63            pylab.fill(x, y, color='#ffffff', aa=True) 
64            pylab.plot(x, y, color='red', alpha=0.5, aa=True, lw=1.0)
65   
66    # Draw the projected trajectory
67    pylab.arrow(-25, -25, 50, 50, color='#999999', aa=True,
68        head_width=1.0, head_length=1.0)
69   
70    for segment in intersection.geoms:
71        x, y = segment.xy
72        pylab.plot(x, y, color='red', aa=True, lw=1.5)
73   
74    # Write the number of patches and the total patch area to the figure
75    pylab.text(-28, 25, 
76        "Patches: %d/%d (%d), total length: %.1f" \
77         % (len(intercepts), len(patches.geoms), 
78            len(intersection.geoms), intersection.length))
79   
80    pylab.savefig('intersect.png')
81   
Note: See TracBrowser for help on using the browser.