Skip to content

Instantly share code, notes, and snippets.

@Khalefa
Last active August 29, 2015 13:57
Show Gist options
  • Save Khalefa/9882466 to your computer and use it in GitHub Desktop.
Save Khalefa/9882466 to your computer and use it in GitHub Desktop.
import math
from subprocess import call
import Image
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return (xtile, ytile)
def num2deg(xtile, ytile, zoom):
n = 2.0 ** zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
return (lat_deg, lon_deg)
def get_image(x,y,z):
s="http://localhost/osm/"+str(z)+"/"+str(x)+"/"+str(y)+".png"
ss="-O"+str(z)+"_"+str(x)+"_"+str(y)+".png"
call(["wget",s,ss])
z=14
dx=10
dy=10
out="out.png"
out_image = Image.new("RGB", (256*(2*dx+1), 256*(2*dy+1)))
lat=29.90650 #29.83269
lon=31.18373 #31.18578
(x,y)=deg2num(lon,lat,z)
for i in range(x-dx,x+dx):
for j in range(y-dy,y+dy):
get_image(i,j,z)
filename=str(z)+"_"+str(i)+"_"+str(j)+".png"
out_image.paste(Image.open(filename),(256*(i-(x-dx)),256*(j-(y-dy))))
call(["rm","-f",filename])
out_image.save(out)
(x1,y1)=num2deg(x-dx,y-dy,z)
(x2,y2)=num2deg(x+dx,y+dy,z)
print str(x1)+","+str(y1)+":"+str(x2)+","+str(y2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment