Exemplo n.º 1
0
        /// <summary>
        /// Serializes the given scene.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="scene"></param>
        /// <param name="compress"></param>
        public static void Serialize(Stream stream, TagsCollectionBase metaTags, Scene2D scene, bool compress)
        {
            RuntimeTypeModel typeModel = SceneSerializer.BuildRuntimeTypeModel();

            // [MetaIndexLenght:4][Metadata][SeneIndexLength:4][SceneIndex][SceneLengths:4*zoomFactors.length][Scenes]
            // MetaIndexLenght: int The lenght of the meta index.
            // Metadata: a number of serialized tags.
            // SceneIndexLength: int The length of the sceneindex in bytes.
            // SceneIndex: the serialized scene index.
            // SceneLengths: int[] The lengths of the scenes per zoom level as in the zoomfactors array.
            // Scenes: The serialized scenes themselves.

            // serialize meta tags.
            byte[] tagsBytes = (new TagsCollectionSerializer()).Serialize(metaTags);
            stream.Write(BitConverter.GetBytes(tagsBytes.Length), 0, 4);
            stream.Write(tagsBytes, 0, tagsBytes.Length);

            // index index.
            SceneIndex sceneIndex = new SceneIndex();

            sceneIndex.LineStyles    = scene.GetStyleLines();
            sceneIndex.PointStyles   = scene.GetStylePoints();
            sceneIndex.PolygonStyles = scene.GetStylePolygons();
            sceneIndex.TextStyles    = scene.GetStyleTexts();
            sceneIndex.ZoomRanges    = scene.GetZoomRanges();
            sceneIndex.ZoomFactors   = scene.GetZoomFactors();
            sceneIndex.IconImage     = scene.GetImages();

            // write SceneIndex
            long positionAfterMeta = stream.Position;

            stream.Seek(positionAfterMeta + 4, SeekOrigin.Begin);
            long indexStart = stream.Position;

            typeModel.Serialize(stream, sceneIndex);

            // write SeneIndexLength
            int indexSize = (int)(stream.Position - indexStart);

            stream.Seek(positionAfterMeta + 0, SeekOrigin.Begin);
            stream.Write(BitConverter.GetBytes(indexSize), 0, 4);

            // write Scenes.
            stream.Seek(positionAfterMeta + 4 + indexSize + 4 * sceneIndex.ZoomFactors.Length, SeekOrigin.Begin);
            // index into r-trees and serialize.
            int[] lengths = new int[sceneIndex.ZoomFactors.Length];
            for (int idx = 0; idx < lengths.Length; idx++)
            {
                long position = stream.Position;

                Dictionary <uint, SceneObject> sceneAtZoom = scene.GetObjectsAt(idx);
                RTreeMemoryIndex <SceneObject> memoryIndex = new RTreeMemoryIndex <SceneObject>();

                float latestProgress = 0;
                int   sceneObjectIdx = 0;
                foreach (KeyValuePair <uint, SceneObject> sceneObjectPair in sceneAtZoom)
                { // loop over all primitives in order.
                    SceneObject sceneObject = sceneObjectPair.Value;
                    uint        id          = sceneObjectPair.Key;

                    switch (sceneObject.Enum)
                    {
                    case SceneObjectType.IconObject:
                    case SceneObjectType.PointObject:
                    case SceneObjectType.TextObject:
                        OsmSharp.UI.Renderer.Scene.Scene2D.ScenePoint geo = scene.GetPoint(sceneObject.GeoId);
                        PointF2D point = new PointF2D(geo.X, geo.Y);
                        memoryIndex.Add(new BoxF2D(point), sceneObject);
                        break;

                    case SceneObjectType.LineObject:
                    case SceneObjectType.LineTextObject:
                    case SceneObjectType.PolygonObject:
                        OsmSharp.UI.Renderer.Scene.Scene2D.ScenePoints geos = scene.GetPoints(sceneObject.GeoId);
                        memoryIndex.Add(new BoxF2D(geos.X, geos.Y), sceneObject);
                        break;
                    }

                    float progress = (float)System.Math.Round((((double)sceneObjectIdx / (double)sceneAtZoom.Count) * 100));
                    if (progress != latestProgress)
                    {
                        OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                                                        "Indexing scene objects at zoom {1} ({2}/{3})... {0}%", progress, sceneIndex.ZoomFactors[idx],
                                                        sceneObjectIdx, sceneAtZoom.Count);
                        latestProgress = progress;
                    }
                    sceneObjectIdx++;
                }

                // serialize the r-tree.
                OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                                                "Serializing RTRee...");
                SceneObjectRTreeSerializer memoryIndexSerializer = new SceneObjectRTreeSerializer(
                    scene, compress, idx, SceneSerializer.CalculateScaleFactor(sceneIndex.ZoomFactors[idx]));
                memoryIndexSerializer.Serialize(new LimitedStream(stream), memoryIndex);

                lengths[idx] = (int)(stream.Position - position);
            }

            // write SceneLengths
            long end = stream.Position;

            stream.Seek(positionAfterMeta + 4 + indexSize, SeekOrigin.Begin);
            for (int idx = 0; idx < lengths.Length; idx++)
            {
                stream.Write(BitConverter.GetBytes(lengths[idx]), 0, 4);
            }
            stream.Seek(end, SeekOrigin.Begin);
        }
