private static Label CreateLabel(Geometry feature, string text, float rotation, int priority, LabelStyle style, Map map, Graphics g) { //SizeF size = g.MeasureString(text, style.Font); SizeF size = VectorRenderer.SizeOfString(g, text, style.Font); //PointF position = map.WorldToImage(feature.GetBoundingBox().GetCentroid()); PointF position = Transform.WorldtoMap(feature.GetBoundingBox().GetCentroid(), map); position.X = position.X - size.Width * (short)style.HorizontalAlignment * 0.5f; position.Y = position.Y - size.Height * (short)(2 - (int)style.VerticalAlignment) * 0.5f; if (position.X - size.Width > map.Size.Width || position.X + size.Width < 0 || position.Y - size.Height > map.Size.Height || position.Y + size.Height < 0) { return(null); } Label lbl; if (!style.CollisionDetection) { lbl = new Label(text, position, rotation, priority, null, style); } else { //Collision detection is enabled so we need to measure the size of the string lbl = new Label(text, position, rotation, priority, new LabelBox(position.X - size.Width * 0.5f - style.CollisionBuffer.Width, position.Y + size.Height * 0.5f + style.CollisionBuffer.Height, size.Width + 2f * style.CollisionBuffer.Width, size.Height + style.CollisionBuffer.Height * 2f), style); } if (feature is LineString) { LineString line = feature as LineString; //Only label feature if it is long enough, or it is definately wanted if (line.Length / map.PixelSize > size.Width || style.IgnoreLength) { CalculateLabelOnLinestring(line, ref lbl, map); } else { return(null); } } return(lbl); }
private BaseLabel CreateLabel(IFeature fdr, IGeometry feature, string text, float rotation, int priority, LabelStyle style, Map map, IGraphics g, GetLocationMethod _getLocationMethod) { if (feature == null) { return(null); } BaseLabel lbl = null; SizeF size = RendererHelper.SizeOfString(g, text, style.Font); if (feature is ILineal) { var line = feature as ILineString; if (line != null) { if (style.IsTextOnPath == false) { if (size.Width < 0.95 * line.Length / map.PixelWidth || style.IgnoreLength) { var positiveLineString = PositiveLineString(line, false); var lineStringPath = LineStringToPath(positiveLineString, map /*, false*/); var rect = lineStringPath.GetBounds(); if (style.CollisionDetection && !style.CollisionBuffer.IsEmpty) { var cbx = style.CollisionBuffer.Width; var cby = style.CollisionBuffer.Height; rect.Inflate(2 * cbx, 2 * cby); rect.Offset(-cbx, -cby); } var labelBox = new LabelBox(rect); lbl = new PathLabel(text, lineStringPath, 0, priority, labelBox, style); } } else { //get centriod PointF position2 = map.WorldToImage(feature.EnvelopeInternal.Centre); lbl = new Label(text, position2, rotation, priority, style); if (size.Width < 0.95 * line.Length / map.PixelWidth || !style.IgnoreLength) { CalculateLabelAroundOnLineString(line, ref lbl, map, g, size); } } } return(lbl); } var worldPosition = _getLocationMethod == null ? feature.EnvelopeInternal.Centre : _getLocationMethod(fdr); if (worldPosition == null) { return(null); } var position = Transform.WorldtoMap(worldPosition, map); var location = new PointF( position.X - size.Width * (short)style.HorizontalAlignment * 0.5f, position.Y - size.Height * (short)(2 - (int)style.VerticalAlignment) * 0.5f); if (location.X - size.Width > map.Size.Width || location.X + size.Width < 0 || location.Y - size.Height > map.Size.Height || location.Y + size.Height < 0) { return(null); } if (!style.CollisionDetection) { lbl = new Label(text, location, rotation, priority, null, style) { LabelPoint = position } } ; else { //Collision detection is enabled so we need to measure the size of the string lbl = new Label(text, location, rotation, priority, new LabelBox(location.X - size.Width * 0.5f - style.CollisionBuffer.Width, location.Y + size.Height * 0.5f + style.CollisionBuffer.Height, size.Width + 2f * style.CollisionBuffer.Width, size.Height + style.CollisionBuffer.Height * 2f), style) { LabelPoint = position }; } /* * if (feature is LineString) * { * var line = feature as LineString; * * //Only label feature if it is long enough, or it is definately wanted * if (line.Length / map.PixelSize > size.Width || style.IgnoreLength) * { * CalculateLabelOnLinestring(line, ref lbl, map); * } * else * return null; * } */ return(lbl); }
private static BaseLabel CreateLabel(Geometry feature, string text, float rotation, int priority, LabelStyle style, Map map, Graphics g) { BaseLabel lbl = null; SizeF size = VectorRenderer.SizeOfString(g, text, style.Font); if (feature is ILineal) { var line = feature as LineString; if (line != null) { if (size.Width < 0.95 * line.Length / map.PixelWidth || !style.IgnoreLength) { var positiveLineString = PositiveLineString(line, false); var lineStringPath = LineStringToPath(positiveLineString, map /*, false*/); var rect = lineStringPath.GetBounds(); if (style.CollisionDetection && !style.CollisionBuffer.IsEmpty) { var cbx = style.CollisionBuffer.Width; var cby = style.CollisionBuffer.Height; rect.Inflate(2 * cbx, 2 * cby); rect.Offset(-cbx, -cby); } var labelBox = new LabelBox(rect); lbl = new PathLabel(text, lineStringPath, 0, priority, labelBox, style); } } return(lbl); } PointF position = Transform.WorldtoMap(feature.GetBoundingBox().GetCentroid(), map); position.X = position.X - size.Width * (short)style.HorizontalAlignment * 0.5f; position.Y = position.Y - size.Height * (short)(2 - (int)style.VerticalAlignment) * 0.5f; if (position.X - size.Width > map.Size.Width || position.X + size.Width < 0 || position.Y - size.Height > map.Size.Height || position.Y + size.Height < 0) { return(null); } if (!style.CollisionDetection) { lbl = new Label(text, position, rotation, priority, null, style); } else { //Collision detection is enabled so we need to measure the size of the string lbl = new Label(text, position, rotation, priority, new LabelBox(position.X - size.Width * 0.5f - style.CollisionBuffer.Width, position.Y + size.Height * 0.5f + style.CollisionBuffer.Height, size.Width + 2f * style.CollisionBuffer.Width, size.Height + style.CollisionBuffer.Height * 2f), style); } /* * if (feature is LineString) * { * var line = feature as LineString; * * //Only label feature if it is long enough, or it is definately wanted * if (line.Length / map.PixelSize > size.Width || style.IgnoreLength) * { * CalculateLabelOnLinestring(line, ref lbl, map); * } * else * return null; * } */ return(lbl); }