1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 |
|
---|
6 | import java.text.NumberFormat;
|
---|
7 | import java.util.LinkedHashMap;
|
---|
8 | import java.util.Locale;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * A system of units used to express length and area measurements.
|
---|
15 | * @since 3406 (creation)
|
---|
16 | * @since 6992 (extraction in this package)
|
---|
17 | */
|
---|
18 | public class SystemOfMeasurement {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Metric system (international standard).
|
---|
22 | * @since 3406
|
---|
23 | */
|
---|
24 | public static final SystemOfMeasurement METRIC = new SystemOfMeasurement(1, "m", 1000, "km", 10000, "ha");
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Chinese system.
|
---|
28 | * @since 3406
|
---|
29 | */
|
---|
30 | public static final SystemOfMeasurement CHINESE = new SystemOfMeasurement(1.0/3.0, "\u5e02\u5c3a" /* chi */, 500, "\u5e02\u91cc" /* li */);
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Imperial system (British Commonwealth and former British Empire).
|
---|
34 | * @since 3406
|
---|
35 | */
|
---|
36 | public static final SystemOfMeasurement IMPERIAL = new SystemOfMeasurement(0.3048, "ft", 1609.344, "mi", 4046.86, "ac");
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Nautical mile system (navigation, polar exploration).
|
---|
40 | * @since 5549
|
---|
41 | */
|
---|
42 | public static final SystemOfMeasurement NAUTICAL_MILE = new SystemOfMeasurement(185.2, "kbl", 1852, "NM");
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Known systems of measurement.
|
---|
46 | * @since 3406
|
---|
47 | */
|
---|
48 | public static final Map<String, SystemOfMeasurement> ALL_SYSTEMS;
|
---|
49 | static {
|
---|
50 | ALL_SYSTEMS = new LinkedHashMap<>();
|
---|
51 | ALL_SYSTEMS.put(marktr("Metric"), METRIC);
|
---|
52 | ALL_SYSTEMS.put(marktr("Chinese"), CHINESE);
|
---|
53 | ALL_SYSTEMS.put(marktr("Imperial"), IMPERIAL);
|
---|
54 | ALL_SYSTEMS.put(marktr("Nautical Mile"), NAUTICAL_MILE);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** First value, in meters, used to translate unit according to above formula. */
|
---|
58 | public final double aValue;
|
---|
59 | /** Second value, in meters, used to translate unit according to above formula. */
|
---|
60 | public final double bValue;
|
---|
61 | /** First unit used to format text. */
|
---|
62 | public final String aName;
|
---|
63 | /** Second unit used to format text. */
|
---|
64 | public final String bName;
|
---|
65 | /** Specific optional area value, in squared meters, between {@code aValue*aValue} and {@code bValue*bValue}. Set to {@code -1} if not used.
|
---|
66 | * @since 5870 */
|
---|
67 | public final double areaCustomValue;
|
---|
68 | /** Specific optional area unit. Set to {@code null} if not used.
|
---|
69 | * @since 5870 */
|
---|
70 | public final String areaCustomName;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * System of measurement. Currently covers only length (and area) units.
|
---|
74 | *
|
---|
75 | * If a quantity x is given in m (x_m) and in unit a (x_a) then it translates as
|
---|
76 | * x_a == x_m / aValue
|
---|
77 | *
|
---|
78 | * @param aValue First value, in meters, used to translate unit according to above formula.
|
---|
79 | * @param aName First unit used to format text.
|
---|
80 | * @param bValue Second value, in meters, used to translate unit according to above formula.
|
---|
81 | * @param bName Second unit used to format text.
|
---|
82 | */
|
---|
83 | public SystemOfMeasurement(double aValue, String aName, double bValue, String bName) {
|
---|
84 | this(aValue, aName, bValue, bName, -1, null);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * System of measurement. Currently covers only length (and area) units.
|
---|
89 | *
|
---|
90 | * If a quantity x is given in m (x_m) and in unit a (x_a) then it translates as
|
---|
91 | * x_a == x_m / aValue
|
---|
92 | *
|
---|
93 | * @param aValue First value, in meters, used to translate unit according to above formula.
|
---|
94 | * @param aName First unit used to format text.
|
---|
95 | * @param bValue Second value, in meters, used to translate unit according to above formula.
|
---|
96 | * @param bName Second unit used to format text.
|
---|
97 | * @param areaCustomValue Specific optional area value, in squared meters, between {@code aValue*aValue} and {@code bValue*bValue}.
|
---|
98 | * Set to {@code -1} if not used.
|
---|
99 | * @param areaCustomName Specific optional area unit. Set to {@code null} if not used.
|
---|
100 | *
|
---|
101 | * @since 5870
|
---|
102 | */
|
---|
103 | public SystemOfMeasurement(double aValue, String aName, double bValue, String bName, double areaCustomValue, String areaCustomName) {
|
---|
104 | this.aValue = aValue;
|
---|
105 | this.aName = aName;
|
---|
106 | this.bValue = bValue;
|
---|
107 | this.bName = bName;
|
---|
108 | this.areaCustomValue = areaCustomValue;
|
---|
109 | this.areaCustomName = areaCustomName;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Returns the text describing the given distance in this system of measurement.
|
---|
114 | * @param dist The distance in metres
|
---|
115 | * @return The text describing the given distance in this system of measurement.
|
---|
116 | */
|
---|
117 | public String getDistText(double dist) {
|
---|
118 | return getDistText(dist, null, 0.01);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Returns the text describing the given distance in this system of measurement.
|
---|
123 | * @param dist The distance in metres
|
---|
124 | * @param format A {@link NumberFormat} to format the area value
|
---|
125 | * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}
|
---|
126 | * @return The text describing the given distance in this system of measurement.
|
---|
127 | * @since 6422
|
---|
128 | */
|
---|
129 | public String getDistText(final double dist, final NumberFormat format, final double threshold) {
|
---|
130 | double a = dist / aValue;
|
---|
131 | if (!Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false) && a > bValue / aValue)
|
---|
132 | return formatText(dist / bValue, bName, format);
|
---|
133 | else if (a < threshold)
|
---|
134 | return "< " + formatText(threshold, aName, format);
|
---|
135 | else
|
---|
136 | return formatText(a, aName, format);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Returns the text describing the given area in this system of measurement.
|
---|
141 | * @param area The area in square metres
|
---|
142 | * @return The text describing the given area in this system of measurement.
|
---|
143 | * @since 5560
|
---|
144 | */
|
---|
145 | public String getAreaText(double area) {
|
---|
146 | return getAreaText(area, null, 0.01);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Returns the text describing the given area in this system of measurement.
|
---|
151 | * @param area The area in square metres
|
---|
152 | * @param format A {@link NumberFormat} to format the area value
|
---|
153 | * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}
|
---|
154 | * @return The text describing the given area in this system of measurement.
|
---|
155 | * @since 6422
|
---|
156 | */
|
---|
157 | public String getAreaText(final double area, final NumberFormat format, final double threshold) {
|
---|
158 | double a = area / (aValue*aValue);
|
---|
159 | boolean lowerOnly = Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false);
|
---|
160 | boolean customAreaOnly = Main.pref.getBoolean("system_of_measurement.use_only_custom_area_unit", false);
|
---|
161 | if ((!lowerOnly && areaCustomValue > 0 && a > areaCustomValue / (aValue*aValue) && a < (bValue*bValue) / (aValue*aValue)) || customAreaOnly)
|
---|
162 | return formatText(area / areaCustomValue, areaCustomName, format);
|
---|
163 | else if (!lowerOnly && a >= (bValue*bValue) / (aValue*aValue))
|
---|
164 | return formatText(area / (bValue * bValue), bName + "\u00b2", format);
|
---|
165 | else if (a < threshold)
|
---|
166 | return "< " + formatText(threshold, aName + "\u00b2", format);
|
---|
167 | else
|
---|
168 | return formatText(a, aName + "\u00b2", format);
|
---|
169 | }
|
---|
170 |
|
---|
171 | private static String formatText(double v, String unit, NumberFormat format) {
|
---|
172 | if (format != null) {
|
---|
173 | return format.format(v) + " " + unit;
|
---|
174 | }
|
---|
175 | return String.format(Locale.US, "%." + (v<9.999999 ? 2 : 1) + "f %s", v, unit);
|
---|
176 | }
|
---|
177 | }
|
---|