Exemplo 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;
                        }
                    }
                }
            }
        }
Exemplo n.º 3
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)
        {
            Dictionary<Scene2D.ScenePoints, Scene2DStylesSet> lines = new Dictionary<Scene2D.ScenePoints, Scene2DStylesSet>();
            Dictionary<PointF2D, HashSet<Scene2D.ScenePoints>> endpoints = new Dictionary<PointF2D, HashSet<Scene2D.ScenePoints>>();
            Dictionary<uint, SceneObject> sceneObjects = source.GetSceneObjectsAt(idx);
            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);
                    }
                    stylesSet.AddStyleLine(sceneLineObject.StyleId);

                    //var sceneLineObject = (sceneObject.Value as SceneLineObject);
                    //Scene2D.ScenePoints scenePoints = source.GetPoints(sceneLineObject.GeoId);
                    //StyleLine styleLine = source.GetStyleLine(sceneLineObject.StyleId);

                    //uint? pointsId = target.AddPoints(scenePoints.X, scenePoints.Y);
                    //if (pointsId.HasValue)
                    //{
                    //    target.AddStyleLine(pointsId.Value, styleLine.Layer, styleLine.MinZoom, styleLine.MaxZoom,
                    //        styleLine.Color, styleLine.Width, styleLine.LineJoin, styleLine.Dashes);
                    //}
                }
                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);
                    }
                    stylesSet.AddStyleLineText(sceneLineTextObject.StyleId, sceneLineTextObject.TextId);

                    //var sceneLineTextObject = (sceneObject.Value as SceneLineTextObject);
                    //Scene2D.ScenePoints scenePoints = source.GetPoints(sceneLineTextObject.GeoId);
                    //StyleText styleText = source.GetStyleText(sceneLineTextObject.StyleId);
                    //string text = source.GetText(sceneLineTextObject.TextId);

                    //uint? pointsId = target.AddPoints(scenePoints.X, scenePoints.Y);
                    //if (pointsId.HasValue)
                    //{
                    //    target.AddStyleLineText(pointsId.Value, styleText.Layer, styleText.MinZoom, styleText.MaxZoom,
                    //        styleText.Color, styleText.Size, text, styleText.Font, styleText.HaloColor, styleText.HaloRadius);
                    //}
                }
                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)
                {
                    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 ({1}/{2})... {0}%", progress, totalLines - lines.Count, totalLines);
                    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(lines, x, y, line.Value, out found);
                while (found != null)
                { // TODO: keep expanding and duplicating until not possible anymore.
                    // remove the found line.
                    lines.Remove(found);

                    // 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(lines, x, y, line.Value, out found);
                    mergeCount++;
                }

                // add the new points.
                uint? pointsId = target.AddPoints(x, y);

                // 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;
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Serializes the given scene.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="scene"></param>
        /// <param name="compress"></param>
        public static void Serialize(Stream stream, TagsCollectionBase metaTags, Scene2D scene, bool compress)
        {
            RuntimeTypeModel typeModel = SceneSerializer.BuildRuntimeTypeModel();

            // [MetaIndexLenght:4][Metadata][SeneIndexLength:4][SceneIndex][SceneLengths:4*zoomFactors.length][Scenes]
            // MetaIndexLenght: int The lenght of the meta index.
            // Metadata: a number of serialized tags.
            // SceneIndexLength: int The length of the sceneindex in bytes.
            // SceneIndex: the serialized scene index.
            // SceneLengths: int[] The lengths of the scenes per zoom level as in the zoomfactors array.
            // Scenes: The serialized scenes themselves.

            // serialize meta tags.
            (new TagsCollectionSerializer()).SerializeWithSize(metaTags, stream);

            // index index.
            SceneIndex sceneIndex = new SceneIndex();
            sceneIndex.LineStyles = scene.GetStyleLines();
            sceneIndex.PointStyles = scene.GetStylePoints();
            sceneIndex.PolygonStyles = scene.GetStylePolygons();
            sceneIndex.TextStyles = scene.GetStyleTexts();
            sceneIndex.ZoomRanges = scene.GetZoomRanges();
            sceneIndex.ZoomFactors = scene.GetZoomFactors();
            sceneIndex.IconImage = scene.GetImages();

            // write SceneIndex
            long positionAfterMeta = stream.Position;
            stream.Seek(positionAfterMeta + 4, SeekOrigin.Begin);
            long indexStart = stream.Position;
            typeModel.Serialize(stream, sceneIndex);

            // write SeneIndexLength
            int indexSize = (int)(stream.Position - indexStart);
            stream.Seek(positionAfterMeta + 0, SeekOrigin.Begin);
            stream.Write(BitConverter.GetBytes(indexSize), 0, 4);

            // write Scenes.
            stream.Seek(positionAfterMeta + 4 + indexSize + 4 * sceneIndex.ZoomFactors.Length, SeekOrigin.Begin);
            // index into r-trees and serialize.
            int[] lengths = new int[sceneIndex.ZoomFactors.Length];
            for (int idx = 0; idx < lengths.Length; idx++)
            {
                long position = stream.Position;

                Dictionary<uint, SceneObject> sceneAtZoom = scene.GetObjectsAt(idx);
                RTreeMemoryIndex<SceneObject> memoryIndex = new RTreeMemoryIndex<SceneObject>(50, 100);

                float latestProgress = 0;
                int sceneObjectIdx = 0;
                foreach (KeyValuePair<uint, SceneObject> sceneObjectPair in sceneAtZoom)
                { // loop over all primitives in order.
                    SceneObject sceneObject = sceneObjectPair.Value;
                    uint id = sceneObjectPair.Key;

                    switch (sceneObject.Enum)
                    {
                        case SceneObjectType.IconObject:
                        case SceneObjectType.PointObject:
                        case SceneObjectType.TextObject:
                            OsmSharp.UI.Renderer.Scene.Scene2D.ScenePoint geo = scene.GetPoint(sceneObject.GeoId);
                            PointF2D point = new PointF2D(geo.X, geo.Y);
                            memoryIndex.Add(new BoxF2D(point), sceneObject);
                            break;
                        case SceneObjectType.LineObject:
                        case SceneObjectType.LineTextObject:
                        case SceneObjectType.PolygonObject:
                            OsmSharp.UI.Renderer.Scene.Scene2D.ScenePoints geos = scene.GetPoints(sceneObject.GeoId);
                            memoryIndex.Add(new BoxF2D(geos.X, geos.Y), sceneObject);
                            break;
                    }

                    float progress = (float)System.Math.Round((((double)sceneObjectIdx / (double)sceneAtZoom.Count) * 100));
                    if (progress != latestProgress)
                    {
                        OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                            "Indexing scene objects at zoom {1} ({2}/{3})... {0}%", progress, sceneIndex.ZoomFactors[idx],
                                sceneObjectIdx, sceneAtZoom.Count);
                        latestProgress = progress;
                    }
                    sceneObjectIdx++;
                }

                // serialize the r-tree.
                OsmSharp.Logging.Log.TraceEvent("SceneSerializer", OsmSharp.Logging.TraceEventType.Information,
                    "Serializing RTRee...");
                SceneObjectRTreeSerializer memoryIndexSerializer = new SceneObjectRTreeSerializer(
                    scene, compress, idx, SceneSerializer.CalculateScaleFactor(sceneIndex.ZoomFactors[idx]));
                memoryIndexSerializer.Serialize(new LimitedStream(stream), memoryIndex);

                lengths[idx] = (int)(stream.Position - position);
            }

            // write SceneLengths
            long end = stream.Position;
            stream.Seek(positionAfterMeta + 4 + indexSize, SeekOrigin.Begin);
            for (int idx = 0; idx < lengths.Length; idx++)
            {
                stream.Write(BitConverter.GetBytes(lengths[idx]), 0, 4);
            }
            stream.Seek(end, SeekOrigin.Begin);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Serializes one leaf.
        /// </summary>
        /// <param name="typeModel"></param>
        /// <param name="data"></param>
        /// <param name="boxes"></param>
        /// <returns></returns>
        protected override byte[] Serialize(RuntimeTypeModel typeModel,
                                            List <SceneObject> data, List <BoxF2D> boxes)
        {
            int scaleFactor = _scaleFactor;

            Dictionary <uint, int> addedPoint  = new Dictionary <uint, int>();
            Dictionary <uint, int> addedPoints = new Dictionary <uint, int>();

            SceneObjectBlock leafData = new SceneObjectBlock();

            //leafData.PointsIndexes = new List<int>();
            leafData.PointsX = new List <long>();
            leafData.PointsY = new List <long>();

            leafData.IconPointId = new List <int>();
            leafData.IconImageId = new List <uint>();

            leafData.LinePointsId = new List <int>();
            leafData.LineStyleId  = new List <uint>();

            leafData.LineTextPointsId = new List <int>();
            leafData.LineTextStyleId  = new List <uint>();
            leafData.LineTextText     = new List <string>();

            leafData.PointPointId = new List <int>();
            leafData.PointStyleId = new List <uint>();

            leafData.PolygonPointsId = new List <int>();
            leafData.PolygonStyleId  = new List <uint>();

            leafData.TextPointPointId = new List <int>();
            leafData.TextPointStyleId = new List <uint>();
            leafData.TextPointText    = new List <string>();

            foreach (SceneObject sceneObject in data)
            {
                int geoId = -1;
                OsmSharp.UI.Renderer.Scene.Scene2D.ScenePoint  point;
                OsmSharp.UI.Renderer.Scene.Scene2D.ScenePoints points;
                switch (sceneObject.Enum)
                {
                case SceneObjectType.IconObject:
                    // get point.
                    point = _scene.GetPoint(sceneObject.GeoId);

                    // set point data and keep id.
                    if (!addedPoint.TryGetValue(sceneObject.GeoId, out geoId))
                    {     // the point was not added yet.
                        geoId = leafData.PointsX.Count;
                        //leafData.PointsIndexes.Add(geoId);
                        leafData.PointsX.Add((long)(scaleFactor * point.X));
                        leafData.PointsY.Add((long)(scaleFactor * point.Y));
                        addedPoint.Add(sceneObject.GeoId, geoId);
                    }
                    leafData.IconPointId.Add(geoId);     // add point.

                    // add image id.
                    leafData.IconImageId.Add(sceneObject.StyleId);
                    break;

                case SceneObjectType.PointObject:
                    // get point.
                    point = _scene.GetPoint(sceneObject.GeoId);

                    // set point data and keep id.
                    if (!addedPoint.TryGetValue(sceneObject.GeoId, out geoId))
                    {     // the point was not added yet.
                        geoId = leafData.PointsX.Count;
                        leafData.PointsX.Add((long)(scaleFactor * point.X));
                        leafData.PointsY.Add((long)(scaleFactor * point.Y));
                        //leafData.PointsIndexes.Add(leafData.PointsY.Count);
                        addedPoint.Add(sceneObject.GeoId, geoId);
                    }
                    leafData.PointPointId.Add(geoId);

                    // add point style.
                    leafData.PointStyleId.Add(sceneObject.StyleId);
                    break;

                case SceneObjectType.TextObject:
                    // get point.
                    point = _scene.GetPoint(sceneObject.GeoId);

                    // set point data and keep id.
                    if (!addedPoint.TryGetValue(sceneObject.GeoId, out geoId))
                    {     // the point was not added yet.
                        geoId = leafData.PointsX.Count;
                        leafData.PointsX.Add((long)(scaleFactor * point.X));
                        leafData.PointsY.Add((long)(scaleFactor * point.Y));
                        //leafData.PointsIndexes.Add(leafData.PointsY.Count);
                        addedPoint.Add(sceneObject.GeoId, geoId);
                    }
                    leafData.TextPointPointId.Add(geoId);

                    // add point style.
                    leafData.TextPointStyleId.Add(sceneObject.StyleId);

                    // add text.
                    leafData.TextPointText.Add(
                        _scene.GetText((sceneObject as SceneTextObject).TextId));
                    break;

                case SceneObjectType.LineObject:
                    // get points.
                    points = _scene.GetPoints(sceneObject.GeoId);

                    // set points data and keep id.
                    if (!addedPoints.TryGetValue(sceneObject.GeoId, out geoId))
                    {     // the point was not added yet.
                        geoId = leafData.PointsX.Count;
                        leafData.PointsX.AddRange(points.X.ConvertToLongArray(scaleFactor));
                        leafData.PointsY.AddRange(points.Y.ConvertToLongArray(scaleFactor));
                        //leafData.PointsIndexes.Add(leafData.PointsY.Count);
                        addedPoints.Add(sceneObject.GeoId, geoId);
                    }
                    leafData.LinePointsId.Add(geoId);

                    // add point style.
                    leafData.LineStyleId.Add(sceneObject.StyleId);
                    break;

                case SceneObjectType.LineTextObject:
                    // get points.
                    points = _scene.GetPoints(sceneObject.GeoId);

                    // set points data and keep id.
                    if (!addedPoints.TryGetValue(sceneObject.GeoId, out geoId))
                    {     // the point was not added yet.
                        geoId = leafData.PointsX.Count;
                        leafData.PointsX.AddRange(points.X.ConvertToLongArray(scaleFactor));
                        leafData.PointsY.AddRange(points.Y.ConvertToLongArray(scaleFactor));
                        //leafData.PointsIndexes.Add(leafData.PointsY.Count);
                        addedPoints.Add(sceneObject.GeoId, geoId);
                    }
                    leafData.LineTextPointsId.Add(geoId);

                    // add point style.
                    leafData.LineTextStyleId.Add(sceneObject.StyleId);

                    // add text.
                    leafData.LineTextText.Add(
                        _scene.GetText((sceneObject as SceneLineTextObject).TextId));
                    break;

                case SceneObjectType.PolygonObject:
                    // get points.
                    points = _scene.GetPoints(sceneObject.GeoId);

                    // set points data and keep id.
                    if (!addedPoints.TryGetValue(sceneObject.GeoId, out geoId))
                    {     // the point was not added yet.
                        geoId = leafData.PointsX.Count;
                        leafData.PointsX.AddRange(points.X.ConvertToLongArray(scaleFactor));
                        leafData.PointsY.AddRange(points.Y.ConvertToLongArray(scaleFactor));
                        //leafData.PointsIndexes.Add(leafData.PointsY.Count);
                        addedPoints.Add(sceneObject.GeoId, geoId);
                    }
                    leafData.PolygonPointsId.Add(geoId);

                    // add point style.
                    leafData.PolygonStyleId.Add(sceneObject.StyleId);
                    break;
                }
            }

            // get the mins.
            leafData.PointsXMin = leafData.PointsX.Min();
            leafData.PointsYMin = leafData.PointsY.Min();

            // encode.
            for (int idx = 0; idx < leafData.PointsX.Count; idx++)
            {
                leafData.PointsX[idx] = leafData.PointsX[idx] - leafData.PointsXMin;
                leafData.PointsY[idx] = leafData.PointsY[idx] - leafData.PointsYMin;
            }

            // serialize.
            MemoryStream stream = new MemoryStream();

            typeModel.Serialize(stream, leafData);
            byte[] serializedData = stream.ToArray();
            stream.Dispose();
            if (_compressed)
            { // compress.
                serializedData = GZipStream.CompressBuffer(serializedData);
            }
            return(serializedData);
        }