node.jsmapnik

Rendering feature labels in mapnik


I am trying to render a mapnik stylesheet but nothing I try will render the labels. Here is my stylesheet:

<Map srs="+init=epsg:4326">
  <Style name="Polygon">
    <Rule>
      <LineSymbolizer stroke="black" stroke-width="2" />
      <TextSymbolizer face-name="DejaVu Sans" placement="interior" allow-overlap="true">[name]</TextSymbolizer>
    </Rule>
  </Style>
  <Layer name="polygon" srs="+init=epsg:4326">
    <StyleName>Polygon</StyleName>
    <Datasource>
      <Parameter name="type">geojson</Parameter>
      <Parameter name="inline"><![CDATA[
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "coordinates": [[
          [18.6609649658203, -32.59028569040753],
          [18.6536693572998, -32.59762547484460],
          [18.6737108230590, -32.59892704873228],
          [18.6609649658203, -32.59028569040753]
        ]],
        "type": "Polygon"
      },
      "properties": {
        "name": "polygon"
      }
    }
  ]
}
      ]]>
      </Parameter>
    </Datasource>
  </Layer>
</Map>

The map itself renders fine, but I can't get a label to appear. I am using node-mapnik which I've wrapped in a service inside a docker image, my code is here. I am loading the default fonts, and I have tried several variations on the syntax for <TextSymbolizer> but nothing makes any difference, the labels just don't appear.


Solution

  • For anyone else encountering this (because mapnik's docs are incomplete and in cases plain wrong) you need to load system fonts, not default fonts. In the code:

    const mapnik = require('mapnik');
    mapnik.register_system_fonts();
    

    Mapnik's example code for node shows mapnik.register_default_fonts();, but the folder that the fonts are supposed to be loaded from is empty and I can't see where they are supposed to come from.