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

Revision 1545, 1.6 KB (checked in by seang, 5 weeks ago)

Add 'xy' property to coordinate sequences and some geometries. Provides read-
only access to separate x and y coordinate value arrays. Much better for use
with matplotlib and numpy.

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
26# Illustrate the results using matplotlib's pylab interface
27pylab.figure(num=None, figsize=(4, 4), dpi=180)
28
29for patch in patches.geoms:
30    assert patch.geom_type in ['Polygon']
31    assert patch.is_valid
32
33    # Fill and outline each patch
34    x, y = patch.exterior.xy
35    pylab.fill(x, y, color='#cccccc', aa=True) 
36    pylab.plot(x, y, color='#666666', aa=True, lw=1.0)
37
38    # Do the same for the holes of the patch
39    for hole in patch.interiors:
40        x, y = hole.xy
41        pylab.fill(x, y, color='#ffffff', aa=True) 
42        pylab.plot(x, y, color='#999999', aa=True, lw=1.0)
43
44# Plot the original points
45pylab.plot([p.x for p in points], [p.y for p in points], 'b,', alpha=0.75)
46
47# Write the number of patches and the total patch area to the figure
48pylab.text(-25, 25, 
49    "Patches: %d, total area: %.2f" % (len(patches.geoms), patches.area))
50
51pylab.savefig('dissolve.png')
52
Note: See TracBrowser for help on using the browser.