Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
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);
            }
        }