use of org.opengis.referencing.datum.PrimeMeridian in project sis by apache.
the class DefaultGeodeticCRS method formatTo.
/**
* Formats this CRS as a <cite>Well Known Text</cite> {@code GeodeticCRS[…]} element.
* More information about the WKT format is documented in subclasses.
*
* @return {@code "GeodeticCRS"} (WKT 2) or {@code "GeogCS"}/{@code "GeocCS"} (WKT 1).
*/
@Override
protected String formatTo(final Formatter formatter) {
WKTUtilities.appendName(this, formatter, null);
CoordinateSystem cs = getCoordinateSystem();
final Convention convention = formatter.getConvention();
final boolean isWKT1 = (convention.majorVersion() == 1);
final boolean isGeographicWKT1 = isWKT1 && (cs instanceof EllipsoidalCS);
if (isGeographicWKT1 && cs.getDimension() == 3) {
/*
* Version 1 of WKT format did not have three-dimensional GeographicCRS. Instead, such CRS were formatted
* as a CompoundCRS made of a two-dimensional GeographicCRS with a VerticalCRS for the ellipsoidal height.
* Note that such compound is illegal in WKT 2 and ISO 19111 standard, as ellipsoidal height shall not be
* separated from the geographic component. So we perform this separation only at WKT 1 formatting time.
*/
SingleCRS first = CRS.getHorizontalComponent(this);
SingleCRS second = CRS.getVerticalComponent(this, true);
if (first != null && second != null) {
// Should not be null, but we are paranoiac.
if (AxisDirection.UP.equals(AxisDirections.absolute(cs.getAxis(0).getDirection()))) {
// It is very unusual to have VerticalCRS first, but our code tries to be robust.
final SingleCRS t = first;
first = second;
second = t;
}
formatter.newLine();
formatter.append(WKTUtilities.toFormattable(first));
formatter.newLine();
formatter.append(WKTUtilities.toFormattable(second));
formatter.newLine();
return WKTKeywords.Compd_CS;
}
}
/*
* Unconditionally format the datum element, followed by the prime meridian.
* The prime meridian is part of datum according ISO 19111, but is formatted
* as a sibling (rather than a child) element in WKT for historical reasons.
*/
// Gives subclasses a chance to override.
final GeodeticDatum datum = getDatum();
formatter.newLine();
formatter.append(WKTUtilities.toFormattable(datum));
formatter.newLine();
final PrimeMeridian pm = datum.getPrimeMeridian();
final Unit<Angle> angularUnit = AxisDirections.getAngularUnit(cs, null);
if (// Really this specific enum, not Convention.isSimplified().
convention != Convention.WKT2_SIMPLIFIED || ReferencingUtilities.getGreenwichLongitude(pm, Units.DEGREE) != 0) {
final Unit<Angle> oldUnit = formatter.addContextualUnit(angularUnit);
formatter.indent(1);
formatter.append(WKTUtilities.toFormattable(pm));
formatter.indent(-1);
formatter.newLine();
formatter.restoreContextualUnit(angularUnit, oldUnit);
}
/*
* Get the coordinate system to format. This will also determine the units to write and the keyword to
* return in WKT 1 format. Note that for the WKT 1 format, we need to replace the coordinate system by
* an instance conform to the legacy conventions.
*
* We can not delegate the work below to subclasses, because XML unmarshalling of a geodetic CRS will
* NOT create an instance of a subclass (because the distinction between geographic and geocentric CRS
* is not anymore in ISO 19111:2007).
*/
final boolean isBaseCRS;
if (isWKT1) {
if (!isGeographicWKT1) {
// If not geographic, then presumed geocentric.
if (cs instanceof CartesianCS) {
cs = Legacy.forGeocentricCRS((CartesianCS) cs, true);
} else {
// SphericalCS was not supported in WKT 1.
formatter.setInvalidWKT(cs, null);
}
}
isBaseCRS = false;
} else {
isBaseCRS = isBaseCRS(formatter);
}
/*
* Format the coordinate system, except if this CRS is the base CRS of an AbstractDerivedCRS in WKT 2 format.
* This is because ISO 19162 omits the coordinate system definition of enclosed base CRS in order to simplify
* the WKT. The 'formatCS(…)' method may write axis unit before or after the axes depending on whether we are
* formatting WKT version 1 or 2 respectively.
*
* Note that even if we do not format the CS, we may still write the units if we are formatting in "simplified"
* mode (as opposed to the more verbose mode). This looks like the opposite of what we would expect, but this is
* because formatting the unit here allow us to avoid repeating the unit in projection parameters when this CRS
* is part of a ProjectedCRS. Note however that in such case, the units to format are the angular units because
* the linear units will be formatted in the enclosing PROJCS[…] element.
*/
if (!isBaseCRS || convention == Convention.INTERNAL) {
// Will also format the axes unit.
formatCS(formatter, cs, ReferencingUtilities.getUnit(cs), isWKT1);
} else if (convention.isSimplified()) {
formatter.append(formatter.toContextualUnit(angularUnit));
}
/*
* For WKT 1, the keyword depends on the subclass: "GeogCS" for GeographicCRS or "GeocCS" for GeocentricCRS.
* However we can not rely on the subclass for choosing the keyword, because after XML unmarhaling we only
* have a GeodeticCRS. We need to make the choice in this base class. The CS type is a sufficient criterion.
*/
if (isWKT1) {
return isGeographicWKT1 ? WKTKeywords.GeogCS : WKTKeywords.GeocCS;
} else {
return isBaseCRS ? WKTKeywords.BaseGeodCRS : formatter.shortOrLong(WKTKeywords.GeodCRS, WKTKeywords.GeodeticCRS);
}
}
use of org.opengis.referencing.datum.PrimeMeridian in project sis by apache.
the class BursaWolfParameters method verify.
/**
* Verifies parameters validity after initialization of {@link DefaultGeodeticDatum}.
* This method requires that the prime meridian of the target datum is either the same
* than the enclosing {@code GeodeticDatum}, or Greenwich. We put this restriction for
* avoiding ambiguity about whether the longitude rotation should be applied before or
* after the datum shift.
*
* <p>If the target prime meridian is Greenwich, then SIS will assume that the datum shift
* needs to be applied in a coordinate system having Greenwich as the prime meridian.</p>
*
* <p><b>Maintenance note:</b>
* if the above policy regarding prime meridians is modified, then some {@code createOperationStep(…)} method
* implementations in {@link org.apache.sis.referencing.operation.CoordinateOperationFinder} may need to be
* revisited. See especially the methods creating a transformation between a pair of {@code GeocentricCRS} or
* between a pair of {@code GeographicCRS} (tip: search for {@code DefaultGeodeticDatum}).</p>
*
* @param pm the prime meridian of the enclosing {@code GeodeticDatum}.
*/
void verify(final PrimeMeridian pm) throws IllegalArgumentException {
if (targetDatum != null) {
final PrimeMeridian actual = targetDatum.getPrimeMeridian();
if (actual.getGreenwichLongitude() != 0 && !Utilities.equalsIgnoreMetadata(pm, actual)) {
throw new IllegalArgumentException(Resources.format(Resources.Keys.MismatchedPrimeMeridian_2, IdentifiedObjects.getName(pm, null), IdentifiedObjects.getName(actual, null)));
}
}
ensureFinite("tX", tX);
ensureFinite("tY", tY);
ensureFinite("tZ", tZ);
ensureFinite("rX", rX);
ensureFinite("rY", rY);
ensureFinite("rZ", rZ);
// For preventing zero or negative value on the matrix diagonal.
ensureBetween("dS", -PPM, PPM, dS);
}
use of org.opengis.referencing.datum.PrimeMeridian in project sis by apache.
the class DefaultPrimeMeridian method equals.
/**
* Compares this prime meridian with the specified object for equality.
*
* @param object the object to compare to {@code this}.
* @param mode {@link ComparisonMode#STRICT STRICT} for performing a strict comparison, or
* {@link ComparisonMode#IGNORE_METADATA IGNORE_METADATA} for comparing only properties
* relevant to coordinate transformations.
* @return {@code true} if both objects are equal.
*/
@Override
public boolean equals(final Object object, final ComparisonMode mode) {
if (object == this) {
// Slight optimization.
return true;
}
if (super.equals(object, mode))
switch(mode) {
case STRICT:
{
final DefaultPrimeMeridian that = (DefaultPrimeMeridian) object;
return Numerics.equals(this.greenwichLongitude, that.greenwichLongitude) && Objects.equals(this.angularUnit, that.angularUnit);
}
case BY_CONTRACT:
{
final PrimeMeridian that = (PrimeMeridian) object;
return Numerics.equals(getGreenwichLongitude(), that.getGreenwichLongitude()) && Objects.equals(getAngularUnit(), that.getAngularUnit());
}
default:
{
final double v1 = getGreenwichLongitude(Units.DEGREE);
final double v2 = ReferencingUtilities.getGreenwichLongitude((PrimeMeridian) object, Units.DEGREE);
if (mode == ComparisonMode.IGNORE_METADATA) {
/*
* We relax the check on unit of measurement because EPSG uses sexagesimal degrees
* for the Greenwich meridian. Requirying the same unit would make more difficult
* for isWGS84(…) methods to recognize EPSG's WGS84. We allow this relaxation here
* because the unit of the prime meridian is usually not inherited by axes (indeed,
* they are often not the same in the EPSG dataset). The same is not true for other
* objects like DefaultEllipsoid.
*/
return Numerics.equals(v1, v2);
} else if (Numerics.epsilonEqual(v1, v2, Formulas.ANGULAR_TOLERANCE)) {
return true;
}
assert (mode != ComparisonMode.DEBUG) : Numerics.messageForDifference("greenwichLongitude", v1, v2);
}
}
return false;
}
use of org.opengis.referencing.datum.PrimeMeridian in project sis by apache.
the class StandardDefinitionsTest method testCreateGeographicCRS.
/**
* Compares the values created by {@code StandardDefinitions} against hard-coded constants.
* This method tests the following methods:
*
* <ul>
* <li>{@link StandardDefinitions#createEllipsoid(short)}</li>
* <li>{@link StandardDefinitions#createGeodeticDatum(short, Ellipsoid, PrimeMeridian)}</li>
* <li>{@link StandardDefinitions#createGeographicCRS(short, GeodeticDatum, EllipsoidalCS)}</li>
* </ul>
*
* The geodetic objects are compared against the {@link HardCodedCRS}, {@link HardCodedDatum} and
* {@link GeodeticDatumMock} constants. Actually this is more a test of the above-cited constants
* than a {@code StandardDefinitions} one - in case of test failure, any of those classes could be
* at fault.
*/
@Test
@DependsOnMethod("testCreateAxis")
public void testCreateGeographicCRS() {
final PrimeMeridian pm = StandardDefinitions.primeMeridian();
final EllipsoidalCS cs = (EllipsoidalCS) StandardDefinitions.createCoordinateSystem((short) 6422);
for (final CommonCRS e : CommonCRS.values()) {
final Ellipsoid ellipsoid = StandardDefinitions.createEllipsoid(e.ellipsoid);
switch(e) {
case WGS84:
compare(GeodeticDatumMock.WGS84.getEllipsoid(), ellipsoid);
break;
case WGS72:
compare(GeodeticDatumMock.WGS72.getEllipsoid(), ellipsoid);
break;
case NAD83:
compare(GeodeticDatumMock.NAD83.getEllipsoid(), ellipsoid);
break;
case NAD27:
compare(GeodeticDatumMock.NAD27.getEllipsoid(), ellipsoid);
break;
case SPHERE:
compare(GeodeticDatumMock.SPHERE.getEllipsoid(), ellipsoid);
break;
}
final GeodeticDatum datum = StandardDefinitions.createGeodeticDatum(e.datum, ellipsoid, pm);
switch(e) {
case WGS84:
compare(HardCodedDatum.WGS84, datum);
break;
case WGS72:
compare(HardCodedDatum.WGS72, datum);
break;
case SPHERE:
compare(HardCodedDatum.SPHERE, datum);
break;
}
final GeographicCRS crs = StandardDefinitions.createGeographicCRS(e.geographic, datum, cs);
Validators.validate(crs);
switch(e) {
case WGS84:
compare(HardCodedCRS.WGS84, crs);
break;
}
Validators.validate(crs);
}
}
use of org.opengis.referencing.datum.PrimeMeridian in project sis by apache.
the class Proj4 method definition.
/**
* Infers a {@literal Proj.4} definition from the given projected, geographic or geocentric coordinate reference system.
* This method does not need the Proj.4 native library; it can be used in a pure Java application.
* However the returned definition string may differ depending on whether the Proj.4 library is available or not.
*
* @param crs the coordinate reference system for which to create a Proj.4 definition.
* @return the definition of the given CRS in a Proj.4 format.
* @throws FactoryException if the Proj.4 definition string can not be created from the given CRS.
*/
public static String definition(final CoordinateReferenceSystem crs) throws FactoryException {
ArgumentChecks.ensureNonNull("crs", crs);
/*
* If the given CRS object is associated to a Proj.4 structure, let Proj.4 formats itself
* the definition string. Note that this operation may fail if there is no Proj.4 library
* in the current system, or no JNI bindings to that library.
*/
try {
for (final Identifier id : crs.getIdentifiers()) {
if (id instanceof PJ) {
return ((PJ) id).getCode();
}
}
} catch (UnsatisfiedLinkError e) {
// Thrown the first time that we try to use the library.
Logging.unexpectedException(Logging.getLogger(Modules.GDAL), Proj4.class, "definition", e);
} catch (NoClassDefFoundError e) {
// Thrown on all attempts after the first one.
Logging.recoverableException(Logging.getLogger(Modules.GDAL), Proj4.class, "definition", e);
}
/*
* If we found no Proj.4 structure, formats the definition string ourself. The string may differ from
* what Proj.4 would have given. In particular, we do not provide "+init=" or "+datum=" parameter.
* But the definition should still be semantically equivalent.
*/
final String method;
final GeodeticDatum datum;
final ParameterValueGroup parameters;
final CoordinateSystem cs = crs.getCoordinateSystem();
if (crs instanceof GeodeticCRS) {
if (cs instanceof EllipsoidalCS) {
method = "latlon";
} else if (cs instanceof CartesianCS) {
method = "geocent";
} else {
throw new FactoryException(Errors.format(Errors.Keys.UnsupportedCoordinateSystem_1, cs.getClass()));
}
datum = ((GeodeticCRS) crs).getDatum();
parameters = null;
} else if (crs instanceof ProjectedCRS) {
Projection c = ((ProjectedCRS) crs).getConversionFromBase();
datum = ((ProjectedCRS) crs).getDatum();
method = name(c.getMethod());
parameters = c.getParameterValues();
} else {
throw new FactoryException(Errors.format(Errors.Keys.UnsupportedType_1, crs.getClass()));
}
/*
* Append the map projection parameters. Those parameters may include axis lengths (a and b),
* but not necessarily. If axis lengths are specified, then we will ignore the Ellipsoid instance
* associated to the CRS.
*/
final StringBuilder definition = new StringBuilder(100);
definition.append(Proj4Factory.PROJ_PARAM).append(method);
boolean hasSemiMajor = false;
boolean hasSemiMinor = false;
if (parameters != null) {
definition.append(Proj4Factory.STANDARD_OPTIONS);
for (final GeneralParameterValue parameter : parameters.values()) {
if (parameter instanceof ParameterValue<?>) {
final ParameterValue<?> pv = (ParameterValue<?>) parameter;
final Object value;
Unit<?> unit = pv.getUnit();
if (unit != null) {
unit = Units.isAngular(unit) ? Units.DEGREE : unit.getSystemUnit();
// Always in metres or degrees.
value = pv.doubleValue(unit);
} else {
value = pv.getValue();
if (value == null) {
continue;
}
}
final String pn = name(parameter.getDescriptor());
hasSemiMajor |= pn.equals("a");
hasSemiMinor |= pn.equals("b");
definition.append(" +").append(pn).append('=').append(value);
}
}
}
/*
* Append datum information: axis lengths if they were not part of the parameters, then prime meridian.
*/
final Ellipsoid ellipsoid = datum.getEllipsoid();
if (!hasSemiMajor)
definition.append(" +a=").append(ellipsoid.getSemiMajorAxis());
if (!hasSemiMinor)
definition.append(" +b=").append(ellipsoid.getSemiMinorAxis());
final PrimeMeridian pm = datum.getPrimeMeridian();
if (pm != null) {
double lon = pm.getGreenwichLongitude();
final Unit<Angle> unit = pm.getAngularUnit();
if (unit != null) {
lon = unit.getConverterTo(Units.DEGREE).convert(lon);
}
definition.append(" +pm=").append(lon);
}
/*
* Appends axis directions. This method always format a vertical direction (up or down)
* even if the coordinate system is two-dimensional, because Proj.4 seems to require it.
* Also extract axis units in the process.
*/
// Horizontal at index 0, vertical at index 1.
final Unit<?>[] units = new Unit<?>[2];
boolean validCS = true;
definition.append(' ').append(Proj4Factory.AXIS_ORDER_PARAM);
final int dimension = Math.min(cs.getDimension(), 3);
boolean hasVertical = false;
for (int i = 0; i < dimension; i++) {
final CoordinateSystemAxis axis = cs.getAxis(i);
final AxisDirection dir = axis.getDirection();
int unitIndex = 0;
if (!AxisDirections.isCardinal(dir)) {
if (!AxisDirections.isVertical(dir)) {
throw new FactoryException(Errors.format(Errors.Keys.UnsupportedAxisDirection_1, dir));
}
hasVertical = true;
unitIndex = 1;
}
final Unit<?> old = units[unitIndex];
units[unitIndex] = axis.getUnit();
validCS &= (old == null || old.equals(units[unitIndex]));
definition.appendCodePoint(Character.toLowerCase(dir.name().codePointAt(0)));
}
if (!hasVertical && dimension < 3) {
// Add a UP direction if not already present.
definition.append('u');
}
/*
* Append units of measurement, then verify the coordinate system validity.
*/
for (int i = 0; i < units.length; i++) {
final Unit<?> unit = units[i];
if (unit != null && !unit.equals(Units.DEGREE) && !unit.equals(Units.METRE)) {
validCS &= Units.isLinear(unit);
definition.append(" +");
// "+vto_meter" parameter.
if (i == 1)
definition.append('v');
definition.append("to_meter=").append(Units.toStandardUnit(unit));
}
}
/*
* Append the "+towgs84" element if any. This is the last piece of information.
* Note that the use of a "+towgs84" parameter is an "early binding" approach,
* which is usually not recommended. But Proj4 works that way.
*/
if (validCS) {
if (datum instanceof DefaultGeodeticDatum) {
for (final BursaWolfParameters bwp : ((DefaultGeodeticDatum) datum).getBursaWolfParameters()) {
if (Utilities.equalsIgnoreMetadata(CommonCRS.WGS84.datum(), bwp.getTargetDatum())) {
definition.append(" +towgs84=").append(bwp.tX).append(',').append(bwp.tY).append(',').append(bwp.tZ);
if (!bwp.isTranslation()) {
definition.append(',').append(bwp.rX).append(',').append(bwp.rY).append(',').append(bwp.rZ).append(',').append(bwp.dS);
}
break;
}
}
}
return definition.toString();
}
/*
* If we reach this point, we detected a coordinate system that we can not format as a
* Proj.4 definition string. Format an error message with axis directions and units.
*/
definition.setLength(0);
definition.append('(');
for (int i = 0; i < units.length; i++) {
final CoordinateSystemAxis axis = cs.getAxis(i);
if (i != 0)
definition.append(", ");
definition.append(axis.getUnit()).append(' ').append(Types.getCodeName(axis.getDirection()));
}
throw new FactoryException(Errors.format(Errors.Keys.IllegalCoordinateSystem_1, definition.append(')')));
}
Aggregations