Beispiel #1
0
        /// <summary>
        /// Converts a PBF way into an OsmSharp-way.
        /// </summary>
        public static OsmSharp.Way DecodeWay(PrimitiveBlock block, OsmSharp.IO.PBF.Way pbfWay)
        {
            var way = new OsmSharp.Way();

            Encoder.DecodeWay(block, pbfWay, way);
            return(way);
        }
Beispiel #2
0
        public void TestEncodeWay()
        {
            var block = new PrimitiveBlock();

            block.date_granularity = 1000;
            block.granularity      = 100;
            block.lat_offset       = 0;
            block.lon_offset       = 0;
            block.stringtable      = new StringTable();
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(string.Empty)); // always encode empty string as '0'.
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("highway"));
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential"));
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben"));

            var way = new OsmSharp.Way();

            way.Id          = 1;
            way.ChangeSetId = 1;
            way.Tags        = new OsmSharp.Tags.TagsCollection();
            way.Tags.Add("name", "Ben");
            way.TimeStamp = DateTime.Now;
            way.UserId    = 1;
            way.UserName  = "******";
            way.Version   = 1;
            way.Visible   = true;
            way.Nodes     = new long[] { 1, 2 };

            var pbfWay = Encoder.EncodeWay(block, new Dictionary <string, int>(), way);

            Assert.IsNotNull(pbfWay);
            Assert.AreEqual(1, pbfWay.id);
            Assert.AreEqual(2, pbfWay.refs.Count);
            Assert.AreEqual(1, pbfWay.refs[0]);
            Assert.AreEqual(1, pbfWay.refs[1]);
            Assert.AreEqual(1, pbfWay.info.changeset);
            Assert.AreEqual(Encoder.EncodeTimestamp(way.TimeStamp.Value, block.date_granularity), pbfWay.info.timestamp);
            Assert.AreEqual(1, pbfWay.info.uid);
            Assert.AreEqual("Ben", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.info.user_sid]));
            Assert.AreEqual(1, pbfWay.info.version);
            Assert.AreEqual(1, pbfWay.keys.Count);
            Assert.AreEqual("name", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.keys[0]]));
            Assert.AreEqual(1, pbfWay.vals.Count);
            Assert.AreEqual("Ben", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.vals[0]]));
        }
Beispiel #3
0
        /// <summary>
        /// Encodes an OsmSharp-way into a PBF-way.
        /// </summary>
        public static OsmSharp.IO.PBF.Way EncodeWay(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, OsmSharp.Way way)
        {
            var pbfWay = new OsmSharp.IO.PBF.Way();

            pbfWay.id   = way.Id.Value;
            pbfWay.info = new Info();
            if (way.ChangeSetId.HasValue)
            {
                pbfWay.info.changeset = way.ChangeSetId.Value;
            }
            if (way.TimeStamp.HasValue)
            {
                pbfWay.info.timestamp = Encoder.EncodeTimestamp(way.TimeStamp.Value, block.date_granularity);
            }
            if (way.UserId.HasValue)
            {
                pbfWay.info.uid = (int)way.UserId.Value;
            }
            pbfWay.info.user_sid = Encoder.EncodeString(block, reverseStringTable, way.UserName);
            pbfWay.info.version  = 0;
            if (way.Version.HasValue)
            {
                pbfWay.info.version = (int)way.Version.Value;
            }

            if (way.Tags != null)
            {
                foreach (var tag in way.Tags)
                {
                    pbfWay.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key));
                    pbfWay.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value));
                }
            }

            if (way.Nodes != null &&
                way.Nodes.Length > 0)
            {
                pbfWay.refs.Add(way.Nodes[0]);
                for (var i = 1; i < way.Nodes.Length; i++)
                {
                    pbfWay.refs.Add(way.Nodes[i] - way.Nodes[i - 1]);
                }
            }
            return(pbfWay);
        }
Beispiel #4
0
        /// <summary>
        /// Converts a PBF-way into an OsmSharp-way.
        /// </summary>
        public static void DecodeWay(PrimitiveBlock block, OsmSharp.IO.PBF.Way pbfWay, OsmSharp.Way way)
        {
            // make sure old data is gone.
            if (way.Nodes != null &&
                way.Nodes.Length > 0)
            { // clear nodes list.
                way.Nodes = new long[pbfWay.refs.Count];
            }
            if (way.Tags != null)
            { // clear the tags collection.
                way.Tags.Clear();
            }
            if (way.Nodes == null)
            { // create nodes list.
                way.Nodes = new long[pbfWay.refs.Count];
            }
            if (way.Tags == null)
            { // create tags collection.
                way.Tags = new TagsCollection(pbfWay.keys.Count);
            }

            // set new stuff.
            way.Id = pbfWay.id;
            if (pbfWay.refs.Count > 0)
            { // fill nodes-list.
                long nodeId = 0;
                for (int i = 0; i < pbfWay.refs.Count; i++)
                {
                    nodeId       = nodeId + pbfWay.refs[i];
                    way.Nodes[i] = nodeId;
                }
            }
            if (pbfWay.keys.Count > 0)
            {
                for (var i = 0; i < pbfWay.keys.Count; i++)
                {
                    var key   = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.keys[i]]);
                    var value = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.vals[i]]);

                    way.Tags.Add(new Tag(key, value));
                }
            }
            if (pbfWay.info != null)
            { // add the metadata if any.
                way.ChangeSetId = pbfWay.info.changeset;
                way.TimeStamp   = Encoder.DecodeTimestamp(pbfWay.info.timestamp, block.date_granularity);
                way.UserId      = pbfWay.info.uid;
                way.UserName    = null;
                if (block.stringtable != null)
                {
                    way.UserName = System.Text.Encoding.UTF8.GetString(block.stringtable.s[pbfWay.info.user_sid]);
                }
                way.Version = pbfWay.info.version;
            }
            way.Visible = true;
        }