Phone number normalisation with style functions

Hi, Following a discussion on talk-gb about ways of formatting a phone number I decided to see how far I could get with the current style functions in mkgmap. I was very pleasantly surprised! The recent additions of subst with a regex and the part function have allowed me to get an almost perfect result with the countries I have looked at. I thought I would share my experiences in case others also find it useful. The goal is to normalise all POI phone numbers to international format so starting with a + followed by the country code etc. without any punctuation or extraneous information. I put the following code in "inc/phone" and used the include function to invoke that from "inc/address", instead of the existing two lines which derive mkgmap:phone. ========== start of script # reformat phone numbers # get the number to use phone=* | contact:phone=* | addr:phone=* {set cs:phone='${phone}' | '${contact:phone}' | '${addr:phone}'} # only use the first number if there is a list cs:phone=* {set cs:phone='${cs:phone|part:}'} # country-specific rules cs:phone=* & (mkgmap:country=GBR | mkgmap:country=IMN) {set cs:phone='${cs:phone|subst:(0)=>|subst:^00~>+|subst:[()]~>|subst:^0~>+44|subst:^+440~>+44}'} cs:phone=* & mkgmap:country=LUX {set cs:phone='${cs:phone|subst:^00~>+}'} cs:phone=* & mkgmap:country=IRL {set cs:phone='${cs:phone|subst:^00~>+|subst:[- ()]~>|subst:^0~>+353|subst:^+3530~>+353}'} cs:phone=* & mkgmap:country=NLD {set cs:phone='${cs:phone|subst:^00~>+|subst:[- ()]~>|subst:^0~>+31|subst:^+310~>+31}'} cs:phone=* & mkgmap:country=BEL {set cs:phone='${cs:phone|subst:^00~>+|subst:[- ()]~>|subst:^0~>+32|subst:^+320~>+32}'} cs:phone=* & mkgmap:country=FRA {set cs:phone='${cs:phone|subst:^00~>+|subst:^0~>+33}'} cs:phone=* & mkgmap:country=DEU {set cs:phone='${cs:phone|subst:^00~>+|subst:[- ()]~>|subst:^0~>+49|subst:^+490~>+49}'} # lose all non-digits (except for +) cs:phone=* {set cs:phone='${cs:phone|subst:[^0-9+]~>}'} # wrap up cs:phone=* {echo '(${mkgmap:country}) Phone number ${phone} reformatted to ${cs:phone}' } cs:phone=* {add mkgmap:phone='${cs:phone}'; set phone='${cs:phone}'; delete contact:phone; delete addr:phone;} ========== end of script A couple of things I missed: 1) the ability to do variable substitution within the old/new parameters to subst, e.g. subst:${old}~>${new} 2) the ability to use backreferences to parts of the pattern, e.g. subst:(abc)def~>{1} to delete occurrences of "def" when they are preceded by "abc" If these functions were to exist, many of the country-specific stuff could actually be collapsed into one or two rules, with variables for the country code and the trunk prefix (usually a 0 in Europe). Best regards, Colin

Am 24.08.2013 12:55, schrieb Colin Smale:
I put the following code in "inc/phone" and used the include function to invoke that from "inc/address", instead of the existing two lines which derive mkgmap:phone.
looks good ;-) I have made a test with a small area in Germany Bonn + 100Km, i didn't find any malformed phonenumber Bernd

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, thanks for the code (and of course for the idea to do something like this). I will give it a try. While adding it to my style I found maybe a bug with include style files. I have a folder style_rrk, including a folder inc and points-file etc. In inc-directory are several files which are included to points-file etc. Eg. inc/foo and inc/bar If I include inc/bar to inc/foo I also have to write include "inc/bar"; For me the logical expression would be include "bar"; because they are in the same directory? What do you think about this? Henning -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iQEcBAEBAgAGBQJSGLFdAAoJEPFgsWC7jOeTcmIIALL26d05+JwtxMFQrGxQDJji Nc6iSzBsMvnEb00jyXOYg968ISTuMv4U9nNoPlzpxwY45uZSAtgFsDmNkomaCdeB CQcoqQmAuIaE/7oEvBKMooC2jt5pE4tnqtstoXncNQMZzMLO2VqP0OsOwYIChFjD tIIvUYCbbuiej+wiFCy4db0UW+MvnHrfjpiMFOLrV4sUPUBlE3OueqjBY3R/q/wy 6QsQH9VH5dr82YoALW2Di81gE/LQljHWzCZb3qzKwUOE53KIZ0hNBD/H0oYITwVG yuCwjKkC39znCb6TwrIoAU0+DK01S9uGVpbDvatJlHRo+0SsGfwKVQRI02Z4OPo= =LgtP -----END PGP SIGNATURE-----
participants (3)
-
Bernd Weigelt
-
Colin Smale
-
Henning Scholland