使用PostGIS进行轨迹分析

从 sqlite 数据库中读取轨迹数据,存储到 postgis 数据库中,再利用 postgis 的特性进行轨迹分析。

track.db


Program.cs

class Program
{
    static void Main(string[] args)
    {
        List<Track> tracks = new List<Track>();

        try
        {
            #region 从Sqlite中读取轨迹数据

            //Sqlite数据库路径
            string sqlitePath = Path.Combine(Environment.CurrentDirectory, "track.db");

            //数据表名称
            string sqliteTb = "T_1140204000334";

            using (SqliteDB sqlite = new SqliteDB(sqlitePath))
            {
                sqlite.Open();
                DataTable dt = sqlite.ExecuteSQL("select * from " + sqliteTb + ";");
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        tracks.Add(new Track()
                        {
                            B = Convert.ToDouble(dt.Rows[i]["B"]) * 180 / Math.PI,
                            L = Convert.ToDouble(dt.Rows[i]["L"]) * 180 / Math.PI,
                            H = Convert.ToSingle(dt.Rows[i]["H"]),
                            Azi = Convert.ToSingle(dt.Rows[i]["Azi"]),
                            AntH = Convert.ToSingle(dt.Rows[i]["AntH"]),
                            UTCTime = Convert.ToDateTime(dt.Rows[i]["UTCTime"]),
                            Hdop = Convert.ToSingle(dt.Rows[i]["Hdop"]),
                            SolType = Convert.ToInt32(Convert.ToByte(dt.Rows[i]["SolType"]))
                        });
                    }
                }
            }

            Console.WriteLine("Sqlite中的轨迹记录数为:" + tracks.Count);
            Console.WriteLine("---------------------------------------------------------------------");

            #endregion

            #region 向PostgreSQL中存入轨迹数据

            using (PostgresqlDB postgresql = new PostgresqlDB("localhost", "5432", "postgres", "123456", "test"))
            {
                postgresql.Open();

                string tableName = "t_1_1_20150519103523";

                StringBuilder sql = new StringBuilder();

                #region 写入数据

                //删除数据表
                if (postgresql.QueryTableIsExist(tableName))
                {
                    sql.Clear();
                    sql.AppendLine("drop table " + tableName + ";");
                    postgresql.ExecuteNonQuery(sql.ToString());
                }

                postgresql.BeginTransaction();

                try
                {
                    //创建数据表
                    sql.Clear();
                    sql.AppendLine("create table " + tableName + "(");
                    sql.AppendLine("id serial not null,");
                    sql.AppendLine("coord geometry(point, 4326) not null,");
                    sql.AppendLine("h real not null,");
                    sql.AppendLine("azi real not null,");
                    sql.AppendLine("ant_h real not null,");
                    sql.AppendLine("utc_time timestamp with time zone not null,");
                    sql.AppendLine("hdop real not null,");
                    sql.AppendLine("sol_type integer not null,");
                    sql.AppendLine("constraint " + tableName + "_pkey primary key (id)");
                    sql.AppendLine(");");

                    postgresql.ExecuteNonQuery(sql.ToString());

                    //插入记录
                    for (int i = 0; i < tracks.Count; i++)
                    {
                        sql.Clear();
                        sql.AppendLine("insert into " + tableName + "(coord,h,azi,ant_h,utc_time,hdop,sol_type) values(");
                        sql.AppendLine(string.Format("ST_GeomFromText('point({0} {1})',4326),", tracks[i].B, tracks[i].L));
                        sql.AppendLine(tracks[i].H + ",");
                        sql.AppendLine(tracks[i].Azi + ",");
                        sql.AppendLine(tracks[i].AntH + ",");
                        sql.AppendLine("'" + tracks[i].UTCTime.ToString("yyyy-MM-dd HH:mm:ss") + "',");
                        sql.AppendLine(tracks[i].Hdop + ",");
                        sql.AppendLine(tracks[i].Sol
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值