Browse Source

Gmt module: Function DrawFrame now using new parameter reading scheme and error reporting

gmtdatadir
Michael Uleysky 8 years ago
parent
commit
6a70f5ffbf
  1. 254
      modules/gmt/modgmt_func.cpp
  2. 4
      modules/gmt/modgmt_func.h

254
modules/gmt/modgmt_func.cpp

@ -129,7 +129,6 @@ const ObjectBase* GMT_LayerShift(const ObjectList* input)
return new ObjectError("LayerShift",err); return new ObjectError("LayerShift",err);
} }
// Draw frame with tics // Draw frame with tics
/* /*
Input: pairs list. Input: pairs list.
@ -147,169 +146,146 @@ framepen - pen for drawing frame. Default is 0.35,black.
ticklen - lenght of tick-marks in cm. Default is 0.18. ticklen - lenght of tick-marks in cm. Default is 0.18.
tickpen - pen for drawing ticks. Default is 0.3,black. tickpen - pen for drawing ticks. Default is 0.3,black.
trans or transparency - transparency level, double from 0 to 100. Default is 0 (opaque). trans or transparency - transparency level, double from 0 to 100. Default is 0 (opaque).
transpmodel - transparency model, string. Choose from Color, ColorBurn, ColorDodge, Darken, Difference, Exclusion, HardLight, Hue, Lighten, Luminosity, Multiply, Normal, Overlay, Saturation, SoftLight, and Screen. Default is Normal. model - transparency model, string. Choose from Color, ColorBurn, ColorDodge, Darken, Difference, Exclusion, HardLight, Hue, Lighten, Luminosity, Multiply, Normal, Overlay, Saturation, SoftLight, and Screen. Default is Normal.
x, xr or xrel - shift layer on x cm in horisontal direction. Default is 0. rx or relx - shift layer on x cm in horisontal direction. Default is 0.
y, yr or yrel - shift layer on y cm in vertical direction. Default is 0. ry or rely - shift layer on y cm in vertical direction. Default is 0.
*/ */
template<class O>
using SConvertor=Convert2Struct<std::string, O>;
template<class O>
using DConvertor=Convert2Struct<double, O>;
const ObjectBase* GMT_DrawFrame(const ObjectList* input) const ObjectBase* GMT_DrawFrame(const ObjectList* input)
{ {
std::string opts="-P -O -K "; struct XYIntVal // Representing intervals for tics intervals
bool suc=true; {
struct gmt_layer layer; bool isauto; double val;
// Defaults XYIntVal():isauto(true),val(0.0) {} // Default is auto
std::string defxtics="down", defytics="left", defint="auto", defdomain="pos", deftranspmodel="Normal"; };
bool defmark=false;
gmt_font deffont; deffont.Convert("12,Times-Roman,black");
double defoffset=0.18, defframewidth=0.1, defticklen=0.18;
gmt_pen defframepen, deftickpen; defframepen.Convert("0.35,black"); deftickpen.Convert("0.3,black");
{ // Get projection
BaseMT2Projection proj(input,"proj","projection");
layer.proj=proj(&suc);
if(!suc) goto fail;
opts+=layer.proj.Value()+" ";
}
{ // Get xint and yint class Base2TInt // Custom convertor class for xy tics intervals
BaseMD2String xint(input,defint,"xint"), yint(input,defint,"yint"); {
double dx,dy; public:
std::string str; using ValueType=struct XYIntVal;
str=xint(&suc); ValueType Convert(const ObjectBase* ob, bool* res, std::string& err)
if(suc && "auto"==str) dx=AutoStep(layer.proj.region.xb,layer.proj.region.xe);
else // No auto, get numeric value
{ {
Base2Double xint(input,"xint"); ValueType ret;
suc=true; ret.isauto=false;
dx=xint(&suc);
if(!suc) goto fail; // Parameter exists, but conversion failed
}
str=yint(&suc); OBTypeM<SConvertor,ObjectString> sp(ob);
if(suc && "auto"==str) dy=AutoStep(layer.proj.region.yb,layer.proj.region.ye); OBTypeM<DConvertor,ObjectReal,ObjectInt,ObjectString> dp(ob);
else // No auto, get numeric value // Check string "auto"
if(sp.Error()==OBTypeErr::OK)
{ {
Base2Double yint(input,"yint"); std::string s=sp(res,std::ref(err));
suc=true; if(!res) {err="Unknown error"; goto fail;} // Impossible case
dy=yint(&suc); tolower(s);
if(!suc) goto fail; // Parameter exists, but conversion failed if("auto"==s) {ret.isauto=true; return ret;}
} }
opts+="-Bx"+ToString(dx)+" -By"+ToString(dy)+" "; // Check double value
switch(dp.Error())
{
case(OBTypeErr::OK):
{
ret.val=dp(res,std::ref(err));
if(ret.val<=0.0) {err="Tics interval must be greater the zero"; break;}
return ret;
};
case(OBTypeErr::NULLPTR): {err="Can't convert zero ObjectBase pointer to something meaningfull"; break;}
case(OBTypeErr::TYPEMISMATCH): {err="Can't convert "+ob->Type()+" to double: type mismatch"; break;}
}
fail:
*res=false;
return ret;
} }
};
{ // Get xtics and ytics std::string err,fakerr;
BaseMD2String x(input,defxtics,"xtics"), y(input,defytics,"ytics"); std::string opts="-P -O -K ";
std::string xtics,ytics;
std::string xaxis,yaxis;
xtics=x(&suc); struct gmt_layer layer;
if(!suc) goto fail; // Conversion failed
if("none"==xtics) xaxis="sn";
if("down"==xtics) xaxis="Sn";
if("up"==xtics) xaxis="sN";
if("both"==xtics) xaxis="SN";
ytics=y(&suc); struct gmt_font deffont;
if(!suc) goto fail; // Conversion failed struct gmt_pen defframepen,deftickpen;
if("none"==ytics) yaxis="we"; deffont.Convert("12,Times-Roman,black",fakerr);
if("left"==ytics) yaxis="We"; defframepen.Convert("0.35,black",fakerr);
if("right"==ytics) yaxis="wE"; deftickpen.Convert("0.3,black",fakerr);
if("both"==ytics) yaxis="WE";
RNFPar<Base2Proj,ObjectGMTProjection> proj("projection");
ONPar<Base2TInt> xint("x[ ]interval"), yint("y[ ]interval");
ONPar<Base2StringD> xtics("x[ ]tics[ ]position","down"), ytics("y[ ]t[ics[ ]position","left");
ONPar<Base2StringD> domain("domain","pos");
ONPar<Base2BoolD> mark("mark",false);
ONPar<Base2FontD> font("font",deffont);
ONPar<Base2DoubleD> offset("offset",0.18);
ONPar<Base2NonNegD> framewidth("f[rame][ ]width",0.1);
ONPar<Base2PenD> framepen("f[rame][ ]pen",defframepen), tickpen("t[icks][ ]pen",deftickpen);
ONPar<Base2DoubleD> ticklen("t[icks][ ]length",0.18);
ONPar<Base2TransD> transp("transparency",0.0);
ONPar<Base2StringD> transpmodel("model","Normal");
ONPar<Base2DoubleD> rx("r[el]x",0.0),ry("r[el]y",0.0);
ParseNamedParameters params(input,proj,xint,yint,xtics,ytics,domain,mark,font,offset,framewidth,framepen,tickpen,ticklen,transp,transpmodel,rx,ry); if(!params) {err=params.Error(); goto fail;} // Error parsing parameters
layer.proj=proj; // Get projection
opts+=layer.proj.Value()+" ";
if(xaxis.empty() || yaxis.empty()) goto fail; // Unknown value of xtics and/or ytics { // Get tics intervals
XYIntVal dx=xint,dy=yint;
opts+="-Bx"+ToString(dx.isauto?AutoStep(layer.proj.region.xb,layer.proj.region.xe):dx.val)+" -By"+ToString(dy.isauto?AutoStep(layer.proj.region.yb,layer.proj.region.ye):dy.val)+" ";
}
{ // Get xtics and ytics positions
std::string xt=xtics, yt=ytics;
std::string xaxis, yaxis;
TemplateComparator none("none"), both("both");
TemplateComparator up("up"), down("down");
TemplateComparator left("left"), right("right");
tolower(xt); tolower(yt);
if(none.Compare(xt)) xaxis="sn";
if(down.Compare(xt)) xaxis="Sn";
if( up.Compare(xt)) xaxis="sN";
if(both.Compare(xt)) xaxis="SN";
if( none.Compare(yt)) yaxis="we";
if( left.Compare(yt)) yaxis="We";
if(right.Compare(yt)) yaxis="wE";
if( both.Compare(yt)) yaxis="WE";
if(xaxis.empty()) {err="Incorrect xtics position "+xtics.Value(); goto fail;} // Unknown value of xtics
if(yaxis.empty()) {err="Incorrect xtics position "+ytics.Value(); goto fail;} // Unknown value of ytics
opts+="-B"+xaxis+yaxis+" "; opts+="-B"+xaxis+yaxis+" ";
} }
{ // Get domain and mark { // Get domain and mark
BaseMD2String d(input,defdomain,"domain"); std::string dom=domain;
BaseMD2Bool m(input,defmark,"mark");
std::string format; std::string format;
TemplateComparator p("positive"),n("negative"),c("centered");
tolower(dom);
std::string domain=d(&suc); if(p.Compare(dom)) format="+ddd:mm:ss";
if(!suc) goto fail; // Conversion failed if(n.Compare(dom)) format="-ddd:mm:ss";
if("pos"==domain) format="+ddd:mm:ss"; if(c.Compare(dom)) format="ddd:mm:ss";
if("neg"==domain) format="-ddd:mm:ss"; if(format.empty()) {err="Incorrect domain value "+domain.Value(); goto fail;} // Unknown domain
if("center"==domain) format="ddd:mm:ss";
if(format.empty()) goto fail; // Unknown domain
if(m(&suc)) format+="F";
if(!suc) goto fail; // Conversion failed
if(mark) format+="F";
opts+="--FORMAT_GEO_MAP="+format+" "; opts+="--FORMAT_GEO_MAP="+format+" ";
} }
{ // Get font opts+="--FONT_ANNOT_PRIMARY="+font.Value().Value()+" "; // Get font
BaseMD2Font f(input,deffont,"font"); opts+="--MAP_ANNOT_OFFSET_PRIMARY="+ToString(offset.Value())+"c "; // Get offset
gmt_font font; opts+="--MAP_FRAME_WIDTH="+ToString(framewidth.Value())+"c "; // Get framewidth
font=f(&suc); opts+="--MAP_FRAME_PEN="+framepen.Value().Value()+" "; // Get framepen
if(!suc) goto fail; // Conversion failed opts+="--MAP_TICK_LENGTH_PRIMARY="+ToString(ticklen.Value())+"c "; // Get ticklen
opts+="--FONT_ANNOT_PRIMARY="+font.Value()+" "; opts+="--MAP_TICK_PEN_PRIMARY="+tickpen.Value().Value()+" "; // Get tickpen
} if(transp!=0) opts+="-t"+ToString(transp.Value())+" "; // Get transparency
opts+="--PS_TRANSPARENCY="+transpmodel.Value()+" "; // Get transparency model
{ // Get offset // Get shift
BaseMD2Double o(input,defoffset,"offset"); layer.shiftx=rx;
double offset; layer.shifty=ry;
offset=o(&suc);
if(!suc) goto fail; // Conversion failed
opts+="--MAP_ANNOT_OFFSET_PRIMARY="+ToString(offset)+"c ";
}
{ // Get framewidth
SearchParameterWDef<double,false,PMin<0> > f(input,defframewidth,"framewidth");
double framewidth;
framewidth=f(&suc);
if(!suc) goto fail; // Conversion failed
opts+="--MAP_FRAME_WIDTH="+ToString(framewidth)+"c ";
}
{ // Get framepen
BaseMD2Pen p(input,defframepen,"framepen");
gmt_pen framepen;
framepen=p(&suc);
if(!suc) goto fail; // Conversion failed
opts+="--MAP_FRAME_PEN="+framepen.Value()+" ";
}
{ // Get ticklen
BaseMD2Double t(input,defticklen,"ticklen","ticklength");
double ticklen;
ticklen=t(&suc);
if(!suc) goto fail; // Conversion failed
opts+="--MAP_TICK_LENGTH_PRIMARY="+ToString(ticklen)+"c ";
}
{ // Get tickpen
BaseMD2Pen t(input,deftickpen,"tickpen");
gmt_pen tickpen;
tickpen=t(&suc);
if(!suc) goto fail; // Conversion failed
opts+="--MAP_TICK_PEN_PRIMARY="+tickpen.Value()+" ";
}
{ // Get transparency
Base2Transp t(input,"trans","transp","transparency");
double transp;
transp=t(&suc);
if(!suc) goto fail; // Conversion failed
if(transp!=0) opts+="-t"+ToString(transp)+" ";
}
{ // Get transpmodel
BaseMD2String tm(input,deftranspmodel,"transpmodel","transparencymodel");
std::string transpmodel;
transpmodel=tm(&suc);
if(!suc) goto fail; // Conversion failed
opts+="--PS_TRANSPARENCY="+transpmodel+" ";
}
{ // Get x, y
BaseMD2Double x(input,0.0,"x","xr","xrel"), y(input,0.0,"y","yr","yrel");
layer.shiftx=x(&suc);
layer.shifty=y(&suc);
if(!suc) goto fail; // Conversion failed
}
{ // Calling psbasemap { // Calling psbasemap
void* gmtapi; void* gmtapi;
@ -328,5 +304,5 @@ const ObjectBase* GMT_DrawFrame(const ObjectList* input)
return new ObjectGMTLayer(layer); return new ObjectGMTLayer(layer);
fail: fail:
return 0; return new ObjectError("DrawFrame",err);
} }

