抓取Mapbar地图切片的一个算法

这个算法去年的时候就写了,自己就是抓着玩,也算是验证了这个算法的可行性,今天特向大家分享一下,主要是根据Mapbarapi里的绘图算法写出来的,实在没什么技术含量。

有想玩的同学可以试试,不过因此引起的后果本人概不负责,请慎重啊!

算法使用PHP写的,将下面代码保存为一个PHP文件后在终端运行: $ php -f filename.php一下就可以了。

代码如下:

/**
 * 抓取mapbar切片的算法 by CrossYou at 2011/01/22
 * 
 * @version 1.0.0
 * @link	http://crossyou.cn/
 */
$mapbarImgRoot = './mapbar/';

//每层地图切片的文件夹名称
$levelFolder = array('W','0','1','2','3','4','5','6','7','8','9','10','11','12','13','16');

//每层地图切片所跨的经度数
$cutImgLonRange=array(90,40,20,10,5,2,1,0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002,0.001);

//每层地图切片所跨的维度数
$cutImgLatRange=array(90*0.8,40*0.8,20*0.8,10*0.8,5*0.8,2*0.8,0.8,0.5*0.8,0.2*0.8,0.1*0.8,0.05*0.8,0.02*0.8,0.01*0.8,0.005*0.8,0.002*0.8,0.001*0.8);

$blockSize=array(10,10,10,10,10,10,10,10,10,10,50,50,50,50,50,50);

