Esempio n. 1
0
        public static IProjection DefaultProjection = new WebMercator(); // = 10000;

        /// <summary>
        /// Calculates the scalefactor to store the coordinates.
        /// </summary>
        /// <param name="zoomFactor"></param>
        /// <returns></returns>
        public static int CalculateScaleFactor(float zoomFactor)
        {
            return((1.0 / Scene2D.CalculateSimplificationEpsilon(DefaultProjection, zoomFactor)).Power10Floor() * 10);
        }
Esempio n. 2
0
        /// <summary>
        /// Merges objects from the given scene for the given zoom level.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="source"></param>
        /// <param name="idx"></param>
        private void MergeObjects(Scene2D target, Scene2D source, int idx)
        {
            var lines      = new Dictionary <Scene2D.ScenePoints, Scene2DStylesSet>();
            var linesIndex = new QuadTree <PointF2D, Scene2D.ScenePoints>();

            var polygons = new Dictionary <Scene2D.ScenePoints, Scene2DStylesSet>();
            //var polygonsIndex = new QuadTree<PointF2D, Scene2D.ScenePoints>();

            Dictionary <uint, SceneObject> sceneObjects = source.GetSceneObjectsAt(idx);
            float zoomFactor = source.GetMaximumZoomFactorAt(idx);
            float epsilon    = source.CalculateSimplificationEpsilon(zoomFactor);

            foreach (var sceneObject in sceneObjects)
            {
                if (sceneObject.Value.Enum == SceneObjectType.LineObject)
                { // the scene object is a line object.
                    var sceneLineObject             = sceneObject.Value as SceneLineObject;
                    Scene2D.ScenePoints scenePoints = source.GetPoints(sceneLineObject.GeoId);
                    Scene2DStylesSet    stylesSet   = null;
                    if (!lines.TryGetValue(scenePoints, out stylesSet))
                    { // create styles set.
                        stylesSet = new Scene2DStylesSet();
                        lines.Add(scenePoints, stylesSet);

                        // add scenePoints to the index.
                        linesIndex.Add(new PointF2D(scenePoints.X[0], scenePoints.Y[0]), scenePoints);
                        linesIndex.Add(new PointF2D(scenePoints.X[scenePoints.X.Length - 1], scenePoints.Y[scenePoints.Y.Length - 1]), scenePoints);
                    }
                    stylesSet.AddStyleLine(sceneLineObject.StyleId);
                }
                else if (sceneObject.Value.Enum == SceneObjectType.LineTextObject)
                {
                    var sceneLineTextObject         = sceneObject.Value as SceneLineTextObject;
                    Scene2D.ScenePoints scenePoints = source.GetPoints(sceneLineTextObject.GeoId);
                    Scene2DStylesSet    stylesSet   = null;
                    if (!lines.TryGetValue(scenePoints, out stylesSet))
                    { // create styles set.
                        stylesSet = new Scene2DStylesSet();
                        lines.Add(scenePoints, stylesSet);

                        // add scenePoints to the index.
                        linesIndex.Add(new PointF2D(scenePoints.X[0], scenePoints.Y[0]), scenePoints);
                        linesIndex.Add(new PointF2D(scenePoints.X[scenePoints.X.Length - 1], scenePoints.Y[scenePoints.Y.Length - 1]), scenePoints);
                    }
                    stylesSet.AddStyleLineText(sceneLineTextObject.StyleId, sceneLineTextObject.TextId);
                }
                else if (sceneObject.Value.Enum == SceneObjectType.IconObject)
                {
                    throw new NotSupportedException("Icons not yet supported!");
                    //var sceneIconObject = (sceneObject.Value as SceneIconObject);
                    //Scene2D.ScenePoint scenePoint = source.GetPoint(sceneIconObject.GeoId);
                    //source.GetStyleIcon(
                    //target.AddIcon(target.AddPoint(scenePoint.X, scenePoint.Y);
                }
                else if (sceneObject.Value.Enum == SceneObjectType.PointObject)
                {
                    var scenePointObject          = (sceneObject.Value as ScenePointObject);
                    Scene2D.ScenePoint scenePoint = source.GetPoint(scenePointObject.GeoId);
                    StylePoint         stylePoint = source.GetStylePoint(scenePointObject.StyleId);

                    target.AddStylePoint(target.AddPoint(scenePoint.X, scenePoint.Y), stylePoint.Layer, stylePoint.MinZoom, stylePoint.MaxZoom,
                                         stylePoint.Color, stylePoint.Size);
                }
                else if (sceneObject.Value.Enum == SceneObjectType.PolygonObject)
                { // the scene object is a polygon.
                    var scenePolygonObject          = (sceneObject.Value as ScenePolygonObject);
                    Scene2D.ScenePoints scenePoints = source.GetPoints(sceneObject.Value.GeoId);
                    Scene2DStylesSet    stylesSet   = null;
                    if (!polygons.TryGetValue(scenePoints, out stylesSet))
                    { // create styles set.
                        stylesSet = new Scene2DStylesSet();
                        polygons.Add(scenePoints, stylesSet);

                        //// add scenePoints to the index.
                        //polygonsIndex.Add(new PointF2D(scenePoints.X[0], scenePoints.Y[0]), scenePoints);
                        //polygonsIndex.Add(new PointF2D(scenePoints.X[scenePoints.X.Length - 1], scenePoints.Y[scenePoints.Y.Length - 1]), scenePoints);
                    }
                    stylesSet.AddStylePolygon(scenePolygonObject.StyleId);

                    //var scenePolygonObject = (sceneObject.Value as ScenePolygonObject);
                    //Scene2D.ScenePoints scenePoints = source.GetPoints(sceneObject.Value.GeoId);
                    //StylePolygon stylePolygon = source.GetStylePolygon(sceneObject.Value.StyleId);

                    //uint? pointsId = target.AddPoints(scenePoints.X, scenePoints.Y);
                    //if (pointsId.HasValue)
                    //{
                    //    target.AddStylePolygon(pointsId.Value, stylePolygon.Layer, stylePolygon.MinZoom, stylePolygon.MaxZoom,
                    //        stylePolygon.Color, stylePolygon.Width, stylePolygon.Fill);
                    //}
                }
                else if (sceneObject.Value.Enum == SceneObjectType.TextObject)
                {
                    var sceneTextObject           = (sceneObject.Value as SceneTextObject);
                    Scene2D.ScenePoint scenePoint = source.GetPoint(sceneObject.Value.GeoId);
                    StyleText          styleText  = source.GetStyleText(sceneTextObject.StyleId);
                    string             text       = source.GetText(sceneTextObject.TextId);

                    target.AddText(target.AddPoint(scenePoint.X, scenePoint.Y), styleText.Layer, styleText.MinZoom, styleText.MaxZoom,
                                   styleText.Size, text, styleText.Color, styleText.HaloColor, styleText.HaloRadius, styleText.Font);
                }
            }

            // loop until there are no more candidates.
            int   totalLines     = lines.Count;
            float latestProgress = 0;

            while (lines.Count > 0)
            {
                var line = lines.First();
                lines.Remove(line.Key);

                // report progress.
                float progress = (float)System.Math.Round((((double)(totalLines - lines.Count) / (double)totalLines) * 100));
                if (progress != latestProgress)
                {
                    OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                                                    "Merging lines @z{3}e{4} ({1}/{2})... {0}%", progress, totalLines - lines.Count, totalLines, zoomFactor, epsilon);
                    latestProgress = progress;
                }

                // copy the coordinates to lists.
                double[] x = line.Key.X.Clone() as double[];
                double[] y = line.Key.Y.Clone() as double[];

                // find a matching line.
                int mergeCount = 1;
                Scene2D.ScenePoints found;
                MatchPosition       foundPosition = this.FindMatch(linesIndex, lines, x, y, line.Value, epsilon, out found);
                while (found != null)
                { // TODO: keep expanding and duplicating until not possible anymore.
                    // remove the found line.
                    lines.Remove(found);

                    // report progress.
                    progress = (float)System.Math.Round((((double)(totalLines - lines.Count) / (double)totalLines) * 100));
                    if (progress != latestProgress)
                    {
                        OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                                                        "Merging lines @z{3}e{4} ({1}/{2})... {0}%", progress, totalLines - lines.Count, totalLines, zoomFactor, epsilon);
                        latestProgress = progress;
                    }

                    // add the line.
                    int lengthBefore = x.Length;
                    Array.Resize(ref x, x.Length + found.X.Length - 1);
                    Array.Resize(ref y, y.Length + found.Y.Length - 1);

                    switch (foundPosition)
                    {
                    case MatchPosition.FirstFirst:
                        found.X.InsertToReverse(1, x, 0, found.X.Length - 1);
                        found.Y.InsertToReverse(1, y, 0, found.Y.Length - 1);
                        break;

                    case MatchPosition.FirstLast:
                        found.X.InsertTo(0, x, 0, found.X.Length - 1);
                        found.Y.InsertTo(0, y, 0, found.Y.Length - 1);
                        break;

                    case MatchPosition.LastFirst:
                        found.X.CopyTo(x, lengthBefore - 1);
                        found.Y.CopyTo(y, lengthBefore - 1);
                        break;

                    case MatchPosition.LastLast:
                        found.X.CopyToReverse(x, lengthBefore - 1);
                        found.Y.CopyToReverse(y, lengthBefore - 1);
                        break;
                    }

                    // select a new line.
                    foundPosition = this.FindMatch(linesIndex, lines, x, y, line.Value, epsilon, out found);
                    mergeCount++;
                }

                // simplify first.
                double[][] simplified = OsmSharp.Math.Algorithms.SimplifyCurve.Simplify(new double[][] { x, y },
                                                                                        epsilon);

                // add the new points.
                uint?pointsId = target.AddPoints(simplified[0], simplified[1]);

                // add points again with appropriate styles.
                if (pointsId.HasValue)
                {
                    foreach (var style in line.Value)
                    {
                        var scene2DStyleLine = (style as Scene2DStyleLine);
                        if (scene2DStyleLine != null)
                        {
                            StyleLine styleLine = source.GetStyleLine(scene2DStyleLine.StyleLineId);
                            target.AddStyleLine(pointsId.Value, styleLine.Layer, styleLine.MinZoom, styleLine.MaxZoom,
                                                styleLine.Color, styleLine.Width, styleLine.LineJoin, styleLine.Dashes);
                            continue;
                        }
                        var scene2DStyleLineText = (style as Scene2DStyleLineText);
                        if (scene2DStyleLineText != null)
                        {
                            StyleText styleText = source.GetStyleLineText(scene2DStyleLineText.StyleLineTextId);
                            string    text      = source.GetText(scene2DStyleLineText.TextId);
                            target.AddStyleLineText(pointsId.Value, styleText.Layer, styleText.MinZoom, styleText.MaxZoom,
                                                    styleText.Color, styleText.Size, text, styleText.Font, styleText.HaloColor, styleText.HaloRadius);
                            continue;
                        }
                    }
                }
            }

            // loop until there are no more candidates.
            totalLines     = polygons.Count;
            latestProgress = 0;
            while (polygons.Count > 0)
            {
                var polygon = polygons.First();
                polygons.Remove(polygon.Key);

                // report progress.
                float progress = (float)System.Math.Round((((double)(totalLines - polygons.Count) / (double)totalLines) * 100));
                if (progress != latestProgress)
                {
                    OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                                                    "Merging polygons @z{3}e{4} ({1}/{2})... {0}%", progress, totalLines - polygons.Count, totalLines, zoomFactor, epsilon);
                    latestProgress = progress;
                }

                // copy the coordinates to lists.
                double[] x = polygon.Key.X.Clone() as double[];
                double[] y = polygon.Key.Y.Clone() as double[];

                //// find a matching line.
                //int mergeCount = 1;
                //Scene2D.ScenePoints found;
                //MatchPosition foundPosition = this.FindMatch(linesIndex, lines, x, y, line.Value, epsilon, out found);
                //while (found != null)
                //{ // TODO: keep expanding and duplicating until not possible anymore.
                //    // remove the found line.
                //    lines.Remove(found);

                //    // report progress.
                //    progress = (float)System.Math.Round((((double)(totalLines - lines.Count) / (double)totalLines) * 100));
                //    if (progress != latestProgress)
                //    {
                //        OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                //            "Merging lines @z{3}e{4} ({1}/{2})... {0}%", progress, totalLines - lines.Count, totalLines, zoomFactor, epsilon);
                //        latestProgress = progress;
                //    }

                //    // add the line.
                //    int lengthBefore = x.Length;
                //    Array.Resize(ref x, x.Length + found.X.Length - 1);
                //    Array.Resize(ref y, y.Length + found.Y.Length - 1);

                //    switch (foundPosition)
                //    {
                //        case MatchPosition.FirstFirst:
                //            found.X.InsertToReverse(1, x, 0, found.X.Length - 1);
                //            found.Y.InsertToReverse(1, y, 0, found.Y.Length - 1);
                //            break;
                //        case MatchPosition.FirstLast:
                //            found.X.InsertTo(0, x, 0, found.X.Length - 1);
                //            found.Y.InsertTo(0, y, 0, found.Y.Length - 1);
                //            break;
                //        case MatchPosition.LastFirst:
                //            found.X.CopyTo(x, lengthBefore - 1);
                //            found.Y.CopyTo(y, lengthBefore - 1);
                //            break;
                //        case MatchPosition.LastLast:
                //            found.X.CopyToReverse(x, lengthBefore - 1);
                //            found.Y.CopyToReverse(y, lengthBefore - 1);
                //            break;
                //    }

                //    // select a new line.
                //    foundPosition = this.FindMatch(linesIndex, lines, x, y, line.Value, epsilon, out found);
                //    mergeCount++;
                //}

                // simplify first.
                double[][] simplified = OsmSharp.Math.Algorithms.SimplifyCurve.Simplify(new double[][] { x, y },
                                                                                        epsilon);

                // add the new points.
                uint?pointsId = target.AddPoints(simplified[0], simplified[1]);

                // add points again with appropriate styles.
                if (pointsId.HasValue)
                {
                    foreach (var style in polygon.Value)
                    {
                        var scene2DStylePolygon = (style as Scene2DStylePolygon);
                        if (scene2DStylePolygon != null)
                        {
                            StylePolygon stylePolygon = source.GetStylePolygon(scene2DStylePolygon.StylePolygonId);
                            target.AddStylePolygon(pointsId.Value, stylePolygon.Layer, stylePolygon.MinZoom, stylePolygon.MaxZoom,
                                                   stylePolygon.Color, stylePolygon.Width, stylePolygon.Fill);
                            continue;
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Renders the primities for the given scene.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="view"></param>
        /// <param name="zoomFactor"></param>
        /// <param name="primitives"></param>
        private bool RenderPrimitives(Target2DWrapper <TTarget> target, View2D view, float zoomFactor,
                                      IEnumerable <Primitive2D> primitives)
        {
            try {
                // calculate current simplification epsilon.
                double epsilon = Scene2D.CalculateSimplificationEpsilon(new WebMercator(), zoomFactor);

                // loop over all primitives in the scene.
                int simplifiedLines = 0;
                int droppedLines    = 0;
                foreach (Primitive2D primitive in primitives)
                {     // the primitive is visible.
                    if (_cancelFlag)
                    {
                        return(false);                        // stop rendering on cancel and return false for an incomplete rendering.
                    }

                    if (primitive == null)
                    {
                        continue;
                    }
                    double[] x, y;
                    switch (primitive.Primitive2DType)
                    {
                    case Primitive2DType.Line2D:
                        Line2D line = (Line2D)primitive;

                        x = line.X;
                        y = line.Y;
                        if (x.Length > 4 && line.MaxZoom > zoomFactor * 2 && line.MaxZoom < 512)
                        {     // try and simplify.
                            double[][] simplified = OsmSharp.Math.Algorithms.SimplifyCurve.Simplify(new double[][] { x, y },
                                                                                                    epsilon);
                            if (simplified[0].Length < line.X.Length)
                            {
                                simplifiedLines++;
                                x = simplified[0];
                                y = simplified[1];
                            }
                            double distance = epsilon * 2;
                            if (simplified[0].Length == 2)
                            {     // check if the simplified version is smaller than epsilon.
                                distance = System.Math.Sqrt(
                                    System.Math.Pow((simplified[0][0] - simplified[0][1]), 2) +
                                    System.Math.Pow((simplified[1][0] - simplified[1][1]), 2));
                            }
                            if (distance < epsilon)
                            {
                                droppedLines++;
                                continue;
                            }
                        }
                        this.DrawLine(target, x, y, line.Color,
                                      this.FromPixels(target, view, line.Width), line.LineJoin, line.Dashes);
                        break;

                    case Primitive2DType.Polygon2D:
                        Polygon2D polygon = (Polygon2D)primitive;

                        x = polygon.X;
                        y = polygon.Y;
                        //if (x.Length > 4 && polygon.MaxZoom > zoomFactor * 2 && polygon.MaxZoom < 512)
                        //{ // try and simplify.
                        //    double[][] simplified = OsmSharp.Math.Algorithms.SimplifyCurve.Simplify(new double[][] { x, y },
                        //        epsilon);
                        //    if (simplified[0].Length < polygon.X.Length)
                        //    {
                        //        simplifiedLines++;
                        //        x = simplified[0];
                        //        y = simplified[1];
                        //    }
                        //    double distance = epsilon * 2;
                        //    if (simplified[0].Length == 2)
                        //    { // check if the simplified version is smaller than epsilon.
                        //        distance = System.Math.Sqrt(
                        //            System.Math.Pow((simplified[0][0] - simplified[0][1]), 2) +
                        //            System.Math.Pow((simplified[1][0] - simplified[1][1]), 2));
                        //    }
                        //    //if (distance < epsilon)
                        //    //{
                        //    //    droppedLines++;
                        //    //    continue;
                        //    //}
                        //}
                        this.DrawPolygon(target, x, y, polygon.Color,
                                         this.FromPixels(target, view, polygon.Width), polygon.Fill);
                        break;

                    case Primitive2DType.LineText2D:
                        LineText2D lineText = (LineText2D)primitive;
                        this.DrawLineText(target, lineText.X, lineText.Y, lineText.Text, lineText.Color,
                                          this.FromPixels(target, view, lineText.Size), lineText.HaloColor, lineText.HaloRadius, lineText.Font);
                        break;

                    case Primitive2DType.Point2D:
                        Point2D point = (Point2D)primitive;
                        this.DrawPoint(target, point.X, point.Y, point.Color,
                                       this.FromPixels(target, view, point.Size));
                        break;

                    case Primitive2DType.Icon2D:
                        Icon2D icon = (Icon2D)primitive;
                        this.DrawIcon(target, icon.X, icon.Y, icon.Image);
                        break;

                    case Primitive2DType.ImageTilted2D:
                        ImageTilted2D imageTilted = (ImageTilted2D)primitive;
                        this.DrawImage(target, imageTilted.Bounds, imageTilted.NativeImage);
                        break;

                    case Primitive2DType.Image2D:
                        Image2D image = (Image2D)primitive;
                        this.DrawImage(target, image.Left, image.Top, image.Right, image.Bottom, image.NativeImage);
                        break;

                    case Primitive2DType.Text2D:
                        Text2D text = (Text2D)primitive;
                        this.DrawText(target, text.X, text.Y, text.Text, text.Color,
                                      this.FromPixels(target, view, text.Size), text.HaloColor, text.HaloRadius, text.Font);
                        break;
                    }
                }
                return(true);
            }
            catch (Exception ex) {
                OsmSharp.Logging.Log.TraceEvent("Renderer2D", OsmSharp.Logging.TraceEventType.Error,
                                                ex.Message);
                throw ex;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Merges objects from the given scene for the given zoom level.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="source"></param>
        /// <param name="idx"></param>
        private void MergeObjects(Scene2D target, Scene2D source, int idx)
        {
            var lines = new Dictionary<Scene2D.ScenePoints, Scene2DStylesSet>();
            var linesIndex = new QuadTree<PointF2D, Scene2D.ScenePoints>();

            var polygons = new Dictionary<Scene2D.ScenePoints, Scene2DStylesSet>();
            //var polygonsIndex = new QuadTree<PointF2D, Scene2D.ScenePoints>();

            Dictionary<uint, SceneObject> sceneObjects = source.GetSceneObjectsAt(idx);
            float zoomFactor = source.GetMaximumZoomFactorAt(idx);
            float epsilon = source.CalculateSimplificationEpsilon(zoomFactor);
            foreach (var sceneObject in sceneObjects)
            {
                if (sceneObject.Value.Enum == SceneObjectType.LineObject)
                { // the scene object is a line object.
                    var sceneLineObject = sceneObject.Value as SceneLineObject;
                    Scene2D.ScenePoints scenePoints = source.GetPoints(sceneLineObject.GeoId);
                    Scene2DStylesSet stylesSet = null;
                    if (!lines.TryGetValue(scenePoints, out stylesSet))
                    { // create styles set.
                        stylesSet = new Scene2DStylesSet();
                        lines.Add(scenePoints, stylesSet);

                        // add scenePoints to the index.
                        linesIndex.Add(new PointF2D(scenePoints.X[0], scenePoints.Y[0]), scenePoints);
                        linesIndex.Add(new PointF2D(scenePoints.X[scenePoints.X.Length - 1], scenePoints.Y[scenePoints.Y.Length - 1]), scenePoints);
                    }
                    stylesSet.AddStyleLine(sceneLineObject.StyleId);
                }
                else if (sceneObject.Value.Enum == SceneObjectType.LineTextObject)
                {
                    var sceneLineTextObject = sceneObject.Value as SceneLineTextObject;
                    Scene2D.ScenePoints scenePoints = source.GetPoints(sceneLineTextObject.GeoId);
                    Scene2DStylesSet stylesSet = null;
                    if (!lines.TryGetValue(scenePoints, out stylesSet))
                    { // create styles set.
                        stylesSet = new Scene2DStylesSet();
                        lines.Add(scenePoints, stylesSet);

                        // add scenePoints to the index.
                        linesIndex.Add(new PointF2D(scenePoints.X[0], scenePoints.Y[0]), scenePoints);
                        linesIndex.Add(new PointF2D(scenePoints.X[scenePoints.X.Length - 1], scenePoints.Y[scenePoints.Y.Length - 1]), scenePoints);
                    }
                    stylesSet.AddStyleLineText(sceneLineTextObject.StyleId, sceneLineTextObject.TextId);
                }
                else if (sceneObject.Value.Enum == SceneObjectType.IconObject)
                {
                    throw new NotSupportedException("Icons not yet supported!");
                    //var sceneIconObject = (sceneObject.Value as SceneIconObject);
                    //Scene2D.ScenePoint scenePoint = source.GetPoint(sceneIconObject.GeoId);
                    //source.GetStyleIcon(
                    //target.AddIcon(target.AddPoint(scenePoint.X, scenePoint.Y);
                }
                else if (sceneObject.Value.Enum == SceneObjectType.PointObject)
                {
                    var scenePointObject = (sceneObject.Value as ScenePointObject);
                    Scene2D.ScenePoint scenePoint = source.GetPoint(scenePointObject.GeoId);
                    StylePoint stylePoint = source.GetStylePoint(scenePointObject.StyleId);

                    target.AddStylePoint(target.AddPoint(scenePoint.X, scenePoint.Y), stylePoint.Layer, stylePoint.MinZoom, stylePoint.MaxZoom,
                        stylePoint.Color, stylePoint.Size);
                }
                else if (sceneObject.Value.Enum == SceneObjectType.PolygonObject)
                { // the scene object is a polygon.
                    var scenePolygonObject = (sceneObject.Value as ScenePolygonObject);
                    Scene2D.ScenePoints scenePoints = source.GetPoints(sceneObject.Value.GeoId);
                    Scene2DStylesSet stylesSet = null;
                    if (!polygons.TryGetValue(scenePoints, out stylesSet))
                    { // create styles set.
                        stylesSet = new Scene2DStylesSet();
                        polygons.Add(scenePoints, stylesSet);

                        //// add scenePoints to the index.
                        //polygonsIndex.Add(new PointF2D(scenePoints.X[0], scenePoints.Y[0]), scenePoints);
                        //polygonsIndex.Add(new PointF2D(scenePoints.X[scenePoints.X.Length - 1], scenePoints.Y[scenePoints.Y.Length - 1]), scenePoints);
                    }
                    stylesSet.AddStylePolygon(scenePolygonObject.StyleId);

                    //var scenePolygonObject = (sceneObject.Value as ScenePolygonObject);
                    //Scene2D.ScenePoints scenePoints = source.GetPoints(sceneObject.Value.GeoId);
                    //StylePolygon stylePolygon = source.GetStylePolygon(sceneObject.Value.StyleId);

                    //uint? pointsId = target.AddPoints(scenePoints.X, scenePoints.Y);
                    //if (pointsId.HasValue)
                    //{
                    //    target.AddStylePolygon(pointsId.Value, stylePolygon.Layer, stylePolygon.MinZoom, stylePolygon.MaxZoom,
                    //        stylePolygon.Color, stylePolygon.Width, stylePolygon.Fill);
                    //}
                }
                else if (sceneObject.Value.Enum == SceneObjectType.TextObject)
                {
                    var sceneTextObject = (sceneObject.Value as SceneTextObject);
                    Scene2D.ScenePoint scenePoint = source.GetPoint(sceneObject.Value.GeoId);
                    StyleText styleText = source.GetStyleText(sceneTextObject.StyleId);
                    string text = source.GetText(sceneTextObject.TextId);

                    target.AddText(target.AddPoint(scenePoint.X, scenePoint.Y), styleText.Layer, styleText.MinZoom, styleText.MaxZoom,
                        styleText.Size, text, styleText.Color, styleText.HaloColor, styleText.HaloRadius, styleText.Font);
                }
            }

            // loop until there are no more candidates.
            int totalLines = lines.Count;
            float latestProgress = 0;
            while (lines.Count > 0)
            {
                var line = lines.First();
                lines.Remove(line.Key);

                // report progress.
                float progress = (float)System.Math.Round((((double)(totalLines - lines.Count) / (double)totalLines) * 100));
                if (progress != latestProgress)
                {
                    OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                        "Merging lines @z{3}e{4} ({1}/{2})... {0}%", progress, totalLines - lines.Count, totalLines, zoomFactor, epsilon);
                    latestProgress = progress;
                }

                // copy the coordinates to lists.
                double[] x = line.Key.X.Clone() as double[];
                double[] y = line.Key.Y.Clone() as double[];

                // find a matching line.
                int mergeCount = 1;
                Scene2D.ScenePoints found;
                MatchPosition foundPosition = this.FindMatch(linesIndex, lines, x, y, line.Value, epsilon, out found);
                while (found != null)
                { // TODO: keep expanding and duplicating until not possible anymore.
                    // remove the found line.
                    lines.Remove(found);

                    // report progress.
                    progress = (float)System.Math.Round((((double)(totalLines - lines.Count) / (double)totalLines) * 100));
                    if (progress != latestProgress)
                    {
                        OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                            "Merging lines @z{3}e{4} ({1}/{2})... {0}%", progress, totalLines - lines.Count, totalLines, zoomFactor, epsilon);
                        latestProgress = progress;
                    }

                    // add the line.
                    int lengthBefore = x.Length;
                    Array.Resize(ref x, x.Length + found.X.Length - 1);
                    Array.Resize(ref y, y.Length + found.Y.Length - 1);

                    switch (foundPosition)
                    {
                        case MatchPosition.FirstFirst:
                            found.X.InsertToReverse(1, x, 0, found.X.Length - 1);
                            found.Y.InsertToReverse(1, y, 0, found.Y.Length - 1);
                            break;
                        case MatchPosition.FirstLast:
                            found.X.InsertTo(0, x, 0, found.X.Length - 1);
                            found.Y.InsertTo(0, y, 0, found.Y.Length - 1);
                            break;
                        case MatchPosition.LastFirst:
                            found.X.CopyTo(x, lengthBefore - 1);
                            found.Y.CopyTo(y, lengthBefore - 1);
                            break;
                        case MatchPosition.LastLast:
                            found.X.CopyToReverse(x, lengthBefore - 1);
                            found.Y.CopyToReverse(y, lengthBefore - 1);
                            break;
                    }

                    // select a new line.
                    foundPosition = this.FindMatch(linesIndex, lines, x, y, line.Value, epsilon, out found);
                    mergeCount++;
                }

                // simplify first.
                double[][] simplified = OsmSharp.Math.Algorithms.SimplifyCurve.Simplify(new double[][] { x, y },
                                                            epsilon);

                // add the new points.
                uint? pointsId = target.AddPoints(simplified[0], simplified[1]);

                // add points again with appropriate styles.
                if (pointsId.HasValue)
                {
                    foreach (var style in line.Value)
                    {
                        var scene2DStyleLine = (style as Scene2DStyleLine);
                        if (scene2DStyleLine != null)
                        {
                            StyleLine styleLine = source.GetStyleLine(scene2DStyleLine.StyleLineId);
                            target.AddStyleLine(pointsId.Value, styleLine.Layer, styleLine.MinZoom, styleLine.MaxZoom,
                                styleLine.Color, styleLine.Width, styleLine.LineJoin, styleLine.Dashes);
                            continue;
                        }
                        var scene2DStyleLineText = (style as Scene2DStyleLineText);
                        if (scene2DStyleLineText != null)
                        {
                            StyleText styleText = source.GetStyleLineText(scene2DStyleLineText.StyleLineTextId);
                            string text = source.GetText(scene2DStyleLineText.TextId);
                            target.AddStyleLineText(pointsId.Value, styleText.Layer, styleText.MinZoom, styleText.MaxZoom,
                                styleText.Color, styleText.Size, text, styleText.Font, styleText.HaloColor, styleText.HaloRadius);
                            continue;
                        }
                    }
                }
            }

            // loop until there are no more candidates.
            totalLines = polygons.Count;
            latestProgress = 0;
            while (polygons.Count > 0)
            {
                var polygon = polygons.First();
                polygons.Remove(polygon.Key);

                // report progress.
                float progress = (float)System.Math.Round((((double)(totalLines - polygons.Count) / (double)totalLines) * 100));
                if (progress != latestProgress)
                {
                    OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                        "Merging polygons @z{3}e{4} ({1}/{2})... {0}%", progress, totalLines - polygons.Count, totalLines, zoomFactor, epsilon);
                    latestProgress = progress;
                }

                // copy the coordinates to lists.
                double[] x = polygon.Key.X.Clone() as double[];
                double[] y = polygon.Key.Y.Clone() as double[];

                //// find a matching line.
                //int mergeCount = 1;
                //Scene2D.ScenePoints found;
                //MatchPosition foundPosition = this.FindMatch(linesIndex, lines, x, y, line.Value, epsilon, out found);
                //while (found != null)
                //{ // TODO: keep expanding and duplicating until not possible anymore.
                //    // remove the found line.
                //    lines.Remove(found);

                //    // report progress.
                //    progress = (float)System.Math.Round((((double)(totalLines - lines.Count) / (double)totalLines) * 100));
                //    if (progress != latestProgress)
                //    {
                //        OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                //            "Merging lines @z{3}e{4} ({1}/{2})... {0}%", progress, totalLines - lines.Count, totalLines, zoomFactor, epsilon);
                //        latestProgress = progress;
                //    }

                //    // add the line.
                //    int lengthBefore = x.Length;
                //    Array.Resize(ref x, x.Length + found.X.Length - 1);
                //    Array.Resize(ref y, y.Length + found.Y.Length - 1);

                //    switch (foundPosition)
                //    {
                //        case MatchPosition.FirstFirst:
                //            found.X.InsertToReverse(1, x, 0, found.X.Length - 1);
                //            found.Y.InsertToReverse(1, y, 0, found.Y.Length - 1);
                //            break;
                //        case MatchPosition.FirstLast:
                //            found.X.InsertTo(0, x, 0, found.X.Length - 1);
                //            found.Y.InsertTo(0, y, 0, found.Y.Length - 1);
                //            break;
                //        case MatchPosition.LastFirst:
                //            found.X.CopyTo(x, lengthBefore - 1);
                //            found.Y.CopyTo(y, lengthBefore - 1);
                //            break;
                //        case MatchPosition.LastLast:
                //            found.X.CopyToReverse(x, lengthBefore - 1);
                //            found.Y.CopyToReverse(y, lengthBefore - 1);
                //            break;
                //    }

                //    // select a new line.
                //    foundPosition = this.FindMatch(linesIndex, lines, x, y, line.Value, epsilon, out found);
                //    mergeCount++;
                //}

                // simplify first.
                double[][] simplified = OsmSharp.Math.Algorithms.SimplifyCurve.Simplify(new double[][] { x, y },
                                                            epsilon);

                // add the new points.
                uint? pointsId = target.AddPoints(simplified[0], simplified[1]);

                // add points again with appropriate styles.
                if (pointsId.HasValue)
                {
                    foreach (var style in polygon.Value)
                    {
                        var scene2DStylePolygon = (style as Scene2DStylePolygon);
                        if (scene2DStylePolygon != null)
                        {
                            StylePolygon stylePolygon = source.GetStylePolygon(scene2DStylePolygon.StylePolygonId);
                            target.AddStylePolygon(pointsId.Value, stylePolygon.Layer, stylePolygon.MinZoom, stylePolygon.MaxZoom,
                                stylePolygon.Color, stylePolygon.Width, stylePolygon.Fill);
                            continue;
                        }
                    }
                }
            }
        }