Yandex Map downloader

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.
Note: posts here are not necessarily endorsed by the community, and may represent amateur or even bad practices.

Yandex Map downloader

Postby scx » Thu Jul 11, 2013 10:35 am

requirements:
python 2.7.4
Python Imaging Library 1.1.7 for Python 2.7 http://www.pythonware.com/products/pil/
mtTkinter http://tkinter.unpythonic.net/wiki/mtTkinter?action=AttachFile&do=view&target=mtTkinter-0.4.tar.gz



Code: Select all
from mtTkinter import *
import ttk
import urllib2
from PIL import ImageTk, Image
import cStringIO
import time
import os
from tkFileDialog   import asksaveasfile     

class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

width=1024
height=768



liste = []
liste.append("&lang=ru_RU")
liste.append("&lang=tr_TR")
liste.append("&lang=uk_UA")
liste.append("&lang=en_US")

root = Tk()

combo3 = ttk.Combobox(root,  state='readonly')

combo3['values'] = liste
combo3.set(liste[3])
url_language=combo3.get()



dzoom=6
thread_value=8
mx=0
my=0
gmx=-10
gmy=0
tgmx=0
tgmy=0
timer_basla_durdurucu=0
sec=[Point(0,0),Point(width,height)]
murl=['http://sat00.maps.yandex.net/tiles?l=sat&v=3.57.0&x='
      ,'http://sat01.maps.yandex.net/tiles?l=sat&v=3.57.0&x='
      ,'http://sat02.maps.yandex.net/tiles?l=sat&v=3.57.0&x='
      ,'http://sat03.maps.yandex.net/tiles?l=sat&v=3.57.0&x='
      ,'http://sat04.maps.yandex.net/tiles?l=sat&v=3.57.0&x=']

imgs=Image.new("RGB",(0,0), "black")

murl_thread=0




width_s=int(width/256)+1
height_s=int(height/256)+1
timer_basla=1
threads = []
tut_secim=[0,0,0,0]
t=0
yx=int(16)
yy=int(9)
yz=5
yandex_load_calisiyor=[]



panel = Canvas(root,width = width, height = height, bg = 'yellow')



for row in range(width_s+2):
    yandex_load_calisiyor.append([])
    for column in range(height_s+2):
        yandex_load_calisiyor[row].append(0)
        threads.append(0)







img_parca=[]
img_parca_x=[]
img_parca_y=[]
img_parca_tx=[]
img_parca_ty=[]
for row in range(width_s+2):
    img_parca.append([])
    img_parca_x.append([])
    img_parca_y.append([])
    img_parca_tx.append([])
    img_parca_ty.append([])
    for column in range(height_s+2):
        img_parca[row].append(0)
        img_parca_x[row].append(0)
        img_parca_y[row].append(0)
        img_parca_tx[row].append(1)
        img_parca_ty[row].append(1)

imgy = Image.new("RGB", (width,height), "white")

def mousedown(event):

    global mx,my,timer_basla
    timer_basla=1
    mx=event.x
    my=event.y
def mouseup(event):
    global mx,my,timer_basla
    timer_basla=0
    yukle_yandex()
    time.sleep(.2)
    imaj_parca_yapistir()

def point(event):
    global mx,my,gmx,gmy

    fx=event.x-mx
    fy=event.y-my
    sec[0].x+=fx
    sec[0].y+=fy
    sec[1].x+=fx
    sec[1].y+=fy
    gmx+=fx
    gmy+=fy
    mx=event.x
    my=event.y
    imaj_parca_yapistir()
    return

def point3(event):
    global sec,tut_secim
    if(tut_secim[0]==1):sec[0].x=event.x;sec[0].y=event.y;imaj_parca_yapistir();return
    if(tut_secim[1]==1):sec[0].x=event.x;sec[1].y=event.y;imaj_parca_yapistir();return
    if(tut_secim[2]==1):sec[1].x=event.x;sec[0].y=event.y;imaj_parca_yapistir();return
    if(tut_secim[3]==1):sec[1].x=event.x;sec[1].y=event.y;imaj_parca_yapistir();return

    if(abs(sec[0].x-event.x)<10 and abs(sec[0].y-event.y)<10 ):tut_secim[0]=1;return
    if(abs(sec[0].x-event.x)<10 and abs(sec[1].y-event.y)<10 ):tut_secim[1]=1;return
    if(abs(sec[1].x-event.x)<10 and abs(sec[0].y-event.y)<10 ):tut_secim[2]=1;return
    if(abs(sec[1].x-event.x)<10 and abs(sec[1].y-event.y)<10 ):tut_secim[3]=1;return

    sec[0].x=event.x
    sec[0].y=event.y
    sec[1].x=event.x
    sec[1].y=event.y

    return

