This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit b82266fafa3c23739352f61640d5fe6632c28719 Author: Martin Desruisseaux <[email protected]> AuthorDate: Wed May 25 15:14:22 2022 +0200 Use CRS axis name instead of grid axis name when possible. --- .../apache/sis/gui/coverage/GridSliceSelector.java | 49 +++++++++---- .../org/apache/sis/referencing/cs/AxisName.java | 84 ++++++++++++++++++++++ .../sis/referencing/cs/CoordinateSystems.java | 20 +++++- .../apache/sis/referencing/cs/package-info.java | 2 +- .../sis/referencing/cs/CoordinateSystemsTest.java | 13 +++- 5 files changed, 153 insertions(+), 15 deletions(-) diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridSliceSelector.java b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridSliceSelector.java index 5410efc0ba..dd515ddc25 100644 --- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridSliceSelector.java +++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridSliceSelector.java @@ -59,6 +59,7 @@ import org.apache.sis.gui.Widget; import org.apache.sis.gui.map.StatusBar; import org.apache.sis.measure.UnitFormat; import org.apache.sis.referencing.CRS; +import org.apache.sis.referencing.cs.CoordinateSystems; import org.apache.sis.util.iso.Types; import org.apache.sis.util.logging.Logging; import org.apache.sis.util.resources.Vocabulary; @@ -248,18 +249,7 @@ public class GridSliceSelector extends Widget { selected = selected.setRange(dim, min, min); // Initially selected slice. converter.configure(gg, gridToCRS, dim, min, max, envelope, resolution); converter.setTickSpacing(slider, slider.getWidth()); - /* - * Use the coordinate system axis abbreviation with its unit as a label, - * or default to grid axis name if we have ne information about the CRS. - */ - final String name; - final DimensionNameType axis = extent.getAxisType(dim).orElse(null); - if (axis != null) { - name = Types.getCodeTitle(axis).toString(locale); - } else { - name = vocabulary.getString(Vocabulary.Keys.Axis_1, dim); - } - label.setText(vocabulary.toLabel(name)); + label.setText(vocabulary.toLabel(converter.getAxisLabel(extent, vocabulary))); } } children.remove(childrenCount, children.size()); @@ -362,6 +352,7 @@ public class GridSliceSelector extends Widget { if (timeCRS == null) { final Unit<?> unit = axis.getUnit(); if (unit != null) { + final Locale locale = GridSliceSelector.this.locale; final UnitFormat f = new UnitFormat(locale != null ? locale : Locale.getDefault()); f.setStyle(UnitFormat.Style.NAME); unitName = ' ' + f.format(unit); @@ -415,6 +406,40 @@ public class GridSliceSelector extends Widget { } } + /** + * Returns a label for the slider, or {@code null} if unknown. + * The label is derived from the CRS axis name. If none, fallbacks on grid axis name. + * + * @param extent the grid geometry extent, used as a fallback if no CRS axis label is found. + * @param vocabulary localized resources for latitude, longitude, height and time words. + */ + final String getAxisLabel(final GridExtent extent, final Vocabulary vocabulary) { + if (axis != null) { + String label = CoordinateSystems.getShortName(axis, locale); + if (label != null) { + if (timeCRS == null) { + final Unit<?> unit = axis.getUnit(); + if (unit != null) { + String symbol = unit.toString(); + if (!symbol.isEmpty()) { + label = label + " (" + symbol + ')'; + } + } + } + return label; + } + } + /* + * Fallback on grid axis name if no CRS axis name is found. + */ + final DimensionNameType axis = extent.getAxisType(dimension).orElse(null); + if (axis != null) { + return Types.getCodeTitle(axis).toString(locale); + } else { + return vocabulary.getString(Vocabulary.Keys.Axis_1, dimension); + } + } + /** * Invoked when the user begins of finished to adjust the slider position. * The grid extent is updated only after the user finished to adjust. diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/AxisName.java b/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/AxisName.java new file mode 100644 index 0000000000..1b040b28ca --- /dev/null +++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/AxisName.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.referencing.cs; + +import java.util.Locale; +import java.util.regex.Pattern; +import org.opengis.referencing.cs.CoordinateSystemAxis; +import org.apache.sis.referencing.IdentifiedObjects; +import org.apache.sis.util.resources.Vocabulary; + + +/** + * Pattern of {@link CoordinateSystemAxis} together with resource to use for localized axis name. + * + * @author Martin Desruisseaux (Geomatys) + * @version 1.3 + * @since 1.3 + * @module + */ +final class AxisName { + /** + * Regular expressions for matching axis names. + */ + private static final AxisName[] KEYWORDS = { + new AxisName(".*\\blatitudes?\\b.*", Vocabulary.Keys.Latitude), + new AxisName(".*\\blongitudes?\\b.*", Vocabulary.Keys.Longitude), + new AxisName(".*\\bheights?\\b.*", Vocabulary.Keys.Height), + new AxisName(".*\\btimes?\\b.*", Vocabulary.Keys.Time) + }; + + /** + * The pattern to compare with an axis name. + */ + private final Pattern pattern; + + /** + * Key of localized word for the axis. + */ + private final short word; + + /** + * Creates a new pattern for axis name. + */ + private AxisName(final String regex, final short word) { + pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + this.word = word; + } + + /** + * Returns a short (if possible) localized name for the given axis. This method replaces + * names such as "Geodetic latitude" or "Geocentric latitude" by a simple "Latitude" word. + * This method can be used for example in column or row headers when the context is known + * and the space is rare. + * + * @param axis the axis for which to get a short label. + * @param locale desired locale for the label. + * @return a relatively short axis label, in the desired locale if possible. + */ + static String find(final CoordinateSystemAxis axis, final Locale locale) { + final String name = IdentifiedObjects.getName(axis, null); + if (name != null) { + for (final AxisName keyword : KEYWORDS) { + if (keyword.pattern.matcher(name).matches()) { + return Vocabulary.getResources(locale).getString(keyword.word); + } + } + } + return IdentifiedObjects.getDisplayName(axis, locale); + } +} diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/CoordinateSystems.java b/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/CoordinateSystems.java index cedd912c7b..b945ed49a4 100644 --- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/CoordinateSystems.java +++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/CoordinateSystems.java @@ -19,6 +19,7 @@ package org.apache.sis.referencing.cs; import java.util.List; import java.util.ArrayList; import java.util.Arrays; +import java.util.Locale; import java.util.Objects; import javax.measure.Unit; import javax.measure.UnitConverter; @@ -59,7 +60,7 @@ import static java.util.logging.Logger.getLogger; * between two coordinate systems. * * @author Martin Desruisseaux (IRD, Geomatys) - * @version 1.2 + * @version 1.3 * @since 0.4 * @module */ @@ -568,6 +569,23 @@ next: for (final CoordinateSystem cs : targets) { return directions; } + /** + * Returns a short (if possible) localized name for the given axis. This method replaces + * names such as "Geodetic latitude" or "Geocentric latitude" by a simple "Latitude" word. + * This method can be used for example in column or row headers when the context is known + * and the space is rare. + * + * @param axis the axis for which to get a short label. + * @param locale desired locale for the label. + * @return a relatively short axis label, in the desired locale if possible. + * + * @since 1.3 + */ + public static String getShortName(final CoordinateSystemAxis axis, final Locale locale) { + ArgumentChecks.ensureNonNull("axis", axis); + return AxisName.find(axis, locale); + } + /** * Returns the EPSG code of a coordinate system using the units and directions of given axes. * This method ignores axis metadata (names, abbreviation, identifiers, remarks, <i>etc.</i>). diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/package-info.java b/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/package-info.java index 955e459c3b..93184f4dd3 100644 --- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/package-info.java +++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/cs/package-info.java @@ -33,7 +33,7 @@ * and units between two coordinate systems, or filtering axes. * * @author Martin Desruisseaux (IRD, Geomatys) - * @version 1.2 + * @version 1.3 * @since 0.4 * @module */ diff --git a/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/CoordinateSystemsTest.java b/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/CoordinateSystemsTest.java index c8d00ec3a3..cc10dab379 100644 --- a/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/CoordinateSystemsTest.java +++ b/core/sis-referencing/src/test/java/org/apache/sis/referencing/cs/CoordinateSystemsTest.java @@ -16,6 +16,7 @@ */ package org.apache.sis.referencing.cs; +import java.util.Locale; import javax.measure.Unit; import javax.measure.IncommensurableException; import org.opengis.referencing.operation.Matrix; @@ -46,7 +47,7 @@ import static org.apache.sis.test.Assert.*; * Tests the {@link CoordinateSystems} class. * * @author Martin Desruisseaux (IRD, Geomatys) - * @version 1.1 + * @version 1.3 * @since 0.4 * @module */ @@ -326,6 +327,16 @@ public final strictfp class CoordinateSystemsTest extends TestCase { assertEqualsIgnoreMetadata(targetCS, actualCS); } + /** + * Tests {@link CoordinateSystems#getShortName(CoordinateSystemAxis, Locale)} + */ + @Test + public void testGetShortName() { + assertEquals("Latitude", CoordinateSystems.getShortName(HardCodedAxes.GEODETIC_LATITUDE, Locale.ENGLISH)); + assertEquals("Height", CoordinateSystems.getShortName(HardCodedAxes.ELLIPSOIDAL_HEIGHT, Locale.ENGLISH)); + assertEquals("Hauteur", CoordinateSystems.getShortName(HardCodedAxes.ELLIPSOIDAL_HEIGHT, Locale.FRENCH)); + } + /** * Tests {@link CoordinateSystems#getEpsgCode(Class, CoordinateSystemAxis...)} * with an ellipsoidal coordinate system.
