本片介绍python爬虫抓取天地图离线瓦片并在cesium里离线调用。
1.天地图数据:先看天地图数据的格式:图片来源于http://www.mamicode.com/info-detail-2681955.html
以影像为例,数据可以抓取1-18级的,但最好不要级别太高否则内存溢出!!!。更高级的将为空。对于地形只能是1-14级且地形的比较特别就是每张地形切片对应的t0-t7会不一样,估计就是为了反扒才这样设计吧。处理办法还是循环url里t后面的数字,当返回状态不成功时改变数字。
2.爬虫:python2.7网上其他人也有但要注意python2还是3的版本因为urllib的使用是不一样的,被误导还好小编精通爬虫能看出问题
# coding=utf-8
import re
import urllib
import os
import random
import math
import urllib2
import sys
sys.setrecursionlimit(10000)
agents = [
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5',
'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.310.0 Safari/532.9',
'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.514.0 Safari/534.7',
'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/9.0.601.0 Safari/534.14',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/10.0.601.0 Safari/534.14',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.27 (KHTML, like Gecko) Chrome/12.0.712.0 Safari/534.27',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1']
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)
for zoom in range(1,8):#地图级别级桌上右下角范围
lefttop = deg2num(29, 105, zoom)
rightbottom = deg2num(29, 106, zoom)
i = 0
for x in range(lefttop[0], rightbottom[0]):
for y in range(lefttop[1], rightbottom[1]):
imgUrl = "http://t0.tianditu.com/DataServer?T=img_w&x=" + str(x) + "&y=" + str(y) + "&l=" + str(zoom) + "&tk=自己的key"
print(imgUrl )
i+=1
if not os.path.exists( "D:\\tianditu\\img_w\\"+ str(zoom) +"\\"+str(x)):
os.makedirs( "D:\\tianditu\\img_w\\"+ str(zoom) +"\\"+str(x) )
try:
header = {
'User-Agent': random.choice(agents),
'Cookie': 'AspxAutoDetectCookieSupport=1',
}
request = urllib2.Request(imgUrl, None, header)
response = urllib2.urlopen(request)
imgName = str(y) + ".png"
os.chdir(r"D:/tianditu/img_w/"+ str(zoom)+"/"+str(x))
f = open( imgName , 'wb')
f.write(response.read())
f.close()
except Exception:
print( imgUrl )
print(str(i))
print('end')
成功后如下
很开心对吧。接着最后一步离线调用:
3.离线调用:
调用可以本地也可以用tomcat或iis或者nigix代理。最简单的还是本地。没啥不同就是质疑url路径写正确就ok。
var viewer = new Cesium.Viewer('cesiumContainer', {
geocoder: false, //是否显示地名查找控件
infoBox: false,
animation: true, //是否显示动画控件(左下方那个)
timeline: true, //是否显示时间线控件
shadows: false, // 阴影是否被太阳投射
showldAnimate: true, //让场景中的动画自动播放
sceneModePicker: false, //是否显示投影方式控件
fullscreenButton: false, //全屏按钮不显示
homeButton: false,
navigationHelpButton: false,//帮助按钮
sceneMode: Cesium.SceneMode.SCENE3D, //设定3维地图的默认场景模式:Cesium.SceneMode.SCENE2D、Cesium.SceneMode.SCENE3D、Cesium.SceneMode.MORPHING
scene3DOnly: true,//如果设置为true,则所有几何图形以3D模式绘制以节约GPU资源
baseLayerPicker: false, //是否显示图层选择控件
imageryProvider: _bingMap,
imageryProvider:new Cesium.UrlTemplateImageryProvider({
url:'../../../tianditu/img_w/{z}/{x}/{y}.png',
fileExtension : "png",
maximumLevel: 18
}),
// terrainProvider: cesiumTerrainProvider
});
如果你想tomcat也很简单,把文件夹放到tomcat的webpack目录下,怎么允许跨域小编就不教你。