Search in sources :

Example 11 with TableAppender

use of org.apache.sis.io.TableAppender in project sis by apache.

the class StatisticsFormat method format.

/**
 * Formats the given statistics in a tabular format. This method does not check
 * for the statistics on {@linkplain Statistics#differences() differences} - if
 * such statistics are wanted, they must be included in the given array.
 *
 * @param  stats       the statistics to format.
 * @param  toAppendTo  where to format the statistics.
 * @throws IOException if an error occurred while writing to the given appendable.
 */
public void format(final Statistics[] stats, final Appendable toAppendTo) throws IOException {
    /*
         * Inspect the given statistics in order to determine if we shall omit the headers,
         * and if we shall omit the count of NaN values.
         */
    final String[] headers = new String[stats.length];
    boolean showHeaders = false;
    boolean showNaNCount = false;
    for (int i = 0; i < stats.length; i++) {
        final Statistics s = stats[i];
        showNaNCount |= (s.countNaN() != 0);
        final InternationalString header = s.name();
        if (header != null) {
            headers[i] = header.toString(headerLocale);
            showHeaders |= (headers[i] != null);
        }
    }
    char horizontalLine = 0;
    String separator = columnSeparator;
    switch(borderWidth) {
        case 1:
            horizontalLine = '─';
            separator += "│ ";
            break;
        case 2:
            horizontalLine = '═';
            separator += "║ ";
            break;
    }
    final TableAppender table = new TableAppender(toAppendTo, separator);
    final Vocabulary resources = Vocabulary.getResources(headerLocale);
    /*
         * If there is a header for at least one statistics, write the full headers row.
         */
    if (horizontalLine != 0) {
        table.nextLine(horizontalLine);
    }
    if (showHeaders) {
        table.nextColumn();
        for (final String header : headers) {
            if (header != null) {
                table.append(header);
                table.setCellAlignment(TableAppender.ALIGN_CENTER);
            }
            table.nextColumn();
        }
        table.append(lineSeparator);
        if (horizontalLine != 0) {
            table.nextLine(horizontalLine);
        }
    }
    /*
         * Initialize the NumberFormat for formatting integers without scientific notation.
         * This is necessary since the format may have been modified by a previous execution
         * of this method.
         */
    final Format format = getFormat(Double.class);
    if (format instanceof DecimalFormat) {
        // Also disable scientific notation.
        ((DecimalFormat) format).applyPattern("#0");
    } else if (format instanceof NumberFormat) {
        setFractionDigits((NumberFormat) format, 0);
    }
    /*
         * Iterates over the rows to format (count, minimum, maximum, mean, RMS, standard deviation),
         * then iterate over columns (statistics on sample values, on the first derivatives, etc.)
         * The NumberFormat configuration may be different for each column, but we can skip many
         * reconfiguration in the common case where there is only one column.
         */
    boolean needsConfigure = false;
    for (int i = 0; i < KEYS.length; i++) {
        switch(i) {
            case 1:
                if (!showNaNCount)
                    continue;
                else
                    break;
            // Case 3 and others need reconfiguration only if there is more than one column.
            case 2:
                needsConfigure = true;
                break;
            case 3:
                needsConfigure = (stats[0].differences() != null);
                break;
        }
        table.setCellAlignment(TableAppender.ALIGN_LEFT);
        table.append(resources.getString(KEYS[i])).append(':');
        for (final Statistics s : stats) {
            final Number value;
            switch(i) {
                case 0:
                    value = s.count();
                    break;
                case 1:
                    value = s.countNaN();
                    break;
                case 2:
                    value = s.minimum();
                    break;
                case 3:
                    value = s.maximum();
                    break;
                case 4:
                    value = s.mean();
                    break;
                case 5:
                    value = s.rms();
                    break;
                case 6:
                    value = s.standardDeviation(allPopulation);
                    break;
                default:
                    throw new AssertionError(i);
            }
            if (needsConfigure) {
                configure(format, s);
            }
            table.append(beforeFill);
            table.nextColumn(fillCharacter);
            table.append(format.format(value));
            table.setCellAlignment(TableAppender.ALIGN_RIGHT);
        }
        table.append(lineSeparator);
    }
    if (horizontalLine != 0) {
        table.nextLine(horizontalLine);
    }
    /*
         * TableAppender needs to be explicitly flushed in order to format the values.
         */
    table.flush();
}
Also used : Vocabulary(org.apache.sis.util.resources.Vocabulary) DecimalFormat(java.text.DecimalFormat) TableAppender(org.apache.sis.io.TableAppender) InternationalString(org.opengis.util.InternationalString) Format(java.text.Format) DecimalFormat(java.text.DecimalFormat) TabularFormat(org.apache.sis.io.TabularFormat) NumberFormat(java.text.NumberFormat) InternationalString(org.opengis.util.InternationalString) NumberFormat(java.text.NumberFormat)

Example 12 with TableAppender

use of org.apache.sis.io.TableAppender in project sis by apache.

the class LinearTransformBuilder method toString.

/**
 * Returns a string representation of this builder for debugging purpose.
 *
 * @return a string representation of this builder.
 */
@Debug
@Override
public String toString() {
    final StringBuilder buffer = new StringBuilder(Classes.getShortClassName(this)).append('[');
    if (sources != null) {
        buffer.append(sources[0].length).append(" points");
    }
    buffer.append(']');
    if (transform != null) {
        final String lineSeparator = System.lineSeparator();
        buffer.append(':').append(lineSeparator);
        final TableAppender table = new TableAppender(buffer, " ");
        table.setMultiLinesCells(true);
        table.append(Matrices.toString(transform.getMatrix())).nextColumn();
        table.append(lineSeparator).append("  ").append(Vocabulary.format(Vocabulary.Keys.Correlation)).append(" =").nextColumn();
        table.append(Matrices.create(correlation.length, 1, correlation).toString());
        try {
            table.flush();
        } catch (IOException e) {
            // Should never happen since we wrote into a StringBuilder.
            throw new AssertionError(e);
        }
    }
    return buffer.toString();
}
Also used : TableAppender(org.apache.sis.io.TableAppender) IOException(java.io.IOException) Debug(org.apache.sis.util.Debug)