4
modules/gmt/modgmt_func.h

@ -168,6 +168,7 @@ template<class Struct>
using Base2StructD=DefaultConverter<typename TypeStorage<Struct>::Converter>; using Base2StructD=DefaultConverter<typename TypeStorage<Struct>::Converter>;
// Simple aliases of convertors // Simple aliases of convertors
using Base2Bool =Base2Struct<bool>;
using Base2Double=Base2Struct<double>; using Base2Double=Base2Struct<double>;
using Base2String=Base2Struct<std::string>; using Base2String=Base2Struct<std::string>;
using Base2Coord =Base2Struct<struct gmt_coord>; using Base2Coord =Base2Struct<struct gmt_coord>;
@ -182,6 +183,9 @@ using Base2Layer =Base2Struct<struct gmt_layer>;
// Simple aliases of convertors with default values // Simple aliases of convertors with default values
using Base2StringD=Base2StructD<std::string>; using Base2StringD=Base2StructD<std::string>;
using Base2DoubleD=Base2StructD<double>; using Base2DoubleD=Base2StructD<double>;
using Base2BoolD =Base2StructD<bool>;
using Base2PenD =Base2StructD<struct gmt_pen>;
using Base2FontD =Base2StructD<struct gmt_font>;
// Specialised convertors // Specialised convertors
// Convertor with default value for gmt_coord. Added constructor for creating default value from double argument // Convertor with default value for gmt_coord. Added constructor for creating default value from double argument

Loading…
Cancel
Save