gctl_toolkits/archive/xyz2gpx/func.h

117 lines
2.7 KiB
C
Raw Normal View History

2024-09-10 20:25:18 +08:00
#ifndef _FUNC_H
#define _FUNC_H
#include "iostream"
#include "fstream"
#include "sstream"
#include "iomanip"
#include "stdio.h"
#include "string.h"
#include "list"
#define OUTFILE "-o"
#define RESET "\033[0m" /* reset */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
using namespace std;
struct loc
{
double geox,geoy,altitude;
string tag;
};
typedef list<loc> LocList;
class XyzGpx
{
public:
XyzGpx();
~XyzGpx();
int readxyz(char*);
int outgpx(char*);
private:
LocList list_loc;
LocList::iterator il;
loc temp_loc;
double outbox[4];
};
XyzGpx::XyzGpx()
{
outbox[0] = outbox[2] = 1e+30;
outbox[1] = outbox[3] = -1e+30;
}
XyzGpx::~XyzGpx()
{
if(!list_loc.empty()) list_loc.clear();
}
int XyzGpx::readxyz(char* filename)
{
stringstream stemp;
string strtemp;
ifstream xyzin(filename);
if (!xyzin)
{
cout<<filename<<" not found!"<<endl;
return 1;
}
else
{
while(getline(xyzin,strtemp))
{
if (strtemp!="")
{
stemp.clear();
stemp.str(strtemp);
stemp>>temp_loc.tag>>temp_loc.geox>>temp_loc.geoy>>temp_loc.altitude;
list_loc.push_back(temp_loc);
if(temp_loc.geox<outbox[0]) outbox[0] = temp_loc.geox;
if(temp_loc.geox>outbox[1]) outbox[1] = temp_loc.geox;
if(temp_loc.geoy<outbox[2]) outbox[2] = temp_loc.geoy;
if(temp_loc.geoy>outbox[3]) outbox[3] = temp_loc.geoy;
}
}
xyzin.close();
return 0;
}
}
int XyzGpx::outgpx(char* filename)
{
ofstream gpxout(filename);
if (!gpxout)
{
cout<<"can not create "<<filename<<endl;
return 1;
}
else
{
gpxout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"<<endl
<<"<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"MapSource 6.15.6\" version=\"1.1\" "<<endl
<<"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1"<<endl
<<"http://www.topografix.com/GPX/1/1/gpx.xsd\">"<<endl
<<" <metadata>"<<endl
<<" <link href=\"http://www.garmin.com\">"<<endl
<<" <text>Garmin International</text>"<<endl
<<" </link>"<<endl
<<" <time>2014-07-06T00:40:40Z</time>"<<endl
<<" <bounds \"maxlat\"="<<setprecision(8)<<outbox[3]<<" \"maxlon\"="<<outbox[1]<<" \"minlat\"="<<outbox[2]<<" \"minlon\"="<<outbox[0]<<"\"/>"<<endl
<<" </metadata>"<<endl;
for (il=list_loc.begin();il!=list_loc.end();il++)
{
temp_loc = *il;
gpxout<<setprecision(8)
<<" <wpt lat=\""<<temp_loc.geox<<"\" lon=\""<<temp_loc.geoy<<"\">"<<endl
<<" <ele>"<<temp_loc.altitude<<"</ele>"<<endl
<<" <name>"<<temp_loc.tag<<"</name>"<<endl
<<" <sym>Dot, White</sym>"<<endl
<<" <extensions>"<<endl
<<" </extensions>"<<endl
<<" </wpt>"<<endl;
}
gpxout<<"</gpx>";
gpxout.close();
return 0;
}
}
#endif