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

 

posted on   Geography爱好者  阅读(7948)  评论(2编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示