/// <summary> /// Returns the style based on a numeric DataColumn, where style /// properties are linearly interpolated between max and min values. /// </summary> /// <param name="feature">Feature</param> /// <returns><see cref="SharpMap.Styles.IStyle">Style</see> calculated by a linear interpolation between the min/max styles</returns> public override IStyle GetStyle(IFeature feature) { double attr; try { attr = FeatureAttributeAccessorHelper.GetAttributeValue <double>(feature, attributeName); } catch { throw new ApplicationException( "Invalid Attribute type in Gradient Theme - Couldn't parse attribute (must be numerical)"); } if (MinStyle.GetType() != MaxStyle.GetType()) { throw new ArgumentException("MinStyle and MaxStyle must be of the same type"); } switch (MinStyle.GetType().FullName) { case "SharpMap.Styles.VectorStyle": return(CalculateVectorStyle(attr)); case "SharpMap.Styles.LabelStyle": return(CalculateLabelStyle(MinStyle as LabelStyle, MaxStyle as LabelStyle, attr)); default: throw new ArgumentException( "Only SharpMap.Styles.VectorStyle and SharpMap.Styles.LabelStyle are supported for the gradient theme"); } }
public void GetUndefinedAttributeUnsafe() { var testFeature = new TestFeature(); testFeature.Attributes = new DictionaryFeatureAttributeCollection(); var value = FeatureAttributeAccessorHelper.GetAttributeValue(testFeature, "unknown"); Assert.IsNull(value); }
protected virtual string GetText(IFeature feature) { string text = null; if (getLabelMethod != null) { text = getLabelMethod(feature); } if (text == null && LabelColumn != null) { text = FeatureAttributeAccessorHelper.GetAttributeValue <string>(feature, LabelColumn, "", false); } return(text); }
public override IStyle GetStyle(IFeature feature) { string attr = FeatureAttributeAccessorHelper.GetAttributeValue <string>(feature, attributeName); if (attr != null) { foreach (CategorialThemeItem categorialThemeItem in ThemeItems) { if (categorialThemeItem.Category.Equals(attr) || (categorialThemeItem.Value != null && categorialThemeItem.Value.ToString().Equals(attr))) { return(categorialThemeItem.Style); } } } return(DefaultStyle); }
[Category(TestCategory.WorkInProgress)] // slow public void GetAttributeFast() { var testFeature = new TestFeature(); testFeature.Attributes = new DictionaryFeatureAttributeCollection(); object value; TestHelper.AssertIsFasterThan(100, () => { for (int i = 0; i < 10000; i++) { value = FeatureAttributeAccessorHelper.GetAttributeValue( testFeature, "Other", false); } }); }
protected virtual string GetText(IFeature feature) { string text; if (_getLabelMethod != null) { text = _getLabelMethod(feature); } else if (LabelColumn != null) { text = FeatureAttributeAccessorHelper.GetAttributeValue <string>(feature, this.LabelColumn, null); } else { text = null; } return(text); }
private IEnumerable <IComparable> GetAttributeValuesFromFeatures() { var attributeValuesFound = new List <IComparable>(); if (layer == null || layer.DataSource == null) { return(Enumerable.Empty <IComparable>()); } foreach (var feature in layer.DataSource.Features.Cast <IFeature>()) { //check if value can be cast to icomparable ie DBnull value wil yield null var value = FeatureAttributeAccessorHelper.GetAttributeValue(feature, attributeName, false) as IComparable; if (value != null) { attributeValuesFound.Add(value); } } return(attributeValuesFound); }
public override IStyle GetStyle(IFeature feature) { double attr; try { attr = FeatureAttributeAccessorHelper.GetAttributeValue <double>(feature, AttributeName); } catch { throw new ApplicationException( "Invalid Attribute type in Quantity Theme - Couldn't parse attribute (must be numerical)"); } foreach (QuantityThemeItem quantityThemeItem in ThemeItems) { if (quantityThemeItem.Interval.Contains(attr)) { return(quantityThemeItem.Style); } } return(DefaultStyle); }
/// <summary> /// Renders the layer /// </summary> /// <param name="g">Graphics object reference</param> /// <param name="map">Map which is rendered</param> public override void OnRender(System.Drawing.Graphics g, Map map) { if (Style.Enabled && Style.MaxVisible >= map.Zoom && Style.MinVisible < map.Zoom) { if (DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + Name + "'")); } g.TextRenderingHint = this.TextRenderingHint; g.SmoothingMode = this.SmoothingMode; IEnvelope envelope = map.Envelope; //View to render if (CoordinateTransformation != null) { envelope = CoordinateSystems.Transformations.GeometryTransform.TransformBox(envelope, this.CoordinateTransformation.MathTransform.Inverse()); } IList features = DataSource.GetFeatures(envelope); if (features.Count == 0) { return; } //Initialize label collection var labels = new List <Rendering.Label>(); //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection //Render labels for (int i = 0; i < features.Count; i++) { IFeature feature = (IFeature)features[i]; if (this.CoordinateTransformation != null) { ((IFeature)features[i]).Geometry = SharpMap.CoordinateSystems.Transformations.GeometryTransform.TransformGeometry(((IFeature)features[i]).Geometry, this.CoordinateTransformation.MathTransform); } SharpMap.Styles.LabelStyle style = null; if (this.Theme != null) //If thematics is enabled, lets override the style { style = this.Theme.GetStyle(feature) as SharpMap.Styles.LabelStyle; } else { style = this.Style; } float rotation = 0; if (!String.IsNullOrEmpty(this.RotationColumn)) { rotation = FeatureAttributeAccessorHelper.GetAttributeValue <float>(feature, RotationColumn, 0f); } string text = GetText(feature); if (text != null && text != String.Empty) { if (feature.Geometry is IGeometryCollection) { if (this.MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.All) { foreach (IGeometry geom in (feature.Geometry as IGeometryCollection)) { SharpMap.Rendering.Label lbl = CreateLabel(geom, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (this.MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.CommonCenter) { SharpMap.Rendering.Label lbl = CreateLabel(feature.Geometry, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } else if (this.MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.First) { if ((feature.Geometry as IGeometryCollection).Geometries.Length > 0) { SharpMap.Rendering.Label lbl = CreateLabel((feature.Geometry as IGeometryCollection).Geometries[0], text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (this.MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.Largest) { IGeometryCollection coll = (feature.Geometry as IGeometryCollection); if (coll.NumGeometries > 0) { double largestVal = 0; int idxOfLargest = 0; for (int j = 0; j < coll.NumGeometries; j++) { IGeometry geom = coll.Geometries[j]; if (geom is ILineString && ((ILineString)geom).Length > largestVal) { largestVal = ((ILineString)geom).Length; idxOfLargest = j; } if (geom is IMultiLineString && ((IMultiLineString)geom).Length > largestVal) { largestVal = ((ILineString)geom).Length; idxOfLargest = j; } if (geom is IPolygon && ((IPolygon)geom).Area > largestVal) { largestVal = ((IPolygon)geom).Area; idxOfLargest = j; } if (geom is IMultiPolygon && ((IMultiPolygon)geom).Area > largestVal) { largestVal = ((IMultiPolygon)geom).Area; idxOfLargest = j; } } SharpMap.Rendering.Label lbl = CreateLabel(coll.Geometries[idxOfLargest], text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } } else { SharpMap.Rendering.Label lbl = CreateLabel(feature.Geometry, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } } if (labels.Count > 0) //We have labels to render... { if (this.Style.CollisionDetection && this.labelFilter != null) { this.labelFilter(labels); } for (int i = 0; i < labels.Count; i++) { SharpMap.Rendering.VectorRenderingHelper.DrawLabel(g, labels[i].LabelPoint, labels[i].Style.Offset, labels[i].Style.Font, labels[i].Style.ForeColor, labels[i].Style.BackColor, Style.Halo, labels[i].Rotation, labels[i].Text, map); } } labels = null; } }
/// <summary> /// Renders the layer /// </summary> /// <param name="g">Graphics object reference</param> /// <param name="map">Map which is rendered</param> public override void OnRender(System.Drawing.Graphics g, IMap map) { if (Style.Enabled && Style.MaxVisible >= map.Zoom && Style.MinVisible < map.Zoom) { if (DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + Name + "'")); } g.TextRenderingHint = TextRenderingHint; g.SmoothingMode = SmoothingMode; var features = Parent?.GetFeatures(map.Envelope) ?? GetFeatures(map.Envelope); if (!features.Any()) { return; } //Initialize label collection var labels = new List <Rendering.Label>(); //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection //Render labels IGeometry geometry; foreach (var feature in features) { if (CoordinateTransformation != null) { geometry = GeometryTransform.TransformGeometry(feature.Geometry, this.CoordinateTransformation.MathTransform); } else { geometry = feature.Geometry; } ILabelStyle style; if (Theme != null) //If thematics is enabled, lets override the style { style = Theme.GetStyle(feature) as LabelStyle; } else { style = Style; } if (!style.Enabled || (map.Zoom < style.MinVisible || map.Zoom > style.MaxVisible)) { continue; } string text = GetText(feature); if (!string.IsNullOrEmpty(text)) { float rotation = 0; if (!String.IsNullOrEmpty(RotationColumn)) { rotation = FeatureAttributeAccessorHelper.GetAttributeValue <float>(feature, RotationColumn, 0f); } // TODO: Add feature based //int priority = 0; //if (!String.IsNullOrEmpty(RotationColumn)) // rotation = FeatureAttributeAccessorHelper.GetAttributeValue<int>(feature, Priority, 0f); if (geometry is IGeometryCollection) { if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.All) { foreach (IGeometry geom in (geometry as IGeometryCollection)) { Label lbl = CreateLabel(geom, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.CommonCenter) { Label lbl = CreateLabel(geometry, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.First) { if ((geometry as IGeometryCollection).Geometries.Length > 0) { Label lbl = CreateLabel((geometry as IGeometryCollection).Geometries[0], text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.Largest) { var coll = (geometry as IGeometryCollection); if (coll.NumGeometries > 0) { double largestVal = 0; int idxOfLargest = 0; for (int j = 0; j < coll.NumGeometries; j++) { IGeometry geom = coll.Geometries[j]; if (geom is ILineString && ((ILineString)geom).Length > largestVal) { largestVal = ((ILineString)geom).Length; idxOfLargest = j; } if (geom is IMultiLineString && ((IMultiLineString)geom).Length > largestVal) { largestVal = ((ILineString)geom).Length; idxOfLargest = j; } if (geom is IPolygon && ((IPolygon)geom).Area > largestVal) { largestVal = ((IPolygon)geom).Area; idxOfLargest = j; } if (geom is IMultiPolygon && ((IMultiPolygon)geom).Area > largestVal) { largestVal = ((IMultiPolygon)geom).Area; idxOfLargest = j; } } Label lbl = CreateLabel(coll.Geometries[idxOfLargest], text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } } else { var lbl = CreateLabel(geometry, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } } if (labels.Count > 0) //We have labels to render... { if (this.Style.CollisionDetection && this.labelFilter != null) { this.labelFilter(labels); } for (int i = 0; i < labels.Count; i++) { VectorRenderingHelper.DrawLabel(g, labels[i].LabelPoint, labels[i].Style.Offset, labels[i].Style.Font, labels[i].Style.ForeColor, labels[i].Style.BackColor, Style.Halo, labels[i].Rotation, labels[i].Text, map); } } labels = null; } }