Care to share your knowledge about SVG and mapnik?

I have the impression that writing SVG that it’s actually rendered by mapnik is quite hard. Let me show you my latest frustrating failed attempt:

I have my own fork of osm-carto, and I want to render a green hatching pattern on Natural Reserves and National Parks. So I took the patterns/military_red_hatch.svg file, loaded it in InkScape, changed the hatching color from #ff5555 to #55ff55, and saved it as patterns/np_nr_green_hatch.svg.

On the CartoCSS side, I added this code which is also a ‘clone’ of the military red hatch code:

             line-width: 2;
         }
       }
+
+      // ::fill already exists in a previous branch, and seems like either
+      // `carto` and/or mapnik get confused by this
+      ::fill_2 {
+        // polygon-fill: #55ff55;
+        // polygon-opacity: 0.08;
+        polygon-pattern-file: url('patterns/np_nr_green_hatch.svg');
+        polygon-pattern-alignment: global;
+      }
     }
   }
 }

to the #protectted-areas layer. This (of course?) didn’t work. mapnik (actually kosmtik) loads the project fine (and I even compile the thing into MapnikXML to double check), but I get no green hatching. Notice the commented out code; I used this to make sure this is being rendered.

Remembering that InkScape does not always write ‘good SVGs’ (I wish I could point to some discussion mentioning this), I instead openes the original SVG with a text editor, changed the color and saved as above. And it didn’t work either. We’re talking about the same actual contents of the original file with only 4 bytes changed.

Finally, I thought it was a mapnik bug, having a polygon pattern only attachment not rendering, so I instead added the pattern to the ::narrowline attachment:

         [zoom >= 12] {
             line-width: 2;
         }
+
+        polygon-pattern-file: url('patterns/np_nr_green_hatch.svg');
+        polygon-pattern-alignment: global;
       }
     }
   }

This didn’t render either. I have the impression this is not the first time I get frustrated trying to produce SVGs that mapnik accepts and renders. As a last resort, I copied the military file onto the other one and retried. Nothing.

So, is it my SVGs, InkScape, my CartoCSS, mapnik or any combination of those to blame here?

I can’t comment on .svgs (I don’t use them myself, but probably should), but when something doesn’t appear, it normally means that I’ve forgotten to restart apache2 and renderd, or delete cached metatiles below /var, or there 's something cached on the client side.

Assuming it’s none of the above, maybe turn debug on for renderd and check nothing odd in syslog?

I’m not running renderd or Apache. This is development time, so like I mentioned it’s kosmtik + carto + pure mapnik.

OK, so I learned something today. Either kosmtik or mapnik caches the SVGs between reloads/map definition reads. This means even if you change the SVG on disk you won’t notice it on reloads. The only option I found was to kill and restart kosmtik.

Another thing I learned is that colors are hard: I was not seeing the green version of the danger_red_hatch.svg file because it was too close to the colors it was drawn over.

1 Like

I believe it’s a Mapnik thing, as I’ve seen it with non-javascript Mapnik use.

1 Like

Do you know if mod_tile automatically recreates the mapnik object when it reloads the project, or do we need to restart apache to make it reload? I’m not running one right now (I only render some areas of interest), but it’s planned in the near future.

If you’re using apache2 and renderd you’ll have services for both of those and you’ll need to restart both, and ideally be viewing from an incognito client browser window, to see updates.

If you’re using some other web server (kosmtik among other things might be that) and/or something else is talking to mapnik for you, see comments above (it depends).