ArcGIS緊湊型切片讀取與應用3-緊湊型批量轉分散型(附源碼)

1.前言

        上篇介紹了webgis動態加載解析緊湊型切片的例子,現在我們使用逆向思維實現緊湊型切片轉分散型切片,在實際工作中很有用處,緊湊型切片易於拷貝,但讀取只有部署到Arcgis Server才行。相比分散型切片很通用可以部署在類似Geoewebcache的地圖緩存服務器或者也可以直接部署到web服務器下。

       軟件核心功能:

image

(1)支持切片等級範圍選擇。

(2)支持切圖範圍的選擇,有利於局部數據的更新。

(3)支持多線程解析,充分利用系統資源,加快解析速率。

(4)文件命名格式與Arcgis分散型切片相同。

2.核心代碼解析

        1.首先要解析將輸入的座標轉爲切片對應x、y、z值

/// <summary>
/// 通過經緯度獲取切片位置
/// </summary>
/// <param name="lat_deg">緯度</param>
/// <param name="lon_deg">經度</param>
/// <param name="zoom">切片等級</param>
private double[] ConvertTile(double lat_deg, double lon_deg, int zoom)
{
     double lat_rad = (Math.PI / 180) * lat_deg;
     double n = Math.Pow(2, zoom);
     double xtile = Math.Floor((lon_deg + 180.0) / 360.0 * n);
     double ytile = Math.Floor((1.0 - Math.Log(Math.Tan(lat_rad) + (1 / Math.Cos(lat_rad))) / Math.PI) / 2.0 * n);
     return new double[2] { xtile, ytile };
}

       2.通過左上角座標和右上角座標,解析出指定切片等級下的所有緊湊型切片文件

/// <summary>
/// 獲取某一切片等級下的文件對象
/// </summary>
/// <param name="level"></param>
/// <param name="xy">【x1,y1,x2,y2】</param>
/// <returns></returns>
private List<BundleModel> GetLevelBundle(int level, int[] xy)
{
     List<BundleModel> bundleModelList = new List<BundleModel>();
     int minx = ((xy[0] + 1) / 128) * 128;
     int maxx = ((xy[1] + 1) / 128) * 128;
     int miny = ((xy[2] + 1) / 128) * 128;
     int maxy = ((xy[3] + 1) / 128) * 128;
     int xcount = (maxx - minx) / 128 + 1;
     int ycount = (maxy - miny) / 128 + 1;
     for (int x = 0; x < xcount; x++)
     {
         for (int y = 0; y < ycount; y++)
         {
             BundleModel bm = new BundleModel();
             bm.StartX = minx + (x) * 128;
             bm.StartY = miny + (y) * 128;
             var rGroup = Convert.ToInt32(128 * Convert.ToInt32(bm.StartX / 128));
             var cGroup = Convert.ToInt32(128 * Convert.ToInt32(bm.StartY / 128));
             var bundleBase = getBundlePath(textBox1.Text, level, rGroup, cGroup);
             bm.Level = level;
             bm.BundlxDire = bundleBase + ".bundlx";
             bm.BundleDire = bundleBase + ".bundle";
             bm.BundleName = Path.GetFileNameWithoutExtension(bm.BundleDire);
             bundleModelList.Add(bm);
         }
     }
     return bundleModelList;
}

    3.多線程切片實現,將文件平均分給不同線程

//將文件平均分給各個線程
int count = bundleModelList.Count() / threadcount;
int yu = bundleModelList.Count() % threadcount;

if (count == 0)
{
     for (int i = 0; i < bundleModelList.Count; i++)
     {
         List<BundleModel> model = bundleModelList.Skip(i).Take(1).ToList();
         System.Threading.ThreadPool.QueueUserWorkItem((state) =>
         {
             foreach (var item in model)
             {
                 ToImg(item);
             }
             this.BeginInvoke(new Action(() =>
             {

            }));
         }, model);
     }
}
else
{
     for (int i = 0; i < threadcount; i++)
     {
         List<BundleModel> model = bundleModelList.Skip(i * count).Take(count).ToList();
         if (i < yu)
         {
             model.AddRange(bundleModelList.Skip(threadcount * count + i).Take(1).ToList());
         }
         System.Threading.ThreadPool.QueueUserWorkItem((state) =>
         {
             foreach (var item in model)
             {
                 ToImg(item);
             }
             this.BeginInvoke(new Action(() =>
             {

            }));
         }, model);
     }
}

    4.Arcgis散片文件路徑格式的生成

string L="L"+ zeroPad(z, 2);
string C = "C" + zeroPad(x, 8,1);
string R = "R" + zeroPad(y, 8,1);

//保存路徑
string path = textBox2.Text+"\\"+L+"\\"+R+"\\"+C+ ".png";

//文件命名函數

private string zeroPad(int num, int len,int type=0)
{
     string str = num.ToString();
     if (type==1)
     {
          str = num.ToString("X");
     }
     while (str.Length < len)
     {
         str = "0" + str;
     }
     return str;
}

解析結果展示

image

3.結束

     開啓多線程模式切片的速率比較滿意,通過比較簡單的代碼理解了緊湊型切片的所有的細節,我們現在完全可以實現散片型裝緊湊型的文件,有興趣可以反推一下。所有的源代碼已近上傳到了GitHub,歡迎大家指教。

 

百度網盤鏈接:https://pan.baidu.com/s/1I-Bj3EQSN57pQHvKZ2hBUA   提取碼:lliw

github項目地址:https://github.com/HuHongYong/TilerArcgisBundle

作者:ATtuing

出處:http://www.cnblogs.com/ATtuing

本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章