C#拼接地圖瓦片

爲了在AE程序中使用離線的電子地圖,思路以下:html

利用下載工具下載地圖切片,而後利用C#進行切片拼接成一張圖片,最後使用ArcMap進行地理配準,而後發佈成ArcGIS Server 切片服務供程序使用。工具

今天講的就是如何利用C#拼接切片。測試

後記:以後找到了一個更加方便的方法看博客:利用PBS的發佈地圖服務spa

一、切片下載工具網址開源地圖下載器 .net

下載器不是很好用,不過比起收費試用版的仍是能夠的,下載器界面以下:code

二、數據準備,下載好的數據以下圖htm

 

三、按鈕點擊事件blog

  private void button1_Click(object sender, EventArgs e)
        {
            //調用
            TilesBounds tilesBounds = new TilesBounds();
            tilesBounds.minCol = 109173;
            tilesBounds.maxCol = 109256;
            tilesBounds.minRow = 53284;
            tilesBounds.maxRow = 53363;
            //計算切片個數
            int num = (tilesBounds.maxCol - tilesBounds.minCol) * (tilesBounds.maxRow - tilesBounds.minRow);
            progressBar1.Maximum = num * 2;
            progressBar1.Step = 1;

            label3.Text = num.ToString();
            tilesBounds.zoomLevel = 17;
            string outPutFileName = "f:\\18.png";
            string tilePath = @"C:\data\titledata\";
            CombineTiles(tilesBounds, tilePath, outPutFileName);
            MessageBox.Show("拼接完成");
        }

四、將單個切片的像素值賦值給拼接後的圖片事件

 int a = 0;//用於顯示進度條 ////將單個切片的像素值賦值給拼接後的圖片
        private void SaveBitmapBuffered(Bitmap mainbit, string bmppath, int x, int y)
        {
            a++;
            progressBar1.Value = a;
            x = x * 256;
            y = y * 256;
            label4.Text = a.ToString();
            Application.DoEvents();
            Bitmap bt = new Bitmap(bmppath);
            for (int i = 0; i <256; i++)
            {
                for (int j =0; j <256; j++)
                {
                    mainbit.SetPixel(x + i, y + j, bt.GetPixel(i,j));
                }
            }
        }

 

 五、遍歷瓦片並保存拼接後的圖片圖片

      /// <summary>
        /// 遍歷瓦片
        /// </summary>
        private void CombineTiles(TilesBounds tilesBounds, string tilePath, string outPutFileName)
        {
            if (File.Exists(outPutFileName))
            {
                File.Delete(outPutFileName);
            }
            int imageWidth = 256 * (tilesBounds.maxCol - tilesBounds.minCol + 1);
            int imageHeight = 256 * (tilesBounds.maxRow - tilesBounds.minRow + 1);
            Bitmap memoryimg = new Bitmap(imageWidth, imageHeight);//設置拼接後的圖片大小,注意:若是圖片很大,須要將程序設置成64位
            for (int col = tilesBounds.minCol; col <= tilesBounds.maxCol; col++)
            {
                for (int row = tilesBounds.minRow; row <= tilesBounds.maxRow; row++)
                {
                    try
                    {
                        string sourceFileName = tilePath + tilesBounds.zoomLevel.ToString() + "\\" + col.ToString() + "\\" + row.ToString() + ".png";
                        if (File.Exists(sourceFileName))
                        {
                            SaveBitmapBuffered(memoryimg, sourceFileName, col - tilesBounds.minCol, row - tilesBounds.minRow);
                        }
                        else
                        {
                            Console.WriteLine("不存在:" + sourceFileName);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            memoryimg.Save(outPutFileName);//保存合併後的圖片
            memoryimg.Dispose();
        }

六、TilesBounds類

 class TilesBounds
    {
        public int minCol { get; set; }
        public int maxCol { get; set; }
        public int minRow { get; set; }
        public int maxRow { get; set; }
        public int zoomLevel { get; set; }
    }

七、拼接效果以下:

八、源碼以下,附帶測試數據:

http://pan.baidu.com/s/1jIJgJX0

相關文章
相關標籤/搜索