drolbr / Overpass-API

A database engine to query the OpenStreetMap data.
http://overpass-api.de
GNU Affero General Public License v3.0
716 stars 90 forks source link

Feature Request: Understand units for comparisons #590

Closed westnordost closed 4 years ago

westnordost commented 4 years ago

Current situation

The comparison operator can be used to check the width of a street, the speed limit, the max height and weight, for example:

way[highway][maxspeed](if: t['maxspeed'] < 30)

will find all roads that have a very low speed limit below 30 km/h. But wait, what about roads tagged "25 mph"? They will be found by this expression as well, because Overpass is oblivious of units. This limits the usefulness of the comparison operator somewhat, especially for the United States and the United Kingdom and their colonies and overseas territories, which isn't exactly an insignificant part of the world.

Here is an overview over in which countries one can expect which units: https://github.com/westnordost/StreetComplete/blob/master/res/country_metadata/lengthUnits.yml https://github.com/westnordost/StreetComplete/blob/master/res/country_metadata/speedUnits.yml https://github.com/westnordost/StreetComplete/blob/master/res/country_metadata/weightLimitUnits.yml

Feature request

Overpass should when doing a comparison automatically convert values given in the imperial system before comparison. So the expression t['maxspeed'] < 30) or t['maxspeed'] < '19 mph') would evaluate to true for all of 29, 29 kph, 18 mph but not for 19 mph etc.

The same for the other units, such as foot and inch, short tons etc.

Implementation

I think it is not necessary (or feasible) for Overpass to know which tag key (should) use which unit. It would be enough that regardless of the key, if a string that ends with mph is encountered, it is just multiplied by 1.609344 before comparison, the same with the other common units. (The wiki entry is a bit complete about this, I am not sure if it makes sense to support all those mentioned there: https://wiki.openstreetmap.org/wiki/Map_Features/Units)

mmd-osm commented 4 years ago

I believe you need to read this blog post: http://dev.overpass-api.de/blog/compound_values.html#suffix

westnordost commented 4 years ago

Okay, so I understand that something like this is currently possible with way[highway][maxspeed](if: (suffix(t["maxspeed"]) == "mph" ? 1.609344 : 1) * number(t["maxspeed"]) < 50);

Still, I think it would be nice if that unit handling was part of the core functionality and be handled automatically for comparisons.

mmd-osm commented 4 years ago

I haven’t tested it but I would probably add some additional round brackets right before < 50, covering that suffix(t["maxspeed"]) == "mph" ? 1.609344 : 1) * number(t["maxspeed"]) part of the expression

Still, I think it would be nice if that unit handling was part of the core functionality and be handled automatically for comparisons.

Looking at the current design, I would assume this was a deliberate decision not to do exactly that.

westnordost commented 4 years ago

You are probably right, Overpass-QL is otherwise so generic that it might be unexpected if there is an automatic conversion like that.

However, the following would probably fit into how Overpass-QL works: way[highway][maxspeed](if: speed(t['maxspeed']) < 30)

So have converter functions like speed, length, weight etc to use alongside number.

mmd-osm commented 4 years ago

What I really meant was to avoid interpreting tag values in some own logic as much as possible. You see all the discussions around areas. Anything that depends in some way on tag (value) semantics will only increase support and maintenance effort.

westnordost commented 4 years ago

Actually, I haven't seen discussions. You mean the question "when is a closed way an area? -> depends on the tags?". But anyway, if the user specifies how to interpret a value by himself, as shown in my last example, wouldn't that be fine?

westnordost commented 4 years ago

But I understand if these kind of things are not understood as the core functionality of overpass.