Search in sources :

Example 1 with LineAppender

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

the class TransformCommand method run.

/**
 * Transforms coordinates from the files given in argument or from the standard input stream.
 *
 * @return 0 on success, or an exit code if the command failed for a reason other than an uncaught Java exception.
 */
@Override
public int run() throws Exception {
    final CoordinateReferenceSystem sourceCRS = fetchCRS(Option.SOURCE_CRS);
    final CoordinateReferenceSystem targetCRS = fetchCRS(Option.TARGET_CRS);
    /*
         * Read all coordinates, so we can compute the area of interest.
         * This will be used when searching for a coordinate operation.
         */
    GeographicBoundingBox areaOfInterest = null;
    List<double[]> points = Collections.emptyList();
    final boolean useStandardInput = useStandardInput();
    if (useStandardInput || !files.isEmpty()) {
        if (useStandardInput) {
            try (LineNumberReader in = new LineNumberReader(new InputStreamReader(System.in, encoding))) {
                points = readCoordinates(in, "stdin");
            }
        } else {
            for (final String file : files) {
                try (LineNumberReader in = new LineNumberReader(new InputStreamReader(new FileInputStream(file), encoding))) {
                    points = readCoordinates(in, file);
                }
            }
        }
        try {
            final GeographicCRS domainOfValidityCRS = ReferencingUtilities.toNormalizedGeographicCRS(sourceCRS);
            if (domainOfValidityCRS != null) {
                toDomainOfValidity = CRS.findOperation(sourceCRS, domainOfValidityCRS, null).getMathTransform();
                areaOfInterest = computeAreaOfInterest(points);
            }
        } catch (FactoryException e) {
            warning(e);
        }
    }
    operation = CRS.findOperation(sourceCRS, targetCRS, areaOfInterest);
    /*
         * Prints the header: source CRS, target CRS, operation steps and positional accuracy.
         */
    outHeader = new TableAppender(new LineAppender(out), " ");
    outHeader.setMultiLinesCells(true);
    printHeader(Vocabulary.Keys.Source);
    printNameAndIdentifier(operation.getSourceCRS(), false);
    printHeader(Vocabulary.Keys.Destination);
    printNameAndIdentifier(operation.getTargetCRS(), false);
    printHeader(Vocabulary.Keys.Operations);
    printOperations(operation, false);
    outHeader.nextLine();
    printDomainOfValidity(operation.getDomainOfValidity());
    printAccuracy(CRS.getLinearAccuracy(operation));
    if (options.containsKey(Option.VERBOSE)) {
        printDetails();
    }
    outHeader.flush();
    outHeader = null;
    /*
         * At this point we finished to write the header. If there is at least one input file,
         * compute the number of digits to format and perform the actual coordinate operations.
         */
    if (!points.isEmpty()) {
        // Must be set before computeNumFractionDigits(…).
        ordinateWidth = 15;
        coordinateFormat = NumberFormat.getInstance(Locale.US);
        coordinateFormat.setGroupingUsed(false);
        computeNumFractionDigits(operation.getTargetCRS().getCoordinateSystem());
        out.println();
        printAxes(operation.getTargetCRS().getCoordinateSystem());
        out.println();
        transform(points);
        if (errorMessage != null) {
            error(errorMessage, errorCause);
        }
    }
    return 0;
}
Also used : InputStreamReader(java.io.InputStreamReader) FactoryException(org.opengis.util.FactoryException) TableAppender(org.apache.sis.io.TableAppender) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeographicBoundingBox(org.opengis.metadata.extent.GeographicBoundingBox) DefaultGeographicBoundingBox(org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox) InternationalString(org.opengis.util.InternationalString) GeographicCRS(org.opengis.referencing.crs.GeographicCRS) LineAppender(org.apache.sis.io.LineAppender) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader)

Aggregations

FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 LineAppender (org.apache.sis.io.LineAppender)1 TableAppender (org.apache.sis.io.TableAppender)1 DefaultGeographicBoundingBox (org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox)1 GeographicBoundingBox (org.opengis.metadata.extent.GeographicBoundingBox)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 GeographicCRS (org.opengis.referencing.crs.GeographicCRS)1 FactoryException (org.opengis.util.FactoryException)1 InternationalString (org.opengis.util.InternationalString)1