use of org.opengis.referencing.cs.AxisDirection in project sis by apache.
the class AxisDirectionsTest method verifyProperties.
/**
* Asserts that
* {@link AxisDirections#isGrid(AxisDirection)} and
* {@link AxisDirections#isSpatialOrUserDefined(AxisDirection, boolean)}
* return the expected value for all the given axis directions.
*/
private static void verifyProperties(final boolean isSpatial, final boolean isGrid, final boolean isDisplay, final AxisDirection... directions) {
for (final AxisDirection dir : directions) {
final String name = dir.name();
assertEquals(name, isGrid, AxisDirections.isGrid(dir));
assertEquals(name, isSpatial, AxisDirections.isSpatialOrUserDefined(dir, false));
assertEquals(name, isSpatial | isGrid | isDisplay, AxisDirections.isSpatialOrUserDefined(dir, true));
}
}
use of org.opengis.referencing.cs.AxisDirection in project sis by apache.
the class CodesTest method verify.
/**
* Compares the axis directions and units with EPSG definitions.
*
* @throws Exception if an error occurred while fetching the codes or querying the database.
*/
@Test
@SuppressWarnings("unchecked")
public void verify() throws Exception {
final CSAuthorityFactory factory = TestFactorySource.getSharedFactory();
final Field field = Codes.class.getDeclaredField("EPSG");
field.setAccessible(true);
for (final Codes c : ((Map<Codes, ?>) field.get(null)).keySet()) {
final CoordinateSystem cs = factory.createCoordinateSystem(String.valueOf(c.epsg));
final Unit<?> unit = cs.getAxis(0).getUnit();
final AxisDirection[] directions = new AxisDirection[cs.getDimension()];
for (int i = 0; i < directions.length; i++) {
assertEquals(unit, cs.getAxis(i).getUnit());
directions[i] = cs.getAxis(i).getDirection();
}
assertEquals("Codes.lookpup(…)", c.epsg, Codes.lookup(unit, directions));
}
}
use of org.opengis.referencing.cs.AxisDirection in project sis by apache.
the class DirectionAlongMeridian method parse.
/**
* If the specified name is a direction along some specific meridian,
* returns information about that. Otherwise returns {@code null}.
*
* @param name the name to parse.
* @return the parsed name, or {@code null} if it is not a direction along a meridian.
* @throws IllegalArgumentException if the given name looks like a direction along a meridian,
* but an error occurred during parsing.
*/
public static DirectionAlongMeridian parse(final String name) throws IllegalArgumentException {
final Matcher m = EPSG.matcher(name);
if (!m.matches()) {
// Not the expected pattern.
return null;
}
final AxisDirection baseDirection = AxisDirections.find(m.group(1), NORTH_SOUTH);
if (baseDirection == null) {
// We require "North" or "South" direction.
return null;
}
double meridian = Double.parseDouble(m.group(2));
final String group = m.group(3);
if (group != null) {
final AxisDirection sgn = AxisDirections.find(group, EAST_WEST);
if (sgn == null) {
// We require "East" or "West" direction.
return null;
}
if (sgn != AxisDirections.absolute(sgn)) {
meridian = -meridian;
}
}
return new DirectionAlongMeridian(baseDirection, meridian);
}
use of org.opengis.referencing.cs.AxisDirection in project sis by apache.
the class Normalizer method normalize.
/**
* Returns a new axis with the same properties (except identifiers) than given axis,
* but with normalized axis direction and unit of measurement.
*
* @param axis the axis to normalize.
* @param changes the change to apply on axis direction and units.
* @return an axis using normalized direction and units, or {@code axis} if there is no change.
*/
static CoordinateSystemAxis normalize(final CoordinateSystemAxis axis, final AxisFilter changes) {
final Unit<?> unit = axis.getUnit();
final AxisDirection direction = axis.getDirection();
final Unit<?> newUnit = changes.getUnitReplacement(axis, unit);
final AxisDirection newDir = changes.getDirectionReplacement(axis, direction);
/*
* Reuse some properties (name, remarks, etc.) from the existing axis. If the direction changed,
* then the axis name may need change too (e.g. "Westing" → "Easting"). The new axis name may be
* set to "Unnamed", but the caller will hopefully be able to replace the returned instance by
* an instance from the EPSG database with appropriate name.
*/
final boolean sameDirection = newDir.equals(direction);
if (sameDirection && newUnit.equals(unit)) {
return axis;
}
final String abbreviation = axis.getAbbreviation();
final String newAbbr = sameDirection ? abbreviation : AxisDirections.suggestAbbreviation(axis.getName().getCode(), newDir, newUnit);
final Map<String, Object> properties = new HashMap<>();
if (newAbbr.equals(abbreviation)) {
properties.putAll(IdentifiedObjects.getProperties(axis, EXCLUDES));
} else {
properties.put(NAME_KEY, UNNAMED);
}
/*
* Convert the axis range and build the new axis. The axis range will be converted only if
* the axis direction is the same or the opposite, otherwise we do not know what should be
* the new values. In the particular case of opposite axis direction, we need to reverse the
* sign of minimum and maximum values.
*/
if (sameDirection || newDir.equals(AxisDirections.opposite(direction))) {
final UnitConverter c;
try {
c = unit.getConverterToAny(newUnit);
} catch (IncommensurableException e) {
// Use IllegalStateException because the public API is an AbstractCS member method.
throw new IllegalStateException(Resources.format(Resources.Keys.IllegalUnitFor_2, "axis", unit), e);
}
double minimum = c.convert(axis.getMinimumValue());
double maximum = c.convert(axis.getMaximumValue());
if (!sameDirection) {
final double tmp = minimum;
minimum = -maximum;
maximum = -tmp;
}
properties.put(DefaultCoordinateSystemAxis.MINIMUM_VALUE_KEY, minimum);
properties.put(DefaultCoordinateSystemAxis.MAXIMUM_VALUE_KEY, maximum);
properties.put(DefaultCoordinateSystemAxis.RANGE_MEANING_KEY, axis.getRangeMeaning());
}
return new DefaultCoordinateSystemAxis(properties, newAbbr, newDir, newUnit);
}
use of org.opengis.referencing.cs.AxisDirection in project sis by apache.
the class CoordinateSystems method getAxisDirections.
/**
* Returns the axis directions for the specified coordinate system.
* This method guarantees that the returned array is non-null and does not contain any null direction.
*
* @param cs the coordinate system.
* @return the axis directions for the specified coordinate system.
* @throws NullArgumentException if {@code cs} is null, or one of its axes is null,
* or a value returned by {@link CoordinateSystemAxis#getDirection()} is null.
*
* @since 0.8
*/
public static AxisDirection[] getAxisDirections(final CoordinateSystem cs) {
ensureNonNull("cs", cs);
final AxisDirection[] directions = new AxisDirection[cs.getDimension()];
for (int i = 0; i < directions.length; i++) {
final CoordinateSystemAxis axis = cs.getAxis(i);
ensureNonNullElement("cs", i, cs);
ensureNonNullElement("cs[#].direction", i, directions[i] = axis.getDirection());
}
return directions;
}
Aggregations