Beispiel #1
0
        /// <summary>
        /// Encodes an OsmSharp-node into a PBF-node.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.PBF.Node EncodeNode(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, Osm.Node node)
        {
            var pbfNode = new OsmSharp.Osm.PBF.Node();

            Encoder.EncodeNode(block, reverseStringTable, pbfNode, node);
            return(pbfNode);
        }
Beispiel #2
0
 public void ReadAll(PrimitiveBlock block)
 {
     if (block == null)
     {
         throw new ArgumentNullException("block");
     }
 }
Beispiel #3
0
        /// <summary>
        /// Encodes a string.
        /// </summary>
        /// <returns></returns>
        public static int EncodeString(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, string value)
        {
            if (value == null)
            {
                return(0);
            }

            if (block.stringtable == null)
            {
                block.stringtable = new StringTable();
                block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(string.Empty));
                reverseStringTable.Add(string.Empty, 0);
            }

            int id;

            if (reverseStringTable.TryGetValue(value, out id))
            {
                return(id);
            }

            var bytes = System.Text.Encoding.UTF8.GetBytes(value);

            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(value));
            reverseStringTable.Add(value, block.stringtable.s.Count - 1);
            return(block.stringtable.s.Count - 1);
        }
Beispiel #4
0
        /// <summary>
        /// Converts a PBF way into an OsmSharp-way.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.Way DecodeWay(PrimitiveBlock block, OsmSharp.Osm.PBF.Way pbfWay)
        {
            var way = new OsmSharp.Osm.Way();

            Encoder.DecodeWay(block, pbfWay, way);
            return(way);
        }
Beispiel #5
0
        /// <summary>
        /// Converts a PBF way into an OsmSharp-relation.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.Relation DecodeRelation(PrimitiveBlock block, OsmSharp.Osm.PBF.Relation pbfRelation)
        {
            var relation = new OsmSharp.Osm.Relation();

            Encoder.DecodeRelation(block, pbfRelation, relation);
            return(relation);
        }
Beispiel #6
0
        /// <summary>
        /// Converts the PBF node into an OsmSharp-node.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.Node DecodeNode(PrimitiveBlock block, OsmSharp.Osm.PBF.Node pbfNode)
        {
            var node = new OsmSharp.Osm.Node();

            Encoder.DecodeNode(block, pbfNode, node);
            return(node);
        }
Beispiel #7
0
        /// <summary>
        /// Encodes an OsmSharp-node into a PBF-node.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.PBF.Node EncodeNode(PrimitiveBlock block, Dictionary <string, int> reverseStringTable,
                                                       OsmSharp.Osm.PBF.Node pbfNode, Osm.Node node)
        {
            pbfNode.id           = node.Id.Value;
            pbfNode.info         = new Info();
            pbfNode.info.version = 0;
            if (node.ChangeSetId.HasValue)
            {
                pbfNode.info.changeset = node.ChangeSetId.Value;
            }
            else
            {
                pbfNode.info.changeset = 0;
            }
            if (node.TimeStamp.HasValue)
            {
                pbfNode.info.timestamp = Encoder.EncodeTimestamp(node.TimeStamp.Value, block.date_granularity);
            }
            else
            {
                pbfNode.info.timestamp = 0;
            }
            if (node.UserId.HasValue)
            {
                pbfNode.info.uid = (int)node.UserId.Value;
            }
            else
            {
                pbfNode.info.uid = 0;
            }
            pbfNode.info.user_sid = Encoder.EncodeString(block, reverseStringTable, node.UserName);
            if (node.Version.HasValue)
            {
                pbfNode.info.version = (int)node.Version.Value;
            }
            else
            {
                pbfNode.info.version = 0;
            }
            pbfNode.lat = Encoder.EncodeLatLon(node.Latitude.Value, block.lat_offset, block.granularity);
            pbfNode.lon = Encoder.EncodeLatLon(node.Longitude.Value, block.lon_offset, block.granularity);

            if (node.Tags != null)
            {
                foreach (var tag in node.Tags)
                {
                    pbfNode.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key));
                    pbfNode.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value));
                }
            }
            else
            {
                pbfNode.keys.Clear();
                pbfNode.vals.Clear();
            }
            return(pbfNode);
        }
Beispiel #8
0
        /// <summary>
        /// Converts a PBF-way into an OsmSharp-way.
        /// </summary>
        public static void DecodeWay(PrimitiveBlock block, OsmSharp.Osm.PBF.Way pbfWay, OsmSharp.Osm.Way way)
        {
            // make sure old data is gone.
            if (way.Nodes != null &&
                way.Nodes.Count > 0)
            { // clear nodes list.
                way.Nodes.Clear();
            }
            if (way.Tags != null)
            { // clear the tags collection.
                way.Tags.Clear();
            }
            if (way.Nodes == null)
            { // create nodes list.
                way.Nodes = new List <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.Add(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 = (ulong)pbfWay.info.version;
            }
            way.Visible = true;
        }
Beispiel #9
0
        /// <summary>
        /// Writes one PBF primitive block.
        /// </summary>
        /// <param name="block">The block to write.</param>
        public void ReadAll(PrimitiveBlock block)
        {
            // check parameters.
            if (block == null)
            {
                throw new ArgumentNullException("block");
            }

            // TODO: all the important stuff!
        }
Beispiel #10
0
        /// <summary>
        /// Writes one PBF primitive block.
        /// </summary>
        /// <param name="block">The block to write.</param>
        public void ReadAll(PrimitiveBlock block)
        {
            // check parameters.
            if (block == null)
            {
                throw new ArgumentNullException("block");
            }

            // TODO: all the important stuff!
        }
Beispiel #11
0
        public static Node EncodeNode(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, Node pbfNode, OsmSharp.Osm.Node node)
        {
            pbfNode.id             = node.Id.Value;
            pbfNode.info           = new Info();
            pbfNode.info.version   = 0;
            pbfNode.info.changeset = !node.ChangeSetId.HasValue ? 0L : node.ChangeSetId.Value;
            DateTime?timeStamp = node.TimeStamp;

            if (timeStamp.HasValue)
            {
                Info info = pbfNode.info;
                timeStamp = node.TimeStamp;
                int num = Encoder.EncodeTimestamp(timeStamp.Value, (long)block.date_granularity);
                info.timestamp = num;
            }
            else
            {
                pbfNode.info.timestamp = 0;
            }
            long?userId = node.UserId;

            if (userId.HasValue)
            {
                Info info = pbfNode.info;
                userId = node.UserId;
                int num = (int)userId.Value;
                info.uid = num;
            }
            else
            {
                pbfNode.info.uid = 0;
            }
            pbfNode.info.user_sid = Encoder.EncodeString(block, reverseStringTable, node.UserName);
            pbfNode.info.version  = !node.Version.HasValue ? 0 : (int)node.Version.Value;
            pbfNode.lat           = Encoder.EncodeLatLon(node.Latitude.Value, block.lat_offset, (long)block.granularity);
            pbfNode.lon           = Encoder.EncodeLatLon(node.Longitude.Value, block.lon_offset, (long)block.granularity);
            if (node.Tags != null)
            {
                foreach (Tag tag in node.Tags)
                {
                    pbfNode.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key));
                    pbfNode.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value));
                }
            }
            else
            {
                pbfNode.keys.Clear();
                pbfNode.vals.Clear();
            }
            return(pbfNode);
        }
Beispiel #12
0
        public static Way EncodeWay(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, OsmSharp.Osm.Way way)
        {
            Way way1 = new Way();

            way1.id   = way.Id.Value;
            way1.info = new Info();
            long?nullable;

            if (way.ChangeSetId.HasValue)
            {
                Info info = way1.info;
                nullable = way.ChangeSetId;
                long num = nullable.Value;
                info.changeset = num;
            }
            if (way.TimeStamp.HasValue)
            {
                way1.info.timestamp = Encoder.EncodeTimestamp(way.TimeStamp.Value, (long)block.date_granularity);
            }
            nullable = way.UserId;
            if (nullable.HasValue)
            {
                Info info = way1.info;
                nullable = way.UserId;
                int num = (int)nullable.Value;
                info.uid = num;
            }
            way1.info.user_sid = Encoder.EncodeString(block, reverseStringTable, way.UserName);
            way1.info.version  = 0;
            if (way.Version.HasValue)
            {
                way1.info.version = (int)way.Version.Value;
            }
            if (way.Tags != null)
            {
                foreach (Tag tag in way.Tags)
                {
                    way1.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key));
                    way1.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value));
                }
            }
            if (way.Nodes != null && way.Nodes.Count > 0)
            {
                way1.refs.Add(way.Nodes[0]);
                for (int index = 1; index < way.Nodes.Count; ++index)
                {
                    way1.refs.Add(way.Nodes[index] - way.Nodes[index - 1]);
                }
            }
            return(way1);
        }
