部分結果
python
bing地圖瓦片使用QuadKey做爲命名方式。json
在給定level下,把行號tileY和列號tileX轉換爲2進制,而後行列交叉存儲,再轉換爲4進制,即獲得了相應的quadkey。app
四鍵具備幾個有趣的屬性。首先,四鍵的長度(位數)等於相應圖塊的詳細程度。其次,任何圖塊的四鍵以其父圖塊(上一級包含的圖塊)的四鍵開頭。以下例所示,圖塊2是圖塊20到23的父級,圖塊13是圖塊130到133的父級:dom
根據選擇的區域計算四鍵,構建下載連接,使用python requests下載影像瓦片。url
# -*- coding: utf-8 -*- """下載區域影像 從第一層到指定層 """ import requests import os.path import QuadKey.quadkey as quadkey import shutil import secrets as secrets # 下載的最細層 tileZoom = 5 rootTileDir = "tiles_cache" lat_min = -90 lat_max = 90 lon_min = -180 lon_max = 180 # MS doesn't want you hardcoding the URLs to the tile server. This request asks for the Aerial # url template. Replace {quadkey} response = requests.get("https://dev.virtualearth.net/REST/V1/Imagery/Metadata/Aerial?key=%s" % (secrets.bingKey)) # 返回結果 data = response.json() print(data) # grabs the data we need from the response. # 例如:http://ecn.{subdomain}.tiles.virtualearth.net/tiles/a{quadkey}.jpeg?g=7786 tileUrlTemplate = data['resourceSets'][0]['resources'][0]['imageUrl'] # 例如:['t0', 't1', 't2', 't3'] imageDomains = data['resourceSets'][0]['resources'][0]['imageUrlSubdomains'] if (os.path.exists(rootTileDir) == False): os.mkdir(rootTileDir) bingTilesDir = os.path.join(rootTileDir, "bing") if (os.path.exists(bingTilesDir) == False): os.mkdir(bingTilesDir) def get_tiles_by_pixel(tilePixel): """ 下載該點之上的瓦片 :param lat: :param lon: :return: """ """get pixel coordinates""" # tilePixel = quadkey.TileSystem.geo_to_pixel((lat, lon), tileZoom) # print(tilePixel) pixel = tilePixel geo = quadkey.TileSystem.pixel_to_geo(pixel, tileZoom) # 計算四鍵 qk = quadkey.from_geo(geo, tileZoom) # 四鍵 qkStr = str(qk) # qkArray = [] for index in range(tileZoom): qkArray.append(qkStr[0:index + 1]) print(qkArray) # 存放路徑 for qk in qkArray: # 下載影像 tileFileName = "%s/%s.jpg" % (bingTilesDir, qk) if (os.path.exists(tileFileName)): # already downloaded ok = 1 else: print("下載中", end='') url = tileUrlTemplate.replace("{subdomain}", imageDomains[0]) url = url.replace("{quadkey}", qk) url = "%s&key=%s" % (url, secrets.bingKey) response = requests.get(url, stream=True) print(response) with open(tileFileName, 'wb') as out_file: shutil.copyfileobj(response.raw, out_file) del response neededTile = True # 左上爲原點 tilePixelMax = quadkey.TileSystem.geo_to_pixel((lat_max, lon_max), tileZoom) tilePixelMin = quadkey.TileSystem.geo_to_pixel((lat_min, lon_min), tileZoom) print(tilePixelMax) print(tilePixelMin) for x in range(tilePixelMin[0], tilePixelMax[0], 256): for y in range(tilePixelMax[1], tilePixelMin[1], 246): get_tiles_by_pixel((x, y)) # get_tiles(lat_max, lon_max) # get_tiles(lat_max, lon_min) # get_tiles(lat_min, lon_max) # get_tiles(lat_min, lon_min) print('下載完畢')
更多的詳情見小專欄文章:GIS之家小專欄spa
文章尾部提供源代碼下載,對本專欄感興趣的話,能夠關注一波.net