为了在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; } }
七、拼接效果以下:
八、源码以下,附带测试数据: