Browse Source

Gmt module: Conversion function for GMTRegion now using new parameter reading scheme and error reporting.

gmtdatadir
Michael Uleysky 8 years ago
parent
commit
5b466460d6
  1. 98
      modules/gmt/modgmt_func.h

98
modules/gmt/modgmt_func.h

@ -225,96 +225,78 @@ template<>
class Convert2Struct<struct gmt_region, ObjectList>
{
public:
struct gmt_region operator()(const ObjectList* input, bool* issuc) const
struct gmt_region operator()(const ObjectList* input, bool* issuc, std::string& err) const
{
struct gmt_region r;
auto size=input->Size();
bool suc=true;
if(1==size) // Cases 1, 2 and 3
if(1==input->Size()) // Cases 1, 2 and 3
{
Base2Region region(input,0);
r=region(&suc);
Base2Region region;
r=region.Convert(input->At(0),&suc,err);
if(!suc) goto fail; // Conversion failed
return r;
}
// Case 4
{
std::string type;
bool typeexist=false;
{
Base2String t(input,"type");
if(t.Exist())
{
type=t(&suc);
tolower(type);
if(!suc) goto fail; // Conversion failed
typeexist=true;
}
else type="nobbox";
}
if(r.Convert(type)) return r; // type is one of "global*" string, we can return, becuse upd is irrelevant
ONFPar<Base2Region,ObjectGMTRegion> region("region");
ONPar<Base2StringD> type("type","nobbox");
ONPar<Base2Coord> xb("xb"),xe("xe"),yb("yb"),ye("ye");
bool upd;
{ParseNamedParameters params(input,type,region,xb,xe,yb,ye); if(!params) {err=params.Error(); goto fail;} } // Fail to parse by variant 4
bool typeexist=type.Exist();
{
BaseMT2Region updarg(input,"r","region");
r=updarg(&upd,&suc);
if(upd && !suc) goto fail; // Conversion failed or too many arguments
suc=true;
std::string t;
if(r.Convert(type,t)) return r; // type is one of "global*" string, we can return, because upd is irrelevant
}
Base2Coord ixb(input,"xb"), ixe(input,"xe"), iyb(input,"yb"), iye(input,"ye");
if(!(typeexist || ixb.Exist() || ixe.Exist() || iyb.Exist() || iye.Exist() || upd)) goto case5; // No named parameters, no update mode, possibly case 5
struct gmt_coord xb,yb,xe,ye;
bool upd=region.Exist();
if(!(typeexist || xb.Exist() || xe.Exist() || yb.Exist() || ye.Exist() || upd)) goto case5; // No named parameters, no update mode, possibly case 5
struct gmt_coord cxb,cxe,cyb,cye;
bool isbbox=upd?(gmt_region::BBOX==r.type):false;
if(typeexist)
{
if("bbox"==type) isbbox=true;
if("nobbox"==type) isbbox=false;
if("bbox"!=type && "nobbox"!=type) goto fail; // Unknown type
if("bbox"==type.Value()) isbbox=true;
if("nobbox"==type.Value()) isbbox=false;
if("bbox"!=type.Value() && "nobbox"!=type.Value()) goto fail; // Unknown type
}
if(upd)
{
if(!ixb.Exist()) xb=r.xb;
if(!ixe.Exist()) xe=r.xe;
if(!iyb.Exist()) yb=r.yb;
if(!iye.Exist()) ye=r.ye;
cxb=xb.Exist()?xb:region->xb;
cxe=xe.Exist()?xe:region->xe;
cyb=yb.Exist()?yb:region->yb;
cye=ye.Exist()?ye:region->ye;
}
else suc=suc && ixb.Exist() && ixe.Exist() && iyb.Exist() && iye.Exist(); // In "new" mode all parameters must exists
if(ixb.Exist()) xb=ixb(&suc);
if(ixe.Exist()) xe=ixe(&suc);
if(iyb.Exist()) yb=iyb(&suc);
if(iye.Exist()) ye=iye(&suc);
if(!suc) goto fail; // Something wrong
if(!r.Convert(xb,xe,yb,ye,isbbox)) goto fail; // Conversion failed
else
{
suc=suc && xb.Exist() && xe.Exist() && yb.Exist() && ye.Exist(); // In "new" mode all parameters must exists
cxb=xb; cxe=xe; cyb=yb; cye=ye;
}
if(!suc) {err="To specify region all four coordinates must be set"; goto fail;} // Something wrong
if(!r.Convert(cxb,cxe,cyb,cye,isbbox)) {err="Error conversion of coordinates to region"; goto fail;} // Conversion failed
return r; // Case 3 with all parameters
}
case5:
// Case 5
if(4==size || 5==size)
{
bool isbbox=false;
if(5==size)
{
Base2String type(input,4);
std::string str=type(&suc);
if(!suc) goto fail; // Wrong parameter
tolower(str);
if("bbox"!=str || "nobbox"!=str) goto fail; // Unknown fifth parameter
if("bbox"==str) isbbox=true;
}
Base2Coord ixb(input,0), ixe(input,(isbbox?2:1)), iyb(input,(isbbox?1:2)), iye(input,3);
struct gmt_coord xb=ixb(&suc),yb=iyb(&suc),xe=ixe(&suc),ye=iye(&suc);
if(suc)
{
if(!r.Convert(xb,xe,yb,ye,isbbox)) goto fail; // Conversion failed
OPosPar<Base2StringD> type("type","nobbox");
RPosPar<Base2Coord> p1("p1"),p2("p2"),p3("p3"),p4("p4");
ParsePositionalParameters params(input,p1,p2,p3,p4,type);
if(!params) {err=params.Error(); goto fail;} // Fail to parse by variant 5
bool isbbox=("bbox"==type.Value());
if("bbox"!=type.Value() && "nobbox"!=type.Value()) {err="Fifth parameter can be \"bbox\" or \"nobbox\" only"; goto fail;} // Unknown fifth parameter
if(!r.Convert(p1,isbbox?p3:p2,isbbox?p2:p3,p4,isbbox)) {err="Error conversion of coordinates to region"; goto fail;} // Conversion failed
return r; // Case 4 with all parameters
}
}
fail:
*issuc=false;

Loading…
Cancel
Save