Example #1
0
        /// <summary>
        /// Converts simple primitives.
        /// </summary>
        /// <param name="pbfPrimitive"></param>
        /// <returns></returns>
        internal OsmSharp.Osm.OsmGeo Convert(KeyValuePair<PrimitiveBlock, object> pbfPrimitive)
        {
            if (pbfPrimitive.Value == null || pbfPrimitive.Key == null)
            {
                throw new ArgumentNullException("pbfPrimitive");
            }

            PrimitiveBlock block = pbfPrimitive.Key; // get the block properties this object comes from.
            if (pbfPrimitive.Value is OsmSharp.Osm.PBF.Node)
            {
                var node = (pbfPrimitive.Value as OsmSharp.Osm.PBF.Node);
                var simpleNode = new OsmSharp.Osm.Node();
                simpleNode.ChangeSetId = node.info.changeset;
                simpleNode.Id = node.id;
                simpleNode.Latitude = .000000001 * ((double)block.lat_offset
                    + ((double)block.granularity * (double)node.lat));
                simpleNode.Longitude = .000000001 * ((double)block.lon_offset
                    + ((double)block.granularity * (double)node.lon));
                if (node.keys.Count > 0)
                {
                    simpleNode.Tags = new TagsCollection();
                    for (int tag_idx = 0; tag_idx < node.keys.Count; tag_idx++)
                    {
                        string key = Encoding.UTF8.GetString(block.stringtable.s[(int)node.keys[tag_idx]]);
                        string value = Encoding.UTF8.GetString(block.stringtable.s[(int)node.vals[tag_idx]]);

                        if (!simpleNode.Tags.ContainsKey(key))
                        {
                            simpleNode.Tags.Add(new Tag() { Key = key, Value = value });
                        }
                    }
                }
                simpleNode.TimeStamp = Utilities.FromUnixTime((long)node.info.timestamp *
                    (long)block.date_granularity);
                simpleNode.Visible = true;
                simpleNode.Version = (uint)node.info.version;
                simpleNode.UserId = node.info.uid;
                simpleNode.UserName = Encoding.UTF8.GetString(block.stringtable.s[node.info.user_sid]);
                simpleNode.Version = (ulong)node.info.version;
                simpleNode.Visible = true;

                return simpleNode;
            }
            else if (pbfPrimitive.Value is OsmSharp.Osm.PBF.Way)
            {
                var way = (pbfPrimitive.Value as OsmSharp.Osm.PBF.Way);

                var simple_way = new OsmSharp.Osm.Way();
                simple_way.Id = way.id;
                simple_way.Nodes = new List<long>(way.refs.Count);
                long node_id = 0;
                for (int node_idx = 0; node_idx < way.refs.Count; node_idx++)
                {
                    node_id = node_id + way.refs[node_idx];
                    simple_way.Nodes.Add(node_id);
                }
                if (way.keys.Count > 0)
                {
                    simple_way.Tags = new TagsCollection();
                    for (int tag_idx = 0; tag_idx < way.keys.Count; tag_idx++)
                    {
                        string key = Encoding.UTF8.GetString(block.stringtable.s[(int)way.keys[tag_idx]]);
                        string value = Encoding.UTF8.GetString(block.stringtable.s[(int)way.vals[tag_idx]]);

                        if (!simple_way.Tags.ContainsKey(key))
                        {
                            simple_way.Tags.Add(new Tag(key, value));
                        }
                    }
                }
                if (way.info != null)
                { // add the metadata if any.
                    simple_way.ChangeSetId = way.info.changeset;
                    simple_way.TimeStamp = Utilities.FromUnixTime((long)way.info.timestamp *
                        (long)block.date_granularity);
                    simple_way.UserId = way.info.uid;
                    simple_way.UserName = Encoding.UTF8.GetString(block.stringtable.s[way.info.user_sid]);
                    simple_way.Version = (ulong)way.info.version;
                }
                simple_way.Visible = true;

                return simple_way;
            }
            else if (pbfPrimitive.Value is OsmSharp.Osm.PBF.Relation)
            {
                var relation = (pbfPrimitive.Value as OsmSharp.Osm.PBF.Relation);

                var simple_relation = new OsmSharp.Osm.Relation();
                simple_relation.Id = relation.id;
                if (relation.types.Count > 0)
                {
                    simple_relation.Members = new List<OsmSharp.Osm.RelationMember>();
                    long member_id = 0;
                    for (int member_idx = 0; member_idx < relation.types.Count; member_idx++)
                    {
                        member_id = member_id + relation.memids[member_idx];
                        string role = Encoding.UTF8.GetString(
                            block.stringtable.s[relation.roles_sid[member_idx]]);
                        var member = new OsmSharp.Osm.RelationMember();
                        member.MemberId = member_id;
                        member.MemberRole = role;
                        switch (relation.types[member_idx])
                        {
                            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;
                        }

                        simple_relation.Members.Add(member);
                    }
                }
                if (relation.keys.Count > 0)
                {
                    simple_relation.Tags = new TagsCollection();
                    for (int tag_idx = 0; tag_idx < relation.keys.Count; tag_idx++)
                    {
                        string key = Encoding.UTF8.GetString(block.stringtable.s[(int)relation.keys[tag_idx]]);
                        string value = Encoding.UTF8.GetString(block.stringtable.s[(int)relation.vals[tag_idx]]);

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

                return simple_relation;
            }
            throw new Exception(string.Format("PBF primitive with type {0} not supported!",
                pbfPrimitive.GetType().ToString()));
        }
Example #2
0
        /// <summary></summary>
        public static void Test()
        {
            XTrace.WriteLine("创建类型……");

            #region TypeX类型
            TypeX type = TypeX.Create(typeof(FastTest));
            Object obj = type.CreateInstance();
            Debug.Assert(obj != null, "创建实例出错!");

            obj = type.CreateInstance(123);
            Debug.Assert(obj != null, "创建实例出错!");

            //obj = type.CreateInstance("1234");
            //Debug.Assert(obj != null, "创建实例出错!");

            obj = type.CreateInstance(111, "aaa");
            Debug.Assert(obj != null, "创建实例出错!");

            XTrace.WriteLine("创建值类型实例");
            type = TypeX.Create(typeof(ConsoleKeyInfo));
            obj = type.CreateInstance();
            Debug.Assert(obj != null, "创建值类型实例出错!");

            XTrace.WriteLine("创建数组类型实例");
            type = TypeX.Create(typeof(ConsoleKeyInfo[]));
            obj = type.CreateInstance(5);
            Debug.Assert(obj != null, "创建数组类型实例出错!");
            #endregion

            #region 构造函数
            ConstructorInfoX ctr = ConstructorInfoX.Create(typeof(FastTest));
            obj = ctr.CreateInstance();
            Debug.Assert(obj != null, "创建实例出错!");

            ctr = ConstructorInfoX.Create(typeof(FastTest), new Type[] { typeof(Int32) });
            obj = ctr.CreateInstance(123);
            Debug.Assert(obj != null, "创建实例出错!");
            ctr = ConstructorInfoX.Create(typeof(FastTest), new Type[] { typeof(Int32), typeof(String) });
            obj = ctr.CreateInstance(111, "aaa");
            Debug.Assert(obj != null, "创建实例出错!");
            #endregion

            #region 字段
            FieldInfoX field = FieldInfoX.Create(typeof(FastTest), "_ID");
            (obj as FastTest).ID = 111;
            Int32 v = (Int32)field.GetValue(obj);
            Debug.Assert(v == 111, "字段取值出错!");
            field.SetValue(obj, 888);
            v = (Int32)field.GetValue(obj);
            Debug.Assert(v == 888, "字段赋值出错!");

            KeyValuePair<Int32, Int32> kv = new KeyValuePair<int, int>(123456, 222);
            field = FieldInfoX.Create(kv.GetType(), "Key");
            //field.SetValue(kv, 123456);
            v = (Int32)field.GetValue(kv);
            Debug.Assert(v == 123456, "字段取值出错!");

            field = FieldInfoX.Create(typeof(FastTest), "_Name");
            field.SetValue("动态赋值");
            String v2 = (String)field.GetValue();
            Debug.Assert(v2 == "动态赋值", "静态字段出错!");
            #endregion

            #region 属性
            PropertyInfoX p = typeof(FastTest).GetProperty("ID");

            v = (Int32)p.GetValue(obj);
            Debug.Assert(v == 888, "属性取值出错!");
            p.SetValue(obj, 999);
            v = (Int32)p.GetValue(obj);
            Debug.Assert(v == 999, "属性赋值出错!");

            p = PropertyInfoX.Create(typeof(FastTest), "Name");
            field.SetValue("属性动态赋值");
            v2 = (String)field.GetValue();
            Debug.Assert(v2 == "属性动态赋值", "静态字段出错!");
            #endregion

            #region 方法
            MethodInfoX method = MethodInfoX.Create(typeof(FastTest), "Test2");
            method.Invoke(obj);

            method = typeof(FastTest).GetMethod("GetFullName");
            Console.WriteLine(method.Invoke(null, 123, "abc"));
            #endregion
        }