Author Topic: Script to semi-automate conversion of v3 map themes to v4 map themes  (Read 392 times)

Online karlchick

  • More than Newbie
  • *
  • Posts: 40
  • Thanked: 31 times
    • View Profile
  • Device: Samsung Galaxy A3 2017
At the moment I'm converting my OS Map theme from v3 to v4. I have been finding the process very mechanical and repetitive, so much so that once I I done around 2,500 lines out of 10,000 lines by hand I decided to quick put together a script to do most of the changes programatically. 

Attached is a VBScipt that does most of the changes required to convert a v3 theme to v4.

I quickly discovered that  v4 render engine is very intolerant to syntax errors and if any are present it will refuse to display anything. This means manually removing parts of the generated v4 theme until it works and locating the syntax error. The following are some examples of errors I found in several themes:
   - stroke-width="1.2p"
   - <rule case="off_pl"
   - close="yes"  (should be closed="yes")

I have converted the following themes using the script, in some cases requiring some additional custom mods:
   - OS Map
   - Outdoor (because it is small, simple and already has a v4 theme to compare with)
   - Waymark2a

I would consider the output of the script as a starting point only and not a final theme. In some cases it is possible to tweak the theme using the script, see the Outdoor script for an example. In some cases there are differences that have to manually changed in the generated theme. Waymark2a is an example of this, seems the v4 rendering in LoMaps has a bug, the following puts green dashes on service roads as well as raceways:

<rule e="way" k="highway" v="service|raceway" zoom-min="14">
   <line stroke="#ffffff" stroke-width="0.81" />
   <rule e="way" k="highway" v="raceway" zoom-min="14">
      <line stroke="#a5cba5" stroke-width="0.54" scale="all" stroke-dasharray="1,1" stroke-linecap="butt" />
   </rule>
   :


so I had to manually separate service and raceway rules to fix this:

<!-- service raceway -->
<rule e="way" k="highway" v="service" zoom-min="14">
   <line stroke="#ffffff" stroke-width="0.81" />
   :
<rule e="way" k="highway" v="raceway" zoom-min="14">
   <line stroke="#ffffff" stroke-width="0.81" />
   <line stroke="#a5cba5" stroke-width="0.54" scale="all" stroke-dasharray="1,1" stroke-linecap="butt" />
   :
 


I'll add the converted Outdoor and Waymark example themes in the next few posts. I'm using the initial conversion of my OS Map theme as a starting point for a v4 OS Map theme. I'm about 50% complete so far, but am battling with lack of symbol and text scaling in v4...

Please note that this script is a quick hack, there are probably better ways using XML libraries to do this... It does no error checking and you'll probably need to modify it for best results. It is a visual basic script and should work on a Windows command line using the following command line syntax:

     cscript v3_to_v4.vbs <v3 rendertheme>.xml <v4 rendertheme>.xml

Where:

        v3_to_v4.vbs                 -- Name of this script
        <v3 rendertheme>.xml         -- Input file that is a v3 rendertheme file, e.g. VoluntaryV3.xml
        <v4 rendertheme>.xml         -- Output file to be created as a v4 rendertheme file, e.g. VoluntaryV4.xml


Just putting this out there in case it is useful to other theme writers. It isn't really suitable for most people to use, but anyone with a basic understanding of visual basic and renderthemes should be OK.
I would check the examples in the following posts for examples of how to use the custom coding section and supporting functions...

Karl.
« Last Edit: May 01, 2020, 02:02:19 by karlchick »
 
The following users thanked this post: Magellan

Online karlchick

  • More than Newbie
  • *
  • Posts: 40
  • Thanked: 31 times
    • View Profile
  • Device: Samsung Galaxy A3 2017
Re: Script to semi-automate conversion of v3 renderThemes to v4
« Reply #1 on: April 30, 2020, 22:37:04 »
Attached is the Waymark-V2a theme in two forms:
   - Waymark-V4.xml is converted using the attached version of the v3_to_v4_Waymark.vbs script.
   - Waymark-V4a.xml is after some manual tweaks, read on...
 
the V4 of the theme is straight out of the script from V2a, with just one customisation to correct a syntax error:

      line = replace_text(line, "case="+Chr(34), "cat="+Chr(34) )

In the screenshots you can see that V4 has a rendering problem with service|raceway roads. I fixed this manually in V4a.



The script pseudo-converts the v3 extensions for rectangle captions using a thick outlined caption, as these are not supported in v4 mapsforge rendering engine.

The other thing you'll notice is when you switch to a v4 map the zoom level is out by 1, and you can see that some items that render at Z16, do not render at Z15, hence screenshot 4 shows them appearing as you zoom in to Z16.

Note that I have not checked if everything else in the theme works or not. But it probably is a good starting point now for getting a v4 map version of the Waymark-V2a theme working, if required.
« Last Edit: April 30, 2020, 22:41:02 by karlchick »
 
The following users thanked this post: Viajero Perdido, poutnikl

Online karlchick

  • More than Newbie
  • *
  • Posts: 40
  • Thanked: 31 times
    • View Profile
  • Device: Samsung Galaxy A3 2017
Re: Script to semi-automate conversion of v3 renderThemes to v4
« Reply #2 on: April 30, 2020, 22:47:19 »
Here is an example of adding customization code to the script. I used the Outdoor theme from locus store which works with v3 maps because it is a very small and simple theme (only 1400 lines).

In the customisation section of the script is uses the keys and values of rules in the theme to isolate and apply specific modifications to certain tags and their attributes, e.g.:

      if inscope(current_keys, "highway") then
         if current(current_vals) = "motorway_junction" then
            if current_tag = "circle" then
               line = update_tag(line, "r", 1.5, 0)
               line = update_tag(line, "stroke-width", 2, 0)
            end if
            if current_tag = "caption" then
               line = update_tag(line, "font-size", 1, 3)
               if instr(line,"caption k="+Chr(34)+"addr:housenumber"+Chr(34)) > 0 then
                  line = update_tag(line, "dy", 0, 0)
               end if
            end if
         end if
      end if


This looks for lines of the theme that are in scope of k="highway" v="motorway_junction" and then tweaks:
   - the circle's radius and stroke-width
   - its caption's  font-size and dy offset (but only for addr:housenumbers captions).

Here is the theme content before from v3:

<!-- highway exits -->
<rule e="node" k="highway" v="motorway_junction" zoom-min="11" zoom-max="13">
   <circle r="6" scale-radius="false" fill="#dFdFFF" stroke="#FF0000" stroke-width="2.0"/>
</rule>
<rule e="node" k="highway" v="motorway_junction" zoom-min="14">
   <circle r="6" scale-radius="true" fill="#dFdFFF" stroke="#FF0000" stroke-width="1.2"/>
   <caption k="addr:housenumber" display="always" dx="-2" dy="10" priority="95" font-style="bold" font-size="6dp" fill="#FF0000" stroke="#FFFF00" stroke-width="1.2dp" scale-font-size="14,1.3"/>
   <caption k="name" display="always" dy="-14dp" scale-dy-size="16,1.2" font-style="bold" font-size="7dp" fill="#0100C8" stroke="#FFFFFF" stroke-width="1.2dp" scale-font-size="14,1.3"/>
</rule>


and after in the converted v4 version:

<!-- highway exits -->
<rule e="node" k="highway" v="motorway_junction" zoom-min="10" zoom-max="12">
   <circle r="2.7" scale-radius="false" fill="#dFdFFF" stroke="#FF0000" stroke-width="0.9"/>
</rule>
<rule e="node" k="highway" v="motorway_junction" zoom-min="13">
   <circle r="2.7" scale-radius="true" fill="#dFdFFF" stroke="#FF0000" stroke-width="0.54"/>
   <caption k="addr:housenumber" display="always" dy="0" priority="95" font-style="bold" font-size="9" fill="#FF0000" stroke="#FFFF00" stroke-width="1.08" />
   <caption k="name" display="always" dy="-14" font-style="bold" font-size="10" fill="#0100C8" stroke="#FFFFFF" stroke-width="1.08" />
