1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
|
/***********************************************/
/**
* @file plotMapLayer.cpp
*
* @brief plot layers of maps.
*
* @author Andreas Kvas
* @author Torsten Mayer-Guerr
* @author Sebastian Strasser
* @date 2015-10-23
*
*/
/***********************************************/
#define DOCSTRING_PlotMapLayer
#include "base/import.h"
#include "parser/dataVariables.h"
#include "config/configRegister.h"
#include "inputOutput/system.h"
#include "files/filePolygon.h"
#include "files/fileGriddedData.h"
#include "classes/grid/grid.h"
#include "misc/miscGriddedData.h"
#include "plot/plotMisc.h"
#include "plotMapLayer.h"
/***********************************************/
// Latex documentation
static const char *docstringPlotMapLayerGrid = R"(
\subsection{GriddedData}
Creates a regular grid of yxz values. The standard \reference{dataVariables}{general.parser:dataVariables}
are available to select the data column of \configFile{inputfileGriddedData}{griddedData}.
Empty grid cells are not plotted. Cells with more than one value will be set to the mean value.
The grid spacing can be determined automatically for regular rectangular grids otherwise
it must be set with \config{increment}. To get a better display together with some projections
the grid should be internally \config{resample}d to higher resolution.
It is assumed that the points of \configFile{inputfileGriddedData}{griddedData} represents centers of grid cells.
This assumption can be changed with \config{gridlineRegistered} (e.g if the data starts at the north pole).
)";
class PlotMapLayerGrid : public PlotMapLayer
{
Angle incrementLat, incrementLon;
Bool illuminate;
Bool isGridline;
Double intermediateDpi, threshold;
Char interpolationMethod;
Bool resample;
public:
PlotMapLayerGrid(Config &config);
Bool requiresColorBar() const override {return TRUE;}
std::string scriptEntry() const override;
};
/***********************************************/
PlotMapLayerGrid::PlotMapLayerGrid(Config &config)
{
try
{
FileName fileNameGrid;
ExpressionVariablePtr exprValue;
std::string choice;
resample = FALSE;
readConfig(config, "inputfileGriddedData", fileNameGrid, Config::MUSTSET, "", "");
readConfig(config, "value", exprValue, Config::MUSTSET, "data0", "expression to compute values (input columns are named data0, data1, ...)");
readConfig(config, "increment", incrementLon, Config::OPTIONAL, "", "the grid spacing [degrees]");
readConfig(config, "illuminate", illuminate, Config::DEFAULT, "0", "illuminate grid");
if(readConfigSequence(config, "resample", Config::OPTIONAL, "", ""))
{
resample = TRUE;
readConfig(config, "intermediateDpi", intermediateDpi, Config::DEFAULT, "100", "oversample grid for a smoother visual effect");
if(readConfigChoice(config, "interpolationMethod", choice, Config::MUSTSET, "", "interpolation method for oversampling"))
{
if(readConfigChoiceElement(config, "bspline", choice, "B-Spline interpolation")) interpolationMethod = 'b';
if(readConfigChoiceElement(config, "bicubic", choice, "bicubic interpolation")) interpolationMethod = 'c';
if(readConfigChoiceElement(config, "bilinear", choice, "bilinear interpolation")) interpolationMethod = 'l';
if(readConfigChoiceElement(config, "nearest", choice, "nearest neighbour interpolation")) interpolationMethod = 'n';
endChoice(config);
}
readConfig(config, "threshold", threshold, Config::DEFAULT, "0.5", "A threshold of 1.0 requires all (4 or 16) nodes involved in interpolation to be non-NaN. 0.5 will interpolate about half way from a non-NaN value; 0.1 will go about 90% of the way.");
endSequence(config);
}
readConfig(config, "gridlineRegistered", isGridline, Config::DEFAULT, "0", "treat input as point values instead of cell means");
if(isCreateSchema(config)) return;
GriddedData grid;
readFileGriddedData(fileNameGrid, grid);
points = grid.points;
areas = grid.areas;
if(grid.values.size()==0)
throw(Exception("<"+fileNameGrid.str()+"> has no values."));
// try to define grid spacing
// --------------------------
incrementLat = incrementLon;
if(incrementLon <= 0)
{
std::vector<Angle> lambda, phi;
std::vector<Double> radius;
if(!grid.isRectangle(lambda, phi, radius))
throw(Exception("'increment' must be set for non rectangular grids"));
Vector dLambda(lambda.size()-1);
for(UInt k=0; k<dLambda.size(); k++)
dLambda(k) = std::fabs(std::remainder(lambda.at(k+1)-lambda.at(k), 2*PI));
incrementLon = mean(dLambda);
Angle lon;
Double h;
for(UInt i=0; i<phi.size(); i++)
grid.ellipsoid(polar(Angle(0.), phi.at(i), radius.at(i)), lon, phi.at(i), h); // geocentric -> ellipsoidal
Vector dPhi(phi.size()-1);
for(UInt i=0; i<dPhi.size(); i++)
dPhi(i) = std::fabs(phi.at(i+1)-phi.at(i));
incrementLat = mean(dPhi);
}
// evaluate expression
// -------------------
VariableList varList;
addDataVariables(grid, varList);
exprValue->simplify(varList);
data = Matrix(points.size(), 1);
for(UInt i=0; i<points.size(); i++)
{
evaluateDataVariables(grid, i, varList);
data(i, 0) = exprValue->evaluate(varList);
}
if(!isGridline)
{
bufferLon = 0.5 * incrementLon;
bufferLat = 0.5 * incrementLat;
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayerGrid::scriptEntry() const
{
try
{
std::stringstream ss;
ss<<"gmt xyz2grd -bi3d "<<dataFileName<<" -G"<<dataFileName<<".grd -Vn -I"<<incrementLon*RAD2DEG*3600.<<"s/"<<incrementLat*RAD2DEG*3600.<<"s -R"<<PlotBasics::scriptVariable("region")<<(isGridline ? "" : " -r")<<std::endl;
if(illuminate)
ss<<"gmt grdgradient "<<dataFileName<<".grd -Nt0.8 -A45/315 -G"<<dataFileName<<".intense.grd"<<std::endl;
ss<<"gmt grdimage "<<dataFileName<<".grd";
if(resample)
ss<<" -E"<<intermediateDpi<<" -n"<<interpolationMethod<<"+bg+t"<<threshold;
ss<<" -Q -J -R -B -CgroopsPlot.cpt";
if(illuminate)
ss<<" -I"<<dataFileName<<".intense.grd";
ss<<" -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
// Latex documentation
static const char *docstringPlotMapLayerPoints = R"(
\subsection{Points}\label{plotMapLayerType:points}
Draws points (\configClass{symbol}{plotSymbolType}) and/or \configClass{line}{plotLineType}s
between the points. If no \configClass{color}{plotColorType} of the \configClass{symbol}{plotSymbolType}
is given a \configClass{colorbar}{plotColorbarType} is required and the color is determined
by the \config{value} expression. The standard \reference{dataVariables}{general.parser:dataVariables}
are available to select the data column of \configFile{inputfileGriddedData}{griddedData}.
)";
class PlotMapLayerPoints : public PlotMapLayer
{
PlotLinePtr line;
PlotSymbolPtr symbol;
Bool greatCircle;
public:
PlotMapLayerPoints(Config &config);
Bool requiresColorBar() const override {return data.columns();}
std::string scriptEntry() const override;
};
/***********************************************/
PlotMapLayerPoints::PlotMapLayerPoints(Config &config)
{
try
{
FileName fileNameGrid;
ExpressionVariablePtr exprValue;
readConfig(config, "inputfileGriddedData", fileNameGrid, Config::MUSTSET, "", "");
readConfig(config, "value", exprValue, Config::OPTIONAL, "data0", "expression to compute color (input columns are named data0, data1, ...)");
readConfig(config, "symbol", symbol, Config::OPTIONAL, "1", "");
readConfig(config, "line", line, Config::OPTIONAL, "", "style of connecting lines");
readConfig(config, "drawLineAsGreatCircle",greatCircle, Config::DEFAULT, "1", "draw connecting lines as great circles (otherwise, a straight line is drawn instead)");
if(isCreateSchema(config)) return;
// tests
if(!line && !symbol)
throw(Exception("At least one of line and symbol must be set."));
if(symbol && symbol->requiresColorBar() && !exprValue)
throw(Exception("value is needed to determine color of line/symbol"));
if(!symbol || !symbol->requiresColorBar())
exprValue = nullptr;
GriddedData grid;
readFileGriddedData(fileNameGrid, grid);
points = grid.points;
// evaluate expression
// -------------------
if(exprValue)
{
VariableList varList;
addDataVariables(grid, varList);
exprValue->simplify(varList);
data = Matrix(points.size(), 1);
for(UInt i=0; i<points.size(); i++)
{
evaluateDataVariables(grid, i, varList);
data(i, 0) = exprValue->evaluate(varList);
}
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayerPoints::scriptEntry() const
{
try
{
std::stringstream ss;
if(line)
{
ss<<"gmt psxy -bi"<<2+data.columns()<<"d "<<dataFileName<<" -J -R -W"<<line->str();
if(!greatCircle) ss<<" -A";
ss<<" -O -K >> groopsPlot.ps"<<std::endl;
}
if(symbol)
ss<<"gmt psxy -bi"<<2+data.columns()<<"d "<<dataFileName<<" -J -R -S"<<symbol->str()<<" -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
static const char *docstringPlotMapLayerArrows = R"(
\subsection{Arrows}
Draws an arrow for each point in \configFile{inputfileGriddedData}{griddedData}.
The arrows are defined by the expressions \config{valueNorth/East}.
The standard \reference{dataVariables}{general.parser:dataVariables}
are available to select the correspondent data columns of \configFile{inputfileGriddedData}{griddedData}.
The \config{scale} factor converts the input units to cm in the plot.
If no \configClass{color}{plotColorType} is given a \configClass{colorbar}{plotColorbarType} is required
and the color is determined by the \config{value} expression.
With \config{scaleArrow} a reference arrow as legend can be plotted inside or outside the map.
)";
class PlotMapLayerArrows : public PlotMapLayer
{
Double scale;
Double penSize;
Double headSize;
PlotColorPtr penColor;
Double scaleArrowOriginX;
Double scaleArrowOriginY;
Double scaleArrowLength;
std::string scaleArrowUnit;
std::string scaleArrowLabel;
Bool drawScaleArrow;
Bool hasZValues;
public:
PlotMapLayerArrows(Config &config);
Bool requiresColorBar() const override {return hasZValues;}
std::string scriptEntry() const override;
std::string legendEntry(const FileName &workingDirectory, UInt idxLayer) const override;
};
/***********************************************/
PlotMapLayerArrows::PlotMapLayerArrows(Config &config)
{
try
{
drawScaleArrow = FALSE;
FileName fileNameGrid;
ExpressionVariablePtr exprValueNorth, exprValueEast, exprValue;
readConfig(config, "inputfileGriddedData", fileNameGrid, Config::MUSTSET, "", "grid file with north and east values for arrows");
readConfig(config, "valueNorth", exprValueNorth, Config::MUSTSET, "data0", "expression to compute north values (input columns are named data0, data1, ...)");
readConfig(config, "valueEast", exprValueEast, Config::MUSTSET, "data1", "expression to compute east values (input columns are named data0, data1, ...)");
readConfig(config, "value", exprValue, Config::OPTIONAL, "", "expression to compute arrow color (input columns are named data0, data1, ...)");
readConfig(config, "scale", scale, Config::DEFAULT, "50", "[cm per input unit] length scale factor");
readConfig(config, "penSize", penSize, Config::MUSTSET, "1", "[pt] width of arrow shaft");
readConfig(config, "headSize", headSize, Config::MUSTSET, "5", "[pt] size of arrow head, 0: no head, negative: reverse head");
readConfig(config, "color", penColor, Config::OPTIONAL, "", "empty: from value");
if(readConfigSequence(config, "scaleArrow", Config::OPTIONAL, "1", "draw an arrow for scale reference"))
{
readConfig(config, "originX", scaleArrowOriginX, Config::MUSTSET, "0.055", "[0-1] 0: left, 1: right");
readConfig(config, "originY", scaleArrowOriginY, Config::MUSTSET, "0.065", "[0-1] 0: bottom, 1: top");
readConfig(config, "length", scaleArrowLength, Config::MUSTSET, "", "in same unit as valueNorth and valueEast");
readConfig(config, "unit", scaleArrowUnit, Config::MUSTSET, "", "displayed unit text (e.g. 1 cm)");
readConfig(config, "label", scaleArrowLabel, Config::OPTIONAL, "", "description of the arrows");
endSequence(config);
drawScaleArrow = TRUE;
}
if(isCreateSchema(config)) return;
// tests
hasZValues = !penColor && exprValue;
if(!penColor && !exprValue)
throw(Exception("value is needed to determine color of arrows"));
if(!hasZValues)
exprValue = nullptr;
// read data
GriddedData grid;
readFileGriddedData(fileNameGrid, grid);
points = grid.points;
// create data variables
// ---------------------
VariableList varList;
addDataVariables(grid, varList);
std::vector<ExpressionVariablePtr> expressions = {exprValue, exprValueNorth, exprValueEast};
for(ExpressionVariablePtr expr : expressions)
if(expr) expr->simplify(varList);
// evaluate expressions
// --------------------
data = Matrix(points.size(), exprValue ? 3 : 2);
for(UInt i=0; i<points.size(); i++)
{
evaluateDataVariables(grid, i, varList);
UInt idx = 0;
for(auto expression : expressions)
if(expression)
data(i, idx++) = expression->evaluate(varList);
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayerArrows::scriptEntry() const
{
try
{
std::stringstream ss;
// arrows from grid file
ss<<"gmt psxy "<<dataFileName<<" -bi"<<2+data.columns()<<"d -J -R -A -SV"<<headSize<<"p+ea+z"<<scale<<"c";
if(hasZValues)
ss<<" -W"<<penSize<<"p+cl -CgroopsPlot.cpt";
else
ss<<" -W"<<penSize<<"p,"<<penColor->str()<<" -G"<<penColor->str();
ss<<" -O -K >> groopsPlot.ps"<<std::endl;
if(drawScaleArrow)
{
// scale arrow
ss<<"echo "<<scaleArrowOriginX<<" "<<scaleArrowOriginY<<" 90 "<<scale*scaleArrowLength<<"c";
ss<<" | gmt psxy -JX"<<PlotBasics::scriptVariable("width")<<"/"<<PlotBasics::scriptVariable("height")<<" -R0/1/0/1 -A -SV"<<headSize<<"p+ea+jc";
if(penColor)
ss<<" -W"<<penSize<<"p,"<<penColor->str()<<" -G"<<penColor->str();
else
ss<<" -W"<<penSize<<"p,black -Gblack";
ss<<" -O -K >> groopsPlot.ps"<<std::endl;
// scale arrow labels
ss<<"echo "<<scaleArrowOriginX<<" "<<scaleArrowOriginY<<" CT "<<scaleArrowUnit<<" | gmt pstext -F+j -Dj4p -J -R -N -O -K >> groopsPlot.ps"<<std::endl;
if(!scaleArrowLabel.empty())
ss<<"echo "<<scaleArrowOriginX<<" "<<scaleArrowOriginY<<" CB "<<scaleArrowLabel<<" | gmt pstext -F+j -Dj4p -J -R -N -O -K >> groopsPlot.ps"<<std::endl;
ss<<"gmt psbasemap -J"<<PlotBasics::scriptVariable("projection")<<" -R"<<PlotBasics::scriptVariable("region")<<" -B -O -K >> groopsPlot.ps"<<std::endl;
}
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayerArrows::legendEntry(const FileName &workingDirectory, UInt idxLayer) const
{
try
{
if(!drawScaleArrow)
return "";
FileName arrowDataFileName("arrow"+idxLayer%"%i.txt"s);
FileName arrowLabelFileName("arrowLabel"+idxLayer%"%i.txt"s);
std::stringstream ss;
// scale arrow
OutFile arrowFile(workingDirectory.append(arrowDataFileName));
arrowFile<<scaleArrowOriginX<<" "<<scaleArrowOriginY<<" 90 "<<scale*scaleArrowLength<<"c";
ss<<"gmt psxy "<<arrowDataFileName<<" -JX"<<PlotBasics::scriptVariable("width")<<"/"<<PlotBasics::scriptVariable("height")<<" -R0/1/0/1 -A -SV"<<headSize<<"p+ea+jc";
if(penColor)
ss<<" -W"<<penSize<<"p,"<<penColor->str()<<" -G"<<penColor->str();
else
ss<<" -W"<<penSize<<"p,black -Gblack";
ss<<" -O -K >> groopsPlot.ps"<<std::endl;
// scale arrow labels
OutFile arrowLabelFile(workingDirectory.append(arrowLabelFileName));
arrowLabelFile<<scaleArrowOriginX<<" "<<scaleArrowOriginY<<" CT "<<scaleArrowUnit;
if(!scaleArrowLabel.empty())
arrowLabelFile<<std::endl<<scaleArrowOriginX<<" "<<scaleArrowOriginY<<" CB "<<scaleArrowLabel;
ss<<"gmt pstext "<<arrowLabelFileName<<" -F+j -Dj4p -J -R -N -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
// Latex documentation
static const char *docstringPlotMapLayerPolygon = R"(
\subsection{Polygon}
Draws a \configFile{inputfilePolygon}{polygon}.
If \configClass{fillColor}{plotColorType} is not set and a \config{value}
is given the fill color is taken from a \configClass{colorbar}{plotColorbarType}.
)";
class PlotMapLayerPolygon : public PlotMapLayer
{
std::vector<Polygon> polygons;
PlotLinePtr line;
PlotColorPtr fillColor;
Double value;
Bool greatCircle;
public:
PlotMapLayerPolygon(Config &config);
void boundary(const Ellipsoid &ellipsoid, Angle &minL, Angle &maxL, Angle &minB, Angle &maxB) const override;
void writeDataFile(const Ellipsoid &ellipsoid, const FileName &workingDirectory, UInt idxLayer) override;
Bool requiresColorBar() const override {return !fillColor && !std::isnan(value);}
std::string scriptEntry() const override;
};
/***********************************************/
PlotMapLayerPolygon::PlotMapLayerPolygon(Config &config)
{
try
{
FileName fileName;
value = NAN_EXPR;
readConfig(config, "inputfilePolygon", fileName, Config::MUSTSET, "{groopsDataDir}/border/", "");
readConfig(config, "line", line, Config::OPTIONAL, "solid", "style of border lines");
readConfig(config, "fillColor", fillColor, Config::OPTIONAL, "", "polygon fill color (no fill color: determine from value if given, else: no fill)");
readConfig(config, "value", value, Config::OPTIONAL, "", "value to compute fill color from a colorbar (ignored if a fillColor is given)");
readConfig(config, "drawLineAsGreatCircle", greatCircle, Config::DEFAULT, "1", "draw connecting lines as great circles (otherwise, a straight line is drawn instead)");
if(isCreateSchema(config)) return;
// tests
if(!line && !(fillColor || !std::isnan(value)) )
throw(Exception("At least one of line, fillColor or value must be set."));
readFilePolygon(fileName, polygons);
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void PlotMapLayerPolygon::boundary(const Ellipsoid &/*ellipsoid*/, Angle &minL, Angle &maxL, Angle &minB, Angle &maxB) const
{
try
{
for(auto &p : polygons)
for(UInt i=0; i<p.L.size(); i++)
{
minL = std::min(minL, Angle(p.L(i)));
maxL = std::max(maxL, Angle(p.L(i)));
minB = std::min(minB, Angle(p.B(i)));
maxB = std::max(maxB, Angle(p.B(i)));
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void PlotMapLayerPolygon::writeDataFile(const Ellipsoid &/*ellipsoid*/, const FileName &workingDirectory, UInt idxLayer)
{
try
{
dataFileName = "polygon."+idxLayer%"%i.dat"s;
OutFile file(workingDirectory.append(dataFileName));
if(!std::isnan(value)) file<<">-Z"<<value<<std::endl;
for(auto &p : polygons)
{
for(UInt i=0; i<p.L.size(); i++)
{
file<<p.L(i)*RAD2DEG<<" "<<p.B(i)*RAD2DEG<<std::endl;
}
file<<">"<<std::endl;
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayerPolygon::scriptEntry() const
{
try
{
std::stringstream ss;
ss<<"gmt psxy "<<dataFileName<<" -L -J -R";
if(fillColor) ss<<" -G"<<fillColor->str();
else if(!std::isnan(value)) ss<<" -CgroopsPlot.cpt";
if(line)
{
ss<<" -W"<<line->str();
if(!greatCircle) ss<<" -A";
}
ss<<" -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
// Latex documentation
static const char *docstringPlotMapLayerCoast = R"(
\subsection{Coast}
Plots coastlines. GMT provides them in different \config{resolution}s.
Features with an area smaller than \config{minArea} in $km^2$ will not be plotted.
)";
class PlotMapLayerCoast : public PlotMapLayer
{
std::string resolution;
PlotLinePtr line;
PlotColorPtr landColor;
PlotColorPtr oceanColor;
UInt minArea;
public:
PlotMapLayerCoast(Config &config);
std::string scriptEntry() const override;
};
/***********************************************/
PlotMapLayerCoast::PlotMapLayerCoast(Config &config)
{
try
{
std::string choice;
if(readConfigChoice(config, "resolution", choice, Config::MUSTSET, "medium", ""))
{
if(readConfigChoiceElement(config, "crude", choice)) resolution = "c";
if(readConfigChoiceElement(config, "low", choice)) resolution = "l";
if(readConfigChoiceElement(config, "medium", choice)) resolution = "i";
if(readConfigChoiceElement(config, "high", choice)) resolution = "h";
if(readConfigChoiceElement(config, "full", choice)) resolution = "f";
endChoice(config);
}
readConfig(config, "line", line, Config::OPTIONAL, R"({"solid": {"width": "1"}})", "line style for coastlines");
readConfig(config, "landColor", landColor, Config::OPTIONAL, "", "fill land area");
readConfig(config, "oceanColor", oceanColor, Config::OPTIONAL, "", "fill ocean area");
readConfig(config, "minArea", minArea, Config::DEFAULT, "5000", "[km^2] features with a smaller area than this are dropped");
if(isCreateSchema(config)) return;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayerCoast::scriptEntry() const
{
try
{
std::stringstream ss;
ss<<"gmt pscoast -J -R -D"<<resolution<<"+ -A"<<minArea;
if(line)
ss<<" -W"<<line->str();
if(landColor)
ss<<" -G"<<landColor->str();
if(oceanColor)
ss<<" -S"<<oceanColor->str();
ss<<" -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
// Latex documentation
static const char *docstringPlotMapLayerRivers = R"(
\subsection{Rivers}
Plots rivers and lakes. GMT provides different classes
(\url{https://docs.generic-mapping-tools.org/latest/coast.html}).
)";
class PlotMapLayerRivers : public PlotMapLayer
{
std::vector<std::string> riverclass;
PlotLinePtr line;
public:
PlotMapLayerRivers(Config &config);
std::string scriptEntry() const override;
};
/***********************************************/
PlotMapLayerRivers::PlotMapLayerRivers(Config &config)
{
try
{
std::string choice;
if(readConfigChoice(config, "class", choice, Config::MUSTSET, "", ""))
{
if(readConfigChoiceElement(config, "riversCanalsLakes", choice, "")) riverclass.push_back("a");
if(readConfigChoiceElement(config, "riversCanals", choice, "")) riverclass.push_back("A");
if(readConfigChoiceElement(config, "permanentRiversLakes", choice, "")) riverclass.push_back("r");
if(readConfigChoiceElement(config, "permanentRivers", choice, "")) riverclass.push_back("R");
if(readConfigChoiceElement(config, "intermittentRivers", choice, "")) riverclass.push_back("i");
if(readConfigChoiceElement(config, "canals", choice, "")) riverclass.push_back("c");
if(readConfigChoiceElement(config, "singleClass", choice, ""))
{
std::vector<UInt> types;
readConfig(config, "class", types, Config::MUSTSET, "", "0-10. See GMT documentation");
for(UInt k=0; k<types.size(); k++)
riverclass.push_back(types.at(k)%"%i"s);
}
endChoice(config);
}
readConfig(config, "line", line, Config::MUSTSET, R"({"solid": {"width": "1"}})", "");
if(isCreateSchema(config)) return;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayerRivers::scriptEntry() const
{
try
{
std::stringstream ss;
ss<<"gmt pscoast -J -R";
for(UInt k=0; k<riverclass.size(); k++)
ss<<" -I"<<riverclass.at(k)<<"/"<<line->str();
ss<<" -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
// Latex documentation
static const char *docstringPlotMapLayerPolitical = R"(
\subsection{PoliticalBoundary}
Plots national boundaries. GMT provides them in different \config{resolution}s.
)";
class PlotMapLayerPolitical : public PlotMapLayer
{
std::string resolution;
PlotLinePtr line;
public:
PlotMapLayerPolitical(Config &config);
std::string scriptEntry() const override;
};
/***********************************************/
PlotMapLayerPolitical::PlotMapLayerPolitical(Config &config)
{
try
{
std::string choice;
if(readConfigChoice(config, "resolution", choice, Config::MUSTSET, "medium", ""))
{
if(readConfigChoiceElement(config, "crude", choice, "")) resolution = "c";
if(readConfigChoiceElement(config, "low", choice, "")) resolution = "l";
if(readConfigChoiceElement(config, "medium", choice, "")) resolution = "i";
if(readConfigChoiceElement(config, "high", choice, "")) resolution = "h";
if(readConfigChoiceElement(config, "full", choice, "")) resolution = "f";
endChoice(config);
}
readConfig(config, "line", line, Config::MUSTSET, R"({"solid": {"width": "0.25"}})", "");
if(isCreateSchema(config)) return;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayerPolitical::scriptEntry() const
{
try
{
std::stringstream ss;
ss<<"gmt pscoast -J -R -N1/"<<line->str()<<" -D"<<resolution<<"+ -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
// Latex documentation
static const char *docstringPlotMapLayerBlueMarble = R"(
\subsection{BlueMarble}
An image of the Earth's surface as seen from outer space -
the image is known as \emph{blue marble}. The directory of \config{inputfileChannels}
contains several files in different resolutions representing the Earth's surface each
month throughout a year.
\fig{!hb}{0.8}{blueMarble}{fig:blueMarbleMap}{The blue marble.}
)";
class PlotMapLayerBlueMarble : public PlotMapLayer
{
FileName fileNameImage;
Double brightness;
Bool illuminate;
FileName fileNameTopography;
Angle azimuth, elevation;
Double amplitude;
Double ambient, diffuse, specular, shine;
public:
PlotMapLayerBlueMarble(Config &config);
void writeDataFile(const Ellipsoid &ellipsoid, const FileName &workingDirectory, UInt idxLayer) override;
std::string scriptEntry() const override;
};
/***********************************************/
PlotMapLayerBlueMarble::PlotMapLayerBlueMarble(Config &config)
{
try
{
readConfig(config, "inputfileImage", fileNameImage, Config::MUSTSET, "{groopsDataDir}/plot/bluemarble/1800x900/bluemarble.05.jpg", "Blue Marble image file");
readConfig(config, "brightness", brightness, Config::DEFAULT, "0.0", "brightness of bitmap [-1, 1]");
if(readConfigSequence(config, "illuminate", Config::OPTIONAL, "", "add hillshade based on topography"))
{
readConfig(config, "inputfileTopography", fileNameTopography, Config::MUSTSET, "{groopsDataDir}/plot/bluemarble/1800x900/bluemarble.topography.grd", "GMT grid file containing topography.");
readConfig(config, "azimuth", azimuth, Config::DEFAULT, "315", "direction of lighting source [deg]");
readConfig(config, "elevation", elevation, Config::DEFAULT, "45", "direction of lighting source [deg]");
readConfig(config, "ambient", ambient, Config::DEFAULT, "0.1", "ambient lighting");
readConfig(config, "diffuse", diffuse, Config::DEFAULT, "0.6", "diffuse lighting");
readConfig(config, "specular", specular, Config::DEFAULT, "0.0", "specular reflection");
readConfig(config, "shine", shine, Config::DEFAULT, "0.0", "surface shine");
readConfig(config, "amplitude", amplitude, Config::DEFAULT, "0.1", "scale gradient by factor");
endSequence(config);
}
if(isCreateSchema(config)) return;
illuminate = !fileNameTopography.empty();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void PlotMapLayerBlueMarble::writeDataFile(const Ellipsoid &/*ellipsoid*/, const FileName &/*workingDirectory*/, UInt idxLayer)
{
dataFileName = "blueMarble."+idxLayer%"%i.dat"s;
}
/***********************************************/
std::string PlotMapLayerBlueMarble::scriptEntry() const
{
try
{
std::stringstream ss;
if(illuminate)
{
ss<<"gmt grdgradient "<<fileNameTopography<<" -G"<<dataFileName;
ss<<" -Em"<<azimuth*RAD2DEG<<"/"<<elevation*RAD2DEG<<"+a"<<ambient<<"+d"<<diffuse<<"+p"<<specular<<"+s"<<shine<<std::endl;
ss<<"gmt grdmath "<<dataFileName<<" "<<amplitude<<" MUL = "<<dataFileName<<std::endl;
}
ss<<"gmt grdimage "<<fileNameImage;
if(!illuminate)
ss<<" -I"<<brightness;
else
ss<<" -I"<<dataFileName;
ss<<" -Q -J -R -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
// Latex documentation
static const char *docstringPlotMapLayerText = R"(
\subsection{Text}
Writes a \config{text} at \config{originLongitude} and \config{originLatitude} position in the map.
With \config{clip} the text is cutted at the boundaries of the plotting area.
)";
class PlotMapLayerText : public PlotMapLayer
{
Angle lat, lon;
Double xOffset, yOffset;
Double fontSize;
PlotColorPtr fontColor;
std::string text;
std::string alignment;
Bool clip;
public:
PlotMapLayerText(Config &config);
void writeDataFile(const Ellipsoid &ellipsoid, const FileName &workingDirectory, UInt idxLayer) override;
std::string scriptEntry() const override;
};
/***********************************************/
PlotMapLayerText::PlotMapLayerText(Config &config)
{
try
{
readConfig(config, "text", text, Config::MUSTSET, "", "");
readConfig(config, "originLongitude", lon, Config::MUSTSET, "", "[deg]");
readConfig(config, "originLatitude", lat, Config::MUSTSET, "", "[deg]");
readConfig(config, "offsetX", xOffset, Config::DEFAULT, "0", "[cm] x-offset from origin");
readConfig(config, "offsetY", yOffset, Config::DEFAULT, "0", "[cm] y-offset from origin");
readConfig(config, "alignment", alignment, Config::DEFAULT, "LB", "L, C, R (left, center, right) and T, M, B (top, middle, bottom)");
readConfig(config, "fontSize", fontSize, Config::DEFAULT, "10", "");
readConfig(config, "fontColor", fontColor, Config::MUSTSET, "", "");
readConfig(config, "clip", clip, Config::DEFAULT, "1", "clip at boundaries");
if(isCreateSchema(config)) return;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void PlotMapLayerText::writeDataFile(const Ellipsoid &/*ellipsoid*/, const FileName &workingDirectory, UInt idxLayer)
{
dataFileName = "text."+idxLayer%"%i.txt"s;
OutFile file(workingDirectory.append(dataFileName));
file<<lon*RAD2DEG<<" "<<lat*RAD2DEG<<" "<<text;
}
/***********************************************/
std::string PlotMapLayerText::scriptEntry() const
{
try
{
std::stringstream ss;
ss<<"gmt pstext "<<dataFileName<<" -F+f"<<fontSize<<"p,,"<<fontColor->str()<<"+j"<<alignment<<" -D"<<xOffset<<"/"<<yOffset<<" -J -R "<<(clip?"":"-N")<<" -O -K >> groopsPlot.ps"<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
GROOPS_REGISTER_CLASS(PlotMapLayer, "plotMapLayerType",
PlotMapLayerGrid,
PlotMapLayerPoints,
PlotMapLayerArrows,
PlotMapLayerPolygon,
PlotMapLayerCoast,
PlotMapLayerRivers,
PlotMapLayerPolitical,
PlotMapLayerBlueMarble,
PlotMapLayerText)
GROOPS_READCONFIG_CLASS(PlotMapLayer, "plotMapLayerType")
/***********************************************/
PlotMapLayerPtr PlotMapLayer::create(Config &config, const std::string &name)
{
try
{
PlotMapLayerPtr plotMapLayer;
std::string type;
readConfigChoice(config, name, type, Config::MUSTSET, "", "plot layers");
if(readConfigChoiceElement(config, "griddedData", type, "data with regular sampling"))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerGrid(config));
if(readConfigChoiceElement(config, "points", type, "colored points"))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerPoints(config));
if(readConfigChoiceElement(config, "arrows", type, "colored arrows"))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerArrows(config));
if(readConfigChoiceElement(config, "polygon", type, "boundaries"))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerPolygon(config));
if(readConfigChoiceElement(config, "coast", type, "coast lines, ..."))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerCoast(config));
if(readConfigChoiceElement(config, "rivers", type, "major rivers"))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerRivers(config));
if(readConfigChoiceElement(config, "politicalBoundary", type, "political boundaries"))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerPolitical(config));
if(readConfigChoiceElement(config, "blueMarble", type, "blue marble"))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerBlueMarble(config));
if(readConfigChoiceElement(config, "text", type, "text"))
plotMapLayer = PlotMapLayerPtr(new PlotMapLayerText(config));
endChoice(config);
return plotMapLayer;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void PlotMapLayer::boundary(const Ellipsoid &ellipsoid, Angle &minL, Angle &maxL, Angle &minB, Angle &maxB) const
{
try
{
for(UInt k=0; k<points.size(); k++)
{
Angle lon, lat;
Double h;
ellipsoid(points.at(k), lon, lat, h);
minL = std::min(minL, lon-bufferLon);
maxL = std::max(maxL, lon+bufferLon);
minB = std::min(minB, lat-bufferLat);
maxB = std::max(maxB, lat+bufferLat);
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void PlotMapLayer::getIntervalZ(Bool isLogarithmic, Double &minZ, Double &maxZ) const
{
try
{
if(!requiresColorBar())
return;
UInt count = 0;
Double avg = 0.;
for(UInt i=0; i<data.rows(); i++)
if(!std::isnan(data(i, 0)) && (!isLogarithmic || (data(i, 0) > 0)))
{
minZ = std::min(minZ, data(i, 0));
maxZ = std::max(maxZ, data(i, 0));
avg += std::fabs(data(i, 0));
count++;
}
avg /= count;
if(!isLogarithmic)
{
minZ = (minZ >= 0) ? 0 : -3*avg;
maxZ = +3*avg;
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void PlotMapLayer::writeDataFile(const Ellipsoid &ellipsoid, const FileName &workingDirectory, UInt idxLayer)
{
try
{
if(!points.size())
return;
dataFileName = "data."+idxLayer%"%i.dat"s;
OutFile file(workingDirectory.append(dataFileName), std::ios::out | std::ios::binary);
for(UInt i=0; i<points.size(); i++)
{
Angle lon, lat;
Double h;
ellipsoid(points.at(i), lon, lat, h);
std::vector<Double> line = {lon*RAD2DEG, lat*RAD2DEG};
file.write(reinterpret_cast<char*>(line.data()), line.size()*sizeof(Double));
for(UInt k=0; k<data.columns(); k++)
file.write(reinterpret_cast<char*>(&data(i, k)), sizeof(Double));
}
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayer::scriptStatisticsInfo(UInt fontSize, Double width, const FileName &workingDirectory, UInt idxLayer) const
{
try
{
if(!requiresColorBar())
return std::string();
Double rms, avg, vmin, vmax, mean;
MiscGriddedData::statistics(Vector(data.column(0)), areas, rms, avg, vmin, vmax, mean);
FileName fileNameStatistics("statistics"+idxLayer%"%i.txt"s);
OutFile statisticsFile(workingDirectory.append(fileNameStatistics));
statisticsFile<<"0.5 0.0 "<<fontSize<<"p CB min="<<vmin<<", max="<<vmax<<", mean="<<mean<<", rms="<<rms;
std::stringstream ss;
ss<<"gmt pstext "<<fileNameStatistics<<" -F+f+j -Y-"<<fontSize<<"p -JX"<<width<<"c/"<<fontSize<<"p -R0/1/0/1 -N -O -K >> groopsPlot.ps "<<PlotBasics::scriptError2Null()<<std::endl;
return ss.str();
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
std::string PlotMapLayer::legendEntry(const FileName &/*workingDirectory*/, UInt /*idxLayer*/) const
{
try
{
return "";
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
|