def mouseup3(event):
    global tut_secim
    tut_secim[0]=0
    tut_secim[1]=0
    tut_secim[2]=0
    tut_secim[3]=0
    imaj_parca_yapistir()

def yandex_load(x,y,hx,hy):
    global yandex_load_calisiyor,gmx,gmy,yy,yx,yz,sec,murl_thread,combo3
    if(yandex_load_calisiyor[x][y]==1):threads[x*width_s+y]._Thread__stop()
    yandex_load_calisiyor[x][y]=1;

    url=murl[murl_thread]+str(yx+hx)+'&y='+str(yy+hy)+'&z='+str(yz)+url_language
   
    murl_thread+=1
    if(murl_thread>4):murl_thread=0


    file =urllib2.urlopen(url,timeout = 5)
    time.sleep(0.05)
    img_parca[x][y] = Image.open(cStringIO.StringIO(file.read()))
    file.close()

    yandex_load_calisiyor[x][y]=0;


def yukle_yandex2():
    global mx,my,gmx,gmy,img_parca_x,img_parca_y,t,threads
    for x in range(0, width_s+1):
        for y in range(0, height_s+1):
            ox=int((-gmx+img_parca_x[x][y])/256.0)
            oy=int((-gmy+img_parca_y[x][y])/256.0)
            img_parca[x][y]=0
            threads[x*width_s+y] = threading.Thread(target=yandex_load,args=(x,y,int(ox),int(oy)))
            threads[x*width_s+y].start()

            img_parca_tx[x][y]=ox
            img_parca_ty[x][y]=oy

def yukle_yandex():
    global mx,my,gmx,gmy,img_parca_x,img_parca_y,t,threads
    for x in range(0, width_s+1):
        for y in range(0, height_s+1):
            ox=int((-gmx+img_parca_x[x][y])/256.0)
            oy=int((-gmy+img_parca_y[x][y])/256.0)
            if (ox!=img_parca_tx[x][y] or oy!=img_parca_ty[x][y]):
                img_parca[x][y]=0
                threads[x*width_s+y] = threading.Thread(target=yandex_load,args=(x,y,int(ox),int(oy)))
                threads[x*width_s+y].start()

            img_parca_tx[x][y]=ox
            img_parca_ty[x][y]=oy
    time.sleep(0.001)

timer_y=0
def timer():
    global gmx,gmy,t,timer_basla,timer_y,imgy,ImageTk,img_s,panel,timer_basla_durdurucu,labely
    try:
        while True:
            yapistir=0

            gmx+=0
            gmy+=0
            for x in range(0,width_s+1):
                for y in range(0,height_s+1):
                    if (yandex_load_calisiyor[x][y]==1 or img_parca[x][y]==0):
                        yapistir=1
                        timer_y=0

            if(yapistir==0):timer_y+=1
            if (timer_basla==1):yukle_yandex()
            time.sleep(.5)
            if (timer_basla==1 or timer_y<1):imaj_parca_yapistir()

            timer_basla_durdurucu+=1
            if(timer_basla_durdurucu==4):timer_basla=0
 
            labely.config(text=str(yz))




    except:
           print "Timer error !"
           timer()





def imaj_parca_yapistir():

    global img_s,panel,imgy,mx,my,gmx,gmy,panel,img_parca_x,img_parca_y,tgmx,tgmy

    for x in range(0,width_s+1):
        for y in range(0,height_s+1):

            img_parca_x[x][y]=(gmx+x*256)%(width+256)
            img_parca_y[x][y]=(gmy+y*256)%(height+256)
            imgy.paste(img_parca[x][y],(img_parca_x[x][y]-256,img_parca_y[x][y]-256,img_parca_x[x][y],img_parca_y[x][y]))



    img_s = ImageTk.PhotoImage(imgy)
    panel.delete("all")
    panel.create_image(0, 0, image = img_s, anchor="nw")
    panel.create_rectangle(sec[0].x, sec[0].y, sec[1].x, sec[1].y,outline="red")
    panel.create_rectangle(sec[0].x-10, sec[0].y-10, sec[0].x+10, sec[0].y+10,outline="red")
    panel.create_rectangle(sec[0].x-10, sec[1].y-10, sec[0].x+10, sec[1].y+10,outline="red")
    panel.create_rectangle(sec[1].x-10, sec[0].y-10, sec[1].x+10, sec[0].y+10,outline="red")
    panel.create_rectangle(sec[1].x-10, sec[1].y-10, sec[1].x+10, sec[1].y+10,outline="red")
    panel.create_rectangle(gmx, gmy, gmx+5, gmy+5,fill="blue")


