随机GPS数据(地图坐标)
昨天又接了个小任务,还是GPS的,要做一个随机生成GPS坐标数据的工具,而且要求生成的数据不能太"离谱",也就是说要在一定范围之内,因为是模拟车行,所以可能有个限速的要求吧,不能一辆汽车每小时1w公里的速度行驶。。那就~~!
快下班接的任务~~!然后。。马上关机回家。。吃饭,看了会电视~~!11点了。。才想起来有任务。打开电脑~~!
既然是不能太离谱。。那就要在某个范围内移动。。唉,百度了下经纬度的2点距离公式~~!
double lat1,long1;//经度,纬度1
double lat2,long2;//经度,纬度2
const float pi = float(3.1415926);//pi
const float EarthRadiu = float(6378.245);//地球半径
float Wei_Km = ( pi * EarthRadiu )/180 ; //每度纬度多少公里
double lat0; //比较纬度,取两点小纬度的那个
if(lat1<lat2) lat0 = lat1;
else lat0 = lat2;
Jing_Km = (pi * (EarthRadiu * float( cos(Lat*pi/180 ))))/180;//在lat0上,每度经度间的距离
double cx, cy, r;//x方向距离,y方向距离,两点距离;
cx = abs(lat2-lat1) * Wei_Km;//根据纬度差计算距离
cy = abs(long2-long1)*Jing_Km;//根据经度差计算距离
r = sqrt(cx*cx + cy *cy);
唉看了一大堆,三角函数。。。弧度。。。。公里。。英尺。。弄的头晕了。
以前算这个都是口算啊,唉,大学几年以后。。什么都忘记了~~!
还好我们有google,baidu,还有"病"。。信息检索这么发达的社会,还愁什么呢。唉~~!不会玩检索的人怎么冲浪呢~~!
检索了半天中文网页~~!没发现一个有用的,唉,直接google英文吧。Random longitude latitude 第一条记录俺就发现了想要的东西。。
http://www.geomidpoint.com/random/calculation.html
Calculation Methods - Random Points
详细内容自己看去吧,呵呵看不懂有google翻译。
意思大概就是2种方法,一个圆形区域一个矩形区域。详细内容只能意会不能言转,因为感觉把英语翻译成汉语是件十分恶心的事情,所以就不多说了,
-
Circular region
-
B. Rectangular region
Circular region calculation detail
- Convert all latitudes and longitudes to radians.
- rand1 and rand2 are unique random numbers generated in the range 0 to 1.0.
- Given the initial values startlat, startlon and maxdist. (maxdist is in miles or kms).
- For the mean radius of the earth use:
radiusEarth = 3960.056052 miles or radiusEarth = 6372.796924 km - Convert maximum distance to radians.
maxdist=maxdist/radiusEarth - Compute a random distance from 0 to maxdist scaled so that points on larger circles have a greater probability of being chosen than points on smaller circles as described earlier.
dist = acos(rand1*(cos(maxdist) - 1) + 1) - Compute a random bearing from 0 to 2*PI radians (0 to 360 degrees), with all bearings having an equal probability of being chosen.
brg = 2*PI*rand2 - Use the starting point, random distance and random bearing to calculate the coordinates of the final random point.
lat = asin(sin(startlat)*cos(dist) + cos(startlat)*sin(dist)*cos(brg))
lon = startlon + atan2(sin(brg)*sin(dist)*cos(startlat), cos(dist)-sin(startlat)*sin(lat)) - If lon is less than -PI then:
lon = lon + 2*PI
If lon is greater than PI then:
lon = lon - 2*PI
Circular region calculation detail
- Convert all latitudes and longitudes to radians.
- rand1 and rand2 are unique random numbers generated in the range 0 to 1.0.
- Given the initial values startlat, startlon and maxdist. (maxdist is in miles or kms).
- For the mean radius of the earth use:
radiusEarth = 3960.056052 miles or radiusEarth = 6372.796924 km - Convert maximum distance to radians.
maxdist=maxdist/radiusEarth - Compute a random distance from 0 to maxdist scaled so that points on larger circles have a greater probability of being chosen than points on smaller circles as described earlier.
dist = acos(rand1*(cos(maxdist) - 1) + 1) - Compute a random bearing from 0 to 2*PI radians (0 to 360 degrees), with all bearings having an equal probability of being chosen.
brg = 2*PI*rand2 - Use the starting point, random distance and random bearing to calculate the coordinates of the final random point.
lat = asin(sin(startlat)*cos(dist) + cos(startlat)*sin(dist)*cos(brg))
lon = startlon + atan2(sin(brg)*sin(dist)*cos(startlat), cos(dist)-sin(startlat)*sin(lat)) - If lon is less than -PI then:
lon = lon + 2*PI
If lon is greater than PI then:
lon = lon - 2*PI
大概讲述了计算过程,正准备还是用c#进行解析上面的内容。。。看了一下首页的图标突然发现了很多功能~~!唉,有的时候还是要多点击一下的~~!
Random point Generator哈哈这不是我正需要的么?
爱因斯坦都不记自己家的电话,咱何必费那劲用c#解释那2段方法呢?
打开http://www.geomidpoint.com/random/
右键查看源码Ctrl+f 输入.js只有一个结果
<script type="text/javascript" src="jFunc.js">
下载吧呵呵,虽然js方面是个小菜鸟,但是学习的精神是挡不住的~~!
点了跟烟,开始研究js~~!…………
主要算法calculate()
其实就是用JavaScript解释了一下上面那2段英文
好,c#转码~~!…………15分钟搞定,测试一下
~~!怎么随机出来的都是一样的。。。好吧线程休息一下
这次就对了,距离在设定的500Km以内,没什么问题。
睡了~~!
话说第二天~~!
来弄数据库的东西,根据数据库的设备生成"合理"数据
直接上图
根据最大速度,在间隔时间内生成一个点,然后继续以最后的点为中心继续移动~~!
主要方法:
var cosdif = Math.Cos(maxdist) - 1;
var sinstartlat = Math.Sin(startlat);
var cosstartlat = Math.Cos(startlat);
double dist = 0;
var rad360 = 2 * Math.PI;//圆周率
for (var i = 0; i < p; i++)//点数
{
//Thread.Sleep(100);
dist = Math.Acos((new Random().NextDouble() * cosdif + 1));//随机数
brg[0] = rad360 * new Random().NextDouble();
lat = Math.Asin(sinstartlat * Math.Cos(dist) + cosstartlat * Math.Sin(dist) * Math.Cos(brg[0]));
lon = deg(normalizeLongitude(startlon * 1 + Math.Atan2(Math.Sin(brg[0]) * Math.Sin(dist) * cosstartlat, Math.Cos(dist) - sinstartlat * Math.Sin(lat))));
lat = deg(lat);
dist = Math.Round(dist * radiusEarth * 10000) / 10000;
brg[0] = Math.Round(deg(brg[0]) * 1000) / 1000;//随机距离
rtnList.Add(new GPSData() { Latitude = padZeroRight(lat), longtitude = padZeroRight(lon), distance = dist.ToString(), bearing = brg[j].ToString() });
}
就写到这里吧,也算个记录了~~!