</rule>


I should point out that the stroke-width of circles also have a scale of 0.9 and 0.25 applied to it elsewhere in the script, hence 2 * 0.9 * 0.25 * 2 = 0.9. As you can guess, I was just applying correction by trial error until things looked reasonable...

Note the parameters for update_tag are:
   - update_tag( line, tag_name, scale, offset )
and it applies a scale and offset to numerical tag values. E.g. The following line:

         line = update_tag(line, "font-size", 2, -4)

when applied to the following theme line:

   <pathText k="name" font-style="bold" font-size="9" fill="#606060" stroke="#ffffff" stroke-width="2.0"/>

will result in the font-size being processed as:  new-font-size = (old-font-size * 2) - 4 = 14. i.e.:

   <pathText k="name" font-style="bold" font-size="14" fill="#606060" stroke="#ffffff" stroke-width="2.0"/>.

The script has a variable called "zoom_offset" which is set to -1 in this example. It subtracts 1 from all the zoom-min and zoom-max values to readjust the v4 converted theme to look similar in scales as the original v3 theme. Of course, if there are map elements that appear at zoom level 14, the will not appear at zoom level 13 in the v4 map.

The screenshots show the original v3 Outdoor theme, converted to v4 theme and the original OutdoorV4 theme.



I'm posting the converted outdoor theme only as an example of what the script can do. I haven't checked it extensively and you are probably better off using the official OutdoorV4 theme.
« Last Edit: May 01, 2020, 00:49:21 by karlchick »
 
The following users thanked this post: jonny.blue

Online karlchick

  • More than Newbie
  • *
  • Posts: 40
  • Thanked: 31 times
    • View Profile
  • Device: Samsung Galaxy A3 2017
Re: Script to semi-automate conversion of v3 renderThemes to v4
« Reply #3 on: April 30, 2020, 23:29:45 »
If you wish to try out any of these converted themes, you should be able to copy the v4 xml file into the directory containing the original v3 theme file, if you are using the latest version of Locus Map as this now seems to support multiple theme files in the same directory. 

If you really want to use the converted theme and want the same icon, you could create a copy of the PNG icon file with the same name as the v4 theme.
 
The following users thanked this post: Andrew Heard

Offline poutnikl

  • Master of Locus
  • ****
  • Posts: 607
  • Thanked: 63 times
    • View Profile
    • My Brouter Profiles
  • Device: Xiaomi MI A2 (6G/128G) / A10 / LM3 Pro + LM4 alpha
I like WaymarksV2a very much and my favourite feature is the selective choice of turning on/off international, national, regional and local cycle networks. That is not possible in Elevate theme I usually use for cycling scenario.

Thank you very much for your work, as I miss the theme for V4 maps and I can imagine how much work it is.

I have noticed, the V4a / Cycling does not unfortunately display the cycle networks at any zoom yet.

Sent from my Xiaomi MI A2 / Android 10, via Tapatalk
« Last Edit: May 01, 2020, 06:52:20 by poutnikl »
https://github.com/poutnikl/Brouter-profiles/wiki
When a user digs profiles up from the GitHub repositories,
they will be hopefully wrapped in some wiki knowledge....  :-)
Try also https://github.com/poutnikl/Brouter-profiles/wiki/Determining-optimal-bicycle-profile
 

Online karlchick

  • More than Newbie
  • *
  • Posts: 40
  • Thanked: 31 times
    • View Profile
  • Device: Samsung Galaxy A3 2017
Thank you very much for your work, as I miss the theme for V4 maps and I can imagine how much work it is.

I've not actually spent much effort on the Waymark theme. Apart from finding syntax errors, the script did all the work.

I have noticed, the V4a / Cycling does not unfortunately display the cycle networks at any zoom yet.

As said, the Waymark-V2a theme was developed for LoMap v3 maps. The converted version may well not display everything correctly in v4 maps if the maps use different tagging rules.

