/// <summary> /// 缩放至图层 /// </summary> /// <param name="layer">选中图层</param> /// <returns></returns> public void Extent(MapLayer layer) { float[] MBR = new float[4]; layer.GetExtent(MBR); float minX = MBR[0]; float minY = MBR[1]; float maxX = MBR[2]; float maxY = MBR[3]; mOffsetX = MBR[0]; mOffsetY = MBR[3]; if (maxX - minX == 0 && maxY - minY == 0) { return; } else if (maxY - minY == 0) { _DisplayScale = (maxX - minX) / 780; mOffsetY = minY - _DisplayScale * 445; } else if ((maxX - minX) / (maxY - minY) > 1.7528) { _DisplayScale = (maxX - minX) / 780; } else { _DisplayScale = (maxY - minY) / 445; } Refresh(); //触发事件 if (DisplayScaleChanged != null) { DisplayScaleChanged(this); } }
//绘制tiff图层 private void DrawTiffLayer(Graphics g, MapLayer curLayer, int index) { //PictureBox pictureBox = new PictureBox(); float[] MBR = new float[4]; curLayer.GetExtent(MBR); int width, height; //根据比例尺确定图片大小和位置 PointF locationPoint = FromMapPoint(new PointF(MBR[0], MBR[3])); if (locationPoint.Y < this.Height && locationPoint.X < this.Width) { width = (int)((MBR[2] - MBR[0]) / _DisplayScale); height = (int)((MBR[3] - MBR[1]) / _DisplayScale); Dataset ds = Gdal.Open(curLayer.FilePath, Access.GA_ReadOnly); int imgWidth = ds.RasterXSize; int imgHeight = ds.RasterYSize; float[] r = new float[width * height]; Band band = ds.GetRasterBand(1); band.ReadRaster(0, 0, imgWidth, imgHeight, r, width, height, 0, 0); //获取NoData值 double nodata; int handle = 1; band.GetNoDataValue(out nodata, out handle); double[] MinMax = { 0, 0 }; band.ComputeRasterMinMax(MinMax, 0); int i, j; Color newColor = Color.Transparent; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { float value = r[i + j * width]; //像元值 if (value > nodata + 1) { if (MapLayers[index].Style.Styles.Count == 0) { int value1 = 0; if (MinMax[0] < -300)//DEM数据拉伸 { if (value >= -128) { value1 = (int)((value + 128) / (MinMax[1] + 128) * 205.0 + 50); } } else { value1 = (int)((value - MinMax[0]) / (MinMax[1] - MinMax[0]) * 235.0 + 20); } newColor = Color.FromArgb(value1, value1, value1); } else if (MapLayers[index].Style.Styles[0].Rules[0].RasterSymbol.ColorMap.Count == 0) { int value1 = 0; if (MinMax[0] < -300)//DEM数据拉伸 { if (value >= -128) { value1 = (int)((value + 128) / (MinMax[1] + 128) * 205.0 + 50); } } else { value1 = (int)((value - MinMax[0]) / (MinMax[1] - MinMax[0]) * 235.0 + 20); } newColor = Color.FromArgb(value1, value1, value1); newColor = Color.FromArgb(Convert.ToInt32(255 * curLayer.Style.Styles[0].Rules[0].RasterSymbol.Opacity), newColor); } else // 按照Style绘制 { for (int eCount = 0; eCount < curLayer.Style.Styles[0].Rules[0].RasterSymbol.ColorMap.Count - 1; ++eCount) { if (value >= curLayer.Style.Styles[0].Rules[0].RasterSymbol.ColorMap[eCount].Quantity && value <= curLayer.Style.Styles[0].Rules[0].RasterSymbol.ColorMap[eCount + 1].Quantity) { newColor = Color.FromArgb(Convert.ToInt32(255 * curLayer.Style.Styles[0].Rules[0].RasterSymbol.ColorMap[eCount].Opacity), ColorTranslator.FromHtml(curLayer.Style.Styles[0].Rules[0].RasterSymbol.ColorMap[eCount].Color)); break; } } } } g.FillRectangle(new SolidBrush(newColor), new RectangleF(locationPoint.X + i, locationPoint.Y + j, 1, 1)); } } ds.FlushCache(); ds.Dispose(); } }