python制作的google map分片下载工具


 用途:

在线下载google map的切片地图,自定义坐标范围、级别范围、下载服务器、下载路径。方便制作离线地图。

代码及说明:

配置文件用户设置参数 StevenGoogleMapToolConfig.py

#!/usr/bin/evn python
# -*- coding:utf-8 -*-

#下载服务器mt0~mt3

#绘制地图
#http://mt0.google.cn/vt/lyrs=m@187000000&hl=zh-CN&gl=cn&src=app&x=104&y=52&z=7&s=Galil

#卫星地图
#http://mt0.google.cn/vt/lyrs=s@118&hl=zh-CN&gl=cn&src=app&x=104&y=52&z=7&s=Gali

#下载模板 需要传入4个参数使用
DOWNURL='http://%s/vt/lyrs=m@187000000&hl=zh-CN&gl=cn&src=app&x=%d&y=%d&z=%d&s=Galil'

TITLE = 'Google地图下载工具 v0.99 steven.f.yang@gmail.com'
WIDTH = 900
HEIGHT = 600

#图片大小
#IMGSIZE = 256

#标签文字

mapServers = ['mt0.google.cn', 'mt1.google.cn', 'mt2.google.cn', 'mt3.google.cn']

#地图级别 0~19

网上下载的使用工具 globalmaptiles.py


#!/usr/bin/env python
###############################################################################
# $Id$
#
# Project:	GDAL2Tiles, Google Summer of Code 2007 & 2008
#           Global Map Tiles Classes
# Purpose:	Convert a raster into TMS tiles, create KML SuperOverlay EPSG:4326,
#			generate a simple HTML viewers based on Google Maps and OpenLayers
# Author:	Klokan Petr Pridal, klokan at klokan dot cz
# Web:		http://www.klokan.cz/projects/gdal2tiles/
#
###############################################################################
# Copyright (c) 2008 Klokan Petr Pridal. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################

"""
globalmaptiles.py

Global Map Tiles as defined in Tile Map Service (TMS) Profiles
==============================================================

Functions necessary for generation of global tiles used on the web.
It contains classes implementing coordinate conversions for:

  - GlobalMercator (based on EPSG:900913 = EPSG:3785)
       for Google Maps, Yahoo Maps, Microsoft Maps compatible tiles
  - GlobalGeodetic (based on EPSG:4326)
       for OpenLayers Base Map and Google Earth compatible tiles

More info at:

http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification
http://wiki.osgeo.org/wiki/WMS_Tiling_Client_Recommendation
http://msdn.microsoft.com/en-us/library/bb259689.aspx
http://code.google.com/apis/maps/documentation/overlays.html#Google_Maps_Coordinates

Created by Klokan Petr Pridal on 2008-07-03.
Google Summer of Code 2008, project GDAL2Tiles for OSGEO.

In case you use this class in your product, translate it to another language
or find it usefull for your project please let me know.
My email: klokan at klokan dot cz.
I would like to know where it was used.

Class is available under the open-source GDAL license (www.gdal.org).
"""

import math

