From 043a7a7d7a06cee2eff393e186a839b7b14af760 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期六, 19 八月 2023 14:41:17 +0800 Subject: [PATCH] 1 --- DataLoader/MainWindow.xaml.cs | 6 .gitignore | 1 DataLoader/CS/GdalHelper.cs | 476 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ExportMap/export.html | 2 4 files changed, 481 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index fe28d4d..32d2f53 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ /ExportMap/TerraBuilder/mpt /ExportMap/TerraBuilder/temp /MySqlCtrl +/CutTiles diff --git a/DataLoader/CS/GdalHelper.cs b/DataLoader/CS/GdalHelper.cs index 390ebca..865acdf 100644 --- a/DataLoader/CS/GdalHelper.cs +++ b/DataLoader/CS/GdalHelper.cs @@ -3,15 +3,18 @@ using OSGeo.OSR; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; -using System.Text; using System.Threading.Tasks; +using System.Windows; +using System.Windows.Markup; namespace DataLoader.CS { public class GdalHelper { + #region 鎴愬憳鍙橀噺+鏋勯�犲嚱鏁�+鍒濆鍖� private static bool isInited = false; private static GdalHelper instance = null; @@ -80,9 +83,11 @@ Ogr.RegisterAll(); Gdal.AllRegister(); } + #endregion public void ReadTiff() { + string outPath = "D:\\xyz\\ce\\xyz"; string tif = "D:\\Moon\\data\\dom_tif\\moon.tif"; Dataset ds = Gdal.Open(tif, Access.GA_ReadOnly); @@ -132,5 +137,474 @@ return new double[] { x, y }; } + + #region GDAL鍒囧浘 + private Geometry GetMinPoint(Dataset ds) + { + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + + string epsg = ds.GetSpatialRef().GetAuthorityCode(null); + double xMin = transform[0]; + double yMin = transform[3]; + + Geometry point = new Geometry(wkbGeometryType.wkbPoint); + point.AddPoint(xMin, yMin, 0); + if ("4326" == epsg) + { + point.AssignSpatialReference(sr4326); + return point; + } + + point.AssignSpatialReference(ds.GetSpatialRef()); + point.TransformTo(sr4326); + return point; + } + + private Geometry GetMaxPoint(Dataset ds) + { + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + + string epsg = ds.GetSpatialRef().GetAuthorityCode(null); + double xMax = transform[0] + (ds.RasterXSize * transform[1]); + double yMax = transform[3] + (ds.RasterYSize * transform[1]); + + Geometry point = new Geometry(wkbGeometryType.wkbPoint); + point.AddPoint(xMax, yMax, 0); + if ("4326" == epsg) + { + point.AssignSpatialReference(sr4326); + return point; + } + + point.AssignSpatialReference(ds.GetSpatialRef()); + point.TransformTo(sr4326); + return point; + } + + public void GenerateTiles() + { + string outPath = "D:\\xyz\\ce\\xyz2"; + string file = "D:\\xyz\\ce\\5_A1.tif"; + if (!File.Exists(file)) return; + + Stopwatch sw = new Stopwatch(); + sw.Start(); // 绋嬪簭寮�濮嬫椂闂� + Dataset ds = null; + try + { + ds = Gdal.Open(file, Access.GA_Update); + if (null == ds) return; + + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + int rasterCount = ds.RasterCount; + Console.WriteLine(string.Format("Origin = ({0}, {1})", transform[0], transform[3])); + Console.WriteLine(string.Format("Pixel Size = ({0}, {1})", transform[1], transform[5])); + + // 4.1 棣栧厛鑾峰彇鍘熷褰卞儚鐨勫湴鐞嗗潗鏍囪寖鍥� + Geometry minPoint = GetMinPoint(ds); + Geometry maxPoint = GetMaxPoint(ds); + double xmin = minPoint.GetX(0); + double ymin = minPoint.GetY(0); + double xmax = maxPoint.GetX(0); + double ymax = maxPoint.GetY(0); + Geometry imageBound = CreatePolygon(xmin, xmax, ymin, ymax, sr4326); + + // 4.2 鑾峰彇鍘熷褰卞儚鐨勫儚绱犲垎杈ㄧ巼 + // 鍘熷鍥惧儚涓滆タ鏂瑰悜鍍忕礌鍒嗚鲸鐜� + double src_w_e_pixel_resolution = (xmax - xmin) / ds.RasterXSize; + // 鍘熷鍥惧儚鍗楀寳鏂瑰悜鍍忕礌鍒嗚鲸鐜� + double src_n_s_pixel_resolution = (ymax - ymin) / ds.RasterYSize; + + // 鑾峰彇Band + Band in_band1 = ds.GetRasterBand(1); + Band in_band2 = ds.GetRasterBand(2); + Band in_band3 = ds.GetRasterBand(3); + //in_band1.Fill(0, 255); // GdalConst.GMF_NODATA + in_band1.SetNoDataValue(0); // -9999 + in_band2.SetNoDataValue(0); + in_band3.SetNoDataValue(0); + + int BufferSize = 256 * 256 * 5; // GdalConst.GDT_Int32 + for (int zoom = 10; zoom <= 18; zoom++) + { + // 4.3 鏍规嵁鍘熷褰卞儚鍦扮悊鑼冨洿姹傝В鍒囩墖琛屽垪鍙� // 缁忕含搴﹁浆鐡︾墖缂栧彿 + int tileRowMax = Lat2tile(ymin, zoom); // 绾害 -90 -> 90 lat + int tileRowMin = Lat2tile(ymax, zoom); + int tileColMin = Lon2tile(xmin, zoom); // 缁忓害 -180 -> 180 lon + int tileColMax = Lon2tile(xmax, zoom); + + Parallel.For(tileColMin, tileColMax + 1, (col) => + { + Parallel.For(tileRowMin, tileRowMax + 1, (row) => + { + // 4.4 姹傚師濮嬪奖鍍忓湴鐞嗚寖鍥翠笌鎸囧畾缂╂斁绾у埆鎸囧畾琛屽垪鍙风殑鍒囩墖浜ら泦 + double tempLatMin = Tile2lat(row + 1, zoom); + double tempLatMax = Tile2lat(row, zoom); + double tempLonMin = Tile2lon(col, zoom); + double tempLonMax = Tile2lon(col + 1, zoom); + + Console.WriteLine(string.Format("{0}\\{1}\\{2}.png", zoom, col, row)); + Geometry tileBound = CreatePolygon(tempLonMin, tempLonMax, tempLatMin, tempLatMax, sr4326); + Geometry intersect = tileBound.Intersection(imageBound); + if (null == intersect) + { + Console.WriteLine(string.Format("{0}\\{1}\\{2}.png锛屼笉瀛樺湪", zoom, col, row)); + return; + } + Envelope env = new Envelope(); + intersect.GetEnvelope(env); + + // 4.5 姹傝В褰撳墠鍒囩墖鐨勫儚绱犲垎杈ㄧ巼(榛樿鍒囩墖澶у皬涓�256*256) + // 鍒囩墖涓滆タ鏂瑰悜鍍忕礌鍒嗚鲸鐜� + double dst_w_e_pixel_resolution = (tempLonMax - tempLonMin) / 256; + // 鍒囩墖鍗楀寳鏂瑰悜鍍忕礌鍒嗚鲸鐜� + double dst_n_s_pixel_resolution = (tempLatMax - tempLatMin) / 256; + + // 4.6 璁$畻浜ら泦鐨勫儚绱犱俊鎭� + // 姹傚垏鍥捐寖鍥村拰鍘熷鍥惧儚浜ら泦鐨勮捣濮嬬偣鍍忕礌鍧愭爣 + int offset_x = (int)((env.MinX - xmin) / src_w_e_pixel_resolution); + int offset_y = (int)Math.Abs((env.MaxY - ymax) / src_n_s_pixel_resolution); + + // 姹傚湪鍒囧浘鍦扮悊鑼冨洿鍐呯殑鍘熷鍥惧儚鐨勫儚绱犲ぇ灏� + int block_xsize = (int)((env.MaxX - env.MinX) / src_w_e_pixel_resolution); + int block_ysize = (int)((env.MaxY - env.MinY) / src_n_s_pixel_resolution); + + // 姹傚師濮嬪浘鍍忓湪鍒囩墖鍐呯殑鍍忕礌澶у皬 + int image_Xbuf = (int)Math.Ceiling((env.MaxX - env.MinX) / dst_w_e_pixel_resolution); + int image_Ybuf = (int)Math.Ceiling(Math.Abs((env.MaxY - env.MinY) / dst_n_s_pixel_resolution)); + + // 姹傚師濮嬪浘鍍忓湪鍒囩墖涓殑鍋忕Щ鍧愭爣 + int imageOffsetX = (int)((env.MinX - tempLonMin) / dst_w_e_pixel_resolution); + int imageOffsetY = (int)Math.Abs((env.MaxY - tempLatMax) / dst_n_s_pixel_resolution); + imageOffsetX = imageOffsetX > 0 ? imageOffsetX : 0; + imageOffsetY = imageOffsetY > 0 ? imageOffsetY : 0; + + // 4.7 浣跨敤GDAL鐨凴eadRaster鏂规硶瀵瑰奖鍍忔寚瀹氳寖鍥磋繘琛岃鍙栦笌鍘嬬缉銆� + // 鎺ㄨ崘鍦ㄥ垏鐗囧墠寤虹珛鍘熷褰卞儚鐨勯噾瀛楀鏂囦欢锛孯eadRaster鍦ㄥ唴閮ㄥ疄鐜颁腑鍙洿鎺ヨ鍙栫浉搴旂骇鍒殑閲戝瓧濉旀枃浠讹紝鎻愰珮鏁堢巼銆� + int[] band1BuffData = new int[BufferSize]; // 256 * 256 * GdalConst.GDT_Int32 + int[] band2BuffData = new int[BufferSize]; + int[] band3BuffData = new int[BufferSize]; + + try + { + in_band1.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, band1BuffData, image_Xbuf, image_Ybuf, 0, 0); + in_band2.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, band2BuffData, image_Xbuf, image_Ybuf, 0, 0); + in_band3.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, band3BuffData, image_Xbuf, image_Ybuf, 0, 0); + + // 4.8 灏嗗垏鐗囨暟鎹啓鍏ユ枃浠� + // 浣跨敤gdal鐨凪EM椹卞姩鍦ㄥ唴瀛樹腑鍒涘缓涓�鍧楀尯鍩熷瓨鍌ㄥ浘鍍忔暟缁� + OSGeo.GDAL.Driver memDriver = Gdal.GetDriverByName("MEM"); + Dataset msmDS = memDriver.Create("msmDS", 256, 256, 4, DataType.GDT_Int32, null); + Band dstBand1 = msmDS.GetRasterBand(1); + Band dstBand2 = msmDS.GetRasterBand(2); + Band dstBand3 = msmDS.GetRasterBand(3); + + // 璁剧疆alpha娉㈡鏁版嵁,瀹炵幇鑳屾櫙閫忔槑 + Band alphaBand = msmDS.GetRasterBand(4); + int[] alphaData = new int[BufferSize]; + for (int index = 0; index < alphaData.Length; index++) + { + if (band1BuffData[index] > 0) alphaData[index] = 255; + } + + // 鍐欏悇涓尝娈垫暟鎹� + dstBand1.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band1BuffData, image_Xbuf, image_Ybuf, 0, 0); + dstBand2.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band2BuffData, image_Xbuf, image_Ybuf, 0, 0); + dstBand3.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band3BuffData, image_Xbuf, image_Ybuf, 0, 0); + alphaBand.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, alphaData, image_Xbuf, image_Ybuf, 0, 0); + + string path = Path.Combine(outPath, zoom.ToString(), col.ToString()); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + + // 浣跨敤PNG椹卞姩灏嗗唴瀛樹腑鐨勫浘鍍忔暟缁勫啓鍏ユ枃浠� + string pngPath = path + "\\" + row + ".png"; + OSGeo.GDAL.Driver pngDriver = Gdal.GetDriverByName("PNG"); + Dataset pngDs = pngDriver.CreateCopy(pngPath, msmDS, 0, null, null, null); + + msmDS.FlushCache(); + pngDs.Dispose(); // 閲婃斁鍐呭瓨 + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + }); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + finally + { + if (null != ds) ds.Dispose(); + sw.Stop(); // 绋嬪簭缁撴潫 + Console.WriteLine("鑰楁椂" + Math.Round(sw.ElapsedMilliseconds / 1000.0, 2) + "s"); // 绉� + } + } + + public void GenerateTiles2() + { + string outPath = "D:\\xyz\\ce\\xyz"; + string file = "D:\\xyz\\ce\\5_A1.tif"; + //string file = "d:\\xyz\\dem\\dem\\33b.tif"; //string file = "D:\\xyz\\dq\\dq.vrt"; + if (!File.Exists(file)) return; + + Stopwatch sw = new Stopwatch(); + sw.Start(); // 绋嬪簭寮�濮嬫椂闂� + Dataset ds = null; + try + { + ds = Gdal.Open(file, Access.GA_Update); + if (null == ds) return; + + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + int rasterCount = ds.RasterCount; + Console.WriteLine(string.Format("Origin = ({0}, {1})", transform[0], transform[3])); + Console.WriteLine(string.Format("Pixel Size = ({0}, {1})", transform[1], transform[5])); + + // 4.1 棣栧厛鑾峰彇鍘熷褰卞儚鐨勫湴鐞嗗潗鏍囪寖鍥� + Geometry minPoint = GetMinPoint(ds); + Geometry maxPoint = GetMaxPoint(ds); + double xmin = minPoint.GetX(0); + double ymin = minPoint.GetY(0); + double xmax = maxPoint.GetX(0); + double ymax = maxPoint.GetY(0); + Geometry imageBound = CreatePolygon(xmin, xmax, ymin, ymax, sr4326); + + // 4.2 鑾峰彇鍘熷褰卞儚鐨勫儚绱犲垎杈ㄧ巼 + // 鍘熷鍥惧儚涓滆タ鏂瑰悜鍍忕礌鍒嗚鲸鐜� + double src_w_e_pixel_resolution = (xmax - xmin) / ds.RasterXSize; + // 鍘熷鍥惧儚鍗楀寳鏂瑰悜鍍忕礌鍒嗚鲸鐜� + double src_n_s_pixel_resolution = (ymax - ymin) / ds.RasterYSize; + + // 鑾峰彇Band + Band in_band1 = ds.GetRasterBand(1); + Band in_band2 = ds.GetRasterBand(2); + Band in_band3 = ds.GetRasterBand(3); + //in_band1.Fill(0, 255); // GdalConst.GMF_NODATA + in_band1.SetNoDataValue(0); // -9999 + in_band2.SetNoDataValue(0); + in_band3.SetNoDataValue(0); + + int BufferSize = 256 * 256 * 5; // GdalConst.GDT_Int32 + for (int zoom = 10; zoom <= 18; zoom++) + { + // 4.3 鏍规嵁鍘熷褰卞儚鍦扮悊鑼冨洿姹傝В鍒囩墖琛屽垪鍙� // 缁忕含搴﹁浆鐡︾墖缂栧彿 + int tileRowMax = Lat2tile(ymin, zoom); // 绾害 -90 -> 90 lat + int tileRowMin = Lat2tile(ymax, zoom); + int tileColMin = Lon2tile(xmin, zoom); // 缁忓害 -180 -> 180 lon + int tileColMax = Lon2tile(xmax, zoom); + //int tileRowMax = lat2tile(latMin, zoom); // 绾害 -90 鈥斺��90 lat + //int tileRowMin = lat2tile(latMax, zoom); // 缁忓害 -180 -- 180 lon + //int tileColMin = lon2tile(lonMin, zoom); + //int tileColMax = lon2tile(lonMax, zoom); + + for (int col = tileColMin; col <= tileColMax; col++) + { + for (int row = tileRowMin; row <= tileRowMax; row++) + { + // 4.4 姹傚師濮嬪奖鍍忓湴鐞嗚寖鍥翠笌鎸囧畾缂╂斁绾у埆鎸囧畾琛屽垪鍙风殑鍒囩墖浜ら泦 + double tempLatMin = Tile2lat(row + 1, zoom); + double tempLatMax = Tile2lat(row, zoom); + double tempLonMin = Tile2lon(col, zoom); + double tempLonMax = Tile2lon(col + 1, zoom); + //double tempLatMin = tile2lat(row + 1, zoom); + //double tempLatMax = tile2lat(row, zoom); + //double tempLonMin = tile2lon(col, zoom); + //double tempLonMax = tile2lon(col + 1, zoom); + + Console.WriteLine(string.Format("{0}\\{1}\\{2}.png", zoom, col, row)); + Geometry tileBound = CreatePolygon(tempLonMin, tempLonMax, tempLatMin, tempLatMax, sr4326); + Geometry intersect = tileBound.Intersection(imageBound); + if (null == intersect) + { + Console.WriteLine(string.Format("{0}\\{1}\\{2}.png锛屼笉瀛樺湪", zoom, col, row)); + continue; + } + Envelope env = new Envelope(); + intersect.GetEnvelope(env); + + // 4.5 姹傝В褰撳墠鍒囩墖鐨勫儚绱犲垎杈ㄧ巼(榛樿鍒囩墖澶у皬涓�256*256) + // 鍒囩墖涓滆タ鏂瑰悜鍍忕礌鍒嗚鲸鐜� + double dst_w_e_pixel_resolution = (tempLonMax - tempLonMin) / 256; + // 鍒囩墖鍗楀寳鏂瑰悜鍍忕礌鍒嗚鲸鐜� + double dst_n_s_pixel_resolution = (tempLatMax - tempLatMin) / 256; + + // 4.6 璁$畻浜ら泦鐨勫儚绱犱俊鎭� + // 姹傚垏鍥捐寖鍥村拰鍘熷鍥惧儚浜ら泦鐨勮捣濮嬬偣鍍忕礌鍧愭爣 + int offset_x = (int)((env.MinX - xmin) / src_w_e_pixel_resolution); + int offset_y = (int)Math.Abs((env.MaxY - ymax) / src_n_s_pixel_resolution); + + // 姹傚湪鍒囧浘鍦扮悊鑼冨洿鍐呯殑鍘熷鍥惧儚鐨勫儚绱犲ぇ灏� + int block_xsize = (int)((env.MaxX - env.MinX) / src_w_e_pixel_resolution); + int block_ysize = (int)((env.MaxY - env.MinY) / src_n_s_pixel_resolution); + + // 姹傚師濮嬪浘鍍忓湪鍒囩墖鍐呯殑鍍忕礌澶у皬 + int image_Xbuf = (int)Math.Ceiling((env.MaxX - env.MinX) / dst_w_e_pixel_resolution); + int image_Ybuf = (int)Math.Ceiling(Math.Abs((env.MaxY - env.MinY) / dst_n_s_pixel_resolution)); + + // 姹傚師濮嬪浘鍍忓湪鍒囩墖涓殑鍋忕Щ鍧愭爣 + int imageOffsetX = (int)((env.MinX - tempLonMin) / dst_w_e_pixel_resolution); + int imageOffsetY = (int)Math.Abs((env.MaxY - tempLatMax) / dst_n_s_pixel_resolution); + imageOffsetX = imageOffsetX > 0 ? imageOffsetX : 0; + imageOffsetY = imageOffsetY > 0 ? imageOffsetY : 0; + + // 4.7 浣跨敤GDAL鐨凴eadRaster鏂规硶瀵瑰奖鍍忔寚瀹氳寖鍥磋繘琛岃鍙栦笌鍘嬬缉銆� + // 鎺ㄨ崘鍦ㄥ垏鐗囧墠寤虹珛鍘熷褰卞儚鐨勯噾瀛楀鏂囦欢锛孯eadRaster鍦ㄥ唴閮ㄥ疄鐜颁腑鍙洿鎺ヨ鍙栫浉搴旂骇鍒殑閲戝瓧濉旀枃浠讹紝鎻愰珮鏁堢巼銆� + int[] band1BuffData = new int[BufferSize]; // 256 * 256 * GdalConst.GDT_Int32 + int[] band2BuffData = new int[BufferSize]; + int[] band3BuffData = new int[BufferSize]; + + try + { + // ReadRaster(int xOff, int yOff, int xSize, int ySize, int[] buffer, int buf_xSize, int buf_ySize, int pixelSpace, int lineSpace) + //in_band1.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, image_Xbuf, image_Ybuf, GdalConst.GDT_Int32, band1BuffData, 0, 0); + //in_band2.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, image_Xbuf, image_Ybuf, GdalConst.GDT_Int32, band2BuffData, 0, 0); + //in_band3.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, image_Xbuf, image_Ybuf, GdalConst.GDT_Int32, band3BuffData, 0, 0); + in_band1.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, band1BuffData, image_Xbuf, image_Ybuf, 0, 0); + in_band2.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, band2BuffData, image_Xbuf, image_Ybuf, 0, 0); + in_band3.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, band3BuffData, image_Xbuf, image_Ybuf, 0, 0); + + // 4.8 灏嗗垏鐗囨暟鎹啓鍏ユ枃浠� + // 浣跨敤gdal鐨凪EM椹卞姩鍦ㄥ唴瀛樹腑鍒涘缓涓�鍧楀尯鍩熷瓨鍌ㄥ浘鍍忔暟缁� + OSGeo.GDAL.Driver memDriver = Gdal.GetDriverByName("MEM"); + Dataset msmDS = memDriver.Create("msmDS", 256, 256, 4, DataType.GDT_Int32, null); + Band dstBand1 = msmDS.GetRasterBand(1); + Band dstBand2 = msmDS.GetRasterBand(2); + Band dstBand3 = msmDS.GetRasterBand(3); + + // 璁剧疆alpha娉㈡鏁版嵁,瀹炵幇鑳屾櫙閫忔槑 + Band alphaBand = msmDS.GetRasterBand(4); + int[] alphaData = new int[BufferSize]; + for (int index = 0; index < alphaData.Length; index++) + { + if (band1BuffData[index] > 0) + { + alphaData[index] = 255; + } + } + + // 鍐欏悇涓尝娈垫暟鎹� + // WriteRaster(int xOff, int yOff, int xSize, int ySize, int[] buffer, int buf_xSize, int buf_ySize, int pixelSpace, int lineSpace) + //dstBand1.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band1BuffData); + //dstBand2.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band2BuffData); + //dstBand3.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band3BuffData); + //alphaBand.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, alphaData); + dstBand1.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band1BuffData, image_Xbuf, image_Ybuf, 0, 0); + dstBand2.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band2BuffData, image_Xbuf, image_Ybuf, 0, 0); + dstBand3.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band3BuffData, image_Xbuf, image_Ybuf, 0, 0); + alphaBand.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, alphaData, image_Xbuf, image_Ybuf, 0, 0); + + //String pngPath = "D:\\xyz\\temp" + "\\" + zoom + "c" + col + "r" + row + ".png"; + //Console.WriteLine("pngPath=" + pngPath); + string path = Path.Combine(outPath, zoom.ToString(), col.ToString()); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + + // 浣跨敤PNG椹卞姩灏嗗唴瀛樹腑鐨勫浘鍍忔暟缁勫啓鍏ユ枃浠� + string pngPath = path + "\\" + row + ".png"; + OSGeo.GDAL.Driver pngDriver = Gdal.GetDriverByName("PNG"); + Dataset pngDs = pngDriver.CreateCopy(pngPath, msmDS, 0, null, null, null); + + // 閲婃斁鍐呭瓨 + msmDS.FlushCache(); + pngDs.Dispose(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + } + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + finally + { + if (null != ds) ds.Dispose(); + sw.Stop(); // 绋嬪簭缁撴潫 + Console.WriteLine("鑰楁椂" + Math.Round(sw.ElapsedMilliseconds / 1000.0, 2) + "s"); // 绉� + } + } + + /// <summary> + /// 鍒涘缓澶氳竟褰� + /// </summary> + public Geometry CreatePolygon(double xmin, double xmax, double ymin, double ymax, SpatialReference sr) + { + string kwt = string.Format("POLYGON (({0} {1},{2} {3},{4} {5},{6} {7},{0} {1}))", xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin); + Geometry geo = Geometry.CreateFromWkt(kwt); + geo.AssignSpatialReference(sr); + + return geo; + } + + /// <summary> + /// 缁忓害杞摝鐗囩紪鍙� + /// </summary> + public static int Lon2tile(double lon, int zoom) + { + return (int)(Math.Floor((lon + 180) / 360 * Math.Pow(2, zoom))); + } + + /// <summary> + /// 绾害杞摝鐗囩紪鍙� + /// </summary> + public static int Lat2tile(double lat, int zoom) + { + return (int)(Math.Floor((1 - Math.Log(Math.Tan(lat * Math.PI / 180) + 1 / Math.Cos(lat * Math.PI / 180)) / Math.PI) / 2 * Math.Pow(2, zoom))); + } + + /// <summary> + /// 鐡︾墖缂栧彿杞粡搴� + /// </summary> + public static double Tile2lon(int col, int zoom) + { + return col / Math.Pow(2.0, zoom) * 360.0 - 180; + } + + /// <summary> + /// 鐡︾墖缂栧彿杞含搴� + /// </summary> + public static double Tile2lat(int row, int zoom) + { + double n = Math.PI - (2.0 * Math.PI * row) / Math.Pow(2.0, zoom); + return ToDegrees(Math.Atan(Math.Sinh(n))); + } + + /// <summary> + /// 甯告暟e + /// </summary> + public static readonly double E = 2.7182818284590452354; + + /// <summary> + /// 甯告暟Pi + /// </summary> + public static readonly double PI = 3.14159265358979323846; + + /// <summary> + /// 搴﹁浆寮у害 + /// </summary> + public static double ToRadians(double angdeg) + { + return angdeg / 180.0 * Math.PI; + } + + /// <summary> + /// 寮у害杞害 + /// </summary> + public static double ToDegrees(double angrad) + { + return angrad * 180.0 / Math.PI; + } + #endregion } } diff --git a/DataLoader/MainWindow.xaml.cs b/DataLoader/MainWindow.xaml.cs index f38af9d..413e3f7 100644 --- a/DataLoader/MainWindow.xaml.cs +++ b/DataLoader/MainWindow.xaml.cs @@ -81,14 +81,16 @@ LogOut.Info("************ 搴旂敤绋嬪簭鍚姩鎴愬姛锛� ************"); } - // 鐧诲綍 + // 璁剧疆 private void Login_MouseLeftButtonDown(object sender, RoutedEventArgs e) { //win = new LoginWin(); //this.Hide(); //win.Show(); - SetLoginInfo(); + //SetLoginInfo(); + //GdalHelper.Instance.ReadTiff(); + GdalHelper.Instance.GenerateTiles(); } public void SetLoginInfo() diff --git a/ExportMap/export.html b/ExportMap/export.html index 38f8400..2655c71 100644 --- a/ExportMap/export.html +++ b/ExportMap/export.html @@ -10,7 +10,7 @@ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="js/jquery.1.12.4.js"></script> <script> - var token = "50b3fd35-7f5b-471e-974c-6240da4b3855"; + var token = "384d7382-ea48-400b-bc92-58cb2b28bcbc"; $(function () { $("#token").html(token); -- Gitblit v1.9.3