Beispiel #13
0
 public static void DecodeWay(PrimitiveBlock block, Way pbfWay, OsmSharp.Osm.Way way)
 {
     if (way.Nodes != null && way.Nodes.Count > 0)
     {
         way.Nodes.Clear();
     }
     if (way.Tags != null)
     {
         way.Tags.Clear();
     }
     if (way.Nodes == null)
     {
         way.Nodes = new List <long>(pbfWay.refs.Count);
     }
     if (way.Tags == null)
     {
         way.Tags = (TagsCollectionBase) new TagsCollection(pbfWay.keys.Count);
     }
     way.Id = new long?(pbfWay.id);
     if (pbfWay.refs.Count > 0)
     {
         long num = 0;
         for (int index = 0; index < pbfWay.refs.Count; ++index)
         {
             num += pbfWay.refs[index];
             way.Nodes.Add(num);
         }
     }
     if (pbfWay.keys.Count > 0)
     {
         for (int index = 0; index < pbfWay.keys.Count; ++index)
         {
             string key = Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.keys[index]]);
             string str = Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.vals[index]]);
             way.Tags.Add(new Tag(key, str));
         }
     }
     if (pbfWay.info != null)
     {
         way.ChangeSetId = new long?(pbfWay.info.changeset);
         way.TimeStamp   = new DateTime?(Encoder.DecodeTimestamp(pbfWay.info.timestamp, (long)block.date_granularity));
         way.UserId      = new long?((long)pbfWay.info.uid);
         way.UserName    = (string)null;
         if (block.stringtable != null)
         {
             way.UserName = Encoding.UTF8.GetString(block.stringtable.s[pbfWay.info.user_sid]);
         }
         way.Version = new ulong?((ulong)pbfWay.info.version);
     }
     way.Visible = new bool?(true);
 }
Beispiel #14
0
        /// <summary>
        /// Encodes an OsmSharp-way into a PBF-way.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.PBF.Way EncodeWay(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, Osm.Way way)
        {
            var pbfWay = new OsmSharp.Osm.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.Count > 0)
            {
                pbfWay.refs.Add(way.Nodes[0]);
                for (var i = 1; i < way.Nodes.Count; i++)
                {
                    pbfWay.refs.Add(way.Nodes[i] - way.Nodes[i - 1]);
                }
            }
            return(pbfWay);
        }
Beispiel #15
0
        public PrimitiveBlock MoveNext()
        {
            if (this._block.primitivegroup != null)
            {
                this._block.primitivegroup.Clear();
            }
            if (this._block.stringtable != null)
            {
                this._block.stringtable.s.Clear();
            }
            PrimitiveBlock primitiveBlock = (PrimitiveBlock)null;
            bool           flag           = true;

            while (flag)
            {
                flag = false;
                int num;
                if (Serializer.TryReadLengthPrefix(this._stream, (PrefixStyle)3, out num))
                {
                    BlobHeader blobHeader;
                    using (LimitedStream limitedStream = new LimitedStream(this._stream, (long)num))
                        blobHeader = ((TypeModel)this._runtimeTypeModel).Deserialize((Stream)limitedStream, (object)null, this._blockHeaderType) as BlobHeader;
                    Blob blob;
                    using (LimitedStream limitedStream = new LimitedStream(this._stream, (long)blobHeader.datasize))
                        blob = ((TypeModel)this._runtimeTypeModel).Deserialize((Stream)limitedStream, (object)null, this._blobType) as Blob;
                    Stream stream = blob.zlib_data != null ? (Stream) new ZLibStreamWrapper((Stream) new MemoryStream(blob.zlib_data)) : (Stream) new MemoryStream(blob.raw);
                    using (stream)
                    {
                        if (blobHeader.type == Encoder.OSMHeader)
                        {
                            ((TypeModel)this._runtimeTypeModel).Deserialize(stream, (object)null, this._headerBlockType);
                            flag = true;
                        }
                        if (blobHeader.type == Encoder.OSMData)
                        {
                            primitiveBlock = ((TypeModel)this._runtimeTypeModel).Deserialize(stream, (object)this._block, this._primitiveBlockType) as PrimitiveBlock;
                        }
                    }
                }
            }
            return(primitiveBlock);
        }
Beispiel #16
0
        /// <summary>
        /// Converts the PBF node into an OsmSharp-node.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.Node DecodeNode(PrimitiveBlock block, OsmSharp.Osm.PBF.Node pbfNode, OsmSharp.Osm.Node node)
        {
            // clear old data.
            if (node.Tags != null)
            { // clear tags.
                node.Tags.Clear();
            }
            if (node.Tags == null)
            { // create tags collection.
                node.Tags = new TagsCollection();
            }

            // set new stuff.
            node.ChangeSetId = pbfNode.info.changeset;
            node.Id          = pbfNode.id;
            node.Latitude    = Encoder.DecodeLatLon(pbfNode.lat, block.lat_offset, block.granularity);
            node.Longitude   = Encoder.DecodeLatLon(pbfNode.lon, block.lon_offset, block.granularity);
            for (var i = 0; i < pbfNode.keys.Count; i++)
            {
                node.Tags.Add(new Tag()
                {
                    Key   = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.keys[i]]),
                    Value = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.vals[i]])
                });
            }
            if (pbfNode.info != null)
            { // add the metadata if any.
                node.TimeStamp = Encoder.DecodeTimestamp(pbfNode.info.timestamp, block.date_granularity);
                node.Visible   = true;
                node.Version   = (uint)pbfNode.info.version;
                node.UserId    = pbfNode.info.uid;
                node.UserName  = null;
                if (block.stringtable != null)
                {
                    node.UserName = System.Text.Encoding.UTF8.GetString(block.stringtable.s[pbfNode.info.user_sid]);
                }
                node.Version = (ulong)pbfNode.info.version;
            }
            node.Visible = true;

            return(node);
        }
Beispiel #17
0
        /// <summary>
        /// Reads PFB OSM data from a stream.
        /// </summary>
        /// <param name="consumer">The consumer to send the data to.</param>
        public void ReadAll(IPBFPrimitiveBlockConsumer consumer)
        {
            // check parameters.
            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }

            // start processing.
            PrimitiveBlock block = this.MoveNext();

            while (block != null)
            {
                // report the next block to the consumer.
                consumer.ProcessPrimitiveBlock(block);

                // move to the next block.
                block = this.MoveNext();
            }
        }
Beispiel #18
0
 public static OsmSharp.Osm.Node DecodeNode(PrimitiveBlock block, Node pbfNode, OsmSharp.Osm.Node node)
 {
     if (node.Tags != null)
     {
         node.Tags.Clear();
     }
     if (node.Tags == null)
     {
         node.Tags = (TagsCollectionBase) new TagsCollection();
     }
     node.ChangeSetId = new long?(pbfNode.info.changeset);
     node.Id          = new long?(pbfNode.id);
     node.Latitude    = new double?(Encoder.DecodeLatLon(pbfNode.lat, block.lat_offset, (long)block.granularity));
     node.Longitude   = new double?(Encoder.DecodeLatLon(pbfNode.lon, block.lon_offset, (long)block.granularity));
     for (int index = 0; index < pbfNode.keys.Count; ++index)
     {
         node.Tags.Add(new Tag()
         {
             Key   = Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.keys[index]]),
             Value = Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.vals[index]])
         });
     }
     if (pbfNode.info != null)
     {
         node.TimeStamp = new DateTime?(Encoder.DecodeTimestamp(pbfNode.info.timestamp, (long)block.date_granularity));
         node.Visible   = new bool?(true);
         node.Version   = new ulong?((ulong)(uint)pbfNode.info.version);
         node.UserId    = new long?((long)pbfNode.info.uid);
         node.UserName  = (string)null;
         if (block.stringtable != null)
         {
             node.UserName = Encoding.UTF8.GetString(block.stringtable.s[pbfNode.info.user_sid]);
         }
         node.Version = new ulong?((ulong)pbfNode.info.version);
     }
     node.Visible = new bool?(true);
     return(node);
 }
Beispiel #19
0
        /// <summary>
        /// Encodes an OsmSharp-relation into a PBF-relation.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.PBF.Relation EncodeRelation(PrimitiveBlock block, Dictionary<string, int> reverseStringTable, Osm.Relation relation)
        {
            var pbfRelation = new OsmSharp.Osm.PBF.Relation();
            pbfRelation.id = relation.Id.Value;
            pbfRelation.info = new Info();
            if (relation.ChangeSetId.HasValue) { pbfRelation.info.changeset = relation.ChangeSetId.Value; }
            if (relation.TimeStamp.HasValue) { pbfRelation.info.timestamp = Encoder.EncodeTimestamp(relation.TimeStamp.Value, block.date_granularity); }
            if (relation.UserId.HasValue) { pbfRelation.info.uid = (int)relation.UserId.Value; }
            pbfRelation.info.user_sid = Encoder.EncodeString(block, reverseStringTable, relation.UserName);
            pbfRelation.info.version = 0;
            if (relation.Version.HasValue) { pbfRelation.info.version = (int)relation.Version.Value; }

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

            if (relation.Members != null &&
                relation.Members.Count > 0)
            {
                pbfRelation.memids.Add(relation.Members[0].MemberId.Value);
                pbfRelation.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[0].MemberRole));
                switch (relation.Members[0].MemberType.Value)
                {
                    case OsmGeoType.Node:
                        pbfRelation.types.Add(Relation.MemberType.NODE);
                        break;
                    case OsmGeoType.Way:
                        pbfRelation.types.Add(Relation.MemberType.WAY);
                        break;
                    case OsmGeoType.Relation:
                        pbfRelation.types.Add(Relation.MemberType.RELATION);
                        break;
                }
                for (var i = 1; i < relation.Members.Count; i++)
                {
                    pbfRelation.memids.Add(relation.Members[i].MemberId.Value -
                        relation.Members[i - 1].MemberId.Value);
                    pbfRelation.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[i].MemberRole));
                    switch(relation.Members[i].MemberType.Value)
                    {
                        case OsmGeoType.Node:
                            pbfRelation.types.Add(Relation.MemberType.NODE);
                            break;
                        case OsmGeoType.Way:
                            pbfRelation.types.Add(Relation.MemberType.WAY);
                            break;
                        case OsmGeoType.Relation:
                            pbfRelation.types.Add(Relation.MemberType.RELATION);
                            break;
                    }
                }
            }
            return pbfRelation;
        }
Beispiel #20
0
        /// <summary>
        /// Encodes an OsmSharp-way into a PBF-way.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.PBF.Way EncodeWay(PrimitiveBlock block, Dictionary<string, int> reverseStringTable, Osm.Way way)
        {
            var pbfWay = new OsmSharp.Osm.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.Count > 0)
            {
                pbfWay.refs.Add(way.Nodes[0]);
                for(var i = 1; i < way.Nodes.Count; i++)
                {
                    pbfWay.refs.Add(way.Nodes[i] - way.Nodes[i - 1]);
                }
            }
            return pbfWay;
        }
Beispiel #21
0
        /// <summary>
        /// Converts a PBF-way into an OsmSharp-way.
        /// </summary>
        public static void DecodeWay(PrimitiveBlock block, OsmSharp.Osm.PBF.Way pbfWay, OsmSharp.Osm.Way way)
        {
            // make sure old data is gone.
            if (way.Nodes != null &&
                way.Nodes.Count > 0)
            { // clear nodes list.
                way.Nodes.Clear();
            }
            if(way.Tags != null)
            { // clear the tags collection.
                way.Tags.Clear();
            }
            if (way.Nodes == null)
            { // create nodes list.
                way.Nodes = new List<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.Add(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 = (ulong)pbfWay.info.version;
            }
            way.Visible = true;
        }
Beispiel #22
0
 /// <summary>
 /// Encodes an OsmSharp-node into a PBF-node.
 /// </summary>
 /// <returns></returns>
 public static OsmSharp.Osm.PBF.Node EncodeNode(PrimitiveBlock block, Dictionary<string, int> reverseStringTable, Osm.Node node)
 {
     var pbfNode = new OsmSharp.Osm.PBF.Node();
     Encoder.EncodeNode(block, reverseStringTable, pbfNode, node);
     return pbfNode;
 }
Beispiel #23
0
        /// <summary>
        /// Converts the PBF node into an OsmSharp-node.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.Node DecodeNode(PrimitiveBlock block, OsmSharp.Osm.PBF.Node pbfNode, OsmSharp.Osm.Node node)
        {
            // clear old data.
            if (node.Tags != null)
            { // clear tags.
                node.Tags.Clear();
            }
            if (node.Tags == null)
            { // create tags collection.
                node.Tags = new TagsCollection();
            }

            // set new stuff.
            node.ChangeSetId = pbfNode.info.changeset;
            node.Id = pbfNode.id;
            node.Latitude = Encoder.DecodeLatLon(pbfNode.lat, block.lat_offset, block.granularity);
            node.Longitude = Encoder.DecodeLatLon(pbfNode.lon, block.lon_offset, block.granularity);
            for (var i = 0; i < pbfNode.keys.Count; i++)
            {
                node.Tags.Add(new Tag()
                {
                    Key = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.keys[i]]),
                    Value = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.vals[i]])
                });
            }
            if (pbfNode.info != null)
            { // add the metadata if any.
                node.TimeStamp = Encoder.DecodeTimestamp(pbfNode.info.timestamp, block.date_granularity);
                node.Visible = true;
                node.Version = (uint)pbfNode.info.version;
                node.UserId = pbfNode.info.uid;
                node.UserName = null;
                if(block.stringtable != null)
                {
                    node.UserName = System.Text.Encoding.UTF8.GetString(block.stringtable.s[pbfNode.info.user_sid]);
                }
                node.Version = (ulong)pbfNode.info.version;
            }
            node.Visible = true;

            return node;
        }
Beispiel #24
0
        /// <summary>
        /// Converts a PBF way into an OsmSharp-relation.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.Relation DecodeRelation(PrimitiveBlock block, OsmSharp.Osm.PBF.Relation pbfRelation,
            OsmSharp.Osm.Relation relation)
        {
            // make sure old data is gone.
            if (relation.Members != null &&
                relation.Members.Count > 0)
            { // clear nodes list.
                relation.Members.Clear();
            }
            if (relation.Tags != null)
            { // clear the tags collection.
                relation.Tags.Clear();
            }
            if (relation.Members == null)
            { // create nodes list.
                relation.Members = new List<OsmSharp.Osm.RelationMember>(pbfRelation.memids.Count);
            }
            if (relation.Tags == null)
            { // create tags collection.
                relation.Tags = new TagsCollection(pbfRelation.keys.Count);
            }

            // add nex stuff.
            relation.Id = pbfRelation.id;
            long memberId = 0;
            for (var i = 0; i < pbfRelation.types.Count; i++)
            {
                memberId = memberId + pbfRelation.memids[i];
                var role = System.Text.Encoding.UTF8.GetString(
                    block.stringtable.s[pbfRelation.roles_sid[i]]);

                var member = new OsmSharp.Osm.RelationMember();
                member.MemberId = memberId;
                member.MemberRole = role;
                switch (pbfRelation.types[i])
                {
                    case Relation.MemberType.NODE:
                        member.MemberType = OsmSharp.Osm.OsmGeoType.Node;
                        break;
                    case Relation.MemberType.WAY:
                        member.MemberType = OsmSharp.Osm.OsmGeoType.Way;
                        break;
                    case Relation.MemberType.RELATION:
                        member.MemberType = OsmSharp.Osm.OsmGeoType.Relation;
                        break;
                }

                relation.Members.Add(member);
            }
            for (int i = 0; i < pbfRelation.keys.Count; i++)
            {
                string key = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.keys[i]]);
                string value = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.vals[i]]);

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

            return relation;
        }
Beispiel #25
0
        public static bool Decode(this PrimitiveBlock block, IPBFOsmPrimitiveConsumer primitivesConsumer, bool ignoreNodes, bool ignoreWays, bool ignoreRelations)
        {
            bool flag = false;

            if (block.primitivegroup != null)
            {
                foreach (PrimitiveGroup primitiveGroup in block.primitivegroup)
                {
                    if (!ignoreNodes && primitiveGroup.dense != null)
                    {
                        int  index1 = 0;
                        long num1   = 0;
                        long num2   = 0;
                        long num3   = 0;
                        long num4   = 0;
                        long num5   = 0;
                        int  num6   = 0;
                        int  num7   = 0;
                        int  num8   = 0;
                        for (int index2 = 0; index2 < primitiveGroup.dense.id.Count; ++index2)
                        {
                            num1 += primitiveGroup.dense.id[index2];
                            num2 += primitiveGroup.dense.lat[index2];
                            num3 += primitiveGroup.dense.lon[index2];
                            if (primitiveGroup.dense.denseinfo != null)
                            {
                                num4 += primitiveGroup.dense.denseinfo.changeset[index2];
                                num5 += primitiveGroup.dense.denseinfo.timestamp[index2];
                                num6 += primitiveGroup.dense.denseinfo.uid[index2];
                                num7 += primitiveGroup.dense.denseinfo.user_sid[index2];
                                num8 += primitiveGroup.dense.denseinfo.version[index2];
                            }
                            Node node = new Node();
                            node.id             = num1;
                            node.info           = new Info();
                            node.info.changeset = num4;
                            node.info.timestamp = (int)num5;
                            node.info.uid       = num6;
                            node.info.user_sid  = num7;
                            node.info.version   = num8;
                            node.lat            = num2;
                            node.lon            = num3;
                            int index3;
                            for (List <int> keysVals = primitiveGroup.dense.keys_vals; keysVals.Count > index1 && keysVals[index1] != 0; index1 = index3 + 1)
                            {
                                node.keys.Add((uint)keysVals[index1]);
                                index3 = index1 + 1;
                                node.vals.Add((uint)keysVals[index3]);
                            }
                            ++index1;
                            flag = true;
                            primitivesConsumer.ProcessNode(block, node);
                        }
                    }
                    else
                    {
                        if (!ignoreNodes && primitiveGroup.nodes != null)
                        {
                            foreach (Node node in primitiveGroup.nodes)
                            {
                                flag = true;
                                primitivesConsumer.ProcessNode(block, node);
                            }
                        }
                        if (!ignoreWays && primitiveGroup.ways != null)
                        {
                            foreach (Way way in primitiveGroup.ways)
                            {
                                flag = true;
                                primitivesConsumer.ProcessWay(block, way);
                            }
                        }
                        if (!ignoreRelations && primitiveGroup.relations != null)
                        {
                            foreach (Relation relation in primitiveGroup.relations)
                            {
                                flag = true;
                                primitivesConsumer.ProcessRelation(block, relation);
                            }
                        }
                    }
                }
            }
            return(flag);
        }
 public void ProcessRelation(PrimitiveBlock block, Relation relation)
 {
     this.Relations.Add(relation);
 }