class GlobalMercator(object):
  """
  TMS Global Mercator Profile
  ---------------------------

  Functions necessary for generation of tiles in Spherical Mercator projection,
  EPSG:900913 (EPSG:gOOglE, Google Maps Global Mercator), EPSG:3785, OSGEO:41001.

  Such tiles are compatible with Google Maps, Microsoft Virtual Earth, Yahoo Maps,
  UK Ordnance Survey OpenSpace API, ...
  and you can overlay them on top of base maps of those web mapping applications.
  
  Pixel and tile coordinates are in TMS notation (origin [0,0] in bottom-left).

  What coordinate conversions do we need for TMS Global Mercator tiles::

       LatLon      <->       Meters      <->     Pixels    <->       Tile     

   WGS84 coordinates   Spherical Mercator  Pixels in pyramid  Tiles in pyramid
       lat/lon            XY in metres     XY pixels Z zoom      XYZ from TMS 
      EPSG:4326           EPSG:900913                                         
       .----.              ---------               --                TMS      
      /      \     <->     |       |     <->     /----/    <->      Google    
      \      /             |       |           /--------/          QuadTree   
       -----               ---------         /------------/                   
     KML, public         WebMapService         Web Clients      TileMapService

  What is the coordinate extent of Earth in EPSG:900913?

    [-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244]
    Constant 20037508.342789244 comes from the circumference of the Earth in meters,
    which is 40 thousand kilometers, the coordinate origin is in the middle of extent.
      In fact you can calculate the constant as: 2 * math.pi * 6378137 / 2.0
    $ echo 180 85 | gdaltransform -s_srs EPSG:4326 -t_srs EPSG:900913
    Polar areas with abs(latitude) bigger then 85.05112878 are clipped off.

  What are zoom level constants (pixels/meter) for pyramid with EPSG:900913?

    whole region is on top of pyramid (zoom=0) covered by 256x256 pixels tile,
    every lower zoom level resolution is always divided by two
    initialResolution = 20037508.342789244 * 2 / 256 = 156543.03392804062

  What is the difference between TMS and Google Maps/QuadTree tile name convention?

    The tile raster itself is the same (equal extent, projection, pixel size),
    there is just different identification of the same raster tile.
    Tiles in TMS are counted from [0,0] in the bottom-left corner, id is XYZ.
    Google placed the origin [0,0] to the top-left corner, reference is XYZ.
    Microsoft is referencing tiles by a QuadTree name, defined on the website:
    http://msdn2.microsoft.com/en-us/library/bb259689.aspx

  The lat/lon coordinates are using WGS84 datum, yeh?

    Yes, all lat/lon we are mentioning should use WGS84 Geodetic Datum.
    Well, the web clients like Google Maps are projecting those coordinates by
    Spherical Mercator, so in fact lat/lon coordinates on sphere are treated as if
    the were on the WGS84 ellipsoid.
   
    From MSDN documentation:
    To simplify the calculations, we use the spherical form of projection, not
    the ellipsoidal form. Since the projection is used only for map display,
    and not for displaying numeric coordinates, we don't need the extra precision
    of an ellipsoidal projection. The spherical projection causes approximately
    0.33 percent scale distortion in the Y direction, which is not visually noticable.

  How do I create a raster in EPSG:900913 and convert coordinates with PROJ.4?

    You can use standard GIS tools like gdalwarp, cs2cs or gdaltransform.
    All of the tools supports -t_srs 'epsg:900913'.

    For other GIS programs check the exact definition of the projection:
    More info at http://spatialreference.org/ref/user/google-projection/
    The same projection is degined as EPSG:3785. WKT definition is in the official
    EPSG database.

    Proj4 Text:
      +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0
      +k=1.0 +units=m +nadgrids=@null +no_defs

    Human readable WKT format of EPGS:900913:
       PROJCS["Google Maps Global Mercator",
           GEOGCS["WGS 84",
               DATUM["WGS_1984",
                   SPHEROID["WGS 84",6378137,298.2572235630016,
                       AUTHORITY["EPSG","7030"]],
                   AUTHORITY["EPSG","6326"]],
               PRIMEM["Greenwich",0],
               UNIT["degree",0.0174532925199433],
               AUTHORITY["EPSG","4326"]],
           PROJECTION["Mercator_1SP"],
           PARAMETER["central_meridian",0],
           PARAMETER["scale_factor",1],
           PARAMETER["false_easting",0],
           PARAMETER["false_northing",0],
           UNIT["metre",1,
               AUTHORITY["EPSG","9001"]]]
  """

  def __init__(self, tileSize=256):
    "Initialize the TMS Global Mercator pyramid"
    self.tileSize = tileSize
    self.initialResolution = 2 * math.pi * 6378137 / self.tileSize
    # 156543.03392804062 for tileSize 256 pixels
    self.originShift = 2 * math.pi * 6378137 / 2.0
    # 20037508.342789244

  def LatLonToMeters(self, lat, lon ):
    "Converts given lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913"

    mx = lon * self.originShift / 180.0
    my = math.log( math.tan((90 + lat) * math.pi / 360.0 )) / (math.pi / 180.0)

    my = my * self.originShift / 180.0
    return mx, my

  def MetersToLatLon(self, mx, my ):
    "Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum"

    lon = (mx / self.originShift) * 180.0
    lat = (my / self.originShift) * 180.0

    lat = 180 / math.pi * (2 * math.atan( math.exp( lat * math.pi / 180.0)) - math.pi / 2.0)
    return lat, lon

  def PixelsToMeters(self, px, py, zoom):
    "Converts pixel coordinates in given zoom level of pyramid to EPSG:900913"

    res = self.Resolution( zoom )
    mx = px * res - self.originShift
    my = py * res - self.originShift
    return mx, my
    
  def MetersToPixels(self, mx, my, zoom):
    "Converts EPSG:900913 to pyramid pixel coordinates in given zoom level"
        
    res = self.Resolution( zoom )
    px = (mx + self.originShift) / res
    py = (my + self.originShift) / res
    return px, py
  
  def PixelsToTile(self, px, py):
    "Returns a tile covering region in given pixel coordinates"

    tx = int( math.ceil( px / float(self.tileSize) ) - 1 )
    ty = int( math.ceil( py / float(self.tileSize) ) - 1 )
    return tx, ty

  def PixelsToRaster(self, px, py, zoom):
    "Move the origin of pixel coordinates to top-left corner"
    
    mapSize = self.tileSize << zoom
    return px, mapSize - py
    
  def MetersToTile(self, mx, my, zoom):
    "Returns tile for given mercator coordinates"
    
    px, py = self.MetersToPixels( mx, my, zoom)
    return self.PixelsToTile( px, py)

  def TileBounds(self, tx, ty, zoom):
    "Returns bounds of the given tile in EPSG:900913 coordinates"
    
    minx, miny = self.PixelsToMeters( tx*self.tileSize, ty*self.tileSize, zoom )
    maxx, maxy = self.PixelsToMeters( (tx+1)*self.tileSize, (ty+1)*self.tileSize, zoom )
    return ( minx, miny, maxx, maxy )

  def TileLatLonBounds(self, tx, ty, zoom ):
    "Returns bounds of the given tile in latutude/longitude using WGS84 datum"

    bounds = self.TileBounds( tx, ty, zoom)
    minLat, minLon = self.MetersToLatLon(bounds[0], bounds[1])
    maxLat, maxLon = self.MetersToLatLon(bounds[2], bounds[3])

    return ( minLat, minLon, maxLat, maxLon )
    
  def Resolution(self, zoom ):
    "Resolution (meters/pixel) for given zoom level (measured at Equator)"
    
    # return (2 * math.pi * 6378137) / (self.tileSize * 2**zoom)
    return self.initialResolution / (2**zoom)
    
  def ZoomForPixelSize(self, pixelSize ):
    "Maximal scaledown zoom of the pyramid closest to the pixelSize."
    
    for i in range(30):
      if pixelSize > self.Resolution(i):
        return i-1 if i!=0 else 0 # We don't want to scale up

  def GoogleTile(self, tx, ty, zoom):
    "Converts TMS tile coordinates to Google Tile coordinates"
    
    # coordinate origin is moved from bottom-left to top-left corner of the extent
    return tx, (2**zoom - 1) - ty

  def QuadTree(self, tx, ty, zoom ):
    "Converts TMS tile coordinates to Microsoft QuadTree"
    
    quadKey = ""
    ty = (2**zoom - 1) - ty
    for i in range(zoom, 0, -1):
      digit = 0
      mask = 1 << (i-1)
      if (tx & mask) != 0:
        digit += 1
      if (ty & mask) != 0:
        digit += 2
      quadKey += str(digit)
      
    return quadKey

