Skip to content

Commit ca7222e

Browse files
author
thiago
committed
First commit
1 parent a90741a commit ca7222e

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

rotate.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from matplotlib import pyplot
2+
from shapely.geometry import LineString
3+
from shapely import affinity
4+
5+
#from figures import SIZE, BLUE, GRAY
6+
7+
GRAY='#858a8c'
8+
BLUE='#00b4fc'
9+
def add_origin(ax, geom, origin):
10+
x, y = xy = affinity.interpret_origin(geom, origin, 2)
11+
ax.plot(x, y, 'o', color=GRAY, zorder=1)
12+
ax.annotate(str(xy), xy=xy, ha='center',
13+
textcoords='offset points', xytext=(0, 8))
14+
15+
16+
def plot_line(ax, ob, color):
17+
x, y = ob.xy
18+
ax.plot(x, y, color=color, alpha=0.7, linewidth=3,
19+
solid_capstyle='round', zorder=2)
20+
21+
fig = pyplot.figure(1, dpi=90)
22+
23+
line = LineString([(1, 3), (1, 1), (4, 1)])
24+
25+
xrange = [0, 5]
26+
yrange = [0, 4]
27+
28+
# 1
29+
ax = fig.add_subplot(121)
30+
31+
plot_line(ax, line, GRAY)
32+
plot_line(ax, affinity.rotate(line, 90, 'center'), BLUE)
33+
add_origin(ax, line, 'center')
34+
35+
ax.set_title(u"90\N{DEGREE SIGN}, default origin (center)")
36+
37+
ax.set_xlim(*xrange)
38+
ax.set_xticks(range(*xrange) + [xrange[-1]])
39+
ax.set_ylim(*yrange)
40+
ax.set_yticks(range(*yrange) + [yrange[-1]])
41+
ax.set_aspect(1)
42+
43+
# 2
44+
ax = fig.add_subplot(122)
45+
46+
plot_line(ax, line, GRAY)
47+
plot_line(ax, affinity.rotate(line, 90, 'centroid'), BLUE)
48+
add_origin(ax, line, 'centroid')
49+
50+
ax.set_title(u"90\N{DEGREE SIGN}, origin='centroid'")
51+
52+
ax.set_xlim(*xrange)
53+
ax.set_xticks(range(*xrange) + [xrange[-1]])
54+
ax.set_ylim(*yrange)
55+
ax.set_yticks(range(*yrange) + [yrange[-1]])
56+
ax.set_aspect(1)
57+
58+
pyplot.show()

skew.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from matplotlib import pyplot
2+
from shapely.wkt import loads as load_wkt
3+
from shapely import affinity
4+
from descartes.patch import PolygonPatch
5+
6+
#from figures import SIZE, BLUE, GRAY
7+
GRAY='#858a8c'
8+
BLUE='#00b4fc'
9+
10+
def add_origin(ax, geom, origin):
11+
x, y = xy = affinity.interpret_origin(geom, origin, 2)
12+
ax.plot(x, y, 'o', color=GRAY, zorder=1)
13+
ax.annotate(str(xy), xy=xy, ha='center',
14+
textcoords='offset points', xytext=(0, 8))
15+
16+
fig = pyplot.figure(1, dpi=90)
17+
18+
# Geometry from JTS TestBuilder with fixed precision model of 100.0
19+
# Using CreateShape > FontGlyphSanSerif and A = triangle.wkt from scale.py
20+
R = load_wkt('''\
21+
POLYGON((2.218 2.204, 2.273 2.18, 2.328 2.144, 2.435 2.042, 2.541 1.895,
22+
2.647 1.702, 3 1, 2.626 1, 2.298 1.659, 2.235 1.777, 2.173 1.873,
23+
2.112 1.948, 2.051 2.001, 1.986 2.038, 1.91 2.064, 1.823 2.08, 1.726 2.085,
24+
1.347 2.085, 1.347 1, 1 1, 1 3.567, 1.784 3.567, 1.99 3.556, 2.168 3.521,
25+
2.319 3.464, 2.441 3.383, 2.492 3.334, 2.536 3.279, 2.604 3.152,
26+
2.644 3.002, 2.658 2.828, 2.651 2.712, 2.63 2.606, 2.594 2.51, 2.545 2.425,
27+
2.482 2.352, 2.407 2.29, 2.319 2.241, 2.218 2.204),
28+
(1.347 3.282, 1.347 2.371, 1.784 2.371, 1.902 2.378, 2.004 2.4, 2.091 2.436,
29+
2.163 2.487, 2.219 2.552, 2.259 2.63, 2.283 2.722, 2.291 2.828, 2.283 2.933,
30+
2.259 3.025, 2.219 3.103, 2.163 3.167, 2.091 3.217, 2.004 3.253, 1.902 3.275,
31+
1.784 3.282, 1.347 3.282))''')
32+
33+
xrange = [0, 5]
34+
yrange = [0, 4]
35+
36+
# 1
37+
ax = fig.add_subplot(121)
38+
39+
patch1a = PolygonPatch(R, facecolor=GRAY, edgecolor=GRAY,
40+
alpha=0.5, zorder=1)
41+
skewR = affinity.skew(R, xs=20, origin=(1, 1))
42+
patch1b = PolygonPatch(skewR, facecolor=BLUE, edgecolor=BLUE,
43+
alpha=0.5, zorder=2)
44+
ax.add_patch(patch1a)
45+
ax.add_patch(patch1b)
46+
47+
add_origin(ax, R, (1, 1))
48+
49+
ax.set_title("a) xs=20, origin(1, 1)")
50+
51+
ax.set_xlim(*xrange)
52+
ax.set_xticks(range(*xrange) + [xrange[-1]])
53+
ax.set_ylim(*yrange)
54+
ax.set_yticks(range(*yrange) + [yrange[-1]])
55+
ax.set_aspect(1)
56+
57+
# 2
58+
ax = fig.add_subplot(122)
59+
60+
patch2a = PolygonPatch(R, facecolor=GRAY, edgecolor=GRAY,
61+
alpha=0.5, zorder=1)
62+
skewR = affinity.skew(R, ys=30)
63+
patch2b = PolygonPatch(skewR, facecolor=BLUE, edgecolor=BLUE,
64+
alpha=0.5, zorder=2)
65+
ax.add_patch(patch2a)
66+
ax.add_patch(patch2b)
67+
68+
add_origin(ax, R, 'center')
69+
70+
ax.set_title("b) ys=30")
71+
72+
ax.set_xlim(*xrange)
73+
ax.set_xticks(range(*xrange) + [xrange[-1]])
74+
ax.set_ylim(*yrange)
75+
ax.set_yticks(range(*yrange) + [yrange[-1]])
76+
ax.set_aspect(1)
77+
78+
pyplot.show()

