org.geotoolkit.console.ReferencingAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geotk-referencing Show documentation
Show all versions of geotk-referencing Show documentation
Implementations of Coordinate Reference Systems (CRS),
conversion and transformation services derived from ISO 19111.
/*
* Geotoolkit.org - An Open Source Java GIS Toolkit
* http://www.geotoolkit.org
*
* (C) 2001-2011, Open Source Geospatial Foundation (OSGeo)
* (C) 2009-2011, Geomatys
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotoolkit.console;
import java.util.*;
import java.io.Reader;
import java.io.Writer;
import java.io.PrintWriter;
import java.io.IOException;
import java.text.NumberFormat;
import org.opengis.metadata.Identifier;
import org.opengis.metadata.citation.Citation;
import org.opengis.referencing.*;
import org.opengis.referencing.operation.*;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.util.InternationalString;
import org.opengis.util.FactoryException;
import org.geotoolkit.factory.Hints;
import org.geotoolkit.factory.FactoryFinder;
import org.geotoolkit.factory.AuthorityFactoryFinder;
import org.geotoolkit.factory.FactoryNotFoundException;
import org.geotoolkit.io.TableWriter;
import org.geotoolkit.io.wkt.Colors;
import org.geotoolkit.io.wkt.WKTFormat;
import org.geotoolkit.resources.Errors;
import org.geotoolkit.resources.Vocabulary;
import org.geotoolkit.parameter.ParameterWriter;
import org.geotoolkit.referencing.CRS;
import org.geotoolkit.referencing.factory.AbstractAuthorityFactory;
import org.geotoolkit.referencing.factory.epsg.PropertyEpsgFactory;
import org.geotoolkit.referencing.datum.DefaultGeodeticDatum;
import org.geotoolkit.referencing.datum.BursaWolfParameters;
import org.geotoolkit.metadata.iso.citation.Citations;
import org.geotoolkit.referencing.factory.FallbackAuthorityFactory;
import static org.geotoolkit.referencing.IdentifiedObjects.NAME_COMPARATOR;
import static org.geotoolkit.console.CommandLine.*;
/**
* The actions run by {@link ReferencingCommands}.
* Contains a few action which have been moved out of {@code ReferencingCommands} for making
* it lighter, and faster to startup when the user just want the summary or the help screen.
*
* @author Martin Desruisseaux (Geomatys)
* @version 3.01
*
* @since 3.00
* @module
*/
final class ReferencingAction {
/**
* The hints for the factory to fetch. Null for now, but may be different in a future version.
*/
private static final Hints HINTS = null;
/**
* The command line.
*/
private final ReferencingCommands cmd;
/**
* The resources.
*/
private final Vocabulary resources;
/**
* The object to use for parsing and formatting WKT.
* Will be created when first needed.
*
* @see #getWktFormat
*/
private transient WKTFormat format;
/**
* Creates a new {@code ReferencingAction}.
*
* @param cmd The command line for which to create a help report.
*/
ReferencingAction(final ReferencingCommands cmd) {
this.cmd = cmd;
resources = Vocabulary.getResources(cmd.locale);
}
/**
* Formats the resources at the given key with bold characters.
*/
private String bold(final int key) {
return cmd.bold(resources.getString(key));
}
/**
* Returns the object to use for parsing and formatting WKT.
*
* @return The WKT parser and formatter.
*/
private WKTFormat getWktFormat() {
if (format == null) {
format = new WKTFormat();
final String authority = cmd.authority;
if (authority != null) {
format.setAuthority(Citations.fromName(authority));
}
if (Boolean.TRUE.equals(cmd.colors)) {
format.setColors(Colors.DEFAULT);
}
format.setIndentation(cmd.indent);
}
return format;
}
/**
* Returns the CRS authority factory to use.
*/
private CRSAuthorityFactory getCRSAuthorityFactory() {
CRSAuthorityFactory factory = CRS.getAuthorityFactory(cmd.forcexy);
final String authority = cmd.authority;
if (authority != null) {
final CRSAuthorityFactory first;
final Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, cmd.forcexy);
try {
first = AuthorityFactoryFinder.getCRSAuthorityFactory(authority, hints);
} catch (FactoryNotFoundException e) {
final PrintWriter err = cmd.err;
err.println(e.getLocalizedMessage());
err.println();
err.flush();
return factory;
}
factory = FallbackAuthorityFactory.create(CRSAuthorityFactory.class, first, factory);
}
return factory;
}
/**
* The separator to put between WKT.
*/
private static char[] getSeparator() {
final char[] separator = new char[79];
Arrays.fill(separator, '\u2500');
return separator;
}
/**
* Invoked when the user did not supplied an action. The default implementation assumes
* that the arguments are authority codes of CRS objects to prints in Well Known
* Text (WKT) format. This is the most common usage of this class.
*/
public void printObjectsWKT(final String[] arguments) {
final PrintWriter out = cmd.out;
final CRSAuthorityFactory factory = getCRSAuthorityFactory();
char[] separator = null;
for (int i=0; i done = new HashSet();
final TableWriter table = new TableWriter(out, TableWriter.SINGLE_VERTICAL_LINE);
final TableWriter notes = new TableWriter(out, " ");
int noteCount = 0;
notes.setMultiLinesCells(true);
table.setMultiLinesCells(true);
table.writeHorizontalSeparator();
table.write(bold(Vocabulary.Keys.AUTHORITY));
table.nextColumn();
table.write(bold(Vocabulary.Keys.DESCRIPTION));
table.nextColumn();
table.write(bold(Vocabulary.Keys.NOTE));
table.writeHorizontalSeparator();
for (AuthorityFactory factory : AuthorityFactoryFinder.getCRSAuthorityFactories(HINTS)) {
final Citation authority = factory.getAuthority();
final Iterator extends Identifier> identifiers = authority.getIdentifiers().iterator();
if (!identifiers.hasNext()) {
// No identifier. Scan next authorities.
continue;
}
if (!done.add(authority)) {
// Already done. Scans next authorities.
continue;
}
table.write(identifiers.next().getCode());
table.nextColumn();
table.write(authority.getTitle().toString().trim());
if (factory instanceof AbstractAuthorityFactory) {
String description;
try {
description = ((AbstractAuthorityFactory) factory).getBackingStoreDescription();
} catch (FactoryException e) {
description = e.getLocalizedMessage();
}
if (description != null) {
final String n = String.valueOf(++noteCount);
table.nextColumn();
table.write('('); table.write(n); table.write(')');
notes.write('('); notes.write(n); notes.write(')');
notes.nextColumn();
notes.write(description.trim());
notes.nextLine();
}
}
table.nextLine();
}
table.writeHorizontalSeparator();
try {
table.flush();
notes.flush();
} catch (IOException e) {
// Should never happen, since we are backed by PrintWriter.
throw new AssertionError(e);
}
}
/**
* Invoked when the user asked the {@code "list codes"} sub-action.
* The default implementation lists all CRS codes.
*/
private void listCodes() {
final CRSAuthorityFactory factory;
final String authority = cmd.authority;
if (authority != null) {
factory = AuthorityFactoryFinder.getCRSAuthorityFactory(authority, HINTS);
} else {
factory = CRS.getAuthorityFactory(cmd.forcexy);
}
final TableWriter table = new TableWriter(cmd.out);
table.writeHorizontalSeparator();
table.write(bold(Vocabulary.Keys.CODE));
table.nextColumn();
table.write(bold(Vocabulary.Keys.DESCRIPTION));
table.writeHorizontalSeparator();
try {
final Set codes = factory.getAuthorityCodes(CoordinateReferenceSystem.class);
for (final String code : codes) {
table.write(code);
table.nextColumn();
try {
final InternationalString description = factory.getDescriptionText(code);
if (description != null) {
table.write(description.toString(cmd.locale));
}
} catch (NoSuchAuthorityCodeException e) {
// Ignore. We will let the cell blank.
}
table.nextLine();
}
} catch (FactoryException e) {
cmd.printException(e);
cmd.exit(INTERNAL_ERROR_EXIT_CODE);
}
table.writeHorizontalSeparator();
try {
table.flush();
} catch (IOException e) {
cmd.printException(e);
cmd.exit(IO_EXCEPTION_EXIT_CODE);
}
}
/**
* Invoked when the user asked the {@code "list"} action. The default implementation
* lists the available factories, authority codes or coordinate operations. The argument
* must be one of {@code "authorities"}, {@code "codes"}, {@code "operations"},
* {@code "conversions"} or {@code "projections"}.
*/
public void list(final String[] arguments) {
final String list = arguments[0];
final Class extends SingleOperation> type;
if (list.equalsIgnoreCase("authorities")) {
listAuthorities();
return;
} else if (list.equalsIgnoreCase("codes")) {
listCodes();
return;
} else if (list.equalsIgnoreCase("operations")) {
type = SingleOperation.class;
} else if (list.equalsIgnoreCase("conversions")) {
type = Conversion.class;
} else if (list.equalsIgnoreCase("projections")) {
type = Projection.class;
} else {
final PrintWriter err = cmd.err;
final Errors resources = Errors.getResources(cmd.locale);
err.println(resources.getString(Errors.Keys.ILLEGAL_ARGUMENT_$1, "list"));
err.println(resources.getString(Errors.Keys.UNKNOWN_TYPE_$1, list));
cmd.exit(ILLEGAL_ARGUMENT_EXIT_CODE);
return;
}
final String authority = cmd.authority;
final MathTransformFactory factory = FactoryFinder.getMathTransformFactory(HINTS);
final Set methods = new TreeSet(NAME_COMPARATOR);
methods.addAll(factory.getAvailableMethods(type));
final ParameterWriter writer = new ParameterWriter(cmd.out);
writer.setLocale(cmd.locale);
writer.setColorEnabled(cmd.colors);
writer.setAuthorities("EPSG:#", (authority != null) ? authority : "Geotk");
try {
writer.summary(methods);
} catch (IOException exception) {
cmd.printException(exception);
cmd.exit(IO_EXCEPTION_EXIT_CODE);
return;
}
}
/**
* Invoked when the user asked the {@code "bursawolfs"} action. The default implementation
* lists the Bursa-Wolf parameters for the specified CRS ou datum objects.
*/
public void bursawolfs(final String[] arguments) {
final NumberFormat nf = NumberFormat.getNumberInstance(cmd.locale);
nf.setMinimumFractionDigits(3);
nf.setMaximumFractionDigits(3);
final TableWriter table = new TableWriter(cmd.out);
table.writeHorizontalSeparator();
final String[] titles = {
resources.getString(Vocabulary.Keys.TARGET),
"dx", "dy", "dz", "ex", "ey", "ez", "ppm"
};
for (int i=0; i op;
try {
op = factory.createFromCoordinateReferenceSystemCodes(arguments[i], arguments[j]);
} catch (FactoryException e) {
cmd.printException(e);
cmd.exit(ILLEGAL_ARGUMENT_EXIT_CODE);
return;
}
for (final CoordinateOperation operation : op) {
if (separator == null) {
separator = getSeparator();
} else {
out.println(separator);
}
final WKTFormat formatter = getWktFormat();
out.println(formatter.format(operation));
}
}
}
}
/**
* Invoked when the user asked the {@code "transform"} action. The default implementation
* prints the math transforms between every pairs of the specified authority code.
*/
public void transform(final String[] arguments) {
final PrintWriter out = cmd.out;
final CRSAuthorityFactory factory = getCRSAuthorityFactory();
final CoordinateOperationFactory opFactory =
AuthorityFactoryFinder.getCoordinateOperationFactory(HINTS);
char[] separator = null;
try {
for (int i=0; i