def buton_yenile():
    imaj_parca_yapistir()

def buton_arttir():
    global yx,yy,yz,threads,gmx,gmy,mx,my

    for x in range(0, width_s+1):
        for y in range(0, height_s+1):
            img_parca[x][y]=0
            img_parca_x[x][y]=(gmx+x*256)%(width+256)
            img_parca_y[x][y]=(gmy+y*256)%(height+256)
            threads[x*width_s+y]._Thread__stop();yandex_load_calisiyor[x][y]=0
    yx=int((yx)*2+(width_s/2.0))
    yy=int((yy)*2+(height_s/2.0))
    gmx=gmx*2
    gmy=gmy*2
    yz+=1
    timer_basla=1

    yukle_yandex2();
    time.sleep(.05)

    imaj_parca_yapistir()
    a = Point(0, 0)
    mousedown(a)
    mouseup(a)


def buton_eksilt():
    global yx,yy,yz,threads,gmx,gmy
    for x in range(0, width_s+1):
        for y in range(0, height_s+1):
            img_parca[x][y]=0
            img_parca_x[x][y]=(gmx+x*256)%(width+256)
            img_parca_y[x][y]=(gmy+y*256)%(height+256)
            threads[x*width_s+y]._Thread__stop();yandex_load_calisiyor[x][y]=0

    yx=int((yx-(width_s/2))/2)
    yy=int((yy-(height_s/2))/2)
    gmx=int(gmx/2)
    gmy=int(gmy/2)

    yz-=1
    timer_basla=1

    yukle_yandex2();
    time.sleep(.05)
    imaj_parca_yapistir()
    a = Point(0, 0)
    mousedown(a)
    mouseup(a)

def buton_h1():
    global murl
    murl=['http://sat00.maps.yandex.net/tiles?l=sat&v=3.57.0&x='
      ,'http://sat01.maps.yandex.net/tiles?l=sat&v=3.57.0&x='
      ,'http://sat02.maps.yandex.net/tiles?l=sat&v=3.57.0&x='
      ,'http://sat03.maps.yandex.net/tiles?l=sat&v=3.57.0&x='
      ,'http://sat04.maps.yandex.net/tiles?l=sat&v=3.57.0&x=']

    yukle_yandex2();
    time.sleep(.05)
    imaj_parca_yapistir()
    timer_basla=1

    return
def buton_h2():
    global murl
    murl=['http://vec01.maps.yandex.net/tiles?l=map&v=2.43.0&x='
      ,'http://vec02.maps.yandex.net/tiles?l=map&v=2.43.0&x='
      ,'http://vec03.maps.yandex.net/tiles?l=map&v=2.43.0&x='
      ,'http://vec04.maps.yandex.net/tiles?l=map&v=2.43.0&x='
      ,'http://vec01.maps.yandex.net/tiles?l=map&v=2.43.0&x=']

    yukle_yandex2();
    time.sleep(.05)
    imaj_parca_yapistir()
    timer_basla=1
    return

def comboselected(event):
    global combo,dzoom
    dzoom=int(combo.get())
   
def comboselected2(event):
    global combo2,thread_value
    thread_value=int(combo2.get())
    print "threads %d"%(thread_value)
def comboselected3(event):
    global url_language
    url_language=combo3.get()
    print combo3.get()
    time.sleep(.05)
    yukle_yandex2();
    time.sleep(.05)
    imaj_parca_yapistir()
    timer_basla=1

   
y_d=[]
murlno=0
for a in range(0,600):
    y_d.append(0)

def y_download3(y,x1,y1,x2,y2,syc):
    global imgs,y_d,murlno,combo3
    y_d[syc]=1
    for x in range(0,int(x2-x1)):
        try:
            imgp=0
            url=murl[murlno]+str(int(x+x1))+'&y='+str(int(y+y1))+'&z='+str(dzoom)+url_language
            file = urllib2.urlopen(url,timeout=10)
            imgp = Image.open(cStringIO.StringIO(file.read()))
            file.close()
            imgs.paste(imgp,(int(x*256),int(y*256),int(x*256+256),int(y*256+256)))
            murlno+=1
            if(murlno>4):murlno=0
            time.sleep(.005)
        except:
            print "y_donload3 error"
    y_d[syc]=0
    return


