版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sb19931201/article/details/52345554
GPS坐标批量转换成百度地图坐标(SQL SERVER 2012+C#)
写在前面T_T
最近项目的事忙的晕头转向,趁周末刷剧间隙整理一波笔记。。。
已经好长一段时间没有接触SQL语句和数据库连接了。昨天帮师兄处理公交SCD数据,又回顾了一下之前使用SQL SERVER 2012+C#做数据预处理,顺便整理了一下以前留下的笔记。
基本的数据库操作
1.查看数据库表数据
SELECT TOP 1000 [Line]
,[stationnum]
,[stationname]
,[Direction]
,[lon]
,[lat]
,[Angle]
,[LineID]
,[LineName]
,[bdlon]
,[bdlat]
FROM [web].[dbo].[StationCoordinate_WenXinBus]
2.增加主键id列,以及新增列
alter table [web].[dbo].[StationCoordinate_WenXinBus] add id int identity primary key
alter TABLE [web].[dbo].[StationCoordinate_WenXinBus] add bdlon nchar(20) NULL
alter TABLE [web].[dbo].[StationCoordinate_WenXinBus] add bdlat nchar(20) NULL
注:使用Visual Stdio连接SQL SERVER时,如果目标表没有主键,表更新会失败。
3.更新某行数据
update [web].[dbo].[StationCoordinate_WenXinBus] set Line = 223 where id=1
4.插入行
INSERT INTO [web].[dbo].[StationCoordinate_WenXinBus]
(Line,stationnum,stationname,Direction,...)
VALUES ('223', '1','刘家下庄','1',...)
5.删除行
delete FROM [web].[dbo].[StationCoordinate_WenXinBus] where id=1
Visual Stdio数据库连接
1.新建C#工程
2.添加Linq to SQL类
3.添加数据库连接
直接加目标表拖动到dbml设计图面上即可,如下图所示:
坐标转换代码(全)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace test8
{
class Program
{
static double pi = 3.14159265358979324;
static double a = 6378245.0;
static double ee = 0.00669342162296594323;
public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
static double[] wgs2gcj(double lat, double lon)
{
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
double magic = Math.Sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.Sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
double[] loc = { mgLat, mgLon };
return loc;
}
static double[] wgs2bd(double lat, double lon)
{
double[] wgs_gcj = wgs2gcj(lat, lon);
double[] gcj_bd = gcj2bd(wgs_gcj[0], wgs_gcj[1]);
return gcj_bd;
}
public static double[] gcj2bd(double lat, double lon)
{
double x = lon, y = lat;
double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
double bd_lon = z * Math.Cos(theta) + 0.0065;
double bd_lat = z * Math.Sin(theta) + 0.006;
return new double[] { bd_lat, bd_lon };
}
public static double[] bd2gcj(double lat, double lon)
{
double x = lon - 0.0065, y = lat - 0.006;
double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
double gg_lon = z * Math.Cos(theta);
double gg_lat = z * Math.Sin(theta);
return new double[] { gg_lat, gg_lon };
}
private static double transformLat(double lat, double lon)
{
double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.Sqrt(Math.Abs(lat));
ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(lon * pi) + 40.0 * Math.Sin(lon / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(lon / 12.0 * pi) + 320 * Math.Sin(lon * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double transformLon(double lat, double lon)
{
double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.Sqrt(Math.Abs(lat));
ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(lat * pi) + 40.0 * Math.Sin(lat / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.Sin(lat / 12.0 * pi) + 300.0 * Math.Sin(lat / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
static void Main(string[] args)
{
using (card_count_allDataContext bls = new card_count_allDataContext())
{
var transline1 = bls.StationCoordinate_WenXinBus.ToList();
List<StationCoordinate_WenXinBus> transline = transline1.OrderBy(b => b.id).ToList();
transline1 = null;
for (int i = 0; i < transline.Count(); i++)//开始循环
{
double lat=0,lon= 0;
Console.WriteLine("开始计算第" + i + "行\r\n");
string []latdfm = transline[i].lat.Split('/');//lat度分秒
string[] londfm = transline[i].lon.Split('/');//lon度分秒
if(latdfm.Length==3&&londfm.Length==3)
{
lat = Convert.ToDouble(latdfm[0]) + Convert.ToDouble(latdfm[1]) / 60 + Convert.ToDouble(latdfm[2]) / 3600;
lon = Convert.ToDouble(londfm[0]) + Convert.ToDouble(londfm[1]) / 60 + Convert.ToDouble(londfm[2]) / 3600;
}
//double lat = Convert.ToDouble(transline[i].lat);
//double lon = Convert.ToDouble(transline[i].lon);
double[] loc = wgs2gcj(lat, lon);// WGS坐标转换为GCJ坐标。
double[] outp = gcj2bd(loc[0], loc[1]);//GCJ坐标转换为百度坐标。
// QDStationCoorinateUnique fk = new QDStationCoorinateUnique();
// fk.StationID = transline[i].StationID;
// fk.StationName = transline[i].StationName;
//fk.lat = transline[i].lat;
//fk.lon = transline[i].lon;
transline[i].bdlat = outp[0].ToString();
transline[i].bdlon = outp[1].ToString();
bls.SubmitChanges();
}
}
Console.WriteLine("计算完毕,正在更新...");
Console.WriteLine("更新完毕");
Console.ReadLine();
}
}
}
后续
在做数据处理的时候,如果没有复杂的逻辑运算,使用SQL语句可以非常方便快捷的批处理。特别是对Python或者R还不太熟悉得同学,SQL绝对是个利器。还有很多SQL语法技巧待我系统的整理后在后续的笔记中更新。