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

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

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

Line 
1# dissolve.py
2#
3# Demonstrate how Shapely can be used to build up a collection of patches by
4# dissolving circular regions and how Shapely supports plotting of the results.
5
6from functools import partial
7import random
8
9import pylab
10
11from shapely.geometry import Point
12from shapely.ops import cascaded_union
13
14# Use a partial function to make 100 points uniformly distributed in a 40x40
15# box centered on 0,0.
16r = partial(random.uniform, -20.0, 20.0)
17points = [Point(r(), r()) for i in range(100)]
18
19# Buffer the points, producing 100 polygon spots
20spots = [p.buffer(2.5) for p in points]
21
22# Perform a cascaded union of the polygon spots, dissolving them into a
23# collection of polygon patches
24patches = cascaded_union(spots)
25
26if __name__ == "__main__":
27    # Illustrate the results using matplotlib's pylab interface
28    pylab.figure(num=None, figsize=(4, 4), dpi=180)
29   
30    for patch in patches.geoms:
31        assert patch.geom_type in ['Polygon']
32        assert patch.is_valid
33   
34        # Fill and outline each patch
35        x, y = patch.exterior.xy
36        pylab.fill(x, y, color='#cccccc', aa=True) 
37        pylab.plot(x, y, color='#666666', aa=True, lw=1.0)
38   
39        # Do the same for the holes of the patch
40        for hole in patch.interiors:
41            x, y = hole.xy
42            pylab.fill(x, y, color='#ffffff', aa=True) 
43            pylab.plot(x, y, color='#999999', aa=True, lw=1.0)
44   
45    # Plot the original points
46    pylab.plot([p.x for p in points], [p.y for p in points], 'b,', alpha=0.75)
47   
48    # Write the number of patches and the total patch area to the figure
49    pylab.text(-25, 25, 
50        "Patches: %d, total area: %.2f" % (len(patches.geoms), patches.area))
51   
52    pylab.savefig('dissolve.png')
53   
Note: See TracBrowser for help on using the browser.