Example 13 with TableAppender

use of org.apache.sis.io.TableAppender in project sis by apache.

the class Metadata method toString.

/**
 * Returns a string representation of this metadata object.
 *
 * @return a string representation of this metadata.
 */
@Override
public String toString() {
    final StringBuilder buffer = new StringBuilder();
    buffer.append("GPX metadata").append(System.lineSeparator());
    final TableAppender table = new TableAppender(buffer);
    table.setMultiLinesCells(true);
    table.appendHorizontalSeparator();
    append(table, "Creator", creator);
    append(table, "Name", name);
    append(table, "Description", description);
    append(table, "Author", author);
    append(table, "Copyright", copyright);
    append(table, "Link(s)", links, System.lineSeparator());
    append(table, "Time", (time != null) ? time.toInstant() : null);
    append(table, "Keywords", keywords, " ");
    append(table, "Bounds", bounds);
    table.appendHorizontalSeparator();
    try {
        table.flush();
    } catch (IOException e) {
        // Should never happen since we are writing to a StringBuilder.
        throw new AssertionError(e);
    }
    return buffer.toString();
}
Also used : TableAppender(org.apache.sis.io.TableAppender) IOException(java.io.IOException)

Example 14 with TableAppender

use of org.apache.sis.io.TableAppender in project sis by apache.

the class MercatorMethodComparison method printErrorForExcentricities.

/**
 * Prints the error of the two methods for various eccentricity values.
 * The intent of this method is to find an eccentricity threshold value where we consider the errors too high.
 *
 * <p>This method is used for determining empirically a value for {@link ConformalProjection#ECCENTRICITY_THRESHOLD}.
 * The current threshold value is shown by inserting a horizontal line separator in the table when that threshold
 * is crossed.</p>
 *
 * @param  min  the first eccentricity value to test.
 * @param  max  the maximal eccentricity value to test.
 * @throws ProjectionException if an error occurred in {@link ConformalProjection#φ(double)}.
 */
public static void printErrorForExcentricities(final double min, final double max) throws ProjectionException {
    final TableAppender table = new TableAppender(out);
    table.appendHorizontalSeparator();
    table.append("Eccentricity");
    table.nextColumn();
    table.append("Mean iterative error");
    table.nextColumn();
    table.append("Maximal iterative error");
    table.nextColumn();
    table.append("Mean series error");
    table.nextColumn();
    table.append("Maximal series error");
    table.nextLine();
    table.appendHorizontalSeparator();
    boolean crossThreshold = false;
    final double step = 0.01;
    double eccentricity;
    for (int i = 0; (eccentricity = min + step * i) < max; i++) {
        if (!crossThreshold && eccentricity >= ConformalProjection.ECCENTRICITY_THRESHOLD) {
            crossThreshold = true;
            table.appendHorizontalSeparator();
        }
        final MercatorMethodComparison alt = new MercatorMethodComparison(eccentricity * eccentricity);
        alt.compare(null, 10000, table);
    }
    table.appendHorizontalSeparator();
    try {
        table.flush();
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
Also used : TableAppender(org.apache.sis.io.TableAppender) IOException(java.io.IOException)

Example 15 with TableAppender

use of org.apache.sis.io.TableAppender in project sis by apache.

the class LogRecordCollector method report.

/**
 * If any tests has emitted log records, report them.
 */
final void report(final Appendable out) throws IOException {
    synchronized (records) {
        final String lineSeparator = System.lineSeparator();
        if (!records.isEmpty()) {
            out.append(lineSeparator).append("The following tests have logged messages at level INFO or higher:").append(lineSeparator).append("See 'org.apache.sis.test.LoggingWatcher' for information about logging during tests.").append(lineSeparator);
            final TableAppender table = new TableAppender(out);
            table.setMultiLinesCells(false);
            table.nextLine('═');
            table.append("Test class");
            table.nextColumn();
            table.append("Test method");
            table.nextColumn();
            table.append("Target logger");
            table.nextColumn();
            table.append("Level");
            table.appendHorizontalSeparator();
            final Iterator<String> it = records.iterator();
            do {
                // Class
                table.append(it.next());
                // Class
                table.nextColumn();
                // Method
                table.append(it.next());
                // Method
                table.nextColumn();
                // Logger
                table.append(it.next());
                // Logger
                table.nextColumn();
                // Level
                table.append(it.next());
                // Level
                table.nextLine();
            } while (it.hasNext());
            table.nextLine('═');
            table.flush();
            records.clear();
        }
    }
}
Also used : TableAppender(org.apache.sis.io.TableAppender)

Aggregations

TableAppender (org.apache.sis.io.TableAppender)15 IOException (java.io.IOException)7 Vocabulary (org.apache.sis.util.resources.Vocabulary)6 IdentifiedObject (org.opengis.referencing.IdentifiedObject)4 InternationalString (org.opengis.util.InternationalString)4 Format (java.text.Format)3 NumberFormat (java.text.NumberFormat)3 TabularFormat (org.apache.sis.io.TabularFormat)3 Debug (org.apache.sis.util.Debug)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 FieldPosition (java.text.FieldPosition)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 GeographicBoundingBox (org.opengis.metadata.extent.GeographicBoundingBox)2 GeographicCRS (org.opengis.referencing.crs.GeographicCRS)2 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1