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;
}
Aggregations