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	itledata";
            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

原文地址:https://www.cnblogs.com/GIScore/p/5965803.html