//绘制矢量图层 private void DrawShpLayer(Graphics g, MapLayer curLayer, int index) { Ogr.RegisterAll(); OSGeo.GDAL.Gdal.SetConfigOption("GDAL_DATA", @".\gdal\data"); DataSource ds = Ogr.Open(curLayer.FilePath, 0); //遍历每个图层 for (int i = 0; i < ds.GetLayerCount(); i++) { Layer layer = ds.GetLayerByIndex(i); Feature feature; //遍历图层中每个要素 int fid = 0; while ((feature = layer.GetNextFeature()) != null) { Geometry geom = feature.GetGeometryRef(); if (geom != null) { string type = geom.GetGeometryName(); if (type == "POLYGON") { DrawPolygon(g, geom, index); } else if (type == "MULTIPOLYGON") { DrawMultiPolygon(g, geom, index); } else if (type == "POINT") { DrawPoint(g, geom, index, fid); } ++fid; } } } ds.FlushCache(); ds.Dispose(); }
//绘制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(); } }