/*------------------------------------------------------------------------------
* convkml.c : google earth kml converter
*
* Copyright (C) 2007-2017 by T.TAKASU, All rights reserved.
*
* references :
* [1] Open Geospatial Consortium Inc., OGC 07-147r2, OGC(R) KML, 2008-04-14
*
* version : $Revision: 1.1 $ $Date: 2008/07/17 21:48:06 $
* history : 2007/01/20 1.0 new
* 2007/03/15 1.1 modify color sequence
* 2007/04/03 1.2 add geodetic height option
* support input of NMEA GGA sentence
* delete altitude info for track
* add time stamp option
* separate readsol.c file
* 2009/01/19 1.3 fix bug on display mark with by-q-flag option
* 2010/05/10 1.4 support api readsolt() change
* 2010/08/14 1.5 fix bug on readsolt() (2.4.0_p3)
* 2017/06/10 1.6 support wild-card in input file
*-----------------------------------------------------------------------------*/
#include "rtklib.h"
/* constants -----------------------------------------------------------------*/
#define SIZP 0.2 /* mark size of rover positions */
#define SIZR 0.3 /* mark size of reference position */
#define TINT 60.0 /* time label interval (sec) */
static const char *head1="";
static const char *head2="";
static const char *mark="http://maps.google.com/mapfiles/kml/pal2/icon18.png";
/* output track --------------------------------------------------------------*/
static void outtrack(FILE *f, const solbuf_t *solbuf, const char *color,
int outalt, int outtime)
{
double pos[3];
int i;
fprintf(f,"\n");
fprintf(f,"Rover Track\n");
fprintf(f,"\n");
fprintf(f,"\n");
if (outalt) fprintf(f,"absolute\n");
fprintf(f,"\n");
for (i=0;in;i++) {
ecef2pos(solbuf->data[i].rr,pos);
if (outalt==0) pos[2]=0.0;
else if (outalt==2) pos[2]-=geoidh(pos);
fprintf(f,"%13.9f,%12.9f,%5.3f\n",pos[1]*R2D,pos[0]*R2D,pos[2]);
}
fprintf(f,"\n");
fprintf(f,"\n");
fprintf(f,"\n");
}
/* output point --------------------------------------------------------------*/
static void outpoint(FILE *fp, gtime_t time, const double *pos,
const char *label, int style, int outalt, int outtime)
{
double ep[6],alt=0.0;
char str[256]="";
fprintf(fp,"\n");
if (*label) fprintf(fp,"%s\n",label);
fprintf(fp,"#P%d\n",style);
if (outtime) {
if (outtime==2) time=gpst2utc(time);
else if (outtime==3) time=timeadd(gpst2utc(time),9*3600.0);
time2epoch(time,ep);
if (!*label&&fmod(ep[5]+0.005,TINT)<0.01) {
sprintf(str,"%02.0f:%02.0f",ep[3],ep[4]);
fprintf(fp,"%s\n",str);
}
sprintf(str,"%04.0f-%02.0f-%02.0fT%02.0f:%02.0f:%05.2fZ",
ep[0],ep[1],ep[2],ep[3],ep[4],ep[5]);
fprintf(fp,"%s\n",str);
}
fprintf(fp,"\n");
if (outalt) {
fprintf(fp,"1\n");
fprintf(fp,"absolute\n");
alt=pos[2]-(outalt==2?geoidh(pos):0.0);
}
fprintf(fp,"%13.9f,%12.9f,%5.3f\n",pos[1]*R2D,
pos[0]*R2D,alt);
fprintf(fp,"\n");
fprintf(fp,"\n");
}
/* save kml file -------------------------------------------------------------*/
static int savekml(const char *file, const solbuf_t *solbuf, int tcolor,
int pcolor, int outalt, int outtime)
{
FILE *fp;
double pos[3];
int i,qcolor[]={0,1,2,5,4,3,0};
char *color[]={
"ffffffff","ff008800","ff00aaff","ff0000ff","ff00ffff","ffff00ff"
};
if (!(fp=fopen(file,"w"))) {
fprintf(stderr,"file open error : %s\n",file);
return 0;
}
fprintf(fp,"%s\n%s\n",head1,head2);
fprintf(fp,"\n");
for (i=0;i<6;i++) {
fprintf(fp,"\n");
}
if (tcolor>0) {
outtrack(fp,solbuf,color[tcolor-1],outalt,outtime);
}
if (pcolor>0) {
fprintf(fp,"\n");
fprintf(fp," Rover Position\n");
for (i=0;in;i++) {
ecef2pos(solbuf->data[i].rr,pos);
outpoint(fp,solbuf->data[i].time,pos,"",
pcolor==5?qcolor[solbuf->data[i].stat]:pcolor-1,outalt,outtime);
}
fprintf(fp,"\n");
}
if (norm(solbuf->rb,3)>0.0) {
ecef2pos(solbuf->rb,pos);
outpoint(fp,solbuf->data[0].time,pos,"Reference Position",0,outalt,0);
}
fprintf(fp,"\n");
fprintf(fp,"\n");
fclose(fp);
return 1;
}
/* convert to google earth kml file --------------------------------------------
* convert solutions to google earth kml file
* args : char *infile I input solutions file (wild-card (*) is expanded)
* char *outfile I output google earth kml file ("":.kml)
* gtime_t ts,te I start/end time (gpst)
* int tint I time interval (s) (0.0:all)
* int qflg I quality flag (0:all)
* double *offset I add offset {east,north,up} (m)
* int tcolor I track color
* (0:none,1:white,2:green,3:orange,4:red,5:yellow)
* int pcolor I point color
* (0:none,1:white,2:green,3:orange,4:red,5:by qflag)
* int outalt I output altitude (0:off,1:elipsoidal,2:geodetic)
* int outtime I output time (0:off,1:gpst,2:utc,3:jst)
* return : status (0:ok,-1:file read,-2:file format,-3:no data,-4:file write)
* notes : see ref [1] for google earth kml file format
*-----------------------------------------------------------------------------*/
extern int convkml(const char *infile, const char *outfile, gtime_t ts,
gtime_t te, double tint, int qflg, double *offset,
int tcolor, int pcolor, int outalt, int outtime)
{
solbuf_t solbuf={0};
double rr[3]={0},pos[3],dr[3];
int i,j,nfile,stat;
char *p,file[1024],*files[MAXEXFILE]={0};
trace(3,"convkml : infile=%s outfile=%s\n",infile,outfile);
/* expand wild-card of infile */
for (i=0;i=0;i--) free(files[i]);
return -4;
}
}
if ((nfile=expath(infile,files,MAXEXFILE))<=0) {
for (i=0;i0.0) {
for (i=0;i<3;i++) solbuf.rb[i]+=dr[i];
}
/* save kml file */
return savekml(file,&solbuf,tcolor,pcolor,outalt,outtime)?0:-4;
}