上一章我们讲解了在IOS中我们如何使用ArcGIS加载底图,展示定位。
在客户端的GIS开发中会经常遇到坐标系的问题,当坐标系不同时,需要对坐标系进行转换,在这一章我们详细讲解什么是坐标系,坐标系如何做坐标转换。
一.什么是坐标系
我们都知道地球是一个不规则的球型,且大地水准面是一个不规则的曲面。
下图是大地水准面到参考椭球面法线方向距离的全球高程异常图(基于EGM96模型)。
正值表示当地大地水准面在椭球面之上,负值表示当地大地水准面在椭球面之下。
既然大地水准面是一个不规则的曲面,不能直接用数学公式表述。这怎么进行大地测量?
早期科学家主要是基于本国水准面数据去创建椭球体模型,使其与本国的大地水准面拟合的误差最小。
二.项目中常用的坐标系
1.地球坐标 (WGS84)
- 国际标准,从GPS设备中取出的数据的坐标系
- 国际地图提供商使用的坐标系
2.火星坐标 (GCJ-02) 也叫国测局坐标系
- 中国标准,从国行移动设备中定位获取的坐标数据使用这个坐标系
- 国家规定: 国内出版的各种地图系统(包括电子形式),必须至少采用GCJ-02对地理位置进行首次加密。
3.百度坐标 (BD-09)
- 百度标准,百度 SDK,百度地图,Geocoding 使用
- 百度在火星坐标上进行了二次加密
三.常见场景坐标系
1.WGS84 : Google国外地图,osm地图等国外地图
2.GCJ-02 : ios地图(在中国内是高德),Google国内地图(.cn域名下),搜搜、阿里云、高德地图、腾讯
3.BD09 : 百度地图
四.墨卡托投影
墨卡托投影是一种“等角正切圆柱投影”,荷兰地图学家墨卡托(Mercator)在1569年拟定:假设地球被围在一个中空的圆柱里,其赤道与圆柱相接触,然后再假想地球中心有一盏灯,把球面上的图形投影到圆柱体上,再把圆柱体展开,这就是一幅标准纬线为零度(即赤道)的“墨卡托投影”绘制出的世界地图。
所以对应的坐标系都有着相应的墨卡托投影,通过一定的计算方法即可得到对应坐标系的墨卡托投影。
即 :
BD09分为BD09LL和BD09MC。其中,BD09LL是在标准经纬度的基础上进行GCJ-02加偏之后,再加上百度自身的加偏算法,也就是在标准经纬度的基础之上进行了两次加偏,该坐标系的坐标值为经纬度格式,单位为度。
BD09 MC是在标准Web墨卡托的基础上进行GCJ-02加偏之后,再加上百度自身的加偏算法,也就是在Web墨卡托的基础之上进行了两次加偏,该坐标系的坐标值为Web墨卡托格式,单位为米。
了解了墨卡托投影后,我们将ArcGIS项目中的WIKD将上述坐标系简写成gcj02,gcj02mc,bdll,bdmc,,wgs84(后面的项目中也会对应的提到这些,gcj02mc与bdmc都是米制)
五.坐标转换
在原项目中新建ArcGISCoordinateConverter 类
ArcGISCoordinateConverter.h文件中:
#import <CoreLocation/CoreLocation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ArcGISCoordinateConverter : NSObject
/**
* @brief 世界标准地理坐标(WGS-84) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
*
* ####只在中国大陆的范围的坐标有效,以外直接返回世界标准坐标
*
* @param location 世界标准地理坐标(WGS-84)
*
* @return 中国国测局地理坐标(GCJ-02)<火星坐标>
*/
+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location;
/**
* @brief 中国国测局地理坐标(GCJ-02) 转换成 世界标准地理坐标(WGS-84)
*
* ####此接口有1-2米左右的误差,需要精确定位情景慎用
*
* @param location 中国国测局地理坐标(GCJ-02)
*
* @return 世界标准地理坐标(WGS-84)
*/
+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location;
/**
* @brief 世界标准地理坐标(WGS-84) 转换成 百度地理坐标(BD-09)
*
* @param location 世界标准地理坐标(WGS-84)
*
* @return 百度地理坐标(BD-09)
*/
+ (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location;
/**
* @brief 中国国测局地理坐标(GCJ-02)<火星坐标> 转换成 百度地理坐标(BD-09)
*
* @param location 中国国测局地理坐标(GCJ-02)<火星坐标>
*
* @return 百度地理坐标(BD-09)
*/
+ (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location;
/**
* @brief 百度地理坐标(BD-09) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
*
* @param location 百度地理坐标(BD-09)
*
* @return 中国国测局地理坐标(GCJ-02)<火星坐标>
*/
+ (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location;
/**
* @brief 百度地理坐标(BD-09) 转换成 世界标准地理坐标(WGS-84)
*
* ####此接口有1-2米左右的误差,需要精确定位情景慎用
*
* @param location 百度地理坐标(BD-09)
*
* @return 世界标准地理坐标(WGS-84)
*/
+ (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location;
/**
* 百度墨卡托坐标转百度经纬度坐标
* @param x
* @param y
* @return
*/
+(CLLocationCoordinate2D) convertMCToLL:(CLLocationCoordinate2D)mercator;
/**
* 百度经纬度坐标转百度墨卡托坐标
* @param lng
* @param lat
* @return
*/
+(CLLocationCoordinate2D) convertLLToMC:(CLLocationCoordinate2D)point;
@end
ArcGISCoordinateConverter.m文件中:
#define LAT_OFFSET_0(x,y) -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x))
#define LAT_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_2 (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_3 (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0
#define LON_OFFSET_0(x,y) 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x))
#define LON_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_2 (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_3 (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0
#define RANGE_LON_MAX 137.8347
#define RANGE_LON_MIN 72.004
#define RANGE_LAT_MAX 55.8271
#define RANGE_LAT_MIN 0.8293
// jzA = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
#define jzA 6378245.0
#define jzEE 0.00669342162296594323
@implementation ArcGISCoordinateConverter
+ (double)transformLat:(double)x bdLon:(double)y
{
double ret = LAT_OFFSET_0(x, y);
ret += LAT_OFFSET_1;
ret += LAT_OFFSET_2;
ret += LAT_OFFSET_3;
return ret;
}
+ (double)transformLon:(double)x bdLon:(double)y
{
double ret = LON_OFFSET_0(x, y);
ret += LON_OFFSET_1;
ret += LON_OFFSET_2;
ret += LON_OFFSET_3;
return ret;
}
+ (BOOL)outOfChina:(double)lat bdLon:(double)lon
{
if (lon < RANGE_LON_MIN || lon > RANGE_LON_MAX)
return true;
if (lat < RANGE_LAT_MIN || lat > RANGE_LAT_MAX)
return true;
return false;
}
+ (CLLocationCoordinate2D)gcj02Encrypt:(double)ggLat bdLon:(double)ggLon
{
CLLocationCoordinate2D resPoint;
double mgLat;
double mgLon;
if ([self outOfChina:ggLat bdLon:ggLon]) {
resPoint.latitude = ggLat;
resPoint.longitude = ggLon;
return resPoint;
}
double dLat = [self transformLat:(ggLon - 105.0)bdLon:(ggLat - 35.0)];
double dLon = [self transformLon:(ggLon - 105.0) bdLon:(ggLat - 35.0)];
double radLat = ggLat / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - jzEE * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((jzA * (1 - jzEE)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (jzA / sqrtMagic * cos(radLat) * M_PI);
mgLat = ggLat + dLat;
mgLon = ggLon + dLon;
resPoint.latitude = mgLat;
resPoint.longitude = mgLon;
return resPoint;
}
+ (CLLocationCoordinate2D)gcj02Decrypt:(double)gjLat gjLon:(double)gjLon {
CLLocationCoordinate2D gPt = [self gcj02Encrypt:gjLat bdLon:gjLon];
double dLon = gPt.longitude - gjLon;
double dLat = gPt.latitude - gjLat;
CLLocationCoordinate2D pt;
pt.latitude = gjLat - dLat;
pt.longitude = gjLon - dLon;
return pt;
}
+ (CLLocationCoordinate2D)bd09Decrypt:(double)bdLat bdLon:(double)bdLon
{
CLLocationCoordinate2D gcjPt;
double x_pi = M_PI * 3000.0 / 180.0;
double x = bdLon - 0.0065, y = bdLat - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
gcjPt.longitude = z * cos(theta);
gcjPt.latitude = z * sin(theta);
return gcjPt;
}
+(CLLocationCoordinate2D)bd09Encrypt:(double)ggLat bdLon:(double)ggLon
{
CLLocationCoordinate2D bdPt;
double x = ggLon, y = ggLat;
double x_pi = M_PI * 3000.0 / 180.0;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
bdPt.longitude = z * cos(theta) + 0.0065;
bdPt.latitude = z * sin(theta) + 0.006;
return bdPt;
}
+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location
{
return [self gcj02Encrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location
{
return [self gcj02Decrypt:location.latitude gjLon:location.longitude];
}
+ (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location
{
CLLocationCoordinate2D gcj02Pt = [self gcj02Encrypt:location.latitude
bdLon:location.longitude];
return [self bd09Encrypt:gcj02Pt.latitude bdLon:gcj02Pt.longitude] ;
}
+ (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location
{
return [self bd09Encrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location
{
return [self bd09Decrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location
{
CLLocationCoordinate2D gcj02 = [self bd09ToGcj02:location];
return [self gcj02Decrypt:gcj02.latitude gjLon:gcj02.longitude];
}
static double EARTHRADIUS = 6370996.81;
static double MCBAND[6] = {12890594.86, 8362377.87, 5591021.0, 3481989.83, 1678043.12, 0.0};
static double LLBAND[6] = {75.0, 60.0, 45.0, 30.0, 15.0, 0.0};
static double MC2LL[6][10] = {{1.410526172116255e-8, 0.00000898305509648872, -1.9939833816331, 200.9824383106796, -187.2403703815547, 91.6087516669843, -23.38765649603339, 2.57121317296198, -0.03801003308653, 17337981.2}, {-7.435856389565537e-9, 0.000008983055097726239, -0.78625201886289, 96.32687599759846, -1.85204757529826, -59.36935905485877, 47.40033549296737, -16.50741931063887, 2.28786674699375, 10260144.86}, {-3.030883460898826e-8, 0.00000898305509983578, 0.30071316287616, 59.74293618442277, 7.357984074871, -25.38371002664745, 13.45380521110908, -3.29883767235584, 0.32710905363475, 6856817.37}, {-1.981981304930552e-8, 0.000008983055099779535, 0.03278182852591, 40.31678527705744, 0.65659298677277, -4.44255534477492, 0.85341911805263, 0.12923347998204, -0.04625736007561, 4482777.06}, {3.09191371068437e-9, 0.000008983055096812155, 0.00006995724062, 23.10934304144901, -0.00023663490511, -0.6321817810242, -0.00663494467273, 0.03430082397953, -0.00466043876332, 2555164.4}, {2.890871144776878e-9, 0.000008983055095805407, -3.068298e-8, 7.47137025468032, -0.00000353937994, -0.02145144861037, -0.00001234426596, 0.00010322952773, -0.00000323890364, 826088.5}};
static double LL2MC[6][10] = {{-0.0015702102444, 111320.7020616939, 1704480524535203.0, -10338987376042340.0, 26112667856603880.0, -35149669176653700.0, 26595700718403920.0, -10725012454188240.0, 1800819912950474.0, 82.5}, {0.0008277824516172526, 111320.7020463578, 647795574.6671607, -4082003173.641316, 10774905663.51142, -15171875531.51559, 12053065338.62167, -5124939663.577472, 913311935.9512032, 67.5}, {0.00337398766765, 111320.7020202162, 4481351.045890365, -23393751.19931662, 79682215.47186455, -115964993.2797253, 97236711.15602145, -43661946.33752821, 8477230.501135234, 52.5}, {0.00220636496208, 111320.7020209128, 51751.86112841131, 3796837.749470245, 992013.7397791013, -1221952.21711287, 1340652.697009075, -620943.6990984312, 144416.9293806241, 37.5}, {-0.0003441963504368392, 111320.7020576856, 278.2353980772752, 2485758.690035394, 6070.750963243378, 54821.18345352118, 9540.606633304236, -2710.55326746645, 1405.483844121726, 22.5}, {-0.0003218135878613132, 111320.7020701615, 0.00369383431289, 823725.6402795718, 0.46104986909093, 2351.343141331292, 1.58060784298199, 8.77738589078284, 0.37238884252424, 7.45}};
/**
* 墨卡托坐标转经纬度坐标
* @param x
* @param y
* @return
*/
+(CLLocationCoordinate2D) convertMCToLL:(CLLocationCoordinate2D)mercator {
NSMutableArray *cF = [NSMutableArray arrayWithCapacity:10];
double x = fabs(mercator.longitude);
double y = fabs(mercator.latitude);
for (int cE = 0; cE < 6; cE++) {
if (y >= MCBAND[cE]) {
for (int i = 0; i <10; i++) {
[cF addObject:[NSNumber numberWithDouble:MC2LL[cE][i]]];
}
break;
}
}
CLLocationCoordinate2D location = [self converterX:x Y:y CE:cF];
return location;
}
/**
* 经纬度坐标转墨卡托坐标
* @param lng
* @param lat
* @return
*/
+(CLLocationCoordinate2D) convertLLToMC:(CLLocationCoordinate2D)point {
NSMutableArray *cE = [NSMutableArray arrayWithCapacity:10];
double lng = [self getLoop:point.longitude min:-180 max:180];
double lat = [self getRange:point.latitude min:-74 max:74];
for (int i = 0; i <6; i++) {
if (lat >= LLBAND[i]) {
cE = [NSMutableArray arrayWithCapacity:10];
for (int j = 0; j <10; j++) {
[cE addObject:[NSNumber numberWithDouble:LL2MC[i][j]]];
}
break;
}
}
if (cE.count>0) {
for (int i = 6 - 1; i >= 0; i--) {
if (lat <= -LLBAND[i]) {
cE = [NSMutableArray arrayWithCapacity:10];
for (int j = 0; j <10; j++) {
[cE addObject:[NSNumber numberWithDouble:LL2MC[i][j]]];
}
break;
}
}
}
return [self converterX:lng Y:lat CE:cE];
}
+(CLLocationCoordinate2D) converterX:(double)x Y:(double)y CE:(NSArray *)arr {
double cE[10];
for (int i = 0; i<10 && i<arr.count; i++) {
cE[i] = [[arr objectAtIndex:i] doubleValue];
}
double xTemp = cE[0] + cE[1] * fabs(x);
double cC = fabs(y) / cE[9];
double yTemp = cE[2] + cE[3] * cC + cE[4] * cC * cC + cE[5] * cC * cC * cC + cE[6] * cC * cC * cC * cC + cE[7] * cC * cC * cC * cC * cC + cE[8] * cC * cC * cC * cC * cC * cC;
xTemp *= (x < 0 ? -1 : 1);
yTemp *= (y < 0 ? -1 : 1);
CLLocationCoordinate2D location = {.longitude=xTemp,.latitude=yTemp};
return location;
}
+(double) getLoop:(double)lng min:(NSInteger)min max:(NSInteger)max {
while (lng > max) {
lng -= max - min;
}
while (lng < min) {
lng += max - min;
}
return lng;
}
+(double) getRange:(double)lat min:(NSInteger)min max:(NSInteger)max{
lat = lat>min?lat:min;
lat = lat<max?lat:max;
return lat;
}
@end
项目中的具体代码就不在描述了,具体的方法都有注释,基本都是经纬度转换和墨卡托米制与对应经纬制的转换方法。以后也不会再具体描述经纬度转换的问题。
网友评论