gctl_toolkits/archive/sph2car/main.cpp

181 lines
4.2 KiB
C++
Raw Normal View History

2024-09-10 20:25:18 +08:00
//将球坐标表示的数据(地形等)转换为直角坐标系,或者生成一个直角坐标系下的球面网络
#include "func.h"
int disp_help()
{
cout
<<"sph2car 0.0.1 - create a spherical grid using Cartersian coordinates and add terrain data into it"<<endl<<endl
<<"usage: sph2car [-f<input-file>]|[-i<dx>/<dy> -r<xmin>/<xmax>/<ymin>/<ymax>] -d<radius|WGS84> -o<output-file>"<<endl
<<" -f add an input terrain data into the output grid"<<endl
<<" -d specify the reference raidus of a sphere, type WGS84 to use the earth's ellipsoid"<<endl
<<" -i the interval of output grid"<<endl
<<" -r the range of output grid"<<endl
<<" -o specify the output name"<<endl<<endl
<<"example: sph2car -fexample.dat -d10000 -i1/1 -r30/80/40/60 -otest.dat"<<endl;
return 0;
}
int typeget(char *str, const char* str2,string &str3)//提出头端的子句
{
char* strp = strstr(str, str2); // 从字符串str中查找str2
if (strp == NULL)
{
return 0; // 如果没有找到,返回
}
string temp = str;
str3=temp.substr(strlen(str2));//提出除str2外的子句
return 1;
}
int nameget(char *str, const char* str2,string &str3)//提出尾端的子句
{
char* strp = strstr(str, str2); // 从字符串str中查找str2
if (strp == NULL)
{
return 0; // 如果没有找到,返回
}
string temp = str;
str3=temp.substr(0,strlen(str)-strlen(str2));//提出除str2外的子句
return 1;
}
string& replace_all(string& str,const string& old_value,const string& new_value,int &num) //替换str中所有lod_value为new_value,返回被替换的lod_value的个数
{
int count = 0;
for(string::size_type pos(0);pos!=string::npos;pos+=new_value.length()){
if((pos=str.find(old_value,pos))!=string::npos){
str.replace(pos,old_value.length(),new_value);
count++;
}
else break;
}
num = count;
return str;
}
int main(int argc, char* argv[])
{
int space_num;
double radius;
int interval[2],range[4];
string temp="";
string input_name="";
string output_name="";
stringstream stemp;
if (argc==1)
{
disp_help();
}
else
{
interval[0]=interval[1]=-1.0;
range[0]=range[1]=range[2]=range[3]=1000;
for (int i = 1; i < argc; i++)
{
if (typeget(argv[i],OUTPUT,temp))
{
if (temp=="")
{
cout<<"no output name, program stopped..."<<endl;
return 0;
}
else
{
output_name = temp;
temp="";
}
}
if (typeget(argv[i],FILE,temp))
{
if (temp=="")
{
cout<<"no input name, program stopped..."<<endl;
return 0;
}
else
{
input_name = temp;
temp="";
}
}
if (typeget(argv[i],INSERT,temp))
{
replace_all(temp,"/"," ",space_num);
stemp.clear();
stemp.str(temp);
if (space_num==1)
{
stemp>>interval[0]>>interval[1];
if (interval[0]<=0||interval[1]<=0)
{
cout<<"wrong intervals, program stopped..."<<endl;
return 0;
}
}
else
{
cout<<"wrong intervals, program stopped..."<<endl;
return 0;
}
}
if (typeget(argv[i],RANGE,temp))
{
replace_all(temp,"/"," ",space_num);
stemp.clear();
stemp.str(temp);
if (space_num==3)
{
stemp>>range[0]>>range[1]>>range[2]>>range[3];
if (range[0]<-180||range[1]>180||range[2]<-90||range[3]>90
||range[0]>range[1]||range[2]>range[3])
{
cout<<"wrong range info, program stopped..."<<endl;
return 0;
}
}
else
{
cout<<"wrong range info, program stopped..."<<endl;
return 0;
}
}
if (typeget(argv[i],RADIUS,temp))
{
if (temp=="")
{
cout<<"no radius info, program stopped..."<<endl;
return 0;
}
else if (!strcmp(temp.c_str(),WGS84))
{
radius = -1.0;
}
else
{
stemp.clear();
stemp.str(temp);
stemp>>radius;
if (radius<0)
{
cout<<"wrong radius info, program stopped..."<<endl;
return 0;
}
}
}
}
if (input_name==""&&interval[0]!=-1&&range[0]!=1000)
{
create_grid(radius,interval,range,output_name);
}
else if (input_name!="")
{
add_grid(radius,input_name,output_name);
}
}
return 0;
}