Beispiel #27
0
        public static void Encode(this PrimitiveBlock block, Dictionary <string, int> reverseStringTable, List <OsmGeo> osmGeos)
        {
            int index1 = 0;
            int index2 = 0;
            int index3 = 0;

            if (block.stringtable != null && block.stringtable.s != null)
            {
                block.stringtable.s.Clear();
            }
            while (index2 < osmGeos.Count)
            {
                PrimitiveGroup primitiveGroup;
                if (index1 < block.primitivegroup.Count)
                {
                    primitiveGroup = block.primitivegroup[index1] ?? new PrimitiveGroup();
                    if (primitiveGroup.dense != null)
                    {
                        if (primitiveGroup.dense.denseinfo != null)
                        {
                            if (primitiveGroup.dense.denseinfo.changeset != null)
                            {
                                primitiveGroup.dense.denseinfo.changeset.Clear();
                            }
                            if (primitiveGroup.dense.denseinfo.timestamp != null)
                            {
                                primitiveGroup.dense.denseinfo.timestamp.Clear();
                            }
                            if (primitiveGroup.dense.denseinfo.uid != null)
                            {
                                primitiveGroup.dense.denseinfo.uid.Clear();
                            }
                            if (primitiveGroup.dense.denseinfo.user_sid != null)
                            {
                                primitiveGroup.dense.denseinfo.user_sid.Clear();
                            }
                            if (primitiveGroup.dense.denseinfo.version != null)
                            {
                                primitiveGroup.dense.denseinfo.version.Clear();
                            }
                        }
                        if (primitiveGroup.dense.id != null)
                        {
                            primitiveGroup.dense.id.Clear();
                        }
                        if (primitiveGroup.dense.keys_vals != null)
                        {
                            primitiveGroup.dense.keys_vals.Clear();
                        }
                        if (primitiveGroup.dense.lat != null)
                        {
                            primitiveGroup.dense.lat.Clear();
                        }
                        if (primitiveGroup.dense.lon != null)
                        {
                            primitiveGroup.dense.lon.Clear();
                        }
                    }
                    if (primitiveGroup.changesets != null)
                    {
                        primitiveGroup.changesets.Clear();
                    }
                    if (primitiveGroup.ways != null)
                    {
                        primitiveGroup.ways.Clear();
                    }
                    if (primitiveGroup.relations != null)
                    {
                        primitiveGroup.relations.Clear();
                    }
                }
                else
                {
                    primitiveGroup = new PrimitiveGroup();
                    block.primitivegroup.Add(primitiveGroup);
                }
                OsmGeoType type   = osmGeos[index2].Type;
                OsmGeo     osmGeo = osmGeos[index2];
                for (; index2 < osmGeos.Count && osmGeos[index2].Type == type; ++index2)
                {
                    switch (type)
                    {
                    case OsmGeoType.Node:
                        if (primitiveGroup.nodes.Count > index3)
                        {
                            Encoder.EncodeNode(block, reverseStringTable, primitiveGroup.nodes[index3], osmGeos[index2] as OsmSharp.Osm.Node);
                        }
                        else
                        {
                            primitiveGroup.nodes.Add(Encoder.EncodeNode(block, reverseStringTable, osmGeos[index2] as OsmSharp.Osm.Node));
                        }
                        ++index3;
                        break;

                    case OsmGeoType.Way:
                        primitiveGroup.ways.Add(Encoder.EncodeWay(block, reverseStringTable, osmGeos[index2] as OsmSharp.Osm.Way));
                        break;

                    case OsmGeoType.Relation:
                        primitiveGroup.relations.Add(Encoder.EncodeRelation(block, reverseStringTable, osmGeos[index2] as OsmSharp.Osm.Relation));
                        break;
                    }
                }
                if (primitiveGroup.nodes != null)
                {
                    while (index3 < primitiveGroup.nodes.Count)
                    {
                        primitiveGroup.nodes.RemoveAt(index3);
                    }
                }
                ++index1;
            }
            while (index1 < block.primitivegroup.Count)
            {
                block.primitivegroup.RemoveAt(index1);
            }
        }
Beispiel #28
0
        public static Relation EncodeRelation(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, OsmSharp.Osm.Relation relation)
        {
            Relation relation1 = new Relation();

            relation1.id   = relation.Id.Value;
            relation1.info = new Info();
            long?nullable;

            if (relation.ChangeSetId.HasValue)
            {
                Info info = relation1.info;
                nullable = relation.ChangeSetId;
                long num = nullable.Value;
                info.changeset = num;
            }
            if (relation.TimeStamp.HasValue)
            {
                relation1.info.timestamp = Encoder.EncodeTimestamp(relation.TimeStamp.Value, (long)block.date_granularity);
            }
            nullable = relation.UserId;
            if (nullable.HasValue)
            {
                Info info = relation1.info;
                nullable = relation.UserId;
                int num = (int)nullable.Value;
                info.uid = num;
            }
            relation1.info.user_sid = Encoder.EncodeString(block, reverseStringTable, relation.UserName);
            relation1.info.version  = 0;
            if (relation.Version.HasValue)
            {
                relation1.info.version = (int)relation.Version.Value;
            }
            if (relation.Tags != null)
            {
                foreach (Tag tag in relation.Tags)
                {
                    relation1.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key));
                    relation1.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value));
                }
            }
            if (relation.Members != null && relation.Members.Count > 0)
            {
                List <long> memids1 = relation1.memids;
                nullable = relation.Members[0].MemberId;
                long num1 = nullable.Value;
                memids1.Add(num1);
                relation1.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[0].MemberRole));
                OsmGeoType?memberType = relation.Members[0].MemberType;
                switch (memberType.Value)
                {
                case OsmGeoType.Node:
                    relation1.types.Add(Relation.MemberType.NODE);
                    break;

                case OsmGeoType.Way:
                    relation1.types.Add(Relation.MemberType.WAY);
                    break;

                case OsmGeoType.Relation:
                    relation1.types.Add(Relation.MemberType.RELATION);
                    break;
                }
                for (int index = 1; index < relation.Members.Count; ++index)
                {
                    List <long> memids2 = relation1.memids;
                    nullable = relation.Members[index].MemberId;
                    long num2 = nullable.Value;
                    nullable = relation.Members[index - 1].MemberId;
                    long num3 = nullable.Value;
                    long num4 = num2 - num3;
                    memids2.Add(num4);
                    relation1.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[index].MemberRole));
                    memberType = relation.Members[index].MemberType;
                    switch (memberType.Value)
                    {
                    case OsmGeoType.Node:
                        relation1.types.Add(Relation.MemberType.NODE);
                        break;

                    case OsmGeoType.Way:
                        relation1.types.Add(Relation.MemberType.WAY);
                        break;

                    case OsmGeoType.Relation:
                        relation1.types.Add(Relation.MemberType.RELATION);
                        break;
                    }
                }
            }
            return(relation1);
        }
        public void TestEncodeNode()
        {
            var block = new PrimitiveBlock();
            block.date_granularity = 1000;
            block.granularity = 100;
            block.lat_offset = 0;
            block.lon_offset = 0;

            var node = new OsmSharp.Osm.Node();
            node.Id = 1;
            node.ChangeSetId = 1;
            node.Latitude = 10;
            node.Longitude = 11;
            node.Tags = new OsmSharp.Collections.Tags.TagsCollection();
            node.Tags.Add("name", "Ben");
            node.TimeStamp = DateTime.Now;
            node.UserId = 1;
            node.UserName = "******";
            node.Version = 1;
            node.Visible = true;

            var pbfNode = Encoder.EncodeNode(block, new Dictionary<string,int>(), node);
            Assert.IsNotNull(pbfNode);
            Assert.AreEqual(1, pbfNode.id);
            Assert.AreEqual(Encoder.EncodeLatLon(10, block.lat_offset, block.granularity), pbfNode.lat);
            Assert.AreEqual(Encoder.EncodeLatLon(11, block.lon_offset, block.granularity), pbfNode.lon);
            Assert.AreEqual(1, pbfNode.info.changeset);
            Assert.AreEqual(Encoder.EncodeTimestamp(node.TimeStamp.Value, block.date_granularity), pbfNode.info.timestamp);
            Assert.AreEqual(1, pbfNode.info.uid);
            Assert.AreEqual("Ben", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.info.user_sid]));
            Assert.AreEqual(1, pbfNode.info.version);
            Assert.AreEqual(1, pbfNode.keys.Count);
            Assert.AreEqual("name", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.keys[0]]));
            Assert.AreEqual(1, pbfNode.vals.Count);
            Assert.AreEqual("Ben", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfNode.vals[0]]));
        }