def buton_save():
    global gmx,gmy,yz,yx,yy,panel2,dzoom,imgs,y_d
    x1=((-gmx+sec[0].x)/256.0+yx)*pow(2,dzoom-yz)
    y1=((-gmy+sec[0].y)/256.0+yy)*pow(2,dzoom-yz)
    x2=((-gmx+sec[1].x)/256.0+yx)*pow(2,dzoom-yz)
    y2=((-gmy+sec[1].y)/256.0+yy)*pow(2,dzoom-yz)
    for a in range(0,600):y_d[a]=0

    if(x1>x2):x1,x2=x2,x1
    if(y1>y2):y1,y2=y2,y1
    x2=x2+3
    y2=y2+3
    r=pow(2,dzoom-yz)
    x1+=r;x2+=r;y1+=r;y2+=r;

    imgs=Image.new("RGB",(int((x2-x1)*256.0)-((int((x2-x1)*256.0)%256)),int((y2-y1)*256.0)-(int((y2-y1)*256.0)%256)), "black")
    syc=0

   
    for y in range(0,int(y2-y1)):
        print "-- %d" % (int(y2-y1)-y)
        time.sleep(.005)
        if(y_d[syc]==0):
            print str(y)
            #thread.start_new_thread( y_download3, (y,x1,y1,x2,y2,syc ) )
            threading.Thread(target=y_download3,args=(y,x1,y1,x2,y2,syc)).start()
               

           
            syc+=1
        if(syc>thread_value):
            syc=0
        else:
            while(y_d[syc]==1):
                syc+=1
                if(syc>thread_value):syc=0
            #thread.start_new_thread( y_download3, (y,x1,y1,x2,y2,syc ) )
            threading.Thread(target=y_download3,args=(y,x1,y1,x2,y2,syc)).start()
           


    count=0
    while (count < syc):
       count = count + 1

       st=""
       for t in range(0,syc):
            print '-----\r[%d] '%(syc-count)+st

       time.sleep(0.0001)
       if (y_d[count]==1):count = count - 1

     
    name=asksaveasfile(mode='w', defaultextension=".jpg")
    imgs.save(name.name)
   
    print name.name
       
       

   
    return




cl = threading.Thread(target=timer)
threads.append(cl)
cl.start()

img_s = ImageTk.PhotoImage(imgy)

panel = Canvas(root,width = width, height = height, bg = 'black')
panel.pack(side=TOP)
panel.create_image(0, 0, image = img_s, anchor="nw")


labelth = Label(root, text="Threads")

labelth.pack(side=LEFT)

combo2 = ttk.Combobox(root,  state='readonly')
combo2.pack(side=LEFT)

combo2['values'] = range(1,65,1)


butony = Button(root, text ="  ", command = buton_yenile)
butonzart = Button(root, text ="-", command = buton_eksilt)
butonzeks = Button(root, text ="+", command = buton_arttir)
butonsave = Button(root, text ="download image", command = buton_save)
butonh1 = Button(root, text ="Satellite", command = buton_h1)
butonh2 = Button(root, text ="Map      ", command = buton_h2)

butony.pack(side=LEFT)
butonzart.pack(side=LEFT)
butonzeks.pack(side=LEFT)
butonsave.pack(side=LEFT)
butonh1.pack(side=LEFT)
butonh2.pack(side=LEFT)






labely = Label(root, text="--")
labely.pack(side=RIGHT)

labelz = Label(root, text="Zoom")
labelz.pack(side=LEFT)

combo = ttk.Combobox(root,  state='readonly')
combo.pack(side=LEFT)
combo['values'] = range(1,20,1)
combo.set(dzoom)
combo.bind('<<ComboboxSelected>>', comboselected)

combo2.set(8)
combo2.bind('<<ComboboxSelected>>', comboselected2)

combo3.bind('<<ComboboxSelected>>', comboselected3)
combo3.pack(side=LEFT)

panel.bind("<B1-Motion>", point)
panel.bind("<B3-Motion>", point3)
panel.bind("<ButtonPress-1>", mousedown)
panel.bind("<ButtonRelease-1>", mouseup)
panel.bind("<ButtonRelease-3>", mouseup3)


root.mainloop()

scx
 
Posts: 1
Joined: Thu Jul 11, 2013 10:18 am

Return to Completed Scripts

Who is online

Users browsing this forum: SnakeGuru and 2 guests