生活随笔
收集整理的這篇文章主要介紹了
[python] 下载天地图切片地图
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
下載xyz地圖
資源
- 下列為常用xyz路由地址
- 為了避免圖片中出現(xiàn)文字標(biāo)注(道路名稱,建筑物名稱等)本文選擇天地圖tian-vec 作為獲取資源對象
var mapUrl
= {"aMap-img": "http://webst0{1-4}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}","aMap-vec": "http://webrd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}","aMap-roadLabel": "http://webst0{1-4}.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}","tian-img": "http://t{0-7}.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=a4ee5c551598a1889adfabff55a5fc27","tian-roadLabel": "http://t{0-7}.tianditu.gov.cn/DataServer?T=cta_w&x={x}&y={y}&l={z}&tk=a4ee5c551598a1889adfabff55a5fc27","tian-label": "http://t{0-7}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=a4ee5c551598a1889adfabff55a5fc27","tian-vec": "http://t{0-7}.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=a4ee5c551598a1889adfabff55a5fc27","tian-ter": "http://t{0-7}.tianditu.gov.cn/DataServer?T=ter_w&x={x}&y={y}&l={z}&tk=a4ee5c551598a1889adfabff55a5fc27","geoq-vec": "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}","geoq-gray": "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}","geoq-blue": "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}","geoq-warm": "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineStreetWarm/MapServer/tile/{z}/{y}/{x}","geoq-line": "http://cache1.arcgisonline.cn/arcgis/rest/services/SimpleFeature/ChinaBoundaryLine/MapServer/tile/{z}/{y}/{x}","geoq-china": "http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/administrative_division_boundaryandlabel/MapServer/tile/{z}/{y}/{x}","geoq-Hydro": "http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/WorldHydroMap/MapServer/tile/{z}/{y}/{x}","geoq-green": "http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/vegetation/MapServer/tile/{z}/{y}/{x}","google-vec": "http://www.google.cn/maps/vt?lyrs=m@189&gl=cn&x={x}&y={y}&z={z}","google-img": "http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}"};
展示資源
- 最終效果如下,矩形框內(nèi)是我們需要獲取的地圖也就是需要爬蟲下載的
加載地圖點(diǎn)選選擇范圍將范圍繪制在地圖上
- 四個(gè)坐標(biāo)轉(zhuǎn)換成四至得到矩形
xsq
= "120.22940239501227;30.226915680225147;120.28948387694587;30.146807031535218"xsqlist
= xsq
.split
(";")xsq_x_list
= []
xsq_y_list
= []for i
in range(len(xsqlist
)):if i
% 2 == 0:xsq_x_list
.append
(float(xsqlist
[i
]))else:xsq_y_list
.append
(float(xsqlist
[i
]))xsq_x_y_list
= []for x
, y
in zip(xsq_x_list
, xsq_y_list
):xsq_x_y_list
.append
([float(x
), float(y
)])MINX
= min(xsq_x_list
)
MAXX
= max(xsq_x_list
)
MINY
= min(xsq_y_list
)
MAXY
= max(xsq_y_list
)print([MINX
, MINY
])
print([MINX
, MAXY
])
print([MAXX
, MAXY
])
print([MAXX
, MINY
])
print([MINX
, MINY
])
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>高德地圖+ol
</title><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.css"integrity="sha256-rQq4Fxpq3LlPQ8yP11i6Z2lAo82b6ACDgd35CKyNEBw=" crossorigin="anonymous"/><script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.js"integrity="sha256-77IKwU93jwIX7zmgEBfYGHcmeO0Fx2MoWB/ooh9QkBA="crossorigin="anonymous"></script><style>#map {width: 100%;height: 100%;position: absolute;}</style>
</head><body>
<div id="map"></div>
<script type="text/javascript">var gaodeMapLayer = new ol.layer.Tile({title: "titile",source: new ol.source.XYZ({url: mapUrl["tian-vec"]})});var fa = new ol.layer.Vector({source: new ol.source.Vector({features: [new ol.Feature({geometry: new ol.geom.Polygon([[[120.22940239501227, 30.146807031535218],[120.22940239501227, 30.226915680225147],[120.28948387694587, 30.226915680225147],[120.28948387694587, 30.146807031535218],[120.22940239501227, 30.146807031535218],]]),name: 'pg'})]}),});var map = new ol.Map({layers: [gaodeMapLayer, fa],view: new ol.View({center: [120, 30],projection: 'EPSG:4326',zoom: 10}),target: 'map'});map.on('singleclick', function (e) {console.log(map.getEventCoordinate(e.originalEvent));})</script>
</body></html>
爬蟲開始
瀏覽器調(diào)試
- 觀察下圖,我們可以發(fā)現(xiàn)右側(cè)小圖片中的那塊內(nèi)容在左側(cè)出現(xiàn)
獲取地址
使用同樣的方式獲取右下角地址
- 左上角地址 http://t3.tianditu.gov.cn/DataServer?T=vec_w&x=27326&y=13492&l=15&tk=a4ee5c551598a1889adfabff55a5fc27
- 右下角地址 http://t4.tianditu.gov.cn/DataServer?T=vec_w&x=27333&y=13507&l=15&tk=a4ee5c551598a1889adfabff55a5fc27
地址如何使用?
不變的是z 在天地圖url中是l ,本文示例l=15
在地址中x y 存在關(guān)系: x 向右增加,y向下增加
本文x范圍 [ 27326,27333 ]
本文y范圍[ 13492, 13507 ]
到目前所有參數(shù)準(zhǔn)備就緒
- tips: 天地圖的key 需要自己注冊 ,本文已經(jīng)提供了
代碼怎么寫?
單個(gè)圖片下載
- http://t3.tianditu.gov.cn/DataServer?T=vec_w&x=27326&y=13492&l=15&tk=a4ee5c551598a1889adfabff55a5fc27 以此為例
import requests
import os
BASE_PATH
= os
.path
.join
(os
.path
.abspath
(os
.curdir
), 'disc')
print(BASE_PATH
)
headers
= {"Connection": "keep-alive","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
}
x
= 27326
y
= 13492
z
= 15
key
= 'a4ee5c551598a1889adfabff55a5fc27'
url
= "http://t3.tianditu.gov.cn/DataServer?T=vec_w&x={}&y={}&l={}&tk={}".format(x
, y
, z
, key
)
fileName
= os
.path
.join
(BASE_PATH
, "x={}y={}z={}.png".format(x
, y
, z
))
if (os
.path
.exists
(fileName
)) == False:r
= requests
.get
(url
=url
, headers
=headers
)if r
.status_code
== 200:with open(fileName
, 'wb') as f
:for chunk
in r
:f
.write
(chunk
)
下載成功
多圖下載
import requests
import os
BASE_PATH
= os
.path
.join
(os
.path
.abspath
(os
.curdir
), 'disc')
BASE_PATH_res
= os
.path
.join
(os
.path
.abspath
(os
.curdir
), 'result')
headers
= {"Connection": "keep-alive","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
}def download_pic(x
, y
, z
):try:key
= 'a4ee5c551598a1889adfabff55a5fc27'for xi
in x
:for yi
in y
:url
= "http://t3.tianditu.gov.cn/DataServer?T=vec_w&x={}&y={}&l={}&tk={}".format(xi
, yi
, z
, key
)fileName
= os
.path
.join
(BASE_PATH
, "x={}y={}z={}.png".format(xi
, yi
, z
))if (os
.path
.exists
(fileName
)) == False:r
= requests
.get
(url
=url
, headers
=headers
)if r
.status_code
== 200:with open(fileName
, 'wb') as f
:for chunk
in r
:f
.write
(chunk
)else:print("訪問異常")except Exception
as e
:print(e
)passif __name__
== '__main__':x
= range(27326 - 1, 27326 + 2) y
= range(13492 - 1, 13492 + 2) z
= 15picSize
= 256download_pic
(x
, y
, z
)
至此我們將一部分圖片下載完成
拼圖
- 在上面多圖下載結(jié)果中我們看到了很多一張張的圖片和我們在地圖上看到的不太一樣,地圖上的是一整張的,所以我們需要將這個(gè)圖拼起來構(gòu)造一張完整的圖,如下圖
- 我們將圖想象成下圖的形式,只需要知道每一個(gè)格子里面填寫什么(圖片名稱)
def merge_pic(x
, y
, z
):picSize
= 256try:li
= []for xi
in x
:lis
= []for yi
in y
:fileName
= os
.path
.join
(BASE_PATH
, "x={}y={}z={}.png".format(xi
, yi
, z
))lis
.append
(fileName
)li
.append
(lis
)oca
= len(x
)ocb
= len(y
)toImage
= Image
.new
('RGBA', (oca
* picSize
, ocb
* picSize
))for i
in range(oca
):for j
in range(ocb
):fromImge
= Image
.open(li
[i
][j
])picx
= 256 * ipicy
= 256 * jloc
= (picx
, picy
)toImage
.paste
(fromImge
, loc
)toImage
.save
(os
.path
.join
(BASE_PATH_res
, "rs.png"))print("構(gòu)造完成輸出圖片")except Exception
as e
:print(e
)pass
完整demo
import requests
from PIL
import Image
import os
BASE_PATH
= os
.path
.join
(os
.path
.abspath
(os
.curdir
), 'disc')
BASE_PATH_res
= os
.path
.join
(os
.path
.abspath
(os
.curdir
), 'result')
headers
= {"Connection": "keep-alive","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
}def download_pic(x
, y
, z
):try:key
= 'a4ee5c551598a1889adfabff55a5fc27'for xi
in x
:for yi
in y
:url
= "http://t3.tianditu.gov.cn/DataServer?T=vec_w&x={}&y={}&l={}&tk={}".format(xi
, yi
, z
, key
)fileName
= os
.path
.join
(BASE_PATH
, "x={}y={}z={}.png".format(xi
, yi
, z
))if (os
.path
.exists
(fileName
)) == False:r
= requests
.get
(url
=url
, headers
=headers
)if r
.status_code
== 200:with open(fileName
, 'wb') as f
:for chunk
in r
:f
.write
(chunk
)else:print("訪問異常")except Exception
as e
:print(e
)passdef merge_pic(x
, y
, z
):picSize
= 256try:li
= []for xi
in x
:lis
= []for yi
in y
:fileName
= os
.path
.join
(BASE_PATH
, "x={}y={}z={}.png".format(xi
, yi
, z
))lis
.append
(fileName
)li
.append
(lis
)oca
= len(x
)ocb
= len(y
)toImage
= Image
.new
('RGBA', (oca
* picSize
, ocb
* picSize
))for i
in range(oca
):for j
in range(ocb
):fromImge
= Image
.open(li
[i
][j
])picx
= 256 * ipicy
= 256 * jloc
= (picx
, picy
)toImage
.paste
(fromImge
, loc
)toImage
.save
(os
.path
.join
(BASE_PATH_res
, "rs.png"))print("構(gòu)造完成輸出圖片")except Exception
as e
:print(e
)passif __name__
== '__main__':x
= range(27326 - 1, 27326 + 2)y
= range(13492 - 1, 13492 + 2)z
= 15download_pic
(x
, y
, z
)merge_pic
(x
, y
, z
)
注
本文代碼及可視化代碼均放在 github 歡迎star & fork
總結(jié)
以上是生活随笔為你收集整理的[python] 下载天地图切片地图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。