
meta {
    title: "Hiking routes DEMO";
    description: "Demo/proof of concept for extended MapCSS rendering of route relations - requires JOSM patch";
    version: "0.1_2011-12-15";
}

/*
 * Key element of this style is that, with the patch, rule declarations are executed for all matching relations,
 * not just the first one.
 * Based on osmc:symbol tag, see http://wiki.openstreetmap.org/wiki/Key:osmc:symbol.
 */

way::osmc {
	_width: 10;
  _opacity: 0.6;
  _dashes_width: 6;
  _font_size: 9;
  _text_offset: 11;

  _count: 0;
  _index: 0;
}

/*
 * Count matching parent relations and set dashes size accordingly 
 */
 
relation[type=route][route=hiking] > way::osmc,
relation[type=route][route=foot] > way::osmc { 
  _count: set_prop(_count, osmc, prop(_count, osmc) + 1); 
}

way::osmc {
  _dashes_space: (prop(_count) > 1) ? 2 : 0;
  _dashes: list(prop(_dashes_width), (prop(_dashes_width) * (prop(_count) - 1)) + prop(_dashes_space));
}

/*
 * dashed route lines
 */
 
relation[osmc:symbol] > way::osmc {
  _color: regex("^([a-z]+):", parent_tag("osmc:symbol"));
  _color: (prop(_color) == "black") ? "white" : prop(_color);
	color: color(prop(_color));
  dashes: prop(_dashes, osmc);
  dashes-offset: prop(_index, osmc) * prop(_dashes_width, osmc);
	width: prop(_width, osmc);
  opacity: prop(_opacity, osmc);
  z-index: -1;

  _index: set_prop(_index, osmc, prop(_index, osmc) + 1);
}

relation[!osmc:symbol][type=route][route=hiking] > way::osmc_none,
relation[!osmc:symbol][type=route][route=foot] > way::osmc_none {
	color: grey;
  dashes: prop(_dashes, osmc);
  dashes-offset: prop(_index, osmc) * prop(_dashes_width, osmc);
	width: prop(_width, osmc);
  opacity: prop(_opacity, osmc);
  z-index: -1;

  _index: set_prop(_index, osmc, prop(_index, osmc) + 1);
}

/*
 * text z16-
 */

way::osmc_text {
  _index: 0;
}

relation[osmc:symbol] > way|z16-::osmc_text {
  text: parent_tag(name);
  _color: regex("^([a-z]+):", parent_tag("osmc:symbol"));
  _color: (prop(_color) == "black") ? "white" : prop(_color);
  text-color: color(prop(_color));
  text-offset: prop(_text_offset, osmc) * (prop(_index, osmc_text) + 1);
  font-size: prop(_font_size, osmc);
  
  _index: set_prop(_index, osmc_text, prop(_index, osmc_text) + 1);
}

relation[!osmc:symbol][type=route][route=hiking] > way|z16-::osmc_none,
relation[!osmc:symbol][type=route][route=foot] > way|z16-::osmc_none {
  text: parent_tag(name);
  text-color: grey;
  text-offset: prop(_text_offset, osmc) * (prop(_index, osmc_text) + 1);
  font-size: prop(_font_size, osmc);

  _index: set_prop(_index, osmc_text, prop(_index, osmc_text) + 1);
}

/*
 * text z14-15
 */

way::osmc_text {
  _index: 0;
}

relation[osmc:symbol] > way|z14-15::osmc_text {
  _ref: parent_tag(ref);
  /* osmc:symbol = waycolor:background:[foreground]:[foreground2:][text:textcolor]
     green:white::Vo:black
     blue:white:blue_cross
  */
  _text: regex("^[a-z]+:\\w+::(.*):[a-z]+", parent_tag("osmc:symbol"));
  _foreground: regex("^[a-z]+:\\w+:(\\w+)", parent_tag("osmc:symbol"));
  /* "any" function (MapCSS/0.2) would be nice here */
  text: (is_prop_set(_ref)) ? prop(_ref) : ((is_prop_set(_text)) ? prop(_text) : prop(_foreground));
  _color: regex("^([a-z]+):", parent_tag("osmc:symbol"));
  _color: (prop(_color) == "black") ? "white" : prop(_color);
  text-color: color(prop(_color));
  text-offset: prop(_text_offset, osmc) * (prop(_index, osmc_text) + 1);
  font-size: prop(_font_size, osmc);
  
  _index: set_prop(_index, osmc_text, prop(_index, osmc_text) + 1);
}