for($zoomLevel=0;$zoomLevel<16;$zoomLevel++){

	$clipXNum=ceil(360/$cutImgLonRange[$zoomLevel]);
	$clipYNum=ceil(90/$cutImgLatRange[$zoomLevel]);
	
	
	$clipLonRange = $cutImgLonRange[$zoomLevel];
	$clipLatRange = $cutImgLatRange[$zoomLevel];
	
	$clipXGap = -180-$clipLonRange/2;
	
	//从缩放级别5开始,只抓取中国范围内的地图,因为此级别以后的,非国内的都是背景图,抓了无意义。
	if($zoomLevel>3){
		
		//--------中国范围的地图
		$west = 73;
		$east = 136;
		$north = 54;
		$south = 3;
		
		$chinaLonRange = 63;//中国经度跨度
		$chinaLatRange = 51;//中国维度跨度
		
		$chinaClipXNum = ceil($chinaLonRange/$clipLonRange);
		$chinaClipYNum = ceil($chinaLatRange/$clipLatRange);
		
		for($i=0;$i<$chinaClipXNum;$i++){
			$lon = $west+$clipLonRange*$i;
			
			for($j=0;$j<$chinaClipYNum;$j++){
				$lat = $south+$clipLatRange*$j;
				
				$clipXNo =  floor($lon/$cutImgLonRange[$zoomLevel]);
				$clipYNo =  floor($lat/$cutImgLatRange[$zoomLevel]);
			
				$clipXNo=($clipXNo)%$clipXNum;
				if($clipXNo>=$clipXNum/2)$clipXNo-=$clipXNum;
				if($clipXNo<-$clipXNum/2)$clipXNo+=$clipXNum;
			
				echo "\n新的切片... => \n";
				echo '经纬度:'.$lon.' , '.$lat."\n";
				echo "-------------------------------\n";
				echo '切片序号:'.$clipXNo.' , '.$clipYNo."\n";
				echo "-------------------------------\n";
				
				//切片分文件夹存放位置
				$folderXNo = (int)floor($clipXNo/$blockSize[$zoomLevel]);
				//切片分文件夹存放位置
				$folderYNo = (int)floor($clipYNo/$blockSize[$zoomLevel]);
				
				if($folderXNo<0){
					$folderXNo+=1;
				}
				
				if($folderYNo<0){
					$folderYNo+=1;
				}
				
				$fileXNo =($clipXNo)-$folderXNo*$blockSize[$zoomLevel];
				$fileYNo =($clipYNo)-$folderYNo*$blockSize[$zoomLevel]; 
				
				$imgPre = 'http://img.mapbar.com/maplite/mapbank/mapbar/';
				
				$imgDir = $levelFolder[$zoomLevel].'/'.$folderXNo.'_'.$folderYNo.'/';
				
				$imgName = $fileXNo.'_'.$fileYNo.'.png';
				
				$imgUrl = $imgPre.$imgDir.$imgName;
				
				$localImgDir = $mapbarImgRoot.$imgDir;
				
				$localImgName = $localImgDir.$imgName;
				if (!is_file($localImgName)){
					createdir($localImgDir);
					if(copy($imgUrl,$localImgName)){
						echo '图片:=> '.$imgDir.$imgName." 成功下载到本地\n\n";
					}
				}
			}
		}
	}else{
		for($i= -$clipXNum/2;$i<=$clipXNum/2;$i++){
			$lon = $clipLonRange*$i;
			if($i<0)$lon+=$clipLonRange/2;
			if($i==0)continue;
			if($i>0)$lon-=$clipLonRange/2;
		
			for($j=-$clipYNum;$j<=$clipYNum;$j++){
				
				$lat = $clipLatRange*$j;
				
				if($j<0)$lat+=$clipLatRange/2;
				if($j==0)continue;
				if($j>0)$lat-=$clipLatRange/2;
				
				$clipXNo =  floor($lon/$cutImgLonRange[$zoomLevel]);
				$clipYNo =  floor($lat/$cutImgLatRange[$zoomLevel]);
				
				$clipXNo=($clipXNo)%$clipXNum;
				if($clipXNo>=$clipXNum/2)$clipXNo-=$clipXNum;
				if($clipXNo<-$clipXNum/2)$clipXNo+=$clipXNum;
				
				echo "\n新的切片... => \n";
				echo '经纬度:'.$lon.' , '.$lat."\n";
				echo "-------------------------------\n";
				echo '切片序号:'.$clipXNo.' , '.$clipYNo."\n";
				echo "-------------------------------\n";
				
				//切片分文件夹存放位置
				$folderXNo = (int)floor($clipXNo/$blockSize[$zoomLevel]);
				//切片分文件夹存放位置
				$folderYNo = (int)floor($clipYNo/$blockSize[$zoomLevel]);
				
				if($folderXNo<0){
					$folderXNo+=1;
				}
				
				if($folderYNo<0){
					$folderYNo+=1;
				}
				
				$fileXNo =($clipXNo)-$folderXNo*$blockSize[$zoomLevel];
				$fileYNo =($clipYNo)-$folderYNo*$blockSize[$zoomLevel]; 
				
				$imgPre = 'http://img.mapbar.com/maplite/mapbank/mapbar/';
				
				$imgDir = $levelFolder[$zoomLevel].'/'.$folderXNo.'_'.$folderYNo.'/';
				
				$imgName = $fileXNo.'_'.$fileYNo.'.png';
				
				$imgUrl = $imgPre.$imgDir.$imgName;
				
				$localImgDir = $mapbarImgRoot.$imgDir;
				
				$localImgName = $localImgDir.$imgName;
				if (!is_file($localImgName)){
					createdir($localImgDir);
					if(copy($imgUrl,$localImgName)){
						echo '图片:=> '.$imgDir.$imgName." 成功下载到本地\n\n";
					}
				}
				
			}
		}
	}
}

/**
 * 创建多级目录 摘自网络
 * 如有好的建议,请到 http://crossyou.cn/给我留言 谢谢!
 * 
 * @since 1.0.5
 * @param string $dir
 */
function createdir($dir){
	
	$array_dir=explode("/",$dir);//把多级目录分别放到数组中
	
	$depth = count($array_dir);
	$path = '';
	for($i=0;$i<$depth;$i++){
		
		$path .= $array_dir[$i]."/";
		
		if(!is_dir($path)){
			mkdir($path);
		}
	}
}

《全文完》

这篇文章目前有3条评论

  1. Paran 2011-03-27 10:44

    你这个主题顶部的日历还很好来
    哈哈 (胜利)

    沙发王 !
  2. Joyce 2011-07-24 09:19

    Full of salient ptonis. Don’t stop believing or writing!

    板凳党 !
Leave a Reply

(必填项)

(必填项)

(可选)