union.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from matplotlib import pyplot
2+
from shapely.geometry import Point
3+
from descartes import PolygonPatch
4+
5+
#from figures import SIZE, BLUE, GRAY
6+
7+
fig = pyplot.figure(1, dpi=90)
8+
9+
a = Point(1, 1).buffer(1.5)
10+
b = Point(2, 1).buffer(1.5)
11+
GRAY='#858a8c'
12+
BLUE='#00b4fc'
13+
14+
# 1
15+
ax = fig.add_subplot(121)
16+
17+
patch1 = PolygonPatch(a, fc=GRAY, ec=GRAY, alpha=0.2, zorder=1)
18+
ax.add_patch(patch1)
19+
patch2 = PolygonPatch(b, fc=GRAY, ec=GRAY, alpha=0.2, zorder=1)
20+
ax.add_patch(patch2)
21+
c = a.union(b)
22+
patchc = PolygonPatch(c, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2)
23+
ax.add_patch(patchc)
24+
25+
ax.set_title('a.union(b)')
26+
27+
xrange = [-1, 4]
28+
yrange = [-1, 3]
29+
ax.set_xlim(*xrange)
30+
ax.set_xticks(range(*xrange) + [xrange[-1]])
31+
ax.set_ylim(*yrange)
32+
ax.set_yticks(range(*yrange) + [yrange[-1]])
33+
ax.set_aspect(1)
34+
35+
def plot_line(ax, ob, color=GRAY):
36+
x, y = ob.xy
37+
ax.plot(x, y, color, linewidth=3, solid_capstyle='round', zorder=1)
38+
39+
#2
40+
ax = fig.add_subplot(122)
41+
42+
plot_line(ax, a.exterior)
43+
plot_line(ax, b.exterior)
44+
45+
u = a.exterior.union(b.exterior)
46+
if u.geom_type in ['LineString', 'LinearRing', 'Point']:
47+
plot_line(ax, u, color=BLUE)
48+
elif u.geom_type is 'MultiLineString':
49+
for p in u:
50+
plot_line(ax, p, color=BLUE)
51+
52+
ax.set_title('a.boundary.union(b.boundary)')
53+
54+
xrange = [-1, 4]
55+
yrange = [-1, 3]
56+
ax.set_xlim(*xrange)
57+
ax.set_xticks(range(*xrange) + [xrange[-1]])
58+
ax.set_ylim(*yrange)
59+
ax.set_yticks(range(*yrange) + [yrange[-1]])
60+
ax.set_aspect(1)
61+
62+
pyplot.show()

0 commit comments

Comments
 (0)