WSL/SLF GitLab Repository

Commit a96066c0 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

The legend class is now free of hard-coded values in the middle of the code...

The legend class is now free of hard-coded values in the middle of the code (all depends on the static consts).
parent 14736bb0
......@@ -26,38 +26,45 @@ namespace mio {
const int legend::bg_color = IOUtils::nodata-1;
const int legend::text_color = IOUtils::nodata-2;
const unsigned int legend::char_width = 6;
const unsigned int legend::char_height = 10;
const unsigned int legend::text_chars_nb = 9; //each label will contain 9 chars
const unsigned int legend::char_width = 6+1; //6 pixels wide + 1 pixel space
const unsigned int legend::text_width = legend::text_chars_nb*legend::char_width; //whole text line
const unsigned int legend::char_space = 1; //horizontal space between two chars
const unsigned int legend::text_width = legend::text_chars_nb*(legend::char_width+legend::char_space); //whole text line
const unsigned int legend::sample_width = legend::char_width*1; //color sample 2 chars wide
const unsigned int legend::sample_text_space = 6;
const unsigned int legend::sample_text_space = 6; //width of the color sample
const unsigned int legend::legend_plot_space = legend::char_width*1;
const unsigned int legend::total_width = legend::legend_plot_space+legend::sample_width+legend::sample_text_space+legend::text_width;
const unsigned int legend::char_height = 10;
const unsigned int legend::interline = 5;
const unsigned int legend::label_height = legend::char_height+legend::interline; //1 char + interline
const unsigned int legend::nb_labels = 11; //every decile + 0 level
const unsigned int legend::nb_labels = 10+1; //every decile + 0 level
const unsigned int legend::total_height = legend::nb_labels*legend::label_height+legend::interline;
const unsigned int legend::font_0[10][6] = {{0,0,1,1,0,0}, {0,1,0,0,1,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,1,0,0,1,0}, {0,0,1,1,0,0}};
const unsigned int legend::font_1[10][6] = {{0,0,0,1,0,0}, {0,0,1,1,0,0}, {0,1,1,1,0,0}, {1,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}};
const unsigned int legend::font_2[10][6] = {{0,1,1,1,1,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,0,0,1,1,0}, {0,0,1,1,0,0}, {0,1,1,0,0,0}, {1,1,0,0,0,0}, {1,1,1,1,1,1}};
const unsigned int legend::font_3[10][6] = {{0,1,1,1,1,0}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,0,1,1,1,0}, {0,0,0,0,1,0}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,1,1,1,1,0}};
const unsigned int legend::font_4[10][6] = {{0,0,0,1,1,0}, {0,0,1,1,1,0}, {0,1,1,1,1,0}, {1,1,0,1,1,0}, {1,0,0,1,1,0}, {1,1,1,1,1,1}, {0,0,0,1,1,0}, {0,0,0,1,1,0}, {0,0,0,1,1,0}, {0,0,1,1,1,1}};
const unsigned int legend::font_5[10][6] = {{1,1,1,1,1,1}, {1,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,1,1,0,0}, {0,0,0,0,1,0}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {1,0,0,0,1,0}, {0,1,1,1,0,0}};
const unsigned int legend::font_6[10][6] = {{0,1,1,1,1,0}, {1,1,0,0,1,0}, {1,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,1,1,0,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,1,1,0,0}};
const unsigned int legend::font_7[10][6] = {{1,1,1,1,1,1}, {0,0,0,0,1,1}, {0,0,0,1,1,0}, {0,0,0,1,0,0}, {0,0,1,1,0,0}, {0,0,1,0,0,0}, {0,1,1,0,0,0}, {0,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,0,0,0,0}};
const unsigned int legend::font_8[10][6] = {{0,0,1,1,0,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,1,1,0,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,1,1,0,0}};
const unsigned int legend::font_9[10][6] = {{0,0,1,1,0,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,1,1,1,1}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,1,0,0,1,1}, {0,1,1,1,1,0}};
const unsigned int legend::font_plus[10][6] = {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {1,1,1,1,1,1}, {1,1,1,1,1,1}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}};
const unsigned int legend::font_minus[10][6] = {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {1,1,1,1,1,1}, {1,1,1,1,1,1}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}};
const unsigned int legend::font_dot[10][6] = {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}};
const unsigned int legend::font_E[10][6] = {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,1,1,1,0,0}, {1,1,0,0,1,0}, {1,1,1,1,1,0}, {1,1,0,0,0,0}, {1,1,0,0,1,0}, {0,1,1,1,0,0}};
const unsigned int legend::font_0[char_height][char_width] = {{0,0,1,1,0,0}, {0,1,0,0,1,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,1,0,0,1,0}, {0,0,1,1,0,0}};
const unsigned int legend::font_1[char_height][char_width] = {{0,0,0,1,0,0}, {0,0,1,1,0,0}, {0,1,1,1,0,0}, {1,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}};
const unsigned int legend::font_2[char_height][char_width] = {{0,1,1,1,1,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,0,0,1,1,0}, {0,0,1,1,0,0}, {0,1,1,0,0,0}, {1,1,0,0,0,0}, {1,1,1,1,1,1}};
const unsigned int legend::font_3[char_height][char_width] = {{0,1,1,1,1,0}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,0,1,1,1,0}, {0,0,0,0,1,0}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,1,1,1,1,0}};
const unsigned int legend::font_4[char_height][char_width] = {{0,0,0,1,1,0}, {0,0,1,1,1,0}, {0,1,1,1,1,0}, {1,1,0,1,1,0}, {1,0,0,1,1,0}, {1,1,1,1,1,1}, {0,0,0,1,1,0}, {0,0,0,1,1,0}, {0,0,0,1,1,0}, {0,0,1,1,1,1}};
const unsigned int legend::font_5[char_height][char_width] = {{1,1,1,1,1,1}, {1,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,1,1,0,0}, {0,0,0,0,1,0}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {1,0,0,0,1,0}, {0,1,1,1,0,0}};
const unsigned int legend::font_6[char_height][char_width] = {{0,1,1,1,1,0}, {1,1,0,0,1,0}, {1,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,1,1,0,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,1,1,0,0}};
const unsigned int legend::font_7[char_height][char_width] = {{1,1,1,1,1,1}, {0,0,0,0,1,1}, {0,0,0,1,1,0}, {0,0,0,1,0,0}, {0,0,1,1,0,0}, {0,0,1,0,0,0}, {0,1,1,0,0,0}, {0,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,0,0,0,0}};
const unsigned int legend::font_8[char_height][char_width] = {{0,0,1,1,0,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,1,1,0,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,1,1,0,0}};
const unsigned int legend::font_9[char_height][char_width] = {{0,0,1,1,0,0}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,1,1}, {0,0,1,1,1,1}, {0,0,0,0,1,1}, {0,0,0,0,1,1}, {0,1,0,0,1,1}, {0,1,1,1,1,0}};
const unsigned int legend::font_plus[char_height][char_width] = {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {1,1,1,1,1,1}, {1,1,1,1,1,1}, {0,0,1,1,0,0}, {0,0,1,1,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}};
const unsigned int legend::font_minus[char_height][char_width] = {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {1,1,1,1,1,1}, {1,1,1,1,1,1}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}};
const unsigned int legend::font_dot[char_height][char_width] = {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,0,1,1,0,0}};
const unsigned int legend::font_E[char_height][char_width] = {{0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,0,0,0,0,0}, {0,1,1,1,0,0}, {1,1,0,0,1,0}, {1,1,1,1,1,0}, {1,1,0,0,0,0}, {1,1,0,0,1,0}, {0,1,1,1,0,0}};
//create a legend of given height
//if hight is insufficient, we don't generate any content, only transparent pixels
//if height is insufficient, we don't generate any content, only transparent pixels
legend::legend(const unsigned int &height, const double &minimum, const double &maximum)
{
drawLegend(height, minimum, maximum);
}
void legend::drawLegend(const unsigned int &height, const double &minimum, const double &maximum)
{
grid.resize(total_width, height, IOUtils::nodata);
const double level_inc = (maximum-minimum)/(double)(nb_labels-1); //the infamous interval thing...
......@@ -87,7 +94,8 @@ double legend::getLegendWidth() {
void legend::writeLine(const double& val, const unsigned int& px_row)
{
std::stringstream ss;
ss << setfill (' ') << setw (9) << left << setprecision(3) << val << endl;
const unsigned int precision = text_chars_nb-6; //full width - (sgn, dot, "e", sgn, two digits exponent)
ss << setfill (' ') << setw(text_chars_nb) << left << setprecision(precision) << val << endl;
const unsigned int x_offset = legend_plot_space+sample_width+sample_text_space;
......@@ -100,7 +108,7 @@ void legend::writeLine(const double& val, const unsigned int& px_row)
for(size_t i=0; i<ss.str().size(); i++) {
char c=ss.str()[i];
const unsigned int px_col = i*char_width+x_offset;
const unsigned int px_col = i*(char_width+char_space)+x_offset;
if(c=='0') writeChar(font_0, px_col, px_row);
if(c=='1') writeChar(font_1, px_col, px_row);
if(c=='2') writeChar(font_2, px_col, px_row);
......@@ -119,11 +127,11 @@ void legend::writeLine(const double& val, const unsigned int& px_row)
}
}
void legend::writeChar(const unsigned int i_char[10][6], const unsigned int& px_col, const unsigned int& px_row)
void legend::writeChar(const unsigned int i_char[char_height][char_width], const unsigned int& px_col, const unsigned int& px_row)
{
for(unsigned int jj=0; jj<10; jj++) {
for(unsigned int ii=0; ii<6; ii++) {
const unsigned int char_px = i_char[9-jj][ii]; //we need to swap vertically each char
for(unsigned int jj=0; jj<char_height; jj++) {
for(unsigned int ii=0; ii<char_width; ii++) {
const unsigned int char_px = i_char[char_height-1-jj][ii]; //we need to swap vertically each char
if(char_px==0)
grid(ii+px_col,jj+px_row+interline) = bg_color;
else
......@@ -306,7 +314,7 @@ double Gradient::getInterpol(const double& val, const std::vector<double>& X, co
if(X[i]==val) return Y[i];
if(i==0) return Y[0];
//if(i==Y.size()) return Y[ Y.size() ]; // not necessary, treated by the formula
const double y = Y[i-1] + (val-X[i-1])/(X[i]-X[i-1]) * (Y[i]-Y[i-1]);
return y;
}
......
......@@ -41,26 +41,28 @@ class legend {
static double getLegendWidth();
const Array2D<double> getLegend();
static const int bg_color; //marker for solid background
static const int text_color; //marker for solid text
static const int bg_color; ///<marker for solid background
static const int text_color; ///<marker for solid text
private:
Array2D<double> grid;
void drawLegend(const unsigned int &height, const double &minimum, const double &maximum);
void writeLine(const double& val, const unsigned int& px_row);
void writeChar(const unsigned int i_char[10][6], const unsigned int& px_col, const unsigned int& px_row);
static const unsigned int text_chars_nb; //each label will contain 9 chars
static const unsigned int char_width; //3 pixels wide + 1 pixel space
static const unsigned int text_width; //nb chars, 3 pixels wide + 1 pixel space
static const unsigned int sample_width; //color sample 2 chars wide
static const unsigned int text_chars_nb;
static const unsigned int char_width;
static const unsigned int char_space;
static const unsigned int text_width;
static const unsigned int sample_width;
static const unsigned int sample_text_space;
static const unsigned int legend_plot_space;
static const unsigned int total_width;
static const unsigned int char_height;
static const unsigned int interline;
static const unsigned int label_height; //1 char + 2 pixels interline
static const unsigned int nb_labels; //every decile + 0 level
static const unsigned int label_height;
static const unsigned int nb_labels;
static const unsigned int total_height;
static const unsigned int font_0[10][6], font_1[10][6], font_2[10][6], font_3[10][6], font_4[10][6];
......
......@@ -25,7 +25,7 @@ using namespace std;
namespace mio {
/**
* @page template PNGIO
* @page pngio PNGIO
* @section template_format Format
* *Put here the informations about the standard format that is implemented*
* No data read, only write (because of gradients)
......@@ -77,7 +77,7 @@ void PNGIO::setOptions()
if(min_size!="") parse_size(min_size, min_w, min_h);
cfg.getValue("png_max_size", "Output", max_size, Config::nothrow);
if(max_size!="") parse_size(max_size, max_w, max_h);
autoscale = true;
cfg.getValue("png_autoscale", "Output", autoscale, Config::nothrow);
has_legend = true;
......@@ -120,7 +120,7 @@ double PNGIO::getScaleFactor(const double& grid_w, const double& grid_h)
const double min_h_factor = (double)min_h / (double)grid_h;
min_factor = std::max(min_w_factor, min_h_factor);
}
double max_factor = IOUtils::nodata;
if(max_w!=IOUtils::unodata) { //max_w & max_h are read together
const double max_w_factor = (double)max_w / (double)grid_w;
......@@ -327,7 +327,7 @@ void PNGIO::createMetadata(const Grid2DObject& grid)
metadata_key.push_back("Longitude");
ss.str(""); ss << fixed << setprecision(6) << lon;
metadata_text.push_back(ss.str());
if(lat<0.) {
metadata_key.push_back("LatitudeRef");
metadata_text.push_back("S");
......@@ -350,7 +350,7 @@ void PNGIO::createMetadata(const Grid2DObject& grid)
metadata_key.push_back("GPSLongitude");
metadata_text.push_back(decimal_to_dms(lon));
}
//add data set timestamp
}
......@@ -372,7 +372,7 @@ void PNGIO::writeMetadata(png_structp &png_ptr, png_infop &info_ptr)
info_text[ii].text = text[ii];
info_text[ii].compression = PNG_TEXT_COMPRESSION_NONE;
}
png_set_text(png_ptr, info_ptr, info_text, nr);
free(info_text);
for(size_t ii=0; ii<nr; ii++) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment