Modify

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#9784 closed enhancement (fixed)

md5 hashcode in mapcss

Reported by: Klumbumbus Owned by: team
Priority: normal Milestone: 14.03
Component: Core mappaint Version:
Keywords: mapcss Cc:

Description

Is there a possibility to implement a function in mapcss which generates a md5 hashcode of a string?

Why I need such a or a similar function:
You may know the mappaint style Coloured Streets. Streets, buildings and address nodes with the same street name are highlighted in the same colour. (See example on Styles/Coloured_Streets). It is one of the most useful mappaint styles for JOSM in my opinion.
Currently it works this way: it uses the first character of the value of the "addr:street" key (resp. "name" for highways) to evaluate the colour. This has some disadvantages.

With a hashcode function this mappaint style could get a huge improvement. By using the first and/or last number of the hashcode of the street name instead of the street name itself, the following improvements result:

  • different streets with the same first letter, which are near to each other are displayed in different colours
  • typos in "name" and "addr:street" are displayed
  • it works worldwide, also in countries, where a lot of streets start and end with the same letter (france, russia,...)
  • it works for all unicode characters and not every single unicode character needs to added manually to the mappaint style code

If there is already a possibility to generate something like a hashcode of a (unicode) string, please tell me, otherwise could you please implement such a function?

Attachments (0)

Change History (23)

comment:1 by Don-vip, 10 years ago

Component: External mappaint styleCore mappaint
Keywords: mapcss added

comment:2 by stoecker, 10 years ago

If we implement such, then a newer one, e.g. sha256 or sha1.

comment:3 by bastiK, 10 years ago

Security of the hash function is not an issue in this case. But performance might be important.

A simple CRC32 checksum should be quite fast.

We may also need some kind of integer operations in mapcss to construct the color from the hash value.

in reply to:  3 comment:4 by stoecker, 10 years ago

Replying to bastiK:

Security of the hash function is not an issue in this case. But performance might be important.

For this application. Who knows for what purpose such a function will be used in future.

A simple CRC32 checksum should be quite fast.

We may also need some kind of integer operations in mapcss to construct the color from the hash value.

Propably CRC32 is a good choice. Nobody expects security from it and it is fast.

Or use a CRC24, it already has the right number of bits for a color. :-)

comment:5 by bastiK, 10 years ago

In 6899/josm:

see #9784 - md5 hashcode in mapcss

comment:6 by bastiK, 10 years ago

Try a style like this:

way[building]["addr:street"] {
    color: hsb_color(CRC32_checksum(tag("addr:street"))/4294967296.0, 0.9, 0.7);
    width: 10;
}

way[highway] {
    color: hsb_color(CRC32_checksum(tag("name"))/4294967296.0, 0.9, 0.7);
    width: 10;
}

Please close ticket, if this does the job for you.

comment:7 by bastiK, 10 years ago

If you need a second "random" value, you can do

CRC32_checksum(concat(tag("name"), "x"))

comment:8 by Klumbumbus, 10 years ago

Thanks for the fast implementation. I will test it and report here.

comment:9 by Don-vip, 10 years ago

Milestone: 14.03

comment:10 by Klumbumbus, 10 years ago

Resolution: fixed
Status: newclosed

It works fine. I'm about to edit the Coloured Streets style and I will release it as soon as the next stable version of JOSM is published.
Big thanks again!

comment:11 by Klumbumbus, 10 years ago

Resolution: fixed
Status: closedreopened

I just noticed, that CRC32_checksum() doesn't work correct for special characters like in the greek or russian alphabet. CRC32_checksum(Я) returns a hashcode of 1684325040. The same for Ш, И, Δ, Ω, ... They all return the same hashcode in JOSM, while on http://www.crc32generator.de/ they all return different hashcodes.
Some other special characters work well e.g. Æ, (although it is not the same hashcode as on http://www.crc32generator.de/).

Last edited 10 years ago by Klumbumbus (previous) (diff)

comment:12 by Don-vip, 10 years ago

Resolution: fixed
Status: reopenedclosed

The PHP implementation likely differs from Java's one.

comment:13 by stoecker, 10 years ago

Resolution: fixed
Status: closedreopened

@Don-Vip:

"They all return the same hashcode in JOSM" --> That should not happen I think. Probably you do not compute the hash over the binary, but over the string and these strings are first converted into a 8bit text before hashing? That would yield to unexpected results for any non-latin texts. The hashing should be done over utf-8 instead.

comment:14 by Klumbumbus, 10 years ago

For the Coloured Streets style we need a hashcode implementation, which can handle all unicode characters.

comment:15 by Don-vip, 10 years ago

Sorry read too fast, indeed the getBytes() call should be replaced by getBytes(UTF8).

comment:16 by Don-vip, 10 years ago

In 6908/josm:

see #9784 - UTF8 encoding of strings used in CRC2 computation

comment:17 by Don-vip, 10 years ago

Can you check tomorrow if it solves the problem? Thanks.

comment:18 by Klumbumbus, 10 years ago

Resolution: fixed
Status: reopenedclosed

It seems it works now correct. Thanks for the fix.

comment:19 by stoecker, 10 years ago

Not that you wonder - I splitted your description for the style properly into a German and an English page. This wiki has support for this :-)

comment:20 by Klumbumbus, 10 years ago

Yes, thats good. Thanks.

comment:21 by stoecker, 10 years ago

P.S. Tell us, when we can deleted the "alternate" style page.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain team.
as The resolution will be set.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.