#---------------------

class GlobalGeodetic(object):
  """
  TMS Global Geodetic Profile
  ---------------------------

  Functions necessary for generation of global tiles in Plate Carre projection,
  EPSG:4326, "unprojected profile".

  Such tiles are compatible with Google Earth (as any other EPSG:4326 rasters)
  and you can overlay the tiles on top of OpenLayers base map.
  
  Pixel and tile coordinates are in TMS notation (origin [0,0] in bottom-left).

  What coordinate conversions do we need for TMS Global Geodetic tiles?

    Global Geodetic tiles are using geodetic coordinates (latitude,longitude)
    directly as planar coordinates XY (it is also called Unprojected or Plate
    Carre). We need only scaling to pixel pyramid and cutting to tiles.
    Pyramid has on top level two tiles, so it is not square but rectangle.
    Area [-180,-90,180,90] is scaled to 512x256 pixels.
    TMS has coordinate origin (for pixels and tiles) in bottom-left corner.
    Rasters are in EPSG:4326 and therefore are compatible with Google Earth.

       LatLon      <->      Pixels      <->     Tiles     

   WGS84 coordinates   Pixels in pyramid  Tiles in pyramid
       lat/lon         XY pixels Z zoom      XYZ from TMS 
      EPSG:4326                                           
       .----.                ----                         
      /      \     <->    /--------/    <->      TMS      
      \      /         /--------------/                   
       -----        /--------------------/                
     WMS, KML    Web Clients, Google Earth  TileMapService
  """

  def __init__(self, tileSize = 256):
    self.tileSize = tileSize

  def LatLonToPixels(self, lat, lon, zoom):
    "Converts lat/lon to pixel coordinates in given zoom of the EPSG:4326 pyramid"

    res = 180 / 256.0 / 2**zoom
    px = (180 + lat) / res
    py = (90 + lon) / res
    return px, py

  def PixelsToTile(self, px, py):
    "Returns coordinates of the tile covering region in pixel coordinates"

    tx = int( math.ceil( px / float(self.tileSize) ) - 1 )
    ty = int( math.ceil( py / float(self.tileSize) ) - 1 )
    return tx, ty

  def Resolution(self, zoom ):
    "Resolution (arc/pixel) for given zoom level (measured at Equator)"
    
    return 180 / 256.0 / 2**zoom
    #return 180 / float( 1 << (8+zoom) )

  def TileBounds(self, tx, ty, zoom):
    "Returns bounds of the given tile"
    res = 180 / 256.0 / 2**zoom
    return (
      tx*256*res - 180,
      ty*256*res - 90,
      (tx+1)*256*res - 180,
      (ty+1)*256*res - 90
    )

