/// <summary> /// 分级渲染单个图层 /// </summary> /// <param name="g"></param> /// <param name="layer"></param> private void RenderAsClassBreaksRenderer(Graphics g, Layer layer) { ClassBreaksRenderer cRenderer = layer.Renderer as ClassBreaksRenderer; // 强转,使图层渲染器为分级渲染器 string field = cRenderer.Field; //图层为点图层 if (layer.FeatureType == typeof(PointD)) { for (int i = 0; i < layer.Features.Count; i++) { double value = Convert.ToDouble(layer.Table.Rows[i][field]); //获取feature的相应值 PointSymbol pSymbol = cRenderer.FindSymbol(value) as PointSymbol; //获取符号类型 PointD point = layer.Features[i] as PointD; //获取id为i的点 //转换为屏幕坐标,绘制 PointD screenPoint = FromMapPoint(point); PointF DrawPoint = new PointF((float)screenPoint.X, (float)screenPoint.Y); pSymbol.DrawPoint(g, DrawPoint); } } //图层为折线图层 else if (layer.FeatureType == typeof(Polyline)) { for (int i = 0; i < layer.Features.Count; i++) { double value = Convert.ToDouble(layer.Table.Rows[i][field]); //获取feature的相应值 LineSymbol lSymbol = cRenderer.FindSymbol(value) as LineSymbol; //获取符号类型 Polyline line = layer.Features[i] as Polyline; //获得折线 List <PointF> screenLine = new List <PointF>(); for (int j = 0; j < line.Data.Count; j++) { PointD point = FromMapPoint(line.Data[j]); //坐标转换 screenLine.Add(new PointF((float)point.X, (float)point.Y)); //添加到绘制队列 } lSymbol.DrawLine(g, screenLine.ToArray()); //绘制 } } //图层为复合折线图层 else if (layer.FeatureType == typeof(MultiPolyline)) { for (int i = 0; i < layer.Features.Count; i++) { double value = Convert.ToDouble(layer.Table.Rows[i][field]); //获取feature的相应值 LineSymbol lSymbol = cRenderer.FindSymbol(value) as LineSymbol; //获取符号类型 MultiPolyline lines = layer.Features[i] as MultiPolyline; //获得复合折线 for (int j = 0; j < lines.Data.Count; j++) { Polyline line = lines.Data[j]; List <PointF> screenLine = new List <PointF>(); for (int k = 0; k < line.Data.Count; k++) { PointD point = FromMapPoint(line.Data[k]); //坐标转换 screenLine.Add(new PointF((float)point.X, (float)point.Y)); //添加到绘制队列 } lSymbol.DrawLine(g, screenLine.ToArray()); //绘制 } } } //图层为多边形图层 else if (layer.FeatureType == typeof(Polygon)) { for (int i = 0; i < layer.Features.Count; i++) { double value = Convert.ToDouble(layer.Table.Rows[i][field]); //获取feature的相应值 PolygonSymbol pSymbol = cRenderer.FindSymbol(value) as PolygonSymbol; //获取符号类型 Polygon polygon = layer.Features[i] as Polygon; //获得折线 List <PointF> screenPolygon = new List <PointF>(); for (int j = 0; j < polygon.Data.Count; j++) { PointD point = FromMapPoint(polygon.Data[j]); //坐标转换 screenPolygon.Add(new PointF((float)point.X, (float)point.Y)); //添加到绘制队列 } pSymbol.DrawPolygon(g, screenPolygon.ToArray()); //绘制 } } //图层为复合折线图层 else if (layer.FeatureType == typeof(MultiPolygon)) { for (int i = 0; i < layer.Features.Count; i++) { double value = Convert.ToDouble(layer.Table.Rows[i][field]); //获取feature的相应值 PolygonSymbol pSymbol = cRenderer.FindSymbol(value) as PolygonSymbol; //获取符号类型 MultiPolygon polygons = layer.Features[i] as MultiPolygon; //获得复合折线 for (int j = 0; j < polygons.Data.Count; j++) { Polygon polygon = polygons.Data[j]; List <PointF> screenPolygon = new List <PointF>(); for (int k = 0; k < polygon.Data.Count; k++) { PointD point = FromMapPoint(polygon.Data[k]); //坐标转换 screenPolygon.Add(new PointF((float)point.X, (float)point.Y)); //添加到绘制队列 } pSymbol.DrawPolygon(g, screenPolygon.ToArray()); //绘制 } } } }
private void RenderLabel(Graphics g, Layer layer) { //获取标注的风格 LabelStyle lStyle = layer.LabelStyle; string sField = lStyle.Field; Font sFont = lStyle.Font; Brush sBrush = new SolidBrush(lStyle.Color); try { //点绘制注记 if (layer.FeatureType == typeof(PointD)) { for (int i = 0; i < layer.Features.Count; i++) { //获取注记位置 PointD LabelLocation = new PointD(); LabelLocation.X = (layer.Features[i].Box.MinX + layer.Features[i].Box.MaxX) / 2; LabelLocation.Y = (layer.Features[i].Box.MinY + layer.Features[i].Box.MaxY) / 2; LabelLocation = FromMapPoint(LabelLocation); PointF screenLocation = new PointF(); screenLocation.X = (float)LabelLocation.X; screenLocation.Y = (float)LabelLocation.Y; //获取注记文本大小,调整注记中心 string labelStr = layer.Table.Rows[i][sField].ToString(); SizeF labelSize = g.MeasureString(labelStr, sFont); screenLocation.X -= labelSize.Width / 2; screenLocation.Y -= labelSize.Height / 2; screenLocation.Y -= 8; //绘图 g.DrawString(labelStr, sFont, sBrush, screenLocation); } } //折线绘制注记 else if (layer.FeatureType == typeof(Polyline)) { for (int i = 0; i < layer.Features.Count; i++) { Polyline polyline = layer.Features[i] as Polyline; int midindex = (int)(polyline.Data.Count / 2); //获取注记位置,为下标中点对应点 PointD LabelLocation = new PointD(); LabelLocation.X = polyline.Data[midindex].X; LabelLocation.Y = polyline.Data[midindex].Y; LabelLocation = FromMapPoint(LabelLocation); PointF screenLocation = new PointF(); screenLocation.X = (float)LabelLocation.X; screenLocation.Y = (float)LabelLocation.Y; //获取注记文本大小,调整注记中心 string labelStr = layer.Table.Rows[i][sField].ToString(); SizeF labelSize = g.MeasureString(labelStr, sFont); screenLocation.X -= labelSize.Width / 2; screenLocation.Y -= labelSize.Height / 2; //绘图 g.DrawString(labelStr, sFont, sBrush, screenLocation); } } //复合折线绘制注记 else if (layer.FeatureType == typeof(MultiPolyline)) { for (int i = 0; i < layer.Features.Count; i++) { MultiPolyline multiline = layer.Features[i] as MultiPolyline; int midindex = (int)(multiline.Data[0].Data.Count / 2); //获取注记位置 PointD LabelLocation = new PointD(); LabelLocation.X = multiline.Data[0].Data[midindex].X; LabelLocation.Y = multiline.Data[0].Data[midindex].Y; LabelLocation = FromMapPoint(LabelLocation); PointF screenLocation = new PointF(); screenLocation.X = (float)LabelLocation.X; screenLocation.Y = (float)LabelLocation.Y; //获取注记文本大小,调整注记中心 string labelStr = layer.Table.Rows[i][sField].ToString(); SizeF labelSize = g.MeasureString(labelStr, sFont); screenLocation.X -= labelSize.Width / 2; screenLocation.Y -= labelSize.Height / 2; //绘图 g.DrawString(labelStr, sFont, sBrush, screenLocation); } } //面、多面的绘制注记 else { for (int i = 0; i < layer.Features.Count; i++) { //获取注记位置 PointD LabelLocation = new PointD(); LabelLocation.X = (layer.Features[i].Box.MinX + layer.Features[i].Box.MaxX) / 2; LabelLocation.Y = (layer.Features[i].Box.MinY + layer.Features[i].Box.MaxY) / 2; LabelLocation = FromMapPoint(LabelLocation); PointF screenLocation = new PointF(); screenLocation.X = (float)LabelLocation.X; screenLocation.Y = (float)LabelLocation.Y; //获取注记文本大小,调整注记中心 string labelStr = layer.Table.Rows[i][sField].ToString(); SizeF labelSize = g.MeasureString(labelStr, sFont); screenLocation.X -= labelSize.Width / 2; screenLocation.Y -= labelSize.Height / 2; //绘图 g.DrawString(labelStr, sFont, sBrush, screenLocation); } } } catch { throw new Exception(); } }
/// <summary> /// 简单渲染单个图层 /// </summary> /// <param name="g"></param> /// <param name="layer"></param> private void RenderAsSimpleRenderer(Graphics g, Layer layer) { SimpleRenderer sRenderer = layer.Renderer as SimpleRenderer; // 强转,使图层渲染器为唯一值渲染器 //图层类型为点 if (layer.FeatureType == typeof(PointD)) { PointSymbol pSymbol = sRenderer.Symbol as PointSymbol; //强转,使符号为点符号 for (int i = 0; i < layer.Features.Count; i++) { PointD point = layer.Features[i] as PointD; //转换为屏幕坐标,绘制 PointD screenPoint = FromMapPoint(point); PointF DrawPoint = new PointF((float)screenPoint.X, (float)screenPoint.Y); pSymbol.DrawPoint(g, DrawPoint); } } //图层类型为折线 else if (layer.FeatureType == typeof(Polyline)) { LineSymbol lSymbol = sRenderer.Symbol as LineSymbol; //强转,使符号为线符号 for (int i = 0; i < layer.Features.Count; i++) { Polyline line = layer.Features[i] as Polyline; //获得折线 List <PointF> screenLine = new List <PointF>(); for (int j = 0; j < line.Data.Count; j++) { PointD point = FromMapPoint(line.Data[j]); //坐标转换 screenLine.Add(new PointF((float)point.X, (float)point.Y)); //添加到绘制队列 } lSymbol.DrawLine(g, screenLine.ToArray()); //绘制 } } //图层类型为复合折线 else if (layer.FeatureType == typeof(MultiPolyline)) { LineSymbol lSymbol = sRenderer.Symbol as LineSymbol; //强转,使符号为线符号 for (int i = 0; i < layer.Features.Count; i++) { MultiPolyline lines = layer.Features[i] as MultiPolyline; //获得复合折线 for (int j = 0; j < lines.Data.Count; j++) { Polyline line = lines.Data[j]; List <PointF> screenLine = new List <PointF>(); for (int k = 0; k < line.Data.Count; k++) { PointD point = FromMapPoint(line.Data[k]); //坐标转换 screenLine.Add(new PointF((float)point.X, (float)point.Y)); //添加到绘制队列 } lSymbol.DrawLine(g, screenLine.ToArray()); //绘制 } } } //图层类型为多边形 else if (layer.FeatureType == typeof(Polygon)) { PolygonSymbol pSymbol = sRenderer.Symbol as PolygonSymbol; //强转,使符号为面符号 for (int i = 0; i < layer.Features.Count; i++) { Polygon polygon = layer.Features[i] as Polygon; //获得折线 List <PointF> screenPolygon = new List <PointF>(); for (int j = 0; j < polygon.Data.Count; j++) { PointD point = FromMapPoint(polygon.Data[j]); //坐标转换 screenPolygon.Add(new PointF((float)point.X, (float)point.Y)); //添加到绘制队列 } pSymbol.DrawPolygon(g, screenPolygon.ToArray()); //绘制 } } //图层类型为复合多边形 else if (layer.FeatureType == typeof(MultiPolygon)) { PolygonSymbol pSymbol = sRenderer.Symbol as PolygonSymbol; //强转,使符号为线符号 for (int i = 0; i < layer.Features.Count; i++) { MultiPolygon polygons = layer.Features[i] as MultiPolygon; //获得复合折线 for (int j = 0; j < polygons.Data.Count; j++) { Polygon polygon = polygons.Data[j]; List <PointF> screenPolygon = new List <PointF>(); for (int k = 0; k < polygon.Data.Count; k++) { PointD point = FromMapPoint(polygon.Data[k]); //坐标转换 screenPolygon.Add(new PointF((float)point.X, (float)point.Y)); //添加到绘制队列 } pSymbol.DrawPolygon(g, screenPolygon.ToArray()); //绘制 } } } }