I don't know if ManfredG is still maintaining the Waymark-V2a theme anymore... or if they wish to work on a v4 theme... If not, perhaps someone who uses the theme extensively might like to use the converted version as a good starting point?
 

Offline poutnikl

  • Master of Locus
  • ****
  • Posts: 607
  • Thanked: 63 times
    • View Profile
    • My Brouter Profiles
  • Device: Xiaomi MI A2 (6G/128G) / A10 / LM3 Pro + LM4 alpha
I see. While I am generally more or less familiar with XML syntax, I cannot say so about map themes and their customisations.

My single attempt to modify zoom levels of displaying regional/local cycle routes in Elevate for V3 maps did not end well, but I may have given up too early. :-)

Sent from my Xiaomi MI A2 / Android 10, via Tapatalk
https://github.com/poutnikl/Brouter-profiles/wiki
When a user digs profiles up from the GitHub repositories,
they will be hopefully wrapped in some wiki knowledge....  :-)
Try also https://github.com/poutnikl/Brouter-profiles/wiki/Determining-optimal-bicycle-profile
 

Online karlchick

  • More than Newbie
  • *
  • Posts: 40
  • Thanked: 31 times
    • View Profile
  • Device: Samsung Galaxy A3 2017
If no-one wants to work a v4 Waymark theme then I might consider to take on the challenge, but at the moment I am focussing all my spare time on converting my OS Map theme to v4. This has been going well so far because I am furloughed off work at the moment, but I return to work next week, so progress will slow then... So it could be weeks or months before I have time to look at other themes...

Regarding changing zoom levels, you have to consider what tags are used and what zoom level the map elements appear at, this is different for different maps. Maps are usually provided with a tag-mapping xml file that defines what tags are used and when they appear.
 

Offline poutnikl

  • Master of Locus
  • ****
  • Posts: 607
  • Thanked: 63 times
    • View Profile
    • My Brouter Profiles
  • Device: Xiaomi MI A2 (6G/128G) / A10 / LM3 Pro + LM4 alpha

Regarding changing zoom levels, you have to consider what tags are used and what zoom level the map elements appear at, this is different for different maps. Maps are usually provided with a tag-mapping xml file that defines what tags are used and when they appear.

Tag-mapping file was not needed. I have now made my mods of Elevate and Elevate  LE theme XMLs, making 3 changes:

LCN/NCN name captions appear already at  Zoom 11, where no RCN/LCN is displayed yet. So it is easily viewed at low zoom long distance planning.

I have cancelled the intense LCN/RCN display at zoom 12-13, as the network is often too dense and it disturbs perception of the map.

I have extended the emphasized routes display, originally for zoom 14+, to zoom levels 12-13, so they are displayed more decently.



Sent from my Xiaomi MI A2 / Android 10, via Tapatalk
« Last Edit: May 03, 2020, 07:24:26 by poutnikl »
https://github.com/poutnikl/Brouter-profiles/wiki
When a user digs profiles up from the GitHub repositories,
they will be hopefully wrapped in some wiki knowledge....  :-)
Try also https://github.com/poutnikl/Brouter-profiles/wiki/Determining-optimal-bicycle-profile
 

Online karlchick

  • More than Newbie
  • *
  • Posts: 40
  • Thanked: 31 times
    • View Profile
  • Device: Samsung Galaxy A3 2017
If no-one wants to work a v4 Waymark theme then I might consider to take on the challenge, but at the moment I am focussing all my spare time on converting my OS Map theme to v4. This has been going well so far because I am furloughed off work at the moment, but I return to work next week, so progress will slow then... So it could be weeks or months before I have time to look at other themes...

I have got a little distracted with the Waymark-V4a theme file - it bothers me when something needs fixing... I got the cycleways, hiking routes and waymark symbols displaying. I have now put the new Waymark-V4a.xml file into its own new [Theme] post.

Now I really must get back to completing the port of my OS Map v3 theme to v4 maps...