offset.dat

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: offset.dat
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using MySql.Data.MySqlClient;
namespace testgps
{
    class Program
    {
        static ArrayList m_array;
        const double M_PI = 3.14159265358979323846264338327950288;
        const double M_E = 2.71828182845904523536028747135266250;
        public class MapCoord
        {
            public int lng { set; get; } //12151表示121.51
            public int lat { set; get; } //3130表示31.30
            public int x_off { set; get; } //地图x轴偏移像素值
            public int y_off { set; get; } //地图y轴偏移像素值
        }
        /// <summary>
        /// 自定义比较类
        /// </summary>
        public class myReverserClass : IComparer
        {
            public int Compare(object x, object y)
            {
                MapCoord data1 = (MapCoord)x, data2 = (MapCoord)y;
                int det_lng = data1.lng - data2.lng;
                if (det_lng != 0)
                    return det_lng;
                else
                    return data1.lat - data2.lat;
            }
        }
        //这就需要一个把经纬度转换成地图xy轴坐标的算法:
        private static double lngToPixel(double lng, int zoom)
        {
            return (lng + 180) * (256L << zoom) / 360;
        }
        private static double latToPixel(double lat, int zoom)
        {
            double siny = Math.Sin(lat * M_PI / 180);
            double y = Math.Log((1 + siny) / (1 - siny));
            return (128 << zoom) * (1 - y / (2 * M_PI));
        }
        //xy轴坐标加上对应的地图xy轴的偏移量,最后还要反过来将最终正确的地图xy轴坐标转换成正确的经纬度
        private static double pixelToLng(double pixelX, int zoom)
        {
            return pixelX * 360 / (256L << zoom) - 180;
        }
        private static double pixelToLat(double pixelY, int zoom)
        {
            double y = 2 * M_PI * (1 - pixelY / (128 << zoom));
            double z = Math.Pow(M_E, y);
            double siny = (z - 1) / (z + 1);
            return Math.Asin(siny) * 180 / M_PI;
        }
        /// <summary>
        /// 将字节转化为具体的数据对象
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        private static MapCoord getMapCoordFromBytes(byte[] buf)
        {
            //数据文档结构是八字节为一个坐标及其偏移量,分别为经度,纬度,x偏移量,y偏移量; 每两字节为一个数据
            MapCoord coord = new MapCoord();
            byte[] b1 = new byte[2], b2 = new byte[2], b3 = new byte[2], b4 = new byte[2];
            Array.Copy(buf, 0, b1, 0, 2);
            Array.Copy(buf, 2, b2, 0, 2);
            Array.Copy(buf, 4, b3, 0, 2);
            Array.Copy(buf, 6, b4, 0, 2);
            coord.lng = System.BitConverter.ToInt16(b1, 0);
            coord.lat = System.BitConverter.ToInt16(b2, 0);
            coord.x_off = System.BitConverter.ToInt16(b3, 0);
            coord.y_off = System.BitConverter.ToInt16(b4, 0);
            string mysqlYunfeng = "server=localhost;port=3306;user id=root;password=admin;database=gps;CharSet=gb2312;";
            string sql = "insert into offset001(lat,log,offx,offy) values(@lat,@log,@offx,@offy)";
            MySqlParameter[] paras = new MySqlParameter[4];
            paras[0] = new MySqlParameter("@lat", coord.lat);
            paras[1] = new MySqlParameter("@log", coord.lng);
            paras[2] = new MySqlParameter("@offx", coord.x_off);
            paras[3] = new MySqlParameter("@offy", coord.y_off);
            MySqlHelper.ExecuteNonQuery(mysqlYunfeng, sql,paras);
            Console.WriteLine(coord.lat);
            return coord;
        }
        public static void  OpenDatFile()
        {
                        //读取数据文件
            //这里读取文件的地方可以单独提出来, 读一次之后保存到内存里, 读取时比较耗时间. 或者放到数据库中去.
            FileStream fs = new FileStream("offset.dat", FileMode.OpenOrCreate, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            int size = (int)fs.Length / 8;
            m_array = new ArrayList();
            for (int i = 0; i < size; i++)
            {
                //按八个字节八个字节来读取, 放在MapCoord对象中,并添加到ArrayList中
                byte[] source = br.ReadBytes(8);
                m_array.Add(getMapCoordFromBytes(source));
            }
            br.Close();
            fs.Close();
            return;
         }
        /// <summary>
        /// WGS84(GPS)坐标转火星坐标
        /// </summary>
        /// <param name="lat">纬度</param>
        /// <param name="lng">经度</param>
        public static void WGS2Mars(double lat, double lng)
        {
            //将要查找的坐标放置在MapCoord对象中
            MapCoord search = new MapCoord();
            search.lat = (int)(lat * 100);
            search.lng = (int)(lng * 100);
            myReverserClass rc = new myReverserClass();
            //执行查找, 查询结果将返回array中的索引值
            int x = m_array.BinarySearch(0, m_array.Count, search, rc);
            //取得查找到的结果并进行计算
            MapCoord ret = (MapCoord)m_array[x];
            double pixY = latToPixel(lat, 18);
            double pixX = lngToPixel(lng, 18);
            pixY += ret.y_off;
            pixX += ret.x_off;
            lat = pixelToLat(pixY, 18);
            lng = pixelToLng(pixX, 18);
            //输出校正后的结果
            Console.WriteLine("欢迎来到火星,坐标: lat:{0},lng:{1} !!!", lat, lng);
        }
        static void Main(string[] args)
        {
            OpenDatFile();
            //测试一下
            WGS2Mars(34.782343, 113.769063);
            WGS2Mars(34.782343, 113.769063);
            WGS2Mars(34.782343, 113.769063);
            Console.ReadLine();
        }
    }
}
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9月前
|
网络协议 BI 调度
NR PRACH(五) type1 RA(4-step)基本过程
无线通信,最重要的前提是建立接收端和发射端之间的时间同步。
|
10月前
UE Operation File [ Read / Write ] DTOperateFile 插件说明
UE Operation File [ Read / Write ] DTOperateFile 插件说明
39 0
|
编译器
relocation value does not fit in 26 bits (offset: 0x10, type: 1)
relocation value does not fit in 26 bits (offset: 0x10, type: 1)
148 0
#748 – 获得按下时对应位置点的大小(Getting the Size of a Contact Point during Raw Touch)
原文:#748 – 获得按下时对应位置点的大小(Getting the Size of a Contact Point during Raw Touch) 原文地址:https://wpf.2000things.com/2013/02/04/748-getting-the-size-of-a-contact-point-during-raw-touch/ 在低级别的触屏Touch 事件中,我们可以获得手指与屏幕接触的位置的面积大小。
956 0
|
SQL 缓存 Oracle
[20180226]exp buffer RECORDLENGTH.txt
[20180226]exp buffer RECORDLENGTH.txt --//虽然已经很少使用exp导致,如果加入direct=y参数,设置RECORDLENGTH参数能加快数据导出.
1224 0
|
编解码 Python
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128) 最近在用Python处理...
2109 0
|
Oracle 关系型数据库 测试技术
[20171204]guaranteed restore point.txt
[20171204]guaranteed restore point.txt --//昨天帮别人升级再次遇到关于相关问题,实际上主要问题在于升级文档没有完成后取消restore point的设置.
1253 0
|
Oracle 关系型数据库 计算机视觉
[20170512]No ADR base is set.txt
[20170512]No ADR base is set.txt --//生产系统一台机器,使用adrci包如下提示No ADR base is set.虽然我可以 $ rlwrap adrci ADRCI: Release 11.
1771 0
|
编解码
codec can&#39;t decode byte 0xe6 in position 0: ordinal not in range
ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)] 错误: 实例 "linux-core" 执行所请求操作失败,实例处于错误状态。
892 0