基于Mapnik创建切片的方案

本文介绍如何使用Python和Mapnik库自定义生成地图切片及格网,包括生成格网函数、从Shapefile读取规则格网并转换为Box格式的方法,以及通过调整地图zoom级别生成不同位置切片的技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

引用 引用 引用根据这些引用内容,警告信息"warning: in the working copy of 'androidPrivacy.json', LF will be replaced by CRLF the next time Git touches it"是在提醒用户在Git操作中,回车和换行的转换将会发生。这是因为Git会在提交时对换行符进行转换以保持统一性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [warning: in the working copy of ‘App.vue‘, LF will be replaced by CRLF the next time Git touches ...](https://blog.youkuaiyun.com/weixin_44953057/article/details/129644219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Git: ‘LF will be replaced by CRLF the next time Git touches it‘ 问题解决与思考](https://blog.youkuaiyun.com/Babylonxun/article/details/126598477)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [warning: in the working copy of ‘...‘, LF will be replaced by CRLF the next time Git touche](https://blog.youkuaiyun.com/weixin_55252589/article/details/129017650)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值