#23702 closed enhancement (fixed)
mapcss: add unit conversion for length values
Reported by: | skyper | Owned by: | team |
---|---|---|---|
Priority: | normal | Milestone: | 24.05 |
Component: | Core | Version: | |
Keywords: | mapcss unit conversion lenght | Cc: | taylor.smock |
Description
A first step was added in r19089, see 23621#comment:13:
Copied from 23621#comment:15:
First impressions seems to be good, but I'm a bit puzzled on some things and have some questions on other things:
1) why does it return
-
on invalid inputs? This makes it fairly prone to errors like:
node[height ^= "-"] { throwError: tr("Surprisingly low value of {0} m", to_float(siunit_length(tag(height)))); }which crashes JOSM if validating an object with height=-1 (or gives a bad message if to_float is omitted, just included it here to showcase a worst case scenario). Why not returning
null
or so? The example mapcss is hypothetically by the way, not a suggestion :)
2) why not supporting negative numbers? For validation purposes, this makes a lot of sense, for instance the key
ele
has a lot of negative values.
3) as it's a conversion, wouldn't
to_SIunit_length
or so be a better name thansiunit_length
?
4) probably it should also support
km
andnmi
as they're explicitly listed on the wiki. Even though they're the defaults and could hypothetically be omitted, some people do include them :)
5) as it's always a Double, why not return it as a Double instead of a String 0:) ?
Bumping the priority a bit since it's almost release day with this new function :)
Attachments (0)
Change History (12)
comment:1 by , 9 months ago
comment:4 by , 9 months ago
Milestone: | → 24.05 |
---|
comment:5 by , 9 months ago
NOTE: This can produce ugly results due to the conversions. Normally that function should have a rounding possibility which rounds the final results to an approximate similar accuracy.
I.e. when input value has 2 significant digits, the result should never have more than 3.
Because it is a lot of work to get that right (especially for the ft+in combo), I leave that to a future update in case it is really needed.
comment:6 by , 9 months ago
Hmm, I don't know what PMD wants:
https://github.com/JOSM/josm/commit/86e4d775139717e88166dc0245ad517a409cb322#annotation_22274148759
Design/AvoidUncheckedExceptionsInSignatures: src/src/org/openstreetmap/josm/tools/Utils.java#L2072 https://pmd.github.io/pmd-6.55.0/pmd_rules_java_design.html#avoiduncheckedexceptionsinsignatures
Working link: https://pmd.github.io/pmd/pmd_rules_java_design.html#avoiduncheckedexceptionsinsignatures
Anybody else with a fix?
comment:8 by , 9 months ago
Literally what it says: don't put unchecked exceptions in signatures.
I would tend to disagree with the sentiment -- no, the consumer doesn't have to handle the exception, but it is nice to know where the exception might be occurring. I'll have to check and see if IDEs look at @throws
or just the method throws
definition.
I'll also check and see if PMD 7.3.0 is less chatty with our code base -- it looks like that check was disabled in 7.0.0 (although it might be re-enabled by now).
-
src/org/openstreetmap/josm/tools/Utils.java
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/src/org/openstreetmap/josm/tools/Utils.java b/src/org/openstreetmap/josm/tools/Utils.java
a b 2069 2069 * @throws IllegalArgumentException if input is no valid length 2070 2070 * @since 19089 2071 2071 */ 2072 public static Double unitToMeter(String s) throws IllegalArgumentException{2072 public static Double unitToMeter(String s) { 2073 2073 s = s.replaceAll(" ", "").replaceAll(",", "."); 2074 2074 Matcher m = PATTERN_LENGTH.matcher(s); 2075 2075 if (m.matches()) {
comment:9 by , 9 months ago
Hmm, so they suggest "@throws" instead of "throws" and not in addition to it because Java does not enforce catching them. One possible view, but I'm not really convinced. I'd rather mark it for ignore ;-)
comment:10 by , 9 months ago
It is still a problem with pmd 7.3.0, but it at least fixed the FPs for static imports (that was an issue with 7.0).
I'll go ahead and do a dependency update Thursday -- there are a lot of other "new" issues in 7.x that I need to go through and fix or ignore, so it isn't going to be a small dependency update.
The big one to ignore/properly configure is going to be MethodNamingConvention
-- it pretty much flags everything in mapcss Functions
.
If you want to see the "new" issues from PMD 7.2.0 as compared to PMD 6.55.0, here is a patch:
-
tools/ivy.xml
diff --git a/tools/ivy.xml b/tools/ivy.xml
a b 20 20 <!-- proguard->default --> 21 21 <dependency org="com.guardsquare" name="proguard-ant" rev="7.4.2" conf="proguard->default"/> 22 22 <!-- pmd->default --> 23 <!-- PMD 7.0.0 has too many false positives right now. When updating, don't forget to add pmd-ant as a new dependency --> 24 <dependency org="net.sourceforge.pmd" name="pmd-core" rev="6.55.0" conf="pmd->default"/> 25 <dependency org="net.sourceforge.pmd" name="pmd-java" rev="6.55.0" conf="pmd->default"/> 26 <dependency org="net.sourceforge.saxon" name="saxon" rev="9.1.0.8" conf="pmd->default"> 27 <artifact name="saxon" type="jar"/> 28 <artifact name="saxon" type="jar" maven:classifier="dom"/> 29 </dependency> 23 <dependency org="net.sourceforge.pmd" name="pmd-core" rev="7.2.0" conf="pmd->default"/> 24 <dependency org="net.sourceforge.pmd" name="pmd-ant" rev="7.2.0" conf="pmd->default"/> 25 <dependency org="net.sourceforge.pmd" name="pmd-java" rev="7.2.0" conf="pmd->default"/> 26 <dependency org="org.xmlresolver" name="xmlresolver" rev="6.0.4" conf="pmd->default"/> 30 27 <!-- spotbugs->default --> 31 28 <dependency org="com.github.spotbugs" name="spotbugs" rev="4.8.4" conf="spotbugs->default"/> 32 29 <dependency org="com.github.spotbugs" name="spotbugs-ant" rev="4.8.4" conf="spotbugs->default"/>
comment:11 by , 9 months ago
I have no issue with PMD messages going up. No need to hold back a version upgrade only to cleanup. We can do so after the upgrade.
comment:12 by , 9 months ago
Ordinarily, I'd agree with you. Except the PMD version bump adds ~830 new issues. I'd rather go through them and fix/ignore before application, especially since I'm not going to do that until Thursday.
Here is a breakdown of the "new" issues:
Count | Name | Notes |
---|---|---|
1 | AvoidUncheckedExceptionsInSignatures | |
7 | ConfusingArgumentToVarargsMethod | |
15 | ConsecutiveAppendsShouldReuse | |
331 | FieldNamingConventions | A good chunk is from xml deserialization; I'll probably whitelist those classes |
1 | FinalFieldCouldBeStatic | |
1 | InefficientStringBuffering | |
8 | LambdaCanBeMethodReference | |
1 | LongVariable | |
67 | MethodNamingConventions | Mostly from MapCSS Functions.java |
2 | PreserveStackTrace | |
2 | ShortMethodName | This is actually a local-only problem due to out-of-tree patches I have |
4 | SignatureDeclareThrowsException | |
66 | SimplifyBooleanReturns | |
21 | UnnecessaryBoxing | |
258 | UnnecessaryFullyQualifiedName | |
8 | UnnecessaryModifier | |
10 | UnnecessaryReturn | |
1 | UnnecessaryVarargsArrayCreation | |
1 | UnusedNullCheckInEquals | |
1 | UnusedPrivateField | |
1 | UseArraysAsList | |
22 | UseDiamondOperator | |
3 | UseExplicitTypes | |
1 | UseIndexOfChar | |
1 | UselessQualifiedThis |
Some of them will be easy to fix, others will be a bit more involved, and some will require configuration changes (FieldNamingConventions
specifically).
EDIT: It looks like we are ignoring some of them already (specifically FieldNamingConventions
). I suspect they got moved around.
It would be nice if the notation of feet plus inches like
6'7''
would also be supported without splitting the values manually prior to converting.