Beispiel #30
0
        /// <summary>
        /// Encodes the nodes into the given block.
        /// </summary>
        public static void Encode(this PrimitiveBlock block, Dictionary <string, int> reverseStringTable, List <OsmGeo> osmGeos)
        {
            var groupIdx  = 0;
            var i         = 0;
            var nodeCount = 0;

            if (block.stringtable != null &&
                block.stringtable.s != null)
            {
                block.stringtable.s.Clear();
            }
            while (i < osmGeos.Count)
            {
                PrimitiveGroup group = null;
                if (groupIdx < block.primitivegroup.Count)
                { // get the existing group and clear it's data.
                    group = block.primitivegroup[groupIdx];
                    if (group == null)
                    {
                        group = new PrimitiveGroup();
                    }
                    if (group.dense != null)
                    {
                        if (group.dense.denseinfo != null)
                        {
                            if (group.dense.denseinfo.changeset != null)
                            {
                                group.dense.denseinfo.changeset.Clear();
                            }
                            if (group.dense.denseinfo.timestamp != null)
                            {
                                group.dense.denseinfo.timestamp.Clear();
                            }
                            if (group.dense.denseinfo.uid != null)
                            {
                                group.dense.denseinfo.uid.Clear();
                            }
                            if (group.dense.denseinfo.user_sid != null)
                            {
                                group.dense.denseinfo.user_sid.Clear();
                            }
                            if (group.dense.denseinfo.version != null)
                            {
                                group.dense.denseinfo.version.Clear();
                            }
                        }
                        if (group.dense.id != null)
                        {
                            group.dense.id.Clear();
                        }
                        if (group.dense.keys_vals != null)
                        {
                            group.dense.keys_vals.Clear();
                        }
                        if (group.dense.lat != null)
                        {
                            group.dense.lat.Clear();
                        }
                        if (group.dense.lon != null)
                        {
                            group.dense.lon.Clear();
                        }
                    }
                    if (group.changesets != null)
                    {
                        group.changesets.Clear();
                    }
                    //if (group.nodes != null) { group.nodes.Clear(); }
                    if (group.ways != null)
                    {
                        group.ways.Clear();
                    }
                    if (group.relations != null)
                    {
                        group.relations.Clear();
                    }
                }
                else
                { // add a new group.
                    group = new PrimitiveGroup();
                    block.primitivegroup.Add(group);
                }

                // build group.
                var groupType = osmGeos[i].Type;
                var current   = osmGeos[i];
                while (i < osmGeos.Count &&
                       osmGeos[i].Type == groupType)
                {
                    switch (groupType)
                    {
                    case OsmGeoType.Node:
                        if (group.nodes.Count > nodeCount)
                        {     // overwrite existing.
                            Encoder.EncodeNode(block, reverseStringTable, group.nodes[nodeCount], osmGeos[i] as Osm.Node);
                        }
                        else
                        {     // write new.
                            group.nodes.Add(Encoder.EncodeNode(block, reverseStringTable, osmGeos[i] as Osm.Node));
                        }
                        nodeCount++;
                        break;

                    case OsmGeoType.Way:
                        group.ways.Add(Encoder.EncodeWay(block, reverseStringTable, osmGeos[i] as Osm.Way));
                        break;

                    case OsmGeoType.Relation:
                        group.relations.Add(Encoder.EncodeRelation(block, reverseStringTable, osmGeos[i] as Osm.Relation));
                        break;
                    }
                    i++;
                }


                // remove obsolete nodes.
                if (group.nodes != null)
                {
                    while (nodeCount < group.nodes.Count)
                    {
                        group.nodes.RemoveAt(nodeCount);
                    }
                }

                // move to the next group.
                groupIdx++;
            }

            while (groupIdx < block.primitivegroup.Count)
            {
                block.primitivegroup.RemoveAt(groupIdx);
            }
        }
        public void TestEncodeRelation()
        {
            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 relation = new OsmSharp.Osm.Relation();
            relation.Id = 1;
            relation.ChangeSetId = 1;
            relation.Tags = new OsmSharp.Collections.Tags.TagsCollection();
            relation.Tags.Add("name", "Ben");
            relation.TimeStamp = DateTime.Now;
            relation.UserId = 1;
            relation.UserName = "******";
            relation.Version = 1;
            relation.Visible = true;
            relation.Members = new List<OsmSharp.Osm.RelationMember>();
            relation.Members.Add(new OsmSharp.Osm.RelationMember()
                {
                    MemberId = 1,
                    MemberRole = "fake role1",
                    MemberType = OsmSharp.Osm.OsmGeoType.Node
                });
            relation.Members.Add(new OsmSharp.Osm.RelationMember()
                {
                    MemberId = 2,
                    MemberRole = "fake role2",
                    MemberType = OsmSharp.Osm.OsmGeoType.Relation
                });

            var pbfRelation = Encoder.EncodeRelation(block, new Dictionary<string, int>(), relation);
            Assert.IsNotNull(pbfRelation);
            Assert.AreEqual(1, pbfRelation.id);
            Assert.AreEqual(2, pbfRelation.memids.Count);
            Assert.AreEqual(1, pbfRelation.memids[0]);
            Assert.AreEqual(1, pbfRelation.memids[1]);
            Assert.AreEqual(2, pbfRelation.roles_sid.Count);
            Assert.AreEqual("fake role1", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.roles_sid[0]]));
            Assert.AreEqual("fake role2", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.roles_sid[1]]));
            Assert.AreEqual(2, pbfRelation.types.Count);
            Assert.AreEqual(Relation.MemberType.NODE, pbfRelation.types[0]);
            Assert.AreEqual(Relation.MemberType.RELATION, pbfRelation.types[1]);
            Assert.AreEqual(1, pbfRelation.info.changeset);
            Assert.AreEqual(Encoder.EncodeTimestamp(relation.TimeStamp.Value, block.date_granularity), pbfRelation.info.timestamp);
            Assert.AreEqual(1, pbfRelation.info.uid);
            Assert.AreEqual("Ben", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.info.user_sid]));
            Assert.AreEqual(1, pbfRelation.info.version);
            Assert.AreEqual(1, pbfRelation.keys.Count);
            Assert.AreEqual("name", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.keys[0]]));
            Assert.AreEqual(1, pbfRelation.vals.Count);
            Assert.AreEqual("Ben", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.vals[0]]));
        }
