通过Navit学习笔记(一)、Navit学习笔记(二)的介绍,我们已经对Navit有了初步的认识。Navit是学习导航程序开发很好的例子,从本文开始,我们对Navit的源代码做简要的分析,以求学习和提高。
navit的内核包括:
1、一个设置地图对象
2、一个或多个车辆
3、一套用于地图解析的图像对象
4、一个用于显示的GUI对象
5、一个路经对象
6、一个导航对象
...
在详细介绍Navit内核前,我们先来学习Navit的坐标模块,坐标模块包括projection投影模块,coor坐标模块。在projection投影模块中有文件projection.h 和 projection.c ,在projection.h中定义了:
枚举对象:
1、projection:主要定义投影的类型定义
*projection_none 不投影,
*projection_mg mg上投影,
*projection_garmin garmin投影,
*projection_screen 屏幕投影,
*projection_utm utm投影,
*projection_gk gk投影
2、enum map_datum 定义了数据的投影坐标系包括none、wgs84标准和dhdn标准
*map_datum_none none,
*map_datum_wgs84 wgs84标准,
*map_datum_dhdn dhdn标准
3、定义了coord结构,改结构使用coord.h中的定义
4、根据枚举projection定义了 enum projection projection_from_name(const char *name,struct coord *offset)函数用于将字符型的投影类型名称转换成字符projection投影枚举类型。
5、定义函数char *projection_to_name(enum projection proj,struct coord *offset)用于将投影类型转换成字符形式。
整个porjection的实现代码如下:
1 #include < string .h > 2 #include < glib.h > 3 #include " coord.h " 4 #include " debug.h " 5 6 #include " projection.h " // 头文件引用 7 8 // 定义投影类型名称结构 9 struct projection_name { 10 enum projection projection; // 枚举 projection 类型 11 char * name; // 投影名称 12 }; 13 14 // 根据projection_name结构定义 projection_names[]数组,数组中projection枚举类型和char类型名称一一对应 15 struct projection_name projection_names[] = { 16 {projection_none, "" }, 17 {projection_mg, " mg " }, 18 {projection_garmin, " garmin " }, 19 {projection_utm, " utm " }, 20 {projection_gk, " gk " }, 21 }; 22 23 // 实现头文件中的projection_from_name函数,其中参数name为projection类型名词,offset为coord结构 24 enum projection 25 projection_from_name( const char * name, struct coord * offset) 26 { 27 int i; 28 int zone; 29 char ns; 30 31 for (i = 0 ; i < sizeof (projection_names) / sizeof ( struct projection_name) ; i ++ ) 32 { 33 if ( ! strcmp(projection_names[i].name, name)) 34 return projection_names[i].projection; 35 } 36 if (offset) { 37 if (sscanf(name, " utm%d%c " , & zone, & ns) == 2 && zone > 0 && zone <= 60 && (ns == ' n ' || ns == ' s ' )) { 38 offset -> x = zone * 1000000 ; 39 offset -> y = (ns == ' s ' ? - 10000000 : 0 ); 40 return projection_utm; 41 } 42 } 43 return projection_none; 44 } 45 // 返回projection类型的名称 46 char * 47 projection_to_name( enum projection proj, struct coord * offset) 48 { 49 int i; 50 51 for (i = 0 ; i < sizeof (projection_names) / sizeof ( struct projection_name) ; i ++ ) { 52 if (projection_names[i].projection == proj) 53 return projection_names[i].name; 54 } 55 return NULL; 56 }在coord.h文件中定义了
1、宏定义
*coord_is_equal(a,b)用于对两个coord结构的相等操作
*为了防止一些平台出现浮点截断误差,定义了
1 #ifdef AVOID_FLOAT 2 /* * 3 * On platforms where we are trying to avoid floats, sometimes we can't. 4 * It is better on these platforms to use single precision floating points 5 * over double percision ones since performance is much better. 6 */ 7 typedef float navit_float; 8 #define navit_sin(x) sinf(x) 9 #define navit_cos(x) cosf(x) 10 #define navit_tan(x) tanf(x) 11 #define navit_atan(x) atanf(x) 12 #define navit_acos(x) acosf(x) 13 #define navit_asin(x) asinf(x) 14 #define navit_sqrt(x) sqrtf(x) 15 #else 16 typedef double navit_float; 17 #define navit_sin(x) sin(x) 18 #define navit_cos(x) cos(x) 19 #define navit_tan(x) tan(x) 20 #define navit_atan(x) atan(x) 21 #define navit_acos(x) acos(x) 22 #define navit_asin(x) asin(x) 23 #define navit_sqrt(x) sqrt(x) 24 #endif
2、结构类型
*coord 定义了int类型的墨卡托坐标系
struct coord { int x; /* !< X-Value */ int y; /* !< Y-Value */ };* pcoord 定义了一个包含projection枚举类型的墨卡托坐标系
struct pcoord { enum projection pro; int x; /* !< X-Value */ int y; /* !< Y-Value */ };*coord_rect 定义了矩形形状的坐标系结构
struct coord_rect { struct coord lu; // 矩形左上点的coord结构 struct coord rl; // 矩形有下点的coord结构 };*coord_d 双精度的墨卡托坐标系
struct coord_d { double x; /* !< X-Value */ double y; /* !< Y-Value */ };*coord_geo_cart WGS84标准的笛卡尔坐标系
struct coord_geo_cart { navit_float x; /* !< X-Value */ navit_float y; /* !< Y-Value */ navit_float z; /* !< Z-Value */ };*struct attr;
3、枚举类型
*coord_format 答应地理坐标的格式化类型枚举
enum coord_format { /* * * Degrees with decimal places.Ie 20.5000 N 110.5000 E */ DEGREES_DECIMAL, /* * * Degrees and minutes.ie 20 30.00 N 110 30.00 E */ DEGREES_MINUTES, /* * * Degrees, minutes and seconds.ie 20 30 30.00 N 110 30 30 E */ DEGREES_MINUTES_SECONDS };4、函数
1 // 根据p获取墨卡托坐标系/点 2 struct coord * coord_get(unsigned char ** p); 3 // 获取新的墨卡托坐标点 4 struct coord * coord_new( int x, int y); 5 // 从attr结构多项中获取坐标系/点 6 struct coord * coord_new_from_attrs( struct attr * parent, struct attr ** attrs); 7 // 销毁墨卡托坐标系/点 8 void coord_destroy( struct coord * c); 9 // 墨卡托坐标系/点分析 10 int coord_parse( const char * c_str, enum projection pro, struct coord * c_ret); 11 // 包含 12 int pcoord_parse( const char * c_str, enum projection pro, struct pcoord * c_ret); 13 // 坐标打印 14 void coord_print( enum projection pro, struct coord * c, FILE * out ); 15 // 新的矩形坐标范围框 16 struct coord_rect * coord_rect_new( struct coord * lu, struct coord * rl); 17 18 void coord_rect_destroy( struct coord_rect * r); 19 // 区域重叠 20 int coord_rect_overlap( struct coord_rect * r1, struct coord_rect * r2); 21 // 区域包含 22 int coord_rect_contains( struct coord_rect * r, struct coord * c); 23 // 区域扩展 24 void coord_rect_extend( struct coord_rect * r, struct coord * c); 25 // 坐标格式化 26 void coord_format( float lat, float lng, enum coord_format, char * buffer, int size);5、coord.h定义的属性、函数
1 enum coord_format; 2 enum projection; 3 struct attr; 4 struct coord; 5 struct coord_rect; 6 struct pcoord; 7 struct coord * coord_get(unsigned char ** p); 8 struct coord * coord_new( int x, int y); 9 struct coord * coord_new_from_attrs( struct attr * parent, struct attr ** attrs); 10 void coord_destroy( struct coord * c); 11 struct coord_rect * coord_rect_new( struct coord * lu, struct coord * rl); 12 void coord_rect_destroy( struct coord_rect * r); 13 int coord_rect_overlap( struct coord_rect * r1, struct coord_rect * r2); 14 int coord_rect_contains( struct coord_rect * r, struct coord * c); 15 void coord_rect_extend( struct coord_rect * r, struct coord * c); 16 int coord_parse( const char * c_str, enum projection pro, struct coord * c_ret); 17 int pcoord_parse( const char * c_str, enum projection pro, struct pcoord * pc_ret); 18 void coord_print( enum projection pro, struct coord * c, FILE * out ); 19 void coord_format( float lat, float lng, enum coord_format fmt, char * buffer, int size); 20 unsigned int coord_hash( const void * key); 21 int coord_equal( const void * a, const void * b);