123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 |
- using System;
- using System.IO;
- using System.ComponentModel;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Globalization;
- using FastReport.Utils;
- using FastReport.Data;
- using FastReport.Map.Import.Shp;
- using System.Drawing.Design;
- namespace FastReport.Map
- {
- /// <summary>
- /// Specifies the type of objects that layer contains.
- /// </summary>
- public enum LayerType
- {
- /// <summary>
- /// The layer contains points.
- /// </summary>
- Point,
- /// <summary>
- /// The layer contains lines.
- /// </summary>
- Line,
- /// <summary>
- /// The layer contains polygons.
- /// </summary>
- Polygon
- }
- /// <summary>
- /// Specifies the spatial source for the layer.
- /// </summary>
- public enum SpatialSource
- {
- /// <summary>
- /// Source is ESRI shapefile.
- /// </summary>
- ShpFile,
- /// <summary>
- /// Source is a latitude/longitude/name provided by an application.
- /// </summary>
- ApplicationData
- }
- /// <summary>
- /// Determines how map labels are displayed.
- /// </summary>
- public enum MapLabelKind
- {
- /// <summary>
- /// No label displayed.
- /// </summary>
- None,
- /// <summary>
- /// The shape name is displayed.
- /// </summary>
- Name,
- /// <summary>
- /// The value is displayed.
- /// </summary>
- Value,
- /// <summary>
- /// Both name and value displayed.
- /// </summary>
- NameAndValue
- }
- /// <summary>
- /// Represents a map layer.
- /// </summary>
- public class MapLayer : Base, IParent, ICustomTypeDescriptor
- {
- #region Fields
- private LayerType type;
- private SpatialSource spatialSource;
- private string shapefile;
- private bool visible;
- private BoundingBox box;
- private ShapeCollection shapes;
- private ShapeStyle defaultShapeStyle;
- private MapPalette palette;
- private DataSourceBase dataSource;
- private string filter;
- // used if SpatialSource is ShpFile
- private string spatialColumn;
- private string spatialValue;
- // used if SpatialSource is ApplicationData
- private string latitudeValue;
- private string longitudeValue;
- private string labelValue;
- //
- private string analyticalValue;
- private string labelColumn;
- private MapLabelKind labelKind;
- private string labelFormat;
- private TotalType function;
- private ColorRanges colorRanges;
- private SizeRanges sizeRanges;
- private float accuracy;
- private float labelsVisibleAtZoom;
- private string zoomPolygon;
- private SortedList<string, double> values;
- private SortedList<string, int> counts;
- #endregion // Fields
- #region Properties
- /// <summary>
- /// Gets or sets a type of layer.
- /// </summary>
- [Browsable(false)]
- public LayerType Type
- {
- get { return type; }
- set { type = value; }
- }
- /// <summary>
- /// Gets or sets the spatial source for the layer.
- /// </summary>
- [DefaultValue(SpatialSource.ShpFile)]
- [Category("Data")]
- public SpatialSource SpatialSource
- {
- get { return spatialSource; }
- set
- {
- spatialSource = value;
- if (value == SpatialSource.ApplicationData)
- {
- LabelColumn = "NAME";
- SpatialColumn = "LOCATION";
- Box.MinX = -180;
- Box.MaxX = 180;
- Box.MinY = -90;
- Box.MaxY = 83.623031;
- }
- }
- }
- /// <summary>
- /// Gets or sets the name of ESRI shapefile.
- /// </summary>
- public string Shapefile
- {
- get { return shapefile; }
- set { shapefile = value; }
- }
- /// <summary>
- /// Gets or sets the data source.
- /// </summary>
- [Category("Data")]
- public DataSourceBase DataSource
- {
- get { return dataSource; }
- set { dataSource = value; }
- }
- /// <summary>
- /// Gets or sets the datasource filter expression.
- /// </summary>
- [Category("Data")]
- [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
- public string Filter
- {
- get { return filter; }
- set { filter = value; }
- }
- /// <summary>
- /// Gets or sets spatial column name.
- /// </summary>
- /// <remarks>
- /// This property is used if the <see cref="SpatialSource"/> is set to <b>ShpFile</b>.
- /// </remarks>
- [Category("Data")]
- [Editor("FastReport.Map.SpatialColumnEditor, FastReport", typeof(UITypeEditor))]
- public string SpatialColumn
- {
- get { return spatialColumn; }
- set { spatialColumn = value; }
- }
- /// <summary>
- /// Gets or sets an expression that returns spatial value.
- /// </summary>
- /// <remarks>
- /// This property is used if the <see cref="SpatialSource"/> is set to <b>ShpFile</b>.
- /// </remarks>
- [Category("Data")]
- [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
- public string SpatialValue
- {
- get { return spatialValue; }
- set { spatialValue = value; }
- }
- /// <summary>
- /// Gets or sets an expression that returns latitude value.
- /// </summary>
- /// <remarks>
- /// This property is used if the <see cref="SpatialSource"/> is set to <b>ApplicationData</b>.
- /// </remarks>
- [Category("Data")]
- [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
- public string LatitudeValue
- {
- get { return latitudeValue; }
- set { latitudeValue = value; }
- }
- /// <summary>
- /// Gets or sets an expression that returns longitude value.
- /// </summary>
- /// <remarks>
- /// This property is used if the <see cref="SpatialSource"/> is set to <b>ApplicationData</b>.
- /// </remarks>
- [Category("Data")]
- [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
- public string LongitudeValue
- {
- get { return longitudeValue; }
- set { longitudeValue = value; }
- }
- /// <summary>
- /// Gets or sets an expression that returns label value.
- /// </summary>
- /// <remarks>
- /// This property is used if the <see cref="SpatialSource"/> is set to <b>ApplicationData</b>.
- /// </remarks>
- [Category("Data")]
- [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
- public string LabelValue
- {
- get { return labelValue; }
- set { labelValue = value; }
- }
- /// <summary>
- /// Gets or sets an expression that returns analytical value.
- /// </summary>
- [Category("Data")]
- [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
- public string AnalyticalValue
- {
- get { return analyticalValue; }
- set { analyticalValue = value; }
- }
- /// <summary>
- /// Gets or sets label's column name.
- /// </summary>
- [Category("Appearance")]
- [Editor("FastReport.Map.SpatialColumnEditor, FastReport", typeof(UITypeEditor))]
- public string LabelColumn
- {
- get { return labelColumn; }
- set { labelColumn = value; }
- }
- /// <summary>
- /// Gets or sets a value that determines how map labels are displayed.
- /// </summary>
- [DefaultValue(MapLabelKind.Name)]
- [Category("Appearance")]
- public MapLabelKind LabelKind
- {
- get { return labelKind; }
- set { labelKind = value; }
- }
- /// <summary>
- /// Gets or sets the format of label's value.
- /// </summary>
- [Category("Appearance")]
- public string LabelFormat
- {
- get { return labelFormat; }
- set { labelFormat = value; }
- }
- /// <summary>
- /// Gets or sets the map accuracy. Lower value is better, but slower.
- /// </summary>
- [DefaultValue(2f)]
- [Category("Appearance")]
- public float Accuracy
- {
- get { return accuracy; }
- set { accuracy = value; }
- }
- /// <summary>
- /// Gets or sets the value that determines the labels visiblity at a certain zoom value.
- /// </summary>
- [DefaultValue(1f)]
- [Category("Appearance")]
- public float LabelsVisibleAtZoom
- {
- get { return labelsVisibleAtZoom; }
- set { labelsVisibleAtZoom = value; }
- }
- /// <summary>
- /// Gets or sets the aggregate function.
- /// </summary>
- [Category("Data")]
- [DefaultValue(TotalType.Sum)]
- public TotalType Function
- {
- get { return function; }
- set { function = value; }
- }
- /// <summary>
- /// Gets or sets a value indicating that the layer is visible.
- /// </summary>
- [DefaultValue(true)]
- [Category("Behavior")]
- public bool Visible
- {
- get { return visible; }
- set { visible = value; }
- }
- /// <summary>
- /// Gets or sets a bounding box of layer.
- /// </summary>
- [Browsable(false)]
- public BoundingBox Box
- {
- get { return box; }
- set { box = value; }
- }
- /// <summary>
- /// Gets a collection of map objects.
- /// </summary>
- [Browsable(false)]
- public ShapeCollection Shapes
- {
- get { return shapes; }
- }
- /// <summary>
- /// Gets the default style of shapes in this layer.
- /// </summary>
- [Category("Appearance")]
- public ShapeStyle DefaultShapeStyle
- {
- get { return defaultShapeStyle; }
- }
- /// <summary>
- /// Gets or sets the palette used to highlight shapes.
- /// </summary>
- [Category("Appearance")]
- [DefaultValue(MapPalette.None)]
- public MapPalette Palette
- {
- get { return palette; }
- set { palette = value; }
- }
- /// <summary>
- /// Gets the color ranges used to highlight shapes based on analytical value.
- /// </summary>
- [Category("Appearance")]
- public ColorRanges ColorRanges
- {
- get { return colorRanges; }
- }
- /// <summary>
- /// Gets the size ranges used to draw points based on analytical value.
- /// </summary>
- [Category("Appearance")]
- public SizeRanges SizeRanges
- {
- get { return sizeRanges; }
- }
- /// <summary>
- /// Gets or sets the expression that returns the name of polygon to zoom.
- /// </summary>
- [Category("Data")]
- [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
- public string ZoomPolygon
- {
- get { return zoomPolygon; }
- set { zoomPolygon = value; }
- }
- /// <summary>
- /// Gets or sets the bounding box as a string.
- /// </summary>
- [Browsable(false)]
- public string BoxAsString
- {
- get { return Box.GetAsString(); }
- set { Box.SetAsString(value); }
- }
- /// <summary>
- /// Gets a reference to the Map object.
- /// </summary>
- [Browsable(false)]
- public MapObject Map
- {
- get { return Parent as MapObject; }
- }
- internal List<string> SpatialColumns
- {
- get
- {
- if (Shapes.Count > 0)
- return Shapes[0].SpatialData.GetKeys();
- return new List<string>();
- }
- }
- internal bool IsShapefileEmbedded
- {
- get { return String.IsNullOrEmpty(Shapefile); }
- }
- #endregion // Properties
- #region Public Methods
- /// <inheritdoc/>
- public override void Assign(Base source)
- {
- base.Assign(source);
- MapLayer src = source as MapLayer;
- Shapefile = src.Shapefile;
- Type = src.Type;
- SpatialSource = src.SpatialSource;
- DataSource = src.DataSource;
- Filter = src.Filter;
- SpatialColumn = src.SpatialColumn;
- SpatialValue = src.SpatialValue;
- Function = src.Function;
- LatitudeValue = src.LatitudeValue;
- LongitudeValue = src.LongitudeValue;
- LabelValue = src.LabelValue;
- AnalyticalValue = src.AnalyticalValue;
- LabelColumn = src.LabelColumn;
- LabelKind = src.LabelKind;
- LabelFormat = src.LabelFormat;
- Accuracy = src.Accuracy;
- LabelsVisibleAtZoom = src.LabelsVisibleAtZoom;
- Visible = src.Visible;
- ZoomPolygon = src.ZoomPolygon;
- Box.Assign(src.Box);
- DefaultShapeStyle.Assign(src.DefaultShapeStyle);
- Palette = src.Palette;
- ColorRanges.Assign(src.ColorRanges);
- SizeRanges.Assign(src.SizeRanges);
- }
- /// <summary>
- /// Draws the layer.
- /// </summary>
- /// <param name="e">The drawing parameters.</param>
- public void Draw(FRPaintEventArgs e)
- {
- if (Visible)
- {
- for (int i = 0; i < Shapes.Count; i++)
- {
- ShapeBase shape = Shapes[i];
- shape.ShapeIndex = i;
- shape.SetPrinting(IsPrinting);
- if (Map.IsDesigning || shape.Visible)
- shape.Draw(e);
- }
- if (LabelKind != MapLabelKind.None && (Map.Zoom >= LabelsVisibleAtZoom))
- {
- foreach (ShapeBase shape in Shapes)
- {
- if (Map.IsDesigning || shape.Visible)
- shape.DrawLabel(e);
- }
- }
- }
- }
- /// <summary>
- /// Finds the shape under cursor.
- /// </summary>
- /// <param name="point">The cursor coordinates.</param>
- /// <returns>The <b>ShapeBase</b> object if found.</returns>
- public ShapeBase HitTest(PointF point)
- {
- if (Visible)
- {
- foreach (ShapeBase shape in Shapes)
- {
- if (shape.HitTest(point))
- return shape;
- }
- }
- return null;
- }
- /// <inheritdoc/>
- public override void Serialize(FRWriter writer)
- {
- MapLayer c = writer.DiffObject as MapLayer;
- base.Serialize(writer);
- if (Shapefile != c.Shapefile)
- {
- // when saving to the report file, convert absolute path to the shapefile to relative path
- // (based on the reportfile path).
- string value = Shapefile;
- if (writer.SerializeTo == SerializeTo.Report && Report != null && !String.IsNullOrEmpty(Report.FileName))
- value = FileUtils.GetRelativePath(Shapefile, Path.GetDirectoryName(Report.FileName));
- writer.WriteStr("Shapefile", value);
- }
- if (Type != c.Type)
- writer.WriteValue("Type", Type);
- if (SpatialSource != c.SpatialSource)
- writer.WriteValue("SpatialSource", SpatialSource);
- if (DataSource != null)
- writer.WriteRef("DataSource", DataSource);
- if (Filter != c.Filter)
- writer.WriteStr("Filter", Filter);
- if (SpatialColumn != c.SpatialColumn)
- writer.WriteStr("SpatialColumn", SpatialColumn);
- if (SpatialValue != c.SpatialValue)
- writer.WriteStr("SpatialValue", SpatialValue);
- if (Function != c.Function)
- writer.WriteValue("Function", Function);
- if (LatitudeValue != c.LatitudeValue)
- writer.WriteStr("LatitudeValue", LatitudeValue);
- if (LongitudeValue != c.LongitudeValue)
- writer.WriteStr("LongitudeValue", LongitudeValue);
- if (LabelValue != c.LabelValue)
- writer.WriteStr("LabelValue", LabelValue);
- if (AnalyticalValue != c.AnalyticalValue)
- writer.WriteStr("AnalyticalValue", AnalyticalValue);
- if (LabelColumn != c.LabelColumn)
- writer.WriteStr("LabelColumn", LabelColumn);
- if (LabelKind != c.LabelKind)
- writer.WriteValue("LabelKind", LabelKind);
- if (LabelFormat != c.LabelFormat)
- writer.WriteStr("LabelFormat", LabelFormat);
- if (Accuracy != c.Accuracy)
- writer.WriteFloat("Accuracy", Accuracy);
- if (LabelsVisibleAtZoom != c.LabelsVisibleAtZoom)
- writer.WriteFloat("LabelsVisibleAtZoom", LabelsVisibleAtZoom);
- if (Visible != c.Visible)
- writer.WriteBool("Visible", Visible);
- if (ZoomPolygon != c.ZoomPolygon)
- writer.WriteStr("ZoomPolygon", ZoomPolygon);
- if (BoxAsString != c.BoxAsString)
- writer.WriteStr("BoxAsString", BoxAsString);
- DefaultShapeStyle.Serialize(writer, "DefaultShapeStyle", c.DefaultShapeStyle);
- if (Palette != c.Palette)
- writer.WriteValue("Palette", Palette);
- ColorRanges.Serialize(writer, "ColorRanges");
- SizeRanges.Serialize(writer, "SizeRanges");
- if (writer.SerializeTo == SerializeTo.Preview && !IsShapefileEmbedded)
- {
- // write children by itself
- foreach (ShapeBase shape in Shapes)
- {
- writer.Write(shape);
- }
- }
- }
- /// <summary>
- /// Creates unique names for all contained objects such as points, lines, polygons, etc.
- /// </summary>
- public void CreateUniqueNames()
- {
- if (Report != null)
- {
- FastNameCreator nameCreator = new FastNameCreator(Report.AllNamedObjects);
- if (String.IsNullOrEmpty(Name))
- nameCreator.CreateUniqueName(this);
- foreach (ShapeBase shape in Shapes)
- {
- if (String.IsNullOrEmpty(shape.Name))
- nameCreator.CreateUniqueName(shape);
- }
- }
- }
- /// <summary>
- /// Reduces the number of points in the shapes in this layer.
- /// </summary>
- /// <param name="accuracy">The accuracy value.</param>
- public void Simplify(double accuracy)
- {
- if (accuracy <= 0)
- return;
- foreach (ShapeBase shape in Shapes)
- {
- shape.Simplify(accuracy);
- }
- }
- private void Zoom_Shape(string value)
- {
- foreach (ShapeBase shape in Shapes)
- {
- if (shape.SpatialValue == value)
- {
- ShapePolygon shapePoly = shape as ShapePolygon;
- using (Bitmap bmp = new Bitmap(1, 1))
- using (Graphics g = Graphics.FromImage(bmp))
- using (GraphicCache cache = new GraphicCache())
- {
- FRPaintEventArgs args = new FRPaintEventArgs(g, 1, 1, cache);
- Map.OffsetX = 0;
- Map.OffsetY = 0;
- Map.Zoom = 1;
- Map.Draw(args);
- // this will calculate largestBoundsRect
- shapePoly.GetGraphicsPath(args).Dispose();
- RectangleF largestBoundsRect = shapePoly.largestBoundsRect;
- float distanceX = (Map.AbsLeft + Map.Width / 2) - (largestBoundsRect.Left + largestBoundsRect.Width / 2);
- float distanceY = (Map.AbsTop + Map.Height / 2) - (largestBoundsRect.Top + largestBoundsRect.Height / 2);
- float width = Map.Width - Map.Padding.Horizontal;
- float height = Map.Height - Map.Padding.Vertical;
- float zoomX = width / largestBoundsRect.Width;
- float zoomY = height / largestBoundsRect.Height;
- float zoom = Math.Min(zoomX, zoomY) * 0.95f;
- Map.OffsetX = distanceX;
- Map.OffsetY = distanceY;
- Map.Zoom = zoom;
- }
- break;
- }
- }
- }
- /// <summary>
- /// Loads the layer contents from ESRI shapefile (*.shp/*.dbf).
- /// </summary>
- /// <param name="fileName">The file name.</param>
- public void LoadShapefile(string fileName)
- {
- using (ShpMapImport import = new ShpMapImport())
- {
- import.ImportMap(Map, this, fileName);
- Simplify(0.03);
- CreateUniqueNames();
- }
- }
- /// <inheritdoc/>
- public override void OnAfterLoad()
- {
- // convert relative path to the shapefile to absolute path (based on the reportfile path).
- if (String.IsNullOrEmpty(Shapefile))
- return;
- Shapefile = Shapefile.Replace('\\', Path.DirectorySeparatorChar);
- if (!Path.IsPathRooted(Shapefile) && Report != null && !String.IsNullOrEmpty(Report.FileName))
- Shapefile = Path.GetDirectoryName(Report.FileName) + Path.DirectorySeparatorChar + Shapefile;
- LoadShapefile(Shapefile);
- }
- #endregion // Public Methods
- #region IParent Members
- /// <inheritdoc/>
- public bool CanContain(Base child)
- {
- return child is ShapeBase;
- }
- /// <inheritdoc/>
- public void GetChildObjects(ObjectCollection list)
- {
- if (IsShapefileEmbedded)
- {
- foreach (ShapeBase shape in Shapes)
- {
- list.Add(shape);
- }
- }
- }
- /// <inheritdoc/>
- public void AddChild(Base child)
- {
- Shapes.Add(child as ShapeBase);
- }
- /// <inheritdoc/>
- public void RemoveChild(Base child)
- {
- Shapes.Remove(child as ShapeBase);
- }
- /// <inheritdoc/>
- public int GetChildOrder(Base child)
- {
- return Shapes.IndexOf(child as ShapeBase);
- }
- /// <inheritdoc/>
- public void SetChildOrder(Base child, int order)
- {
- int oldOrder = child.ZOrder;
- if (oldOrder != -1 && order != -1 && oldOrder != order)
- {
- if (order > Shapes.Count)
- order = Shapes.Count;
- if (oldOrder <= order)
- order--;
- Shapes.Remove(child as ShapeBase);
- Shapes.Insert(order, child as ShapeBase);
- }
- }
- /// <inheritdoc/>
- public void UpdateLayout(float dx, float dy)
- {
- // do nothing
- }
- #endregion
- #region Report Engine
- internal void SaveState()
- {
- ColorRanges.SaveState();
- SizeRanges.SaveState();
- foreach (ShapeBase shape in Shapes)
- {
- shape.SaveState();
- }
- }
- internal void RestoreState()
- {
- ColorRanges.RestoreState();
- SizeRanges.RestoreState();
- foreach (ShapeBase shape in Shapes)
- {
- shape.RestoreState();
- }
- }
- internal void InitializeComponent()
- {
- foreach (ShapeBase shape in Shapes)
- {
- shape.InitializeComponent();
- }
- }
- internal void FinalizeComponent()
- {
- foreach (ShapeBase shape in Shapes)
- {
- shape.FinalizeComponent();
- }
- }
- /// <inheritdoc/>
- public override string[] GetExpressions()
- {
- List<string> expressions = new List<string>();
- if (!String.IsNullOrEmpty(Filter))
- expressions.Add(Filter);
- if (!String.IsNullOrEmpty(SpatialValue))
- expressions.Add(SpatialValue);
- if (!String.IsNullOrEmpty(LatitudeValue))
- expressions.Add(LatitudeValue);
- if (!String.IsNullOrEmpty(LongitudeValue))
- expressions.Add(LongitudeValue);
- if (!String.IsNullOrEmpty(LabelValue))
- expressions.Add(LabelValue);
- if (!String.IsNullOrEmpty(AnalyticalValue))
- expressions.Add(AnalyticalValue);
- if (!String.IsNullOrEmpty(ZoomPolygon))
- expressions.Add(ZoomPolygon);
- return expressions.ToArray();
- }
- internal void GetData()
- {
- InitializeData();
- if (DataSource != null)
- {
- DataSource.Init(Filter);
- DataSource.First();
- while (DataSource.HasMoreRows)
- {
- if (SpatialSource == SpatialSource.ShpFile)
- {
- if (!String.IsNullOrEmpty(SpatialValue) && !String.IsNullOrEmpty(AnalyticalValue))
- {
- object spatialValue = Report.Calc(SpatialValue);
- object analyticalValue = Report.Calc(AnalyticalValue);
- if (spatialValue != null && !(spatialValue is DBNull) &&
- analyticalValue != null && !(analyticalValue is DBNull))
- {
- AddValue(spatialValue.ToString(), Convert.ToDouble(analyticalValue));
- }
- }
- }
- else
- {
- if (!String.IsNullOrEmpty(LatitudeValue) && !String.IsNullOrEmpty(LongitudeValue) &&
- !String.IsNullOrEmpty(LabelValue) && !String.IsNullOrEmpty(AnalyticalValue))
- {
- object latitudeValue = Report.Calc(LatitudeValue);
- object longitudeValue = Report.Calc(LongitudeValue);
- object labelValue = Report.Calc(LabelValue);
- object analyticalValue = Report.Calc(AnalyticalValue);
- if (latitudeValue != null && !(latitudeValue is DBNull) &&
- longitudeValue != null && !(longitudeValue is DBNull) &&
- labelValue != null && !(labelValue is DBNull) &&
- analyticalValue != null && !(analyticalValue is DBNull))
- {
- AddValue(Convert.ToDouble(latitudeValue), Convert.ToDouble(longitudeValue), labelValue.ToString(), Convert.ToDouble(analyticalValue));
- }
- }
- }
- DataSource.Next();
- }
- }
- FinalizeData();
- }
- internal void InitializeData()
- {
- values.Clear();
- counts.Clear();
- if (SpatialSource == SpatialSource.ApplicationData)
- Shapes.Clear();
- }
- internal void FinalizeData()
- {
- double min = 1e10;
- double max = -1e10;
- IList<string> spatialValues = values.Keys;
- // finalize avg calculation, find min and max
- for (int i = 0; i < spatialValues.Count; i++)
- {
- string spatialValue = spatialValues[i];
- double analyticalValue = values[spatialValue];
- if (Function == TotalType.Avg)
- {
- analyticalValue = analyticalValue / counts[spatialValue];
- values[spatialValue] = analyticalValue;
- }
- if (analyticalValue < min)
- min = analyticalValue;
- if (analyticalValue > max)
- max = analyticalValue;
- }
- if (spatialValues.Count > 0)
- {
- ColorRanges.Fill(min, max);
- SizeRanges.Fill(min, max);
- }
- // set shape values
- foreach (ShapeBase shape in Shapes)
- {
- string spatialValue = shape.SpatialValue;
- if (values.ContainsKey(spatialValue))
- shape.Value = values[spatialValue];
- }
- if (!String.IsNullOrEmpty(ZoomPolygon))
- {
- object zoomShape = Report.Calc(ZoomPolygon);
- if (zoomShape != null && !(zoomShape is DBNull))
- Zoom_Shape(zoomShape.ToString());
- }
- }
- /// <summary>
- /// Adds application provided data.
- /// </summary>
- /// <param name="latitude">Latitude value.</param>
- /// <param name="longitude">Longitude value.</param>
- /// <param name="name">The name displayed as a label.</param>
- /// <param name="analyticalValue">Analytical value.</param>
- /// <remarks>
- /// Use this method if the <see cref="SpatialSource"/> is set to <b>ApplicationData</b>.
- /// </remarks>
- public void AddValue(double latitude, double longitude, string name, double analyticalValue)
- {
- string spatialValue = latitude.ToString(CultureInfo.InvariantCulture.NumberFormat) + "," +
- longitude.ToString(CultureInfo.InvariantCulture.NumberFormat);
- if (!values.ContainsKey(spatialValue))
- {
- ShapePoint point = new ShapePoint();
- point.X = longitude;
- point.Y = latitude;
- point.SpatialData.SetValue("NAME", name);
- point.SpatialData.SetValue("LOCATION", spatialValue);
- Shapes.Add(point);
- }
- AddValue(spatialValue, analyticalValue);
- }
- /// <summary>
- /// Adds a spatial/analytical value pair to the list.
- /// </summary>
- /// <param name="spatialValue">The spatial value.</param>
- /// <param name="analyticalValue">The analytical value.</param>
- /// <remarks>
- /// Use this method if the <see cref="SpatialSource"/> is set to <b>ShpFile</b>.
- /// </remarks>
- public void AddValue(string spatialValue, double analyticalValue)
- {
- if (!values.ContainsKey(spatialValue))
- {
- if (Function == TotalType.Count)
- analyticalValue = 1;
- values.Add(spatialValue, analyticalValue);
- counts.Add(spatialValue, 1);
- return;
- }
- double value = values[spatialValue];
- switch (Function)
- {
- case TotalType.Sum:
- case TotalType.Avg:
- value += analyticalValue;
- break;
- case TotalType.Max:
- if (analyticalValue > value)
- value = analyticalValue;
- break;
- case TotalType.Min:
- if (analyticalValue < value)
- value = analyticalValue;
- break;
- case TotalType.Count:
- value++;
- break;
- }
- values[spatialValue] = value;
- counts[spatialValue]++;
- }
- #endregion
- #region ICustomTypeDescriptor Members
- /// <inheritdoc/>
- public PropertyDescriptorCollection GetProperties()
- {
- return GetProperties(null);
- }
- /// <inheritdoc/>
- public PropertyDescriptorCollection GetProperties(Attribute[] attr)
- {
- PropertyDescriptorCollection typeProps = TypeDescriptor.GetProperties(this.GetType(), attr);
- PropertyDescriptorCollection properties = new PropertyDescriptorCollection(null);
- foreach (PropertyDescriptor desc in typeProps)
- {
- bool skip = false;
- if (SpatialSource == SpatialSource.ShpFile)
- {
- skip = desc.Name == "LatitudeValue" || desc.Name == "LongitudeValue" ||
- desc.Name == "LabelValue" || desc.Name == "LabelsVisibleAtZoom";
- }
- else if (SpatialSource == SpatialSource.ApplicationData)
- {
- skip = desc.Name == "SpatialColumn" || desc.Name == "SpatialValue" ||
- desc.Name == "LabelColumn" || desc.Name == "Accuracy";
- }
- if (!skip)
- properties.Add(desc);
- }
- return properties;
- }
- /// <inheritdoc/>
- public String GetClassName()
- {
- return TypeDescriptor.GetClassName(this, true);
- }
- /// <inheritdoc/>
- public AttributeCollection GetAttributes()
- {
- return TypeDescriptor.GetAttributes(this, true);
- }
- /// <inheritdoc/>
- public String GetComponentName()
- {
- return TypeDescriptor.GetComponentName(this, true);
- }
- /// <inheritdoc/>
- public TypeConverter GetConverter()
- {
- return TypeDescriptor.GetConverter(this, true);
- }
- /// <inheritdoc/>
- public EventDescriptor GetDefaultEvent()
- {
- return TypeDescriptor.GetDefaultEvent(this, true);
- }
- /// <inheritdoc/>
- public PropertyDescriptor GetDefaultProperty()
- {
- return TypeDescriptor.GetDefaultProperty(this, true);
- }
- /// <inheritdoc/>
- public object GetEditor(Type editorBaseType)
- {
- return TypeDescriptor.GetEditor(this, editorBaseType, true);
- }
- /// <inheritdoc/>
- public EventDescriptorCollection GetEvents(Attribute[] attributes)
- {
- return TypeDescriptor.GetEvents(this, attributes, true);
- }
- /// <inheritdoc/>
- public EventDescriptorCollection GetEvents()
- {
- return TypeDescriptor.GetEvents(this, true);
- }
- /// <inheritdoc/>
- public object GetPropertyOwner(PropertyDescriptor pd)
- {
- return this;
- }
- #endregion
- /// <summary>
- /// Initializes a new instance of the <see cref="MapLayer"/> class.
- /// </summary>
- public MapLayer()
- {
- visible = true;
- shapefile = "";
- shapes = new ShapeCollection(this);
- box = new BoundingBox();
- values = new SortedList<string, double>();
- counts = new SortedList<string, int>();
- function = TotalType.Sum;
- filter = "";
- spatialColumn = "";
- spatialValue = "";
- latitudeValue = "";
- longitudeValue = "";
- labelValue = "";
- analyticalValue = "";
- labelColumn = "";
- labelFormat = "";
- defaultShapeStyle = new ShapeStyle();
- colorRanges = new ColorRanges();
- sizeRanges = new SizeRanges();
- labelKind = MapLabelKind.Name;
- accuracy = 2;
- labelsVisibleAtZoom = 1;
- zoomPolygon = "";
- BaseName = "Layer";
- SetFlags(Flags.CanShowChildrenInReportTree, false);
- }
- }
- }
|