if __name__ == "__main__":
  import sys, os
    
  def Usage(s = ""):
    print "Usage: globalmaptiles.py [-profile 'mercator'|'geodetic'] zoomlevel lat lon [latmax lonmax]"
    print
    if s:
      print s
      print
    print "This utility prints for given WGS84 lat/lon coordinates (or bounding box) the list of tiles"
    print "covering specified area. Tiles are in the given 'profile' (default is Google Maps 'mercator')"
    print "and in the given pyramid 'zoomlevel'."
    print "For each tile several information is printed including bonding box in EPSG:900913 and WGS84."
    sys.exit(1)

  profile = 'mercator'
  zoomlevel = None
  lat, lon, latmax, lonmax = None, None, None, None
  boundingbox = False

  argv = sys.argv
  i = 1
  while i < len(argv):
    arg = argv[i]

    if arg == '-profile':
      i = i + 1
      profile = argv[i]
    
    if zoomlevel is None:
      zoomlevel = int(argv[i])
    elif lat is None:
      lat = float(argv[i])
    elif lon is None:
      lon = float(argv[i])
    elif latmax is None:
      latmax = float(argv[i])
    elif lonmax is None:
      lonmax = float(argv[i])
    else:
      Usage("ERROR: Too many parameters")

    i = i + 1
  
  if profile != 'mercator':
    Usage("ERROR: Sorry, given profile is not implemented yet.")
  
  if zoomlevel == None or lat == None or lon == None:
    Usage("ERROR: Specify at least 'zoomlevel', 'lat' and 'lon'.")
  if latmax is not None and lonmax is None:
    Usage("ERROR: Both 'latmax' and 'lonmax' must be given.")
  
  if latmax != None and lonmax != None:
    if latmax < lat:
      Usage("ERROR: 'latmax' must be bigger then 'lat'")
    if lonmax < lon:
      Usage("ERROR: 'lonmax' must be bigger then 'lon'")
    boundingbox = (lon, lat, lonmax, latmax)
  
  tz = zoomlevel
  mercator = GlobalMercator()

  mx, my = mercator.LatLonToMeters( lat, lon )
  print "Spherical Mercator (ESPG:900913) coordinates for lat/lon: "
  print (mx, my)
  tminx, tminy = mercator.MetersToTile( mx, my, tz )
  
  if boundingbox:
    mx, my = mercator.LatLonToMeters( latmax, lonmax )
    print "Spherical Mercator (ESPG:900913) cooridnate for maxlat/maxlon: "
    print (mx, my)
    tmaxx, tmaxy = mercator.MetersToTile( mx, my, tz )
  else:
    tmaxx, tmaxy = tminx, tminy
    
  for ty in range(tminy, tmaxy+1):
    for tx in range(tminx, tmaxx+1):
      tilefilename = "%s/%s/%s" % (tz, tx, ty)
      print tilefilename, "( TileMapService: z / x / y )"
    
      gx, gy = mercator.GoogleTile(tx, ty, tz)
      print "\tGoogle:", gx, gy
      quadkey = mercator.QuadTree(tx, ty, tz)
      print "\tQuadkey:", quadkey, '(',int(quadkey, 4),')'
      bounds = mercator.TileBounds( tx, ty, tz)
      print
      print "\tEPSG:900913 Extent: ", bounds
      wgsbounds = mercator.TileLatLonBounds( tx, ty, tz)
      print "\tWGS84 Extent:", wgsbounds
      print "\tgdalwarp -ts 256 256 -te %s %s %s %s %s %s_%s_%s.tif" % (
        bounds[0], bounds[1], bounds[2], bounds[3], "<your-raster-file-in-epsg900913.ext>", tz, tx, ty)
      print

