port to py3 and format with black
This commit is contained in:
parent
dffa71bbdd
commit
a4ced15215
1 changed files with 223 additions and 169 deletions
390
mapbender.py
390
mapbender.py
|
@ -20,56 +20,80 @@ from math import sqrt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
from scipy import interpolate
|
from scipy import interpolate
|
||||||
from itertools import tee, izip
|
from itertools import tee
|
||||||
from matplotlib.patches import Polygon
|
from matplotlib.patches import Polygon
|
||||||
from matplotlib.collections import PatchCollection
|
from matplotlib.collections import PatchCollection
|
||||||
import matplotlib
|
import matplotlib
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
def y2lat(a):
|
def y2lat(a):
|
||||||
return 180.0/math.pi*(2.0*math.atan(math.exp(a*math.pi/180.0))-math.pi/2.0)
|
return (
|
||||||
|
180.0
|
||||||
|
/ math.pi
|
||||||
|
* (2.0 * math.atan(math.exp(a * math.pi / 180.0)) - math.pi / 2.0)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def lat2y(a):
|
def lat2y(a):
|
||||||
return 180.0/math.pi*math.log(math.tan(math.pi/4.0+a*(math.pi/180.0)/2.0))
|
return (
|
||||||
|
180.0
|
||||||
|
/ math.pi
|
||||||
|
* math.log(math.tan(math.pi / 4.0 + a * (math.pi / 180.0) / 2.0))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def pairwise(iterable):
|
def pairwise(iterable):
|
||||||
"s -> (s0,s1), (s1,s2), (s2,s3), ..."
|
"s -> (s0,s1), (s1,s2), (s2,s3), ..."
|
||||||
a, b = tee(iterable, 2)
|
a, b = tee(iterable, 2)
|
||||||
next(b, None)
|
next(b, None)
|
||||||
return izip(a, b)
|
return zip(a, b)
|
||||||
|
|
||||||
|
|
||||||
def triplewise(iterable):
|
def triplewise(iterable):
|
||||||
"s -> (s0,s1,s2), (s1,s2,s3), (s2,s3,s4), ..."
|
"s -> (s0,s1,s2), (s1,s2,s3), (s2,s3,s4), ..."
|
||||||
a,b,c = tee(iterable, 3)
|
a, b, c = tee(iterable, 3)
|
||||||
next(b, None)
|
next(b, None)
|
||||||
next(c, None)
|
next(c, None)
|
||||||
next(c, None)
|
next(c, None)
|
||||||
return izip(a,b,c)
|
return zip(a, b, c)
|
||||||
|
|
||||||
|
|
||||||
# using barycentric coordinates
|
# using barycentric coordinates
|
||||||
def ptInTriangle(p, p0, p1, p2):
|
def ptInTriangle(p, p0, p1, p2):
|
||||||
A = 0.5 * (-p1[1] * p2[0] + p0[1] * (-p1[0] + p2[0]) + p0[0] * (p1[1] - p2[1]) + p1[0] * p2[1]);
|
A = 0.5 * (
|
||||||
sign = -1 if A < 0 else 1;
|
-p1[1] * p2[0]
|
||||||
s = (p0[1] * p2[0] - p0[0] * p2[1] + (p2[1] - p0[1]) * p[0] + (p0[0] - p2[0]) * p[1]) * sign;
|
+ p0[1] * (-p1[0] + p2[0])
|
||||||
t = (p0[0] * p1[1] - p0[1] * p1[0] + (p0[1] - p1[1]) * p[0] + (p1[0] - p0[0]) * p[1]) * sign;
|
+ p0[0] * (p1[1] - p2[1])
|
||||||
return s >= 0 and t >= 0 and (s + t) <= 2 * A * sign;
|
+ p1[0] * p2[1]
|
||||||
|
)
|
||||||
|
sign = -1 if A < 0 else 1
|
||||||
|
s = (
|
||||||
|
p0[1] * p2[0] - p0[0] * p2[1] + (p2[1] - p0[1]) * p[0] + (p0[0] - p2[0]) * p[1]
|
||||||
|
) * sign
|
||||||
|
t = (
|
||||||
|
p0[0] * p1[1] - p0[1] * p1[0] + (p0[1] - p1[1]) * p[0] + (p1[0] - p0[0]) * p[1]
|
||||||
|
) * sign
|
||||||
|
return s >= 0 and t >= 0 and (s + t) <= 2 * A * sign
|
||||||
|
|
||||||
|
|
||||||
def getxing(p0, p1, p2, p3):
|
def getxing(p0, p1, p2, p3):
|
||||||
ux = p1[0]-p0[0]
|
ux = p1[0] - p0[0]
|
||||||
uy = p1[1]-p0[1]
|
uy = p1[1] - p0[1]
|
||||||
vx = p2[0]-p3[0]
|
vx = p2[0] - p3[0]
|
||||||
vy = p2[1]-p3[1]
|
vy = p2[1] - p3[1]
|
||||||
# get multiplicity of u at which u meets v
|
# get multiplicity of u at which u meets v
|
||||||
a = vy*ux-vx*uy
|
a = vy * ux - vx * uy
|
||||||
if a == 0:
|
if a == 0:
|
||||||
# lines are parallel and never meet
|
# lines are parallel and never meet
|
||||||
return None
|
return None
|
||||||
s = (vy*(p3[0]-p0[0])+vx*(p0[1]-p3[1]))/a
|
s = (vy * (p3[0] - p0[0]) + vx * (p0[1] - p3[1])) / a
|
||||||
if 0.0 < s < 1.0:
|
if 0.0 < s < 1.0:
|
||||||
return (p0[0]+s*ux, p0[1]+s*uy)
|
return (p0[0] + s * ux, p0[1] + s * uy)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
# the line p0-p1 is the upper normal to the path
|
# the line p0-p1 is the upper normal to the path
|
||||||
# the line p2-p3 is the lower normal to the path
|
# the line p2-p3 is the lower normal to the path
|
||||||
#
|
#
|
||||||
|
@ -88,134 +112,138 @@ def ptInQuadrilateral(p, p0, p1, p2, p3):
|
||||||
else:
|
else:
|
||||||
return ptInTriangle(p, p0, p1, p2) or ptInTriangle(p, p2, p3, p0)
|
return ptInTriangle(p, p0, p1, p2) or ptInTriangle(p, p2, p3, p0)
|
||||||
|
|
||||||
def get_st(Ax,Ay,Bx,By,Cx,Cy,Dx,Dy,Xx,Xy):
|
|
||||||
d = Bx-Ax-Cx+Dx
|
def get_st(Ax, Ay, Bx, By, Cx, Cy, Dx, Dy, Xx, Xy):
|
||||||
e = By-Ay-Cy+Dy
|
d = Bx - Ax - Cx + Dx
|
||||||
l = Dx-Ax
|
e = By - Ay - Cy + Dy
|
||||||
g = Dy-Ay
|
l = Dx - Ax
|
||||||
h = Cx-Dx
|
g = Dy - Ay
|
||||||
m = Cy-Dy
|
h = Cx - Dx
|
||||||
i = Xx-Dx
|
m = Cy - Dy
|
||||||
j = Xy-Dy
|
i = Xx - Dx
|
||||||
n = g*h-m*l
|
j = Xy - Dy
|
||||||
|
n = g * h - m * l
|
||||||
# calculation for s
|
# calculation for s
|
||||||
a1 = m*d-h*e
|
a1 = m * d - h * e
|
||||||
b1 = n-j*d+i*e
|
b1 = n - j * d + i * e
|
||||||
c1 = l*j-g*i
|
c1 = l * j - g * i
|
||||||
# calculation for t
|
# calculation for t
|
||||||
a2 = g*d-l*e
|
a2 = g * d - l * e
|
||||||
b2 = n+j*d-i*e
|
b2 = n + j * d - i * e
|
||||||
c2 = h*j-m*i
|
c2 = h * j - m * i
|
||||||
s = []
|
s = []
|
||||||
if a1 == 0:
|
if a1 == 0:
|
||||||
s.append(-c1/b1)
|
s.append(-c1 / b1)
|
||||||
else:
|
else:
|
||||||
r1 = b1*b1-4*a1*c1
|
r1 = b1 * b1 - 4 * a1 * c1
|
||||||
if r1 >= 0:
|
if r1 >= 0:
|
||||||
r11 = (-b1+sqrt(r1))/(2*a1)
|
r11 = (-b1 + sqrt(r1)) / (2 * a1)
|
||||||
if -0.0000000001 <= r11 <= 1.0000000001:
|
if -0.0000000001 <= r11 <= 1.0000000001:
|
||||||
s.append(r11)
|
s.append(r11)
|
||||||
r12 = (-b1-sqrt(r1))/(2*a1)
|
r12 = (-b1 - sqrt(r1)) / (2 * a1)
|
||||||
if -0.0000000001 <= r12 <= 1.0000000001:
|
if -0.0000000001 <= r12 <= 1.0000000001:
|
||||||
s.append(r12)
|
s.append(r12)
|
||||||
t = []
|
t = []
|
||||||
if a2 == 0:
|
if a2 == 0:
|
||||||
t.append(-c2/b2)
|
t.append(-c2 / b2)
|
||||||
else:
|
else:
|
||||||
r2 = b2*b2-4*a2*c2
|
r2 = b2 * b2 - 4 * a2 * c2
|
||||||
if r2 >= 0:
|
if r2 >= 0:
|
||||||
r21 = (-b2+sqrt(r2))/(2*a2)
|
r21 = (-b2 + sqrt(r2)) / (2 * a2)
|
||||||
if -0.0000000001 <= r21 <= 1.0000000001:
|
if -0.0000000001 <= r21 <= 1.0000000001:
|
||||||
t.append(r21)
|
t.append(r21)
|
||||||
r22 = (-b2-sqrt(r2))/(2*a2)
|
r22 = (-b2 - sqrt(r2)) / (2 * a2)
|
||||||
if -0.0000000001 <= r22 <= 1.0000000001:
|
if -0.0000000001 <= r22 <= 1.0000000001:
|
||||||
t.append(r22)
|
t.append(r22)
|
||||||
if not s or not t:
|
if not s or not t:
|
||||||
return [],[]
|
return [], []
|
||||||
if len(s) == 1 and len(t) == 2:
|
if len(s) == 1 and len(t) == 2:
|
||||||
s = [s[0],s[0]]
|
s = [s[0], s[0]]
|
||||||
if len(s) == 2 and len(t) == 1:
|
if len(s) == 2 and len(t) == 1:
|
||||||
t = [t[0],t[0]]
|
t = [t[0], t[0]]
|
||||||
return s, t
|
return s, t
|
||||||
|
|
||||||
def main(x,y,width,smoothing,subdiv):
|
|
||||||
halfwidth = width/2.0
|
def main(x, y, width, smoothing, subdiv):
|
||||||
tck,u = interpolate.splprep([x,y],s=smoothing)
|
halfwidth = width / 2.0
|
||||||
unew = np.linspace(0,1.0,subdiv+1)
|
tck, u = interpolate.splprep([x, y], s=smoothing)
|
||||||
out = interpolate.splev(unew,tck)
|
unew = np.linspace(0, 1.0, subdiv + 1)
|
||||||
|
out = interpolate.splev(unew, tck)
|
||||||
heights = []
|
heights = []
|
||||||
offs = []
|
offs = []
|
||||||
height = 0.0
|
height = 0.0
|
||||||
for (ax,ay),(bx,by) in pairwise(zip(*out)):
|
for (ax, ay), (bx, by) in pairwise(list(zip(*out))):
|
||||||
s = ax-bx
|
s = ax - bx
|
||||||
t = ay-by
|
t = ay - by
|
||||||
l = sqrt(s*s+t*t)
|
l = sqrt(s * s + t * t)
|
||||||
offs.append(height)
|
offs.append(height)
|
||||||
height += l
|
height += l
|
||||||
heights.append(l)
|
heights.append(l)
|
||||||
# the border of the first segment is just perpendicular to the path
|
# the border of the first segment is just perpendicular to the path
|
||||||
cx = -out[1][1]+out[1][0]
|
cx = -out[1][1] + out[1][0]
|
||||||
cy = out[0][1]-out[0][0]
|
cy = out[0][1] - out[0][0]
|
||||||
cl = sqrt(cx*cx+cy*cy)/halfwidth
|
cl = sqrt(cx * cx + cy * cy) / halfwidth
|
||||||
dx = out[1][1]-out[1][0]
|
dx = out[1][1] - out[1][0]
|
||||||
dy = -out[0][1]+out[0][0]
|
dy = -out[0][1] + out[0][0]
|
||||||
dl = sqrt(dx*dx+dy*dy)/halfwidth
|
dl = sqrt(dx * dx + dy * dy) / halfwidth
|
||||||
px = [out[0][0]+cx/cl]
|
px = [out[0][0] + cx / cl]
|
||||||
py = [out[1][0]+cy/cl]
|
py = [out[1][0] + cy / cl]
|
||||||
qx = [out[0][0]+dx/dl]
|
qx = [out[0][0] + dx / dl]
|
||||||
qy = [out[1][0]+dy/dl]
|
qy = [out[1][0] + dy / dl]
|
||||||
for (ubx,uby),(ux,uy),(uax,uay) in triplewise(zip(*out)):
|
for (ubx, uby), (ux, uy), (uax, uay) in triplewise(list(zip(*out))):
|
||||||
# get adjacent line segment vectors
|
# get adjacent line segment vectors
|
||||||
ax = ux-ubx
|
ax = ux - ubx
|
||||||
ay = uy-uby
|
ay = uy - uby
|
||||||
bx = uax-ux
|
bx = uax - ux
|
||||||
by = uay-uy
|
by = uay - uy
|
||||||
# normalize length
|
# normalize length
|
||||||
al = sqrt(ax*ax+ay*ay)
|
al = sqrt(ax * ax + ay * ay)
|
||||||
bl = sqrt(bx*bx+by*by)
|
bl = sqrt(bx * bx + by * by)
|
||||||
ax = ax/al
|
ax = ax / al
|
||||||
ay = ay/al
|
ay = ay / al
|
||||||
bx = bx/bl
|
bx = bx / bl
|
||||||
by = by/bl
|
by = by / bl
|
||||||
# get vector perpendicular to sum
|
# get vector perpendicular to sum
|
||||||
cx = -ay-by
|
cx = -ay - by
|
||||||
cy = ax+bx
|
cy = ax + bx
|
||||||
cl = sqrt(cx*cx+cy*cy)/halfwidth
|
cl = sqrt(cx * cx + cy * cy) / halfwidth
|
||||||
px.append(ux+cx/cl)
|
px.append(ux + cx / cl)
|
||||||
py.append(uy+cy/cl)
|
py.append(uy + cy / cl)
|
||||||
# and in the other direction
|
# and in the other direction
|
||||||
dx = ay+by
|
dx = ay + by
|
||||||
dy = -ax-bx
|
dy = -ax - bx
|
||||||
dl = sqrt(dx*dx+dy*dy)/halfwidth
|
dl = sqrt(dx * dx + dy * dy) / halfwidth
|
||||||
qx.append(ux+dx/dl)
|
qx.append(ux + dx / dl)
|
||||||
qy.append(uy+dy/dl)
|
qy.append(uy + dy / dl)
|
||||||
# the border of the last segment is just perpendicular to the path
|
# the border of the last segment is just perpendicular to the path
|
||||||
cx = -out[1][-1]+out[1][-2]
|
cx = -out[1][-1] + out[1][-2]
|
||||||
cy = out[0][-1]-out[0][-2]
|
cy = out[0][-1] - out[0][-2]
|
||||||
cl = sqrt(cx*cx+cy*cy)/halfwidth
|
cl = sqrt(cx * cx + cy * cy) / halfwidth
|
||||||
dx = out[1][-1]-out[1][-2]
|
dx = out[1][-1] - out[1][-2]
|
||||||
dy = -out[0][-1]+out[0][-2]
|
dy = -out[0][-1] + out[0][-2]
|
||||||
dl = sqrt(dx*dx+dy*dy)/halfwidth
|
dl = sqrt(dx * dx + dy * dy) / halfwidth
|
||||||
px.append(out[0][-1]+cx/cl)
|
px.append(out[0][-1] + cx / cl)
|
||||||
py.append(out[1][-1]+cy/cl)
|
py.append(out[1][-1] + cy / cl)
|
||||||
qx.append(out[0][-1]+dx/dl)
|
qx.append(out[0][-1] + dx / dl)
|
||||||
qy.append(out[1][-1]+dy/dl)
|
qy.append(out[1][-1] + dy / dl)
|
||||||
quads = []
|
quads = []
|
||||||
patches = []
|
patches = []
|
||||||
for (p3x,p3y,p2x,p2y),(p0x,p0y,p1x,p1y) in pairwise(zip(px,py,qx,qy)):
|
for (p3x, p3y, p2x, p2y), (p0x, p0y, p1x, p1y) in pairwise(
|
||||||
quads.append(((p0x,p0y),(p1x,p1y),(p2x,p2y),(p3x,p3y)))
|
list(zip(px, py, qx, qy))
|
||||||
polygon = Polygon(((p0x,p0y),(p1x,p1y),(p2x,p2y),(p3x,p3y)), True)
|
):
|
||||||
|
quads.append(((p0x, p0y), (p1x, p1y), (p2x, p2y), (p3x, p3y)))
|
||||||
|
polygon = Polygon(((p0x, p0y), (p1x, p1y), (p2x, p2y), (p3x, p3y)), True)
|
||||||
patches.append(polygon)
|
patches.append(polygon)
|
||||||
containingquad = []
|
containingquad = []
|
||||||
for pt in zip(x,y):
|
for pt in zip(x, y):
|
||||||
# for each point, find the quadrilateral that contains it
|
# for each point, find the quadrilateral that contains it
|
||||||
found = []
|
found = []
|
||||||
for i,(p0,p1,p2,p3) in enumerate(quads):
|
for i, (p0, p1, p2, p3) in enumerate(quads):
|
||||||
if ptInQuadrilateral(pt,p0,p1,p2,p3):
|
if ptInQuadrilateral(pt, p0, p1, p2, p3):
|
||||||
found.append(i)
|
found.append(i)
|
||||||
if found:
|
if found:
|
||||||
if len(found) > 1:
|
if len(found) > 1:
|
||||||
print "point found in two quads"
|
print("point found in two quads")
|
||||||
return None
|
return None
|
||||||
containingquad.append(found[0])
|
containingquad.append(found[0])
|
||||||
else:
|
else:
|
||||||
|
@ -223,34 +251,44 @@ def main(x,y,width,smoothing,subdiv):
|
||||||
# check if the only points for which no quad could be found are in the
|
# check if the only points for which no quad could be found are in the
|
||||||
# beginning or in the end
|
# beginning or in the end
|
||||||
# find the first missing ones:
|
# find the first missing ones:
|
||||||
for i,q in enumerate(containingquad):
|
for i, q in enumerate(containingquad):
|
||||||
if q != None:
|
if q != None:
|
||||||
break
|
break
|
||||||
# find the last missing ones
|
# find the last missing ones
|
||||||
for j,q in izip(xrange(len(containingquad)-1, -1, -1), reversed(containingquad)):
|
for j, q in zip(range(len(containingquad) - 1, -1, -1), reversed(containingquad)):
|
||||||
if q != None:
|
if q != None:
|
||||||
break
|
break
|
||||||
# remove the first and last missing ones
|
# remove the first and last missing ones
|
||||||
if i != 0 or j != len(containingquad)-1:
|
if i != 0 or j != len(containingquad) - 1:
|
||||||
containingquad = containingquad[i:j+1]
|
containingquad = containingquad[i : j + 1]
|
||||||
x = x[i:j+1]
|
x = x[i : j + 1]
|
||||||
y = y[i:j+1]
|
y = y[i : j + 1]
|
||||||
# check if there are any remaining missing ones:
|
# check if there are any remaining missing ones:
|
||||||
if None in containingquad:
|
if None in containingquad:
|
||||||
print "cannot find quad for point"
|
print("cannot find quad for point")
|
||||||
return None
|
return None
|
||||||
for off,h in zip(offs,heights):
|
for off, h in zip(offs, heights):
|
||||||
targetquad = ((0,off+h),(width,off+h),(width,off),(0,off))
|
targetquad = ((0, off + h), (width, off + h), (width, off), (0, off))
|
||||||
patches.append(Polygon(targetquad,True))
|
patches.append(Polygon(targetquad, True))
|
||||||
tx = []
|
tx = []
|
||||||
ty = []
|
ty = []
|
||||||
assert len(containingquad) == len(x) == len(y)
|
assert len(containingquad) == len(x) == len(y)
|
||||||
assert len(out[0]) == len(out[1]) == len(px) == len(py) == len(qx) == len(qy) == len(quads)+1 == len(heights)+1 == len(offs)+1
|
assert (
|
||||||
for (rx,ry),i in zip(zip(x,y),containingquad):
|
len(out[0])
|
||||||
|
== len(out[1])
|
||||||
|
== len(px)
|
||||||
|
== len(py)
|
||||||
|
== len(qx)
|
||||||
|
== len(qy)
|
||||||
|
== len(quads) + 1
|
||||||
|
== len(heights) + 1
|
||||||
|
== len(offs) + 1
|
||||||
|
)
|
||||||
|
for (rx, ry), i in zip(list(zip(x, y)), containingquad):
|
||||||
if i == None:
|
if i == None:
|
||||||
continue
|
continue
|
||||||
(ax,ay),(bx,by),(cx,cy),(dx,dy) = quads[i]
|
(ax, ay), (bx, by), (cx, cy), (dx, dy) = quads[i]
|
||||||
s,t = get_st(ax,ay,bx,by,cx,cy,dx,dy,rx,ry)
|
s, t = get_st(ax, ay, bx, by, cx, cy, dx, dy, rx, ry)
|
||||||
# if more than one solution, take second
|
# if more than one solution, take second
|
||||||
# TODO: investigate if this is always the right solution
|
# TODO: investigate if this is always the right solution
|
||||||
if len(s) != 1 or len(t) != 1:
|
if len(s) != 1 or len(t) != 1:
|
||||||
|
@ -259,13 +297,13 @@ def main(x,y,width,smoothing,subdiv):
|
||||||
else:
|
else:
|
||||||
s = s[0]
|
s = s[0]
|
||||||
t = t[0]
|
t = t[0]
|
||||||
u = s*width
|
u = s * width
|
||||||
v = offs[i]+t*heights[i]
|
v = offs[i] + t * heights[i]
|
||||||
tx.append(u)
|
tx.append(u)
|
||||||
ty.append(v)
|
ty.append(v)
|
||||||
#sx = []
|
# sx = []
|
||||||
#sy = []
|
# sy = []
|
||||||
#for ((x1,y1),(x2,y2)),((ax,ay),(bx,by),(cx,cy),(dx,dy)),off,h in zip(pairwise(zip(*out)),quads,offs,heights):
|
# for ((x1,y1),(x2,y2)),((ax,ay),(bx,by),(cx,cy),(dx,dy)),off,h in zip(pairwise(zip(*out)),quads,offs,heights):
|
||||||
# s,t = get_st(ax,ay,bx,by,cx,cy,dx,dy,x1,y1)
|
# s,t = get_st(ax,ay,bx,by,cx,cy,dx,dy,x1,y1)
|
||||||
# if len(s) != 1 or len(t) != 1:
|
# if len(s) != 1 or len(t) != 1:
|
||||||
# return None
|
# return None
|
||||||
|
@ -283,74 +321,90 @@ def main(x,y,width,smoothing,subdiv):
|
||||||
# create map with
|
# create map with
|
||||||
# python -c 'import logging; logging.basicConfig(level=logging.DEBUG); from landez import ImageExporter; ie = ImageExporter(tiles_url="http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png"); ie.export_image(bbox=(8.0419921875,51.25160146817652,10.074462890625,54.03681240523652), zoomlevel=14, imagepath="image.png")'
|
# python -c 'import logging; logging.basicConfig(level=logging.DEBUG); from landez import ImageExporter; ie = ImageExporter(tiles_url="http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png"); ie.export_image(bbox=(8.0419921875,51.25160146817652,10.074462890625,54.03681240523652), zoomlevel=14, imagepath="image.png")'
|
||||||
im = Image.open("map.png")
|
im = Image.open("map.png")
|
||||||
bbox = [8.0419921875,51.25160146817652,10.074462890625,54.03681240523652]
|
bbox = [8.0419921875, 51.25160146817652, 10.074462890625, 54.03681240523652]
|
||||||
# apply mercator projection
|
# apply mercator projection
|
||||||
bbox[1] = lat2y(bbox[1])
|
bbox[1] = lat2y(bbox[1])
|
||||||
bbox[3] = lat2y(bbox[3])
|
bbox[3] = lat2y(bbox[3])
|
||||||
iw,ih = im.size
|
iw, ih = im.size
|
||||||
data = []
|
data = []
|
||||||
for i,(off,h,(p0,p1,p2,p3)) in enumerate(zip(offs,heights,quads)):
|
for i, (off, h, (p0, p1, p2, p3)) in enumerate(zip(offs, heights, quads)):
|
||||||
# first, account for the offset of the input image
|
# first, account for the offset of the input image
|
||||||
p0 = p0[0]-bbox[0],p0[1]-bbox[1]
|
p0 = p0[0] - bbox[0], p0[1] - bbox[1]
|
||||||
p1 = p1[0]-bbox[0],p1[1]-bbox[1]
|
p1 = p1[0] - bbox[0], p1[1] - bbox[1]
|
||||||
p2 = p2[0]-bbox[0],p2[1]-bbox[1]
|
p2 = p2[0] - bbox[0], p2[1] - bbox[1]
|
||||||
p3 = p3[0]-bbox[0],p3[1]-bbox[1]
|
p3 = p3[0] - bbox[0], p3[1] - bbox[1]
|
||||||
# PIL expects coordinates in counter clockwise order
|
# PIL expects coordinates in counter clockwise order
|
||||||
p1,p3 = p3,p1
|
p1, p3 = p3, p1
|
||||||
# x lon
|
# x lon
|
||||||
# ----- = -----
|
# ----- = -----
|
||||||
# w bbox[2]-bbox[0]
|
# w bbox[2]-bbox[0]
|
||||||
# translate to pixel coordinates
|
# translate to pixel coordinates
|
||||||
p0 = (iw*p0[0])/(bbox[2]-bbox[0]),(ih*p0[1])/(bbox[3]-bbox[1])
|
p0 = (iw * p0[0]) / (bbox[2] - bbox[0]), (ih * p0[1]) / (bbox[3] - bbox[1])
|
||||||
p1 = (iw*p1[0])/(bbox[2]-bbox[0]),(ih*p1[1])/(bbox[3]-bbox[1])
|
p1 = (iw * p1[0]) / (bbox[2] - bbox[0]), (ih * p1[1]) / (bbox[3] - bbox[1])
|
||||||
p2 = (iw*p2[0])/(bbox[2]-bbox[0]),(ih*p2[1])/(bbox[3]-bbox[1])
|
p2 = (iw * p2[0]) / (bbox[2] - bbox[0]), (ih * p2[1]) / (bbox[3] - bbox[1])
|
||||||
p3 = (iw*p3[0])/(bbox[2]-bbox[0]),(ih*p3[1])/(bbox[3]-bbox[1])
|
p3 = (iw * p3[0]) / (bbox[2] - bbox[0]), (ih * p3[1]) / (bbox[3] - bbox[1])
|
||||||
# PIL starts coordinate system at the upper left corner, swap y coord
|
# PIL starts coordinate system at the upper left corner, swap y coord
|
||||||
p0 = int(p0[0]),int(ih-p0[1])
|
p0 = int(p0[0]), int(ih - p0[1])
|
||||||
p1 = int(p1[0]),int(ih-p1[1])
|
p1 = int(p1[0]), int(ih - p1[1])
|
||||||
p2 = int(p2[0]),int(ih-p2[1])
|
p2 = int(p2[0]), int(ih - p2[1])
|
||||||
p3 = int(p3[0]),int(ih-p3[1])
|
p3 = int(p3[0]), int(ih - p3[1])
|
||||||
box=(0,int(ih*(height-off-h)/(bbox[3]-bbox[1])),
|
box = (
|
||||||
int(iw*width/(bbox[2]-bbox[0])),int(ih*(height-off)/(bbox[3]-bbox[1])))
|
0,
|
||||||
quad=(p0[0],p0[1],p1[0],p1[1],p2[0],p2[1],p3[0],p3[1])
|
int(ih * (height - off - h) / (bbox[3] - bbox[1])),
|
||||||
data.append((box,quad))
|
int(iw * width / (bbox[2] - bbox[0])),
|
||||||
im_out = im.transform((int(iw*width/(bbox[2]-bbox[0])),int(ih*height/(bbox[3]-bbox[1]))),Image.MESH,data,Image.BICUBIC)
|
int(ih * (height - off) / (bbox[3] - bbox[1])),
|
||||||
|
)
|
||||||
|
quad = (p0[0], p0[1], p1[0], p1[1], p2[0], p2[1], p3[0], p3[1])
|
||||||
|
data.append((box, quad))
|
||||||
|
im_out = im.transform(
|
||||||
|
(int(iw * width / (bbox[2] - bbox[0])), int(ih * height / (bbox[3] - bbox[1]))),
|
||||||
|
Image.MESH,
|
||||||
|
data,
|
||||||
|
Image.BICUBIC,
|
||||||
|
)
|
||||||
im_out.save("out.png")
|
im_out.save("out.png")
|
||||||
#np.random.seed(seed=0)
|
|
||||||
#colors = 100*np.random.rand(len(patches)/2)+100*np.random.rand(len(patches)/2)
|
# np.random.seed(seed=0)
|
||||||
#p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
|
# colors = 100*np.random.rand(len(patches)//2)+100*np.random.rand(len(patches)//2)
|
||||||
#p.set_array(np.array(colors))
|
# p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
|
||||||
#plt.figure()
|
# p.set_array(np.array(colors))
|
||||||
#plt.axes().set_aspect('equal')
|
# plt.figure()
|
||||||
|
# plt.axes().set_aspect('equal')
|
||||||
##plt.axhspan(0, height, xmin=0, xmax=width)
|
##plt.axhspan(0, height, xmin=0, xmax=width)
|
||||||
#fig, ax = plt.subplots()
|
# fig, ax = plt.subplots()
|
||||||
##ax.add_collection(p)
|
##ax.add_collection(p)
|
||||||
#ax.set_aspect('equal')
|
# ax.set_aspect('equal')
|
||||||
#plt.axis((0,width,0,height))
|
# plt.axis((0,width,0,height))
|
||||||
#plt.imshow(np.asarray(im_out),extent=[0,width,0,height])
|
# plt.imshow(np.asarray(im_out),extent=[0,width,0,height])
|
||||||
#plt.imshow(np.asarray(im),extent=[bbox[0],bbox[2],bbox[1],bbox[3]])
|
# plt.imshow(np.asarray(im),extent=[bbox[0],bbox[2],bbox[1],bbox[3]])
|
||||||
#plt.plot(x,y,out[0],out[1],px,py,qx,qy,tx,ty)
|
# plt.plot(x,y,out[0],out[1],px,py,qx,qy,tx,ty)
|
||||||
#plt.show()
|
# plt.show()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
x = []
|
x = []
|
||||||
y = []
|
y = []
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if len(sys.argv) != 5:
|
if len(sys.argv) != 5:
|
||||||
print "usage: %s data.csv width smoothing N"%sys.argv[0]
|
print("usage: %s data.csv width smoothing N" % sys.argv[0])
|
||||||
print ""
|
print("")
|
||||||
print " data.csv whitespace delimited lon/lat pairs of points along the path"
|
print(
|
||||||
print " width width of the resulting map in degrees"
|
" data.csv whitespace delimited lon/lat pairs of points along the path"
|
||||||
print " smoothing curve smoothing from 0 (exact fit) to higher values (looser fit)"
|
)
|
||||||
print " N amount of quads to split the path into"
|
print(" width width of the resulting map in degrees")
|
||||||
print ""
|
print(
|
||||||
print " example usage:"
|
" smoothing curve smoothing from 0 (exact fit) to higher values (looser fit)"
|
||||||
print " %s Weser-Radweg-Hauptroute.csv 0.286 6 20"%sys.argv[0]
|
)
|
||||||
|
print(" N amount of quads to split the path into")
|
||||||
|
print("")
|
||||||
|
print(" example usage:")
|
||||||
|
print(" %s Weser-Radweg-Hauptroute.csv 0.286 6 20" % sys.argv[0])
|
||||||
exit(1)
|
exit(1)
|
||||||
with open(sys.argv[1]) as f:
|
with open(sys.argv[1]) as f:
|
||||||
for l in f:
|
for l in f:
|
||||||
a,b = l.split()
|
a, b = l.split()
|
||||||
# apply mercator projection
|
# apply mercator projection
|
||||||
b = lat2y(float(b))
|
b = lat2y(float(b))
|
||||||
x.append(float(a))
|
x.append(float(a))
|
||||||
|
@ -358,8 +412,8 @@ if __name__ == '__main__':
|
||||||
width = float(sys.argv[2])
|
width = float(sys.argv[2])
|
||||||
smoothing = float(sys.argv[3])
|
smoothing = float(sys.argv[3])
|
||||||
N = int(sys.argv[4])
|
N = int(sys.argv[4])
|
||||||
main(x,y,width,smoothing,N)
|
main(x, y, width, smoothing, N)
|
||||||
#for smoothing in [1,2,4,8,12]:
|
# for smoothing in [1,2,4,8,12]:
|
||||||
# for subdiv in range(10,30):
|
# for subdiv in range(10,30):
|
||||||
# if main(x,y,width,smoothing,subdiv):
|
# if main(x,y,width,smoothing,subdiv):
|
||||||
# print width,smoothing,subdiv
|
# print width,smoothing,subdiv
|
||||||
|
|
Loading…
Reference in a new issue