基于Mapnik创建切片的方案

1 环境与工具    

操作系统:win7 64bit ; python2.7.14 32bit; mapnik 2.2。

2 切片实现:

2.1 生成格网的函数:

    1)在指定范围内将坐标划分为若干等份生成格网:

def createGrid02(envelopeParam,widthCount=1,heightCont=1):
"""
把输入的mapnik.Box2d按指定的行列数目进行划分,最后返回划分的格网
"""
reslut={}
print envelopeParam.minx,envelopeParam.maxx
s=abs(envelopeParam.maxx-envelopeParam.minx)
width=abs(envelopeParam.minx-envelopeParam.maxx)/(widthCount*1.0)
height=abs(envelopeParam.maxy-envelopeParam.miny)/(heightCont*1.0)
for i in range(0,widthCount):
for j in range(0,heightCont):
box=mapnik.Box2d(envelopeParam.minx+i*width,envelopeParam.miny+j*height,envelopeParam.minx+(i+1)*width,envelopeParam.miny+(j+1)*height)
reslut[(i,j)]=box
return reslut
    2) 在指定范围内根据指定等级自动生成格网:
def createGrid03(envelopeParam,zoom):
"""
根据输入的Box2d范围 和瓦片等级zoom
生成在该范围的上对应等级的格网1~zoom(不包含zoom 级)
每一级对应的切片横纵坐标数目是Power(2,zoom-1)
返回值是字典{(行号,列号,zoom):box}
行、列、等级号从1开始
"""
reslut={}
for i in range(1,zoom):
num=2**(zoom-1)
tempReslut=createGrid02(envelopeParam,widthCount=num,heightCont=num)
for item in tempReslut:
reslut[(item[0][0],item[0][1],i)]=tempReslut[item]
return reslut

    3)从shapefile数据读取规则格网转换为mapnik的box,可以通过shapefile文件自定义生成切片的范围:(需要安装python环境下的gdal)

def getBoxFromShp(shapeFile):
"""
根据shapefile的格网生成grid列表
返回boxs的列表(fid,Box2d)
"""
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","NO")
gdal.SetConfigOption("SHAPE_ENCODING","")
#i=0
ogr.RegisterAll()
ds=ogr.Open(shapeFile,0)
if ds==None:
return
olayer=ds.GetLayerByIndex(0)
if olayer==None:
return
olayer.ResetReading()
oFeature=olayer.GetNextFeature()
if not (olayer.GetGeomType()==3):
return
boxlist=[]
while oFeature is not None:
fid=oFeature.GetFID()
print fid
oGeometry=oFeature.GetGeometryRef()
envelope=oGeometry.GetEnvelope()
box=mapnik.Box2d(envelope[0],envelope[2],envelope[1],envelope[3])
boxlist.append((fid,box))
oFeature=olayer.GetNextFeature()
return boxlist

根据以上的函数基本可以满足自定义格网要求

2.2 生成切片

通过不断的更改map的zoom范围然后产生不同位置上的切片,方法如下:

            map.zoom_to_box(Box b)# b是mapnik 中的Box          
            mapnik.save_map(map,mapXml)
            mapnik.render_to_file(map,png)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值