Search in sources :

Example 1 with Warnings

use of org.apache.sis.io.wkt.Warnings in project sis by apache.

the class ConsistencyTest method parseAndFormat.

/**
 * Formats the given CRS using the given formatter, parses it and reformat again.
 * Then the two WKT are compared.
 *
 * @param  f     the formatter to use.
 * @param  code  the authority code, used only in case of errors.
 * @param  crs   the CRS to test.
 * @return the parsed CRS.
 */
private CoordinateReferenceSystem parseAndFormat(final WKTFormat f, final String code, final CoordinateReferenceSystem crs) {
    String wkt = f.format(crs);
    final Warnings warnings = f.getWarnings();
    if (warnings != null && !warnings.getExceptions().isEmpty()) {
        print(code, "WARNING", warnings.getException(0));
    }
    final CoordinateReferenceSystem parsed;
    try {
        parsed = (CoordinateReferenceSystem) f.parseObject(wkt);
    } catch (ParseException e) {
        print(code, "ERROR", "Can not parse the WKT below.");
        out.println(wkt);
        out.println();
        e.printStackTrace(out);
        fail(e.getLocalizedMessage());
        return null;
    }
    final String again = f.format(parsed);
    final CharSequence[] expectedLines = CharSequences.splitOnEOL(wkt);
    final CharSequence[] actualLines = CharSequences.splitOnEOL(again);
    /*
         * WKT 2 contains a line like below:
         *
         *   METHOD["Transverse Mercator", ID["EPSG", 9807, "8.9"]]
         *
         * But after parsing, the version number disaspear:
         *
         *   METHOD["Transverse Mercator", ID["EPSG", 9807]]
         *
         * This is a side effect of the fact that operation method are hard-coded in Java code.
         * This is normal for our implementation, so remove the version number from the expected lines.
         */
    if (f.getConvention().majorVersion() >= 2) {
        for (int i = 0; i < expectedLines.length; i++) {
            final CharSequence line = expectedLines[i];
            int p = line.length();
            int s = CharSequences.skipLeadingWhitespaces(line, 0, p);
            if (CharSequences.regionMatches(line, s, "METHOD[\"")) {
                assertEquals(code, ',', line.charAt(--p));
                assertEquals(code, ']', line.charAt(--p));
                assertEquals(code, ']', line.charAt(--p));
                if (line.charAt(--p) == '"') {
                    p = CharSequences.lastIndexOf(line, ',', 0, p);
                    expectedLines[i] = line.subSequence(0, p) + "]],";
                }
            }
        }
    }
    /*
         * Now compare the WKT line-by-line.
         */
    final int length = StrictMath.min(expectedLines.length, actualLines.length);
    try {
        for (int i = 0; i < length; i++) {
            assertEquals(code, expectedLines[i], actualLines[i]);
        }
    } catch (AssertionError e) {
        print(code, "ERROR", "WKT are not equal.");
        final TableAppender table = new TableAppender();
        table.nextLine('═');
        table.setMultiLinesCells(true);
        table.append("Original WKT:");
        table.nextColumn();
        table.append("CRS parsed from the WKT:");
        table.nextLine();
        table.appendHorizontalSeparator();
        table.append(wkt);
        table.nextColumn();
        table.append(again);
        table.nextLine();
        table.nextLine('═');
        out.println(table);
        throw e;
    }
    assertEquals("Unexpected number of lines.", expectedLines.length, actualLines.length);
    return parsed;
}
Also used : TableAppender(org.apache.sis.io.TableAppender) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ParseException(java.text.ParseException) Warnings(org.apache.sis.io.wkt.Warnings)

Example 2 with Warnings

use of org.apache.sis.io.wkt.Warnings in project sis by apache.

the class TransformCommand method printDetails.

/**
 * Prints the coordinate operation or math transform in Well Known Text format.
 * This information is printed only if the {@code --verbose} option was specified.
 */
private void printDetails() throws IOException {
    final boolean debug = options.containsKey(Option.DEBUG);
    final WKTFormat f = new WKTFormat(locale, timezone);
    if (colors)
        f.setColors(Colors.DEFAULT);
    f.setConvention(convention);
    CharSequence[] lines = CharSequences.splitOnEOL(f.format(debug ? operation.getMathTransform() : operation));
    for (int i = 0; i < lines.length; i++) {
        if (i == 0) {
            printHeader(Vocabulary.Keys.Details);
        } else {
            printCommentLinePrefix();
            outHeader.nextColumn();
        }
        outHeader.append(lines[i]);
        outHeader.nextLine();
    }
    final Warnings warnings = f.getWarnings();
    if (warnings != null) {
        lines = CharSequences.splitOnEOL(warnings.toString());
        if (lines.length != 0) {
            // Paranoiac check.
            printHeader(Vocabulary.Keys.Note);
            outHeader.append(lines[0]);
            outHeader.nextLine();
        }
    }
}
Also used : WKTFormat(org.apache.sis.io.wkt.WKTFormat) Warnings(org.apache.sis.io.wkt.Warnings)

Aggregations

Warnings (org.apache.sis.io.wkt.Warnings)2 ParseException (java.text.ParseException)1 TableAppender (org.apache.sis.io.TableAppender)1 WKTFormat (org.apache.sis.io.wkt.WKTFormat)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1