HikingBikingMaps/RouteRendering

From OpenStreetMap Wiki
Jump to navigation Jump to search

This page gives technical details for implementing the route rendering described at HikingBikingMaps.

The layers and styles in the following are repeated just once for ncn routes; the repetition for other routes should be straightforward; there is only one layer necessary for all bicycle routes, and one for all hiking routes (if desired, they can probably also be combined into just one layer). Of course, the database parameters have to be filled in appropriately here.

<Layer name="routes-bicycle" status="on" srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs +over">
        <StyleName>bicycle</StyleName>
        <StyleName>shield-ncn</StyleName>
        <StyleName>shield-rcn</StyleName>
        <StyleName>shield-lcn</StyleName>
        <StyleName>bicycle-text</StyleName>
        <Datasource>
                <Parameter name="type">postgis</Parameter>
                <Parameter name="user">database user</Parameter>
                <Parameter name="password">database password</Parameter>
                <Parameter name="dbname">database name</Parameter>
                <Parameter name="estimate_extent">false</Parameter>
                <Parameter name="table">(select way,route,name,ref,network,char_length(ref) as length from planet_osm_line WHERE route='bicycle') as "bicycle"</Parameter>
                <Parameter name="extent">-20037508,-19929239,20037508,19929239</Parameter>
        </Datasource>
</Layer>

Additionally, an existing layer "planet roads text osm" has to be changed to include the route tag in the sql select statement

   <Layer name="planet roads text osm" status="on" srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs +over">
     <StyleName>directions</StyleName>
     <StyleName>roads-text</StyleName>
     <Datasource>
      <Parameter name="type">postgis</Parameter>
      <Parameter name="user">database user</Parameter>
      <Parameter name="password">database password</Parameter>
      <Parameter name="dbname">database name</Parameter>
      <Parameter name="table">
      (select way,highway,landuse,"natural",man_made,waterway,tourism,learning,amenity,place,name,ref,route,oneway,char_length(ref) as length from planet_osm_line where waterway IS NULL and leisure IS NULL and landuse IS NULL) as roads
      </Parameter>
      <Parameter name="estimate_extent">false</Parameter>
      <Parameter name="extent">-20037508,-19929239,20037508,19929239</Parameter>
    </Datasource>
  </Layer>

The styles for ncn routes are (first rule renders transparent routes in high zoom levels, second one renders solid routes in small zoom levels)

<Style name="bicycle">
  <Rule>
    <Filter>[network]='ncn'</Filter>
    <MaxScaleDenominator>100000</MaxScaleDenominator>
    <MinScaleDenominator>100</MinScaleDenominator>
    <LineSymbolizer>
      <CssParameter name="stroke">#ff3333</CssParameter>
      <CssParameter name="stroke-width">12</CssParameter>
      <CssParameter name="stroke-linejoin">round</CssParameter>
      <CssParameter name="stroke-linecap">round</CssParameter>
      <CssParameter name="stroke-opacity">0.5</CssParameter>
    </LineSymbolizer>
  </Rule>
  <Rule>
    <Filter>[network]='ncn'</Filter>
    <MaxScaleDenominator>5000000</MaxScaleDenominator>
    <MinScaleDenominator>100000</MinScaleDenominator>
    <LineSymbolizer>
      <CssParameter name="stroke">#ff3333</CssParameter>
      <CssParameter name="stroke-width">3</CssParameter>
      <CssParameter name="stroke-linejoin">round</CssParameter>
      <CssParameter name="stroke-linecap">round</CssParameter>
      <CssParameter name="stroke-opacity">1.0</CssParameter>
    </LineSymbolizer>
  </Rule>
...
</Style>

The text is rendered using these two styles for the different zoom levels

<Style name="bicycle-text">
  <Rule>
    <Filter>[network]='ncn'</Filter>
    <MaxScaleDenominator>100000</MaxScaleDenominator>
    <MinScaleDenominator>100</MinScaleDenominator>
    <TextSymbolizer name="name" face_name="DejaVu Sans Book" size="10" fill="#ff3333" halo_radius="1" placement="line" spacing="300"/>
  </Rule>
  <Rule>
    <Filter>[network]='ncn'</Filter>
    <MaxScaleDenominator>5000000</MaxScaleDenominator>
    <MinScaleDenominator>100000</MinScaleDenominator>
    <TextSymbolizer name="name" face_name="DejaVu Sans Book" size="10" fill="#ff3333" halo_radius="1" placement="line" spacing="300"/>
  </Rule>
...
</Style>

The shields use a quite repetitive style, which is repeated for different values of the field "[length]" from 1 to 8. This technique is copied from other locations in the style file, and needed to scale the shields according to the width of the ref text. The length field is computed on the fly using the sql code in the layer specification above.

<Style name="shield-ncn">
    <Rule>
      <Filter>[network] = 'ncn' and [length] = 1</Filter>
      <MaxScaleDenominator>1000000</MaxScaleDenominator>
      <MinScaleDenominator>100</MinScaleDenominator>
      <ShieldSymbolizer name="ref" face_name="DejaVu Sans Bold" size="11" fill="#ff3333" placement="line" file="svnmapnik/symbols/ncn_shield1.png" type="png" width="17" height="17" min_distance="100"/>
    </Rule>
...
</Style>

This code makes use of the tag "network", which is by default not included when importing OSM data into the PostGIS database. This is the reason, why the default.style has to be modified, as explained above.

Without further modifications, the route names are rendered twice adjacently, one time in black and one time in the route color. The reason is, that the rendering of road names also renders the route names (in black). This can be avoided by inserting the following rule (rendering nothing) just before the rule with the first <ElseFilter/> of the very long <Style name="roads-text"> style.

    <Rule>
      <MaxScaleDenominator>1000000</MaxScaleDenominator>
      <MinScaleDenominator>100</MinScaleDenominator>
      <Filter>[route] = 'bicycle' or [route] = 'foot'</Filter>
    </Rule>