relation[!osmc:symbol][type=route][route=hiking] > way|z14-15::osmc_none,
relation[!osmc:symbol][type=route][route=foot] > way|z14-15::osmc_none {
  _ref: parent_tag(ref);
  _text: regex("^[a-z]+:\\w+::(.*):[a-z]+", parent_tag("osmc:symbol"));
  _foreground: regex("^[a-z]+:\\w+:(\\w+)", parent_tag("osmc:symbol"));
  /* "any" function (MapCSS/0.2) would be nice here */
  text: (is_prop_set(_ref)) ? prop(_ref) : ((is_prop_set(_text)) ? prop(_text) : prop(_foreground));
  text-color: grey;
  text-offset: prop(_text_offset, osmc) * (prop(_index, osmc_text) + 1);
  font-size: prop(_font_size, osmc);

  _index: set_prop(_index, osmc_text, prop(_index, osmc_text) + 1);
}

/*
 * symbols (for this demo only one per way on middle node)
 */

way::symbol {
  _symbol_drawn: false;
}
 
/* icons, e.g. osmc:symbol=blue:white:blue_cross */
relation[osmc:symbol=~/^[a-z]+:[a-z_]+:[a-z_]+/][eval(!prop(_symbol_drawn, symbol))]
  > way[JOSM_search("-(type:way nodes:-3)")]::icon_background {

  _icon_bg: regex("^[a-z]+:(\\w+):\\w+", parent_tag("osmc:symbol"));
  icon-image: concat("http://www.wanderreitkarte.de/symbols/base_", prop(_icon_bg), ".png");
  icon-width: 15;
  icon-height: 11;
  z-index: 1000;
}
relation[osmc:symbol=~/^[a-z]+:[a-z_]+:[a-z_]+/][eval(!prop(_symbol_drawn, symbol))]
  > way[JOSM_search("-(type:way nodes:-3)")]::icon_foreground {

  _icon_fg: regex("^[a-z]+:\\w+:(\\w+)$", parent_tag("osmc:symbol"));
  icon-image: concat("http://www.wanderreitkarte.de/symbols/icon_", prop(_icon_fg), ".png");
  z-index: 1001;

  _symbol_drawn: set_prop(_symbol_drawn, symbol, true);
}

/* text symbols, e.g. osmc:symbol=green:white::Vo:black */
relation[osmc:symbol=~/^[a-z]+:[a-z_]+::.*:[a-z]+$/][eval(!prop(_symbol_drawn, symbol))] 
  > way[JOSM_search("-(type:way nodes:-3)")]::icon_background {

  symbol-shape: square;
  symbol-size: 20;
  symbol-stroke-width: 1.5;
  symbol-stroke-color: color(regex("^([a-z]+):[a-z_]+::.*:[a-z]+$", parent_tag("osmc:symbol")));
  symbol-fill-color: color(regex("^[a-z]+:([a-z_]+)::.*:[a-z]+$", parent_tag("osmc:symbol")));

  text: regex("^[a-z]+:[a-z_]+::(.*):[a-z]+$", parent_tag("osmc:symbol"));
  text-color: color(regex("^[a-z]+:[a-z_]+::.*:([a-z]+)$", parent_tag("osmc:symbol")));
  text-anchor-vertical: center;
  text-anchor-horizontal: center;
  font-size: 12;
  font-weight: bold;

  z-index: 1000;

  _symbol_drawn: set_prop(_symbol_drawn, symbol, true);
}
