Esempio n. 1
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;
        }
Esempio n. 2
0
        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);
 }
Esempio n. 4
0
        public void TestDecodeBlockWithNode()
        {
            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 node = new Node()
                {
                    id = 1,
                    info = new Info()
                    {
                        changeset = 10,
                        timestamp = 10,
                        uid = 100,
                        user_sid = 2,
                        version = 2
                    },
                    lat = Encoder.EncodeLatLon(10.9, block.lat_offset, block.granularity),
                    lon = Encoder.EncodeLatLon(11.0, block.lat_offset, block.granularity)
                };
            node.keys.Add(0);
            node.vals.Add(1);

            var primitiveGroup = new PrimitiveGroup();
            primitiveGroup.nodes.Add(node);
            block.primitivegroup.Add(primitiveGroup);

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

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