Beispiel #32
0
        /// <summary>
        /// Moves to the next primitive block, returns null at the end.
        /// </summary>
        /// <returns></returns>
        public PrimitiveBlock MoveNext()
        {
            PrimitiveBlock block = null;
            int            length;
            bool           not_found_but = true;

            while (not_found_but)
            {                          // continue if there is still data but not a primitiveblock.
                not_found_but = false; // not found.
                if (Serializer.TryReadLengthPrefix(_stream, PrefixStyle.Fixed32, out length))
                {
                    // TODO: remove some of the v1 specific code.
                    // TODO: this means also to use the built-in capped streams.

                    // code borrowed from: http://stackoverflow.com/questions/4663298/protobuf-net-deserialize-open-street-maps

                    // I'm just being lazy and re-using something "close enough" here
                    // note that v2 has a big-endian option, but Fixed32 assumes little-endian - we
                    // actually need the other way around (network byte order):
                    length = IntLittleEndianToBigEndian((uint)length);

                    BlockHeader header;
                    // again, v2 has capped-streams built in, but I'm deliberately
                    // limiting myself to v1 features
                    using (var tmp = new LimitedStream(_stream, length))
                    {
                        header = Serializer.Deserialize <BlockHeader>(tmp);
                    }
                    Blob blob;
                    using (var tmp = new LimitedStream(_stream, header.datasize))
                    {
                        blob = Serializer.Deserialize <Blob>(tmp);
                    }

                    // construct the source stream, compressed or not.
                    Stream source_stream = null;
                    if (blob.zlib_data == null)
                    { // use a regular uncompressed stream.
                        source_stream = new MemoryStream(blob.raw);
                    }
                    else
                    { // construct a compressed stream.
                        var ms = new MemoryStream(blob.zlib_data);
                        source_stream = new ZLibStreamWrapper(ms);
                    }

                    // use the stream to read the block.
                    using (source_stream)
                    {
                        if (header.type == "OSMHeader")
                        {
                            Serializer.Deserialize <HeaderBlock>(source_stream);
                            not_found_but = true;
                        }

                        if (header.type == "OSMData")
                        {
                            block = Serializer.Deserialize <PrimitiveBlock>(source_stream);
                        }
                    }
                }
            }
            return(block);
        }
        public void TestDecodeBlockWithDenseNodes()
        {
            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")); // 1
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential")); // 2
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben")); // 3
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("track")); // 4
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("trunk")); // 5

            var primitiveGroup = new PrimitiveGroup();
            primitiveGroup.dense = new DenseNodes();
            primitiveGroup.dense.denseinfo = new DenseInfo();
            primitiveGroup.dense.denseinfo.changeset.Add(10);
            primitiveGroup.dense.denseinfo.changeset.Add(1);
            primitiveGroup.dense.denseinfo.changeset.Add(1);

            primitiveGroup.dense.denseinfo.timestamp.Add(10);
            primitiveGroup.dense.denseinfo.timestamp.Add(1);
            primitiveGroup.dense.denseinfo.timestamp.Add(1);

            primitiveGroup.dense.denseinfo.uid.Add(1);
            primitiveGroup.dense.denseinfo.uid.Add(0);
            primitiveGroup.dense.denseinfo.uid.Add(0);

            primitiveGroup.dense.denseinfo.user_sid.Add(3);
            primitiveGroup.dense.denseinfo.user_sid.Add(0);
            primitiveGroup.dense.denseinfo.user_sid.Add(0);

            primitiveGroup.dense.denseinfo.version.Add(1);
            primitiveGroup.dense.denseinfo.version.Add(1);
            primitiveGroup.dense.denseinfo.version.Add(1);

            primitiveGroup.dense.id.Add(1);
            primitiveGroup.dense.id.Add(1);
            primitiveGroup.dense.id.Add(1);

            primitiveGroup.dense.keys_vals.Add(1);
            primitiveGroup.dense.keys_vals.Add(2);
            primitiveGroup.dense.keys_vals.Add(0); // highway=residential.
            primitiveGroup.dense.keys_vals.Add(1);
            primitiveGroup.dense.keys_vals.Add(4);
            primitiveGroup.dense.keys_vals.Add(0); // highway=track.
            primitiveGroup.dense.keys_vals.Add(0); // empty.

            primitiveGroup.dense.lat.Add(Encoder.EncodeLatLon(10.0, block.lat_offset, block.granularity));
            primitiveGroup.dense.lat.Add(Encoder.EncodeLatLon(11.0, block.lat_offset, block.granularity)
                - primitiveGroup.dense.lat[primitiveGroup.dense.lat.Count - 1]);
            primitiveGroup.dense.lat.Add(Encoder.EncodeLatLon(12.0, block.lat_offset, block.granularity)
                - primitiveGroup.dense.lat[primitiveGroup.dense.lat.Count - 1]);

            primitiveGroup.dense.lon.Add(Encoder.EncodeLatLon(100.0, block.lon_offset, block.granularity));
            primitiveGroup.dense.lon.Add(Encoder.EncodeLatLon(110.0, block.lon_offset, block.granularity)
                - primitiveGroup.dense.lon[primitiveGroup.dense.lon.Count - 1]);
            primitiveGroup.dense.lon.Add(Encoder.EncodeLatLon(120.0, block.lon_offset, block.granularity)
                - primitiveGroup.dense.lon[primitiveGroup.dense.lon.Count - 1]);

            block.primitivegroup.Add(primitiveGroup);

            var primitivesConsumer = new PrimitivesConsumerMock();
            block.Decode(primitivesConsumer, false, false, false);

            Assert.AreEqual(3, primitivesConsumer.Nodes.Count);
            Assert.AreEqual(0, primitivesConsumer.Ways.Count);
            Assert.AreEqual(0, primitivesConsumer.Relations.Count);

            var node = primitivesConsumer.Nodes[0];
            Assert.IsNotNull(node);
            Assert.AreEqual(1, node.id);
            Assert.AreEqual(10, node.info.changeset);
            Assert.AreEqual(10, node.info.timestamp);
            Assert.AreEqual(1, node.info.uid);
            Assert.AreEqual(3, node.info.user_sid);
            Assert.AreEqual(1, node.info.version);
            Assert.AreEqual(1, node.keys.Count);
            Assert.AreEqual(1, node.keys[0]);
            Assert.AreEqual(1, node.vals.Count);
            Assert.AreEqual(2, node.vals[0]);

            node = primitivesConsumer.Nodes[1];
            Assert.IsNotNull(node);
            Assert.AreEqual(2, node.id);
            Assert.AreEqual(11, node.info.changeset);
            Assert.AreEqual(11, node.info.timestamp);
            Assert.AreEqual(1, node.info.uid);
            Assert.AreEqual(3, node.info.user_sid);
            Assert.AreEqual(2, node.info.version);
            Assert.AreEqual(1, node.keys.Count);
            Assert.AreEqual(1, node.keys[0]);
            Assert.AreEqual(1, node.vals.Count);
            Assert.AreEqual(4, node.vals[0]);

            node = primitivesConsumer.Nodes[2];
            Assert.IsNotNull(node);
            Assert.AreEqual(3, node.id);
            Assert.AreEqual(12, node.info.changeset);
            Assert.AreEqual(12, node.info.timestamp);
            Assert.AreEqual(1, node.info.uid);
            Assert.AreEqual(3, node.info.user_sid);
            Assert.AreEqual(3, node.info.version);
            Assert.AreEqual(0, node.keys.Count);
            Assert.AreEqual(0, node.vals.Count);
        }
 public void TestEncodeStrings()
 {
     var block = new PrimitiveBlock();
     var reverseStringTable = new Dictionary<string,int>();
     Assert.AreEqual(1, Encoder.EncodeString(block, reverseStringTable, "Ben"));
     Assert.AreEqual(2, Encoder.EncodeString(block, reverseStringTable, "highway"));
     Assert.AreEqual(3, Encoder.EncodeString(block, reverseStringTable, "residential"));
     Assert.AreEqual(1, Encoder.EncodeString(block, reverseStringTable, "Ben"));
     Assert.AreEqual(2, Encoder.EncodeString(block, reverseStringTable, "highway"));
     Assert.AreEqual(4, Encoder.EncodeString(block, reverseStringTable, "Some other string"));
     Assert.AreEqual(5, Encoder.EncodeString(block, reverseStringTable, "Ban"));
 }
Beispiel #35
0
        /// <summary>
        /// Decodes the block.
        /// </summary>
        public static bool Decode(this PrimitiveBlock block, IPBFOsmPrimitiveConsumer primitivesConsumer,
                                  bool ignoreNodes, bool ignoreWays, bool ignoreRelations)
        {
            var success = false;

            if (block.primitivegroup != null)
            {
                foreach (var primitivegroup in block.primitivegroup)
                {
                    if (!ignoreNodes && primitivegroup.dense != null)
                    {
                        int  keyValsIdx       = 0;
                        long currentId        = 0;
                        long currentLat       = 0;
                        long currentLon       = 0;
                        long currentChangeset = 0;
                        long currentTimestamp = 0;
                        int  currentUid       = 0;
                        int  currentUserSid   = 0;
                        int  currentVersion   = 0;

                        for (int idx = 0; idx < primitivegroup.dense.id.Count; idx++)
                        {
                            // do the delta decoding stuff.
                            currentId = currentId +
                                        primitivegroup.dense.id[idx];
                            currentLat = currentLat +
                                         primitivegroup.dense.lat[idx];
                            currentLon = currentLon +
                                         primitivegroup.dense.lon[idx];
                            if (primitivegroup.dense.denseinfo != null)
                            { // add all the metadata.
                                currentChangeset = currentChangeset +
                                                   primitivegroup.dense.denseinfo.changeset[idx];
                                currentTimestamp = currentTimestamp +
                                                   primitivegroup.dense.denseinfo.timestamp[idx];
                                currentUid = currentUid +
                                             primitivegroup.dense.denseinfo.uid[idx];
                                currentUserSid = currentUserSid +
                                                 primitivegroup.dense.denseinfo.user_sid[idx];
                                currentVersion = currentVersion +
                                                 primitivegroup.dense.denseinfo.version[idx];
                            }

                            var node = new Node();
                            node.id             = currentId;
                            node.info           = new Info();
                            node.info.changeset = currentChangeset;
                            node.info.timestamp = (int)currentTimestamp;
                            node.info.uid       = currentUid;
                            node.info.user_sid  = currentUserSid;
                            node.info.version   = currentVersion;
                            node.lat            = currentLat;
                            node.lon            = currentLon;

                            // get the keys/vals.
                            var keyVals = primitivegroup.dense.keys_vals;
                            while (keyVals.Count > keyValsIdx &&
                                   keyVals[keyValsIdx] != 0)
                            {
                                node.keys.Add((uint)keyVals[keyValsIdx]);
                                keyValsIdx++;
                                node.vals.Add((uint)keyVals[keyValsIdx]);
                                keyValsIdx++;
                            }
                            keyValsIdx++;

                            success = true;
                            primitivesConsumer.ProcessNode(block, node);
                        }
                    }
                    else
                    {
                        if (!ignoreNodes && primitivegroup.nodes != null)
                        {
                            foreach (var node in primitivegroup.nodes)
                            {
                                success = true;
                                primitivesConsumer.ProcessNode(block, node);
                            }
                        }
                        if (!ignoreWays && primitivegroup.ways != null)
                        {
                            foreach (var way in primitivegroup.ways)
                            {
                                success = true;
                                primitivesConsumer.ProcessWay(block, way);
                            }
                        }
                        if (!ignoreRelations && primitivegroup.relations != null)
                        {
                            foreach (var relation in primitivegroup.relations)
                            {
                                success = true;
                                primitivesConsumer.ProcessRelation(block, relation);
                            }
                        }
                    }
                }
            }
            return(success);
        }
 public void ProcessWay(PrimitiveBlock block, Way way)
 {
     this.Ways.Add(way);
 }
Beispiel #37
0
        public static OsmSharp.Osm.Relation DecodeRelation(PrimitiveBlock block, Relation pbfRelation, OsmSharp.Osm.Relation relation)
        {
            if (relation.Members != null && relation.Members.Count > 0)
            {
                relation.Members.Clear();
            }
            if (relation.Tags != null)
            {
                relation.Tags.Clear();
            }
            if (relation.Members == null)
            {
                relation.Members = new List <RelationMember>(pbfRelation.memids.Count);
            }
            if (relation.Tags == null)
            {
                relation.Tags = (TagsCollectionBase) new TagsCollection(pbfRelation.keys.Count);
            }
            relation.Id = new long?(pbfRelation.id);
            long num = 0;

            for (int index = 0; index < pbfRelation.types.Count; ++index)
            {
                num += pbfRelation.memids[index];
                string         str            = Encoding.UTF8.GetString(block.stringtable.s[pbfRelation.roles_sid[index]]);
                RelationMember relationMember = new RelationMember();
                relationMember.MemberId   = new long?(num);
                relationMember.MemberRole = str;
                switch (pbfRelation.types[index])
                {
                case Relation.MemberType.NODE:
                    relationMember.MemberType = new OsmGeoType?(OsmGeoType.Node);
                    break;

                case Relation.MemberType.WAY:
                    relationMember.MemberType = new OsmGeoType?(OsmGeoType.Way);
                    break;

                case Relation.MemberType.RELATION:
                    relationMember.MemberType = new OsmGeoType?(OsmGeoType.Relation);
                    break;
                }
                relation.Members.Add(relationMember);
            }
            for (int index = 0; index < pbfRelation.keys.Count; ++index)
            {
                string key = Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.keys[index]]);
                string str = Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.vals[index]]);
                relation.Tags.Add(new Tag(key, str));
            }
            if (pbfRelation.info != null)
            {
                relation.ChangeSetId = new long?(pbfRelation.info.changeset);
                relation.TimeStamp   = new DateTime?(Encoder.DecodeTimestamp(pbfRelation.info.timestamp, (long)block.date_granularity));
                relation.UserId      = new long?((long)pbfRelation.info.uid);
                relation.UserName    = (string)null;
                if (block.stringtable != null)
                {
                    relation.UserName = Encoding.UTF8.GetString(block.stringtable.s[pbfRelation.info.user_sid]);
                }
                relation.Version = new ulong?((ulong)pbfRelation.info.version);
            }
            relation.Visible = new bool?(true);
            return(relation);
        }
Beispiel #38
0
 /// <summary>
 /// Converts the PBF node into an OsmSharp-node.
 /// </summary>
 /// <returns></returns>
 public static OsmSharp.Osm.Node DecodeNode(PrimitiveBlock block, OsmSharp.Osm.PBF.Node pbfNode)
 {
     var node = new OsmSharp.Osm.Node();
     Encoder.DecodeNode(block, pbfNode, node);
     return node;
 }
        public void TestDecodeWay()
        {
            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 pbfWay = new Way()
            {
                id = 1,
                info = new Info()
                {
                    changeset = 10,
                    timestamp = 10,
                    uid = 100,
                    user_sid = 3,
                    version = 2
                }
            };
            pbfWay.keys.Add(1);
            pbfWay.vals.Add(2);
            pbfWay.refs.Add(0);
            pbfWay.refs.Add(1);

            var way = Encoder.DecodeWay(block, pbfWay);
            Assert.IsNotNull(way);
            Assert.AreEqual(1, way.Id);
            Assert.AreEqual(10, way.ChangeSetId);
            Assert.AreEqual(Utilities.FromUnixTime(10000), way.TimeStamp);
            Assert.AreEqual(OsmSharp.Osm.OsmGeoType.Way, way.Type);
            Assert.AreEqual(100, way.UserId);
            Assert.AreEqual("Ben", way.UserName);
            Assert.AreEqual(2, way.Version);
            Assert.AreEqual(2, way.Nodes.Count);
            Assert.AreEqual(0, way.Nodes[0]);
            Assert.AreEqual(1, way.Nodes[1]);
        }
Beispiel #40
0
 /// <summary>
 /// Converts a PBF way into an OsmSharp-relation.
 /// </summary>
 /// <returns></returns>
 public static OsmSharp.Osm.Relation DecodeRelation(PrimitiveBlock block, OsmSharp.Osm.PBF.Relation pbfRelation)
 {
     var relation = new OsmSharp.Osm.Relation();
     Encoder.DecodeRelation(block, pbfRelation, relation);
     return relation;
 }
        public void TestDecodeBlockWithWay()
        {
            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("highway"));
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential"));
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben"));

            var pbfWay = new Way()
            {
                id = 1,
                info = new Info()
                {
                    changeset = 10,
                    timestamp = 10,
                    uid = 100,
                    user_sid = 2,
                    version = 2
                }
            };
            pbfWay.keys.Add(0);
            pbfWay.vals.Add(1);
            pbfWay.refs.Add(0);
            pbfWay.refs.Add(1);

            var primitiveGroup = new PrimitiveGroup();
            primitiveGroup.ways.Add(pbfWay);
            block.primitivegroup.Add(primitiveGroup);

            var primitivesConsumer = new PrimitivesConsumerMock();
            block.Decode(primitivesConsumer, false, false, false);

            Assert.AreEqual(0, primitivesConsumer.Nodes.Count);
            Assert.AreEqual(1, primitivesConsumer.Ways.Count);
            Assert.AreEqual(0, primitivesConsumer.Relations.Count);
        }
Beispiel #42
0
 /// <summary>
 /// Converts a PBF way into an OsmSharp-way.
 /// </summary>
 /// <returns></returns>
 public static OsmSharp.Osm.Way DecodeWay(PrimitiveBlock block, OsmSharp.Osm.PBF.Way pbfWay)
 {
     var way = new OsmSharp.Osm.Way();
     Encoder.DecodeWay(block, pbfWay, way);
     return way;
 }
Beispiel #43
0
        /// <summary>
        /// Converts a PBF way into an OsmSharp-relation.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.Relation DecodeRelation(PrimitiveBlock block, OsmSharp.Osm.PBF.Relation pbfRelation,
                                                           OsmSharp.Osm.Relation relation)
        {
            // make sure old data is gone.
            if (relation.Members != null &&
                relation.Members.Count > 0)
            { // clear nodes list.
                relation.Members.Clear();
            }
            if (relation.Tags != null)
            { // clear the tags collection.
                relation.Tags.Clear();
            }
            if (relation.Members == null)
            { // create nodes list.
                relation.Members = new List <OsmSharp.Osm.RelationMember>(pbfRelation.memids.Count);
            }
            if (relation.Tags == null)
            { // create tags collection.
                relation.Tags = new TagsCollection(pbfRelation.keys.Count);
            }

            // add nex stuff.
            relation.Id = pbfRelation.id;
            long memberId = 0;

            for (var i = 0; i < pbfRelation.types.Count; i++)
            {
                memberId = memberId + pbfRelation.memids[i];
                var role = System.Text.Encoding.UTF8.GetString(
                    block.stringtable.s[pbfRelation.roles_sid[i]]);

                var member = new OsmSharp.Osm.RelationMember();
                member.MemberId   = memberId;
                member.MemberRole = role;
                switch (pbfRelation.types[i])
                {
                case Relation.MemberType.NODE:
                    member.MemberType = OsmSharp.Osm.OsmGeoType.Node;
                    break;

                case Relation.MemberType.WAY:
                    member.MemberType = OsmSharp.Osm.OsmGeoType.Way;
                    break;

                case Relation.MemberType.RELATION:
                    member.MemberType = OsmSharp.Osm.OsmGeoType.Relation;
                    break;
                }

                relation.Members.Add(member);
            }
            for (int i = 0; i < pbfRelation.keys.Count; i++)
            {
                string key   = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.keys[i]]);
                string value = System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.vals[i]]);

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

            return(relation);
        }
Beispiel #44
0
        /// <summary>
        /// Encodes an OsmSharp-node into a PBF-node.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.PBF.Node EncodeNode(PrimitiveBlock block, Dictionary<string, int> reverseStringTable,
            OsmSharp.Osm.PBF.Node pbfNode, Osm.Node node)
        {
            pbfNode.id = node.Id.Value;
            pbfNode.info = new Info();
            pbfNode.info.version = 0;
            if (node.ChangeSetId.HasValue)
            {
                pbfNode.info.changeset = node.ChangeSetId.Value;
            }
            else
            {
                pbfNode.info.changeset = 0;
            }
            if (node.TimeStamp.HasValue)
            {
                pbfNode.info.timestamp = Encoder.EncodeTimestamp(node.TimeStamp.Value, block.date_granularity);
            }
            else
            {
                pbfNode.info.timestamp = 0;
            }
            if (node.UserId.HasValue)
            {
                pbfNode.info.uid = (int)node.UserId.Value;
            }
            else
            {
                pbfNode.info.uid = 0;
            }
            pbfNode.info.user_sid = Encoder.EncodeString(block, reverseStringTable, node.UserName);
            if (node.Version.HasValue)
            {
                pbfNode.info.version = (int)node.Version.Value;
            }
            else
            {
                pbfNode.info.version = 0;
            }
            pbfNode.lat = Encoder.EncodeLatLon(node.Latitude.Value, block.lat_offset, block.granularity);
            pbfNode.lon = Encoder.EncodeLatLon(node.Longitude.Value, block.lon_offset, block.granularity);

            if(node.Tags != null)
            {
                foreach(var tag in node.Tags)
                {
                    pbfNode.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key));
                    pbfNode.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value));
                }
            }
            else
            {
                pbfNode.keys.Clear();
                pbfNode.vals.Clear();
            }
            return pbfNode;
        }
        public void TestDecodeRelation()
        {
            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"));
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("fake role"));

            var pbfRelation = new Relation()
            {
                id = 1,
                info = new Info()
                {
                    changeset = 10,
                    timestamp = 10,
                    uid = 100,
                    user_sid = 3,
                    version = 2
                }
            };
            pbfRelation.keys.Add(1);
            pbfRelation.vals.Add(2);
            pbfRelation.memids.Add(10);
            pbfRelation.memids.Add(1); // delta-encoding.
            pbfRelation.roles_sid.Add(4);
            pbfRelation.roles_sid.Add(4);
            pbfRelation.types.Add(Relation.MemberType.NODE);
            pbfRelation.types.Add(Relation.MemberType.WAY);

            var relation = Encoder.DecodeRelation(block, pbfRelation);
            Assert.IsNotNull(relation);
            Assert.AreEqual(1, relation.Id);
            Assert.AreEqual(10, relation.ChangeSetId);
            Assert.AreEqual(Utilities.FromUnixTime(10000), relation.TimeStamp);
            Assert.AreEqual(OsmSharp.Osm.OsmGeoType.Relation, relation.Type);
            Assert.AreEqual(100, relation.UserId);
            Assert.AreEqual("Ben", relation.UserName);
            Assert.AreEqual(2, relation.Version);
            Assert.AreEqual(2, relation.Members.Count);
            Assert.AreEqual(10, relation.Members[0].MemberId);
            Assert.AreEqual(OsmSharp.Osm.OsmGeoType.Node, relation.Members[0].MemberType);
            Assert.AreEqual("fake role", relation.Members[0].MemberRole);
            Assert.AreEqual(11, relation.Members[1].MemberId);
            Assert.AreEqual(OsmSharp.Osm.OsmGeoType.Way, relation.Members[1].MemberType);
            Assert.AreEqual("fake role", relation.Members[1].MemberRole);
        }
Beispiel #46
0
        /// <summary>
        /// Encodes a string.
        /// </summary>
        /// <returns></returns>
        public static int EncodeString(PrimitiveBlock block, Dictionary<string, int> reverseStringTable, string value)
        {
            if (value == null) { return 0; }

            if (block.stringtable == null)
            {
                block.stringtable = new StringTable();
                block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(string.Empty));
                reverseStringTable.Add(string.Empty, 0);
            }

            int id;
            if (reverseStringTable.TryGetValue(value, out id))
            {
                return id;
            }

            var bytes = System.Text.Encoding.UTF8.GetBytes(value);
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(value));
            reverseStringTable.Add(value, block.stringtable.s.Count - 1);
            return block.stringtable.s.Count - 1;
        }
Beispiel #47
0
        /// <summary>
        /// Encodes an OsmSharp-relation into a PBF-relation.
        /// </summary>
        /// <returns></returns>
        public static OsmSharp.Osm.PBF.Relation EncodeRelation(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, Osm.Relation relation)
        {
            var pbfRelation = new OsmSharp.Osm.PBF.Relation();

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

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

            if (relation.Members != null &&
                relation.Members.Count > 0)
            {
                pbfRelation.memids.Add(relation.Members[0].MemberId.Value);
                pbfRelation.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[0].MemberRole));
                switch (relation.Members[0].MemberType.Value)
                {
                case OsmGeoType.Node:
                    pbfRelation.types.Add(Relation.MemberType.NODE);
                    break;

                case OsmGeoType.Way:
                    pbfRelation.types.Add(Relation.MemberType.WAY);
                    break;

                case OsmGeoType.Relation:
                    pbfRelation.types.Add(Relation.MemberType.RELATION);
                    break;
                }
                for (var i = 1; i < relation.Members.Count; i++)
                {
                    pbfRelation.memids.Add(relation.Members[i].MemberId.Value -
                                           relation.Members[i - 1].MemberId.Value);
                    pbfRelation.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[i].MemberRole));
                    switch (relation.Members[i].MemberType.Value)
                    {
                    case OsmGeoType.Node:
                        pbfRelation.types.Add(Relation.MemberType.NODE);
                        break;

                    case OsmGeoType.Way:
                        pbfRelation.types.Add(Relation.MemberType.WAY);
                        break;

                    case OsmGeoType.Relation:
                        pbfRelation.types.Add(Relation.MemberType.RELATION);
                        break;
                    }
                }
            }
            return(pbfRelation);
        }
        public void TestDecodeNode()
        {
            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 pbfNode = new Node()
            {
                id = 1,
                info = new Info()
                {
                    changeset = 10,
                    timestamp = 10,
                    uid = 100,
                    user_sid = 3,
                    version = 2
                },
                lat = Encoder.EncodeLatLon(10.9, block.lat_offset, block.granularity),
                lon = Encoder.EncodeLatLon(11.0, block.lat_offset, block.granularity)
            };
            pbfNode.keys.Add(1);
            pbfNode.vals.Add(2);

            var node = Encoder.DecodeNode(block, pbfNode);
            Assert.IsNotNull(node);
            Assert.AreEqual(1, node.Id);
            Assert.AreEqual(10, node.ChangeSetId);
            Assert.AreEqual(10.9, node.Latitude);
            Assert.AreEqual(11.0, node.Longitude);
            Assert.AreEqual(Utilities.FromUnixTime(10000), node.TimeStamp);
            Assert.AreEqual(OsmSharp.Osm.OsmGeoType.Node, node.Type);
            Assert.AreEqual(100, node.UserId);
            Assert.AreEqual("Ben", node.UserName);
            Assert.AreEqual(2, node.Version);
        }
 public void ProcessNode(PrimitiveBlock block, Node node)
 {
     this.Nodes.Add(node);
 }
        public void TestDecodeBlockWithRelation()
        {
            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("highway"));
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential"));
            block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben"));

            var pbfRelation = new Relation()
            {
                id = 1,
                info = new Info()
                {
                    changeset = 10,
                    timestamp = 10,
                    uid = 100,
                    user_sid = 2,
                    version = 2
                }
            };
            pbfRelation.keys.Add(0);
            pbfRelation.vals.Add(1);
            pbfRelation.memids.Add(10);
            pbfRelation.memids.Add(1); // delta-encoding.
            pbfRelation.roles_sid.Add(3);
            pbfRelation.roles_sid.Add(3);
            pbfRelation.types.Add(Relation.MemberType.NODE);
            pbfRelation.types.Add(Relation.MemberType.WAY);

            var primitiveGroup = new PrimitiveGroup();
            primitiveGroup.relations.Add(pbfRelation);
            block.primitivegroup.Add(primitiveGroup);

            var primitivesConsumer = new PrimitivesConsumerMock();
            block.Decode(primitivesConsumer, false, false, false);

            Assert.AreEqual(0, primitivesConsumer.Nodes.Count);
            Assert.AreEqual(0, primitivesConsumer.Ways.Count);
            Assert.AreEqual(1, primitivesConsumer.Relations.Count);
        }
        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.Osm.Way();
            way.Id = 1;
            way.ChangeSetId = 1;
            way.Tags = new OsmSharp.Collections.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 List<long>();
            way.Nodes.Add(1);
            way.Nodes.Add(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]]));
        }