下载工具主文件 StevenGoogleMapTool.py

#!/usr/bin/evn python
# -*- coding:utf-8 -*-


from StevenGoogleMapToolConfig import *
import Tkinter
import time
import tkFileDialog
import globalmaptiles
import os
from Tkinter import StringVar
import urllib
import thread
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


class GUI(Tkinter.Frame):
    def __init__(self, root):
        Tkinter.Frame.__init__(self, root)
        
        #设置多行框架存放组件
        self.frame = [Tkinter.Frame(padx=3, pady=3), Tkinter.Frame(padx=3, pady=3), Tkinter.Frame(padx=3, pady=3), Tkinter.Frame(padx=3, pady=3)]
        
        #第一行控件
        Tkinter.Label(self.frame[0], text='框选左上角经纬度:经').pack(side=Tkinter.LEFT)
        
        ltlo = StringVar()
        ltlo.set('-179.9')#73.666667
        self.leftTopLon = Tkinter.Entry(self.frame[0], textvariable=ltlo)
        self.leftTopLon.pack(side=Tkinter.LEFT)
        
        Tkinter.Label(self.frame[0], text='维').pack(side=Tkinter.LEFT)
        
        ltla = StringVar()
        ltla.set('85')#53.550000
        self.leftTopLat = Tkinter.Entry(self.frame[0], textvariable=ltla)
        self.leftTopLat.pack(side=Tkinter.LEFT)
        
        Tkinter.Label(self.frame[0], text='框选右下角经纬度:经').pack(side=Tkinter.LEFT)
        
        rblo = StringVar()
        rblo.set('179.9')#135.041667
        self.rightBottomLon = Tkinter.Entry(self.frame[0], textvariable=rblo)
        self.rightBottomLon.pack(side=Tkinter.LEFT)
        
        Tkinter.Label(self.frame[0], text='维').pack(side=Tkinter.LEFT)
        
        rbla = StringVar()
        rbla.set('-85')#3.866667
        self.rightBottomLat = Tkinter.Entry(self.frame[0], textvariable=rbla)
        self.rightBottomLat.pack(side=Tkinter.LEFT)
        
        self.frame[0].pack(expand=0, fill=Tkinter.X)
        
        #第二行控件
        Tkinter.Label(self.frame[1], text='选择下载地图的服务器').pack(side=Tkinter.LEFT)
        
        self.ss = Tkinter.StringVar()
        self.ss.set(mapServers[0])
        Tkinter.OptionMenu(self.frame[1], self.ss, *mapServers).pack(side=Tkinter.LEFT)
        
        self.mapLevelStart = Tkinter.StringVar()
        self.mapLevelStart.set(0)
        self.mapLevelEnd = Tkinter.StringVar()
        self.mapLevelEnd.set(5)
        
        Tkinter.Label(self.frame[1], text='地图下载起始级别').pack(side=Tkinter.LEFT)
        Tkinter.OptionMenu(self.frame[1], self.mapLevelStart, *range(0, 20)).pack(side=Tkinter.LEFT)
        Tkinter.Label(self.frame[1], text='地图下载终止级别').pack(side=Tkinter.LEFT)
        Tkinter.OptionMenu(self.frame[1], self.mapLevelEnd, *range(0, 20)).pack(side=Tkinter.LEFT)
        
        #存放目录
        self.btSaveFolder = Tkinter.Button(self.frame[1], text='选择存放目录', command=self.selectSaveFolder)
        self.btSaveFolder.pack(side=Tkinter.LEFT)
        
        self.btAction = Tkinter.Button(self.frame[1], text='开始下载', bg='red', fg='yellow', command=self.doDownload)
        self.btAction.pack(side=Tkinter.RIGHT, expand=1, fill=Tkinter.X)
        self.frame[1].pack(expand=0, fill=Tkinter.X)
        
        #第三行控件
        
        #文本框滚动条
        self.sl = Tkinter.Scrollbar(self.frame[2])
        self.sl.pack(side='right', fill='y')
        #显示结果的文本框
        self.message = Tkinter.Text(self.frame[2], yscrollcommand=self.sl.set)
        #将滚动条的值与文本框绑定,这样滚动条才有作用
        self.sl.config(command=self.message.yview) 
        self.message.pack(expand=1, fill=Tkinter.BOTH)
        self.message.bind("<KeyPress>", lambda e : "break")
        
        self.frame[2].pack(expand=1, fill=Tkinter.BOTH)
        
        
        #第四行控件
        self.clAction = Tkinter.Button(self.frame[3], text='清空日志', command=self.clear)
        self.clAction.pack(side=Tkinter.LEFT)
        
        Tkinter.Label(self.frame[3], text='Status:').pack(side=Tkinter.LEFT)
        self.lbcount = Tkinter.Label(self.frame[3], text='')
        self.lbcount.pack(side=Tkinter.LEFT)
        
        self.frame[3].pack(expand=0, fill=Tkinter.X)
    
        self.gm = globalmaptiles.GlobalMercator()
    
    def clear(self):
        self.message.delete('1.0', Tkinter.END)
    
    def LatLon2GoogleTile(self, lat, lon, zoom):
        '''坐标转换为GoogleMap瓦片编号'''
        mx, my = self.gm.LatLonToMeters(lat, lon)
        tx, ty = self.gm.MetersToTile(mx, my, zoom)
        return self.gm.GoogleTile(tx, ty, zoom)
        
    def selectSaveFolder(self):
        self.dir = tkFileDialog.askdirectory(initialdir='/')
        self.log('选择存放目录:' + self.dir)
    
    def doDownload(self):
        #多线程处理长时间方法避免UI无响应
        thread.start_new_thread(self.download, ())
        
    def download(self):
        ltlat = float(self.leftTopLat.get())
        ltlon = float(self.leftTopLon.get())
        rblat = float(self.rightBottomLat.get())
        rblon = float(self.rightBottomLon.get())
        ls = int(self.mapLevelStart.get())
        ln = int(self.mapLevelEnd.get())
        total = 0
        
        for i in range(ls, ln + 1):
            #计算这个级别的地图编号
            x, y = self.LatLon2GoogleTile(ltlat, ltlon, i)
            xe, ye = self.LatLon2GoogleTile(rblat, rblon, i)
            s = (xe - x + 1) * (ye - y + 1)
            total += s
            self.log('%s:%d x:%d y:%d ~ x:%d y:%d 瓦片数:%d' % ('下载地图级别', i, x, y, xe, ye, s))
        self.log('%s:%d' % ('下载瓦片数合计', total))

        #return True

        #下载
        froot = self.dir + '/map_' + str(time.time())
        self.log('创建目录:' + froot)
        os.mkdir(froot);
        
        #已经下载计数
        count = 0
        
        #遍历层
        for i in range(ls, ln + 1):
            #创建层目录
            flev = froot + '/' + str(i)
            os.mkdir(flev)
            self.log('创建层级目录:' + flev)
            
            x, y = self.LatLon2GoogleTile(ltlat, ltlon, i)
            xe, ye = self.LatLon2GoogleTile(rblat, rblon, i)
            
            #遍历x
            for j in range(x, xe + 1):
                fx = flev + '/' + str(j)
                os.mkdir(fx)
                self.log('创建X方向目录:' + fx)
                for k in range(y, ye + 1):
                    #下载地址
                    url = DOWNURL % (self.ss.get(), j, k, i)
                    #self.log("下载: x:%d y:%d z:%d" % (j, k, i))
                    urllib.urlretrieve(url, fx + '/' + str(k) + '.png')
                    count += 1
                    self.lbcount['text'] = '已完成:%d/%d 瓦片编号 x:%d y:%d z:%d' % (count, total, j, k, i)
        
        self.log('下载完成!')
        thread.exit_thread()
        
        
        
    def downloadCallback(self, a, b, c):
        '''a,已下载的数据块  b,数据块的大小  c,远程文件的大小'''
        pass
    
    def log(self, msg):
        self.message.insert(Tkinter.END, time.strftime('%Y-%m-%d %H:%M:%S\t', time.localtime(time.time())) + msg + '\n')
        
if __name__ == '__main__':
    root = Tkinter.Tk()
    GUI(root).pack()
    root.title(TITLE)
    root.minsize(WIDTH, HEIGHT)
    #root.maxsize(WIDTH, HEIGHT)
    root.mainloop()

截图



理论上可以下载0~19级所有图片(取决于你的网速和服务器),实验0~10级地图无问题。如有问题发email。


本文转自 shadowkiss 大神的博文

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值