C#拼接地圖瓦片


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

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

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

后記:之后找到了一個更加方便的方法看博客:利用PBS的發布地圖服務

1、切片下載工具網址開源地圖下載器 

下載器不是很好用,不過比起收費試用版的還是可以的,下載器界面如下:

2、數據准備,下載好的數據如下圖

 

3、按鈕點擊事件

  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("拼接完成");
        }

4、將單個切片的像素值賦值給拼接后的圖片

 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));
                }
            }
        }

 

 5、遍歷瓦片並保存拼接后的圖片

      /// <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();
        }

6、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; }
    }

7、拼接效果如下:

8、源碼如下,附帶測試數據:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM