/***************************************************************************** * * This file is part of Mapnik (c++ mapping toolkit) * * Copyright (C) 2011 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *****************************************************************************/ // mapnik #include #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_LIBXML2 #include #endif #include #include #include #include #include #include #include #include #include // boost #include #include #include #include #include #include #include #include #include // agg #include "agg_trans_affine.h" // stl #include using boost::lexical_cast; using boost::bad_lexical_cast; using boost::tokenizer; using std::cerr; using std::endl; namespace mapnik { using boost::optional; class map_parser : boost::noncopyable { public: map_parser( bool strict, std::string const& filename = "" ) : strict_( strict ), filename_( filename ), relative_to_xml_(true), font_manager_(font_engine_), color_grammar_(), // TODO - use xml encoding? tr_("utf8"), expr_grammar_(tr_) {} void parse_map(Map & map, xml_node const& sty, std::string const& base_path=""); private: void parse_map_include( Map & map, xml_node const& include); void parse_style(Map & map, xml_node const& sty); void parse_layer(Map & map, xml_node const& lay); void parse_metawriter(Map & map, xml_node const& lay); void parse_metawriter_in_symbolizer(symbolizer_base &sym, xml_node const& pt); void parse_fontset(Map & map, xml_node const & fset); void parse_font(font_set & fset, xml_node const& f); void parse_rule(feature_type_style & style, xml_node const & r); void parse_point_symbolizer(rule & rule, xml_node const& sym); void parse_line_pattern_symbolizer(rule & rule, xml_node const& sym); void parse_polygon_pattern_symbolizer(rule & rule, xml_node const& sym); void parse_text_symbolizer(rule & rule, xml_node const& sym); void parse_shield_symbolizer(rule & rule, xml_node const& sym); void parse_line_symbolizer(rule & rule, xml_node const& sym); void parse_polygon_symbolizer(rule & rule, xml_node const& sym); void parse_building_symbolizer(rule & rule, xml_node const& sym ); void parse_raster_symbolizer(rule & rule, xml_node const& sym ); void parse_markers_symbolizer(rule & rule, xml_node const& sym ); void parse_raster_colorizer(raster_colorizer_ptr const& rc, xml_node const& node ); void parse_stroke(stroke & strk, xml_node const & sym); expression_ptr parse_expr(std::string const& expr); void ensure_font_face( const std::string & face_name ); std::string ensure_relative_to_xml( boost::optional opt_path ); boost::optional get_opt_color_attr(boost::property_tree::ptree const& node, std::string const& name); bool strict_; std::string filename_; bool relative_to_xml_; std::map datasource_templates_; freetype_engine font_engine_; face_manager font_manager_; std::map file_sources_; std::map fontsets_; mapnik::css_color_grammar color_grammar_; mapnik::transcoder tr_; mapnik::expression_grammar expr_grammar_; }; #include void load_map(Map & map, std::string const& filename, bool strict) { xml_tree tree; tree.set_filename(filename); #ifdef HAVE_LIBXML2 read_xml2(filename, tree.root()); #else try { read_xml(filename, pt, boost::property_tree::xml_parser::no_concat_text|boost::property_tree::xml_parser::no_comments); remove_empty_text_nodes(pt); } catch (const boost::property_tree::xml_parser_error & ex) { throw config_error( ex.what() ); } #endif map_parser parser(strict, filename); parser.parse_map(map, tree.root()); dump_xml(tree.root()); } void load_map_string(Map & map, std::string const& str, bool strict, std::string const& base_path) { xml_tree tree; #ifdef HAVE_LIBXML2 if (!base_path.empty()) read_xml2_string(str, tree.root(), base_path); // accept base_path passed into function else read_xml2_string(str, tree.root(), map.base_path()); // default to map base_path #else try { std::istringstream s(str); // TODO - support base_path? read_xml(s, pt, boost::property_tree::xml_parser::no_concat_text|boost::property_tree::xml_parser::no_comments); remove_empty_text_nodes(pt); } catch (const boost::property_tree::xml_parser_error & ex) { throw config_error( ex.what() ) ; } #endif map_parser parser(strict, base_path); parser.parse_map(map, tree.root(), base_path); } expression_ptr map_parser::parse_expr(std::string const& str) { expression_ptr expr(boost::make_shared(true)); if (!expression_factory::parse_from_string(expr,str,expr_grammar_)) { throw mapnik::config_error( "Failed to parse expression '" + str + "'" ); } return expr; } boost::optional map_parser::get_opt_color_attr(boost::property_tree::ptree const& node, std::string const& name) { boost::optional str = node.get_optional( std::string(".") + name); boost::optional result; if (str && !str->empty()) { mapnik::color c; if (mapnik::color_factory::parse_from_string(c,*str,color_grammar_)) { result.reset(c); } else { throw config_error(std::string("Failed to parse attribute ") + name + "'. Expected color" + " but got '" + *str + "'"); } } return result; } void map_parser::parse_map(Map & map, xml_node const& pt, std::string const& base_path) { try { xml_node const& map_node = pt.get_child("Map"); try { parameters extra_attr; // Check if relative paths should be interpreted as relative to/from XML location // Default is true, and map_parser::ensure_relative_to_xml will be called to modify path optional paths_from_xml = map_node.get_opt_attr("paths-from-xml"); if (paths_from_xml) { relative_to_xml_ = *paths_from_xml; } optional base_path_from_xml = map_node.get_opt_attr("base"); if (!base_path.empty()) { map.set_base_path(base_path); } else if (base_path_from_xml) { map.set_base_path(*base_path_from_xml); } else { boost::filesystem::path xml_path(filename_); // TODO - should we make this absolute? #if (BOOST_FILESYSTEM_VERSION == 3) std::string base = xml_path.parent_path().string(); #else // v2 std::string base = xml_path.branch_path().string(); #endif map.set_base_path(base); } optional bgcolor = map_node.get_opt_attr("background-color"); if (bgcolor) { map.set_background(*bgcolor); } optional image_filename = map_node.get_opt_attr("background-image"); if (image_filename) { map.set_background_image(ensure_relative_to_xml(image_filename)); } map.set_srs(map_node.get_attr("srs", map.srs())); optional buffer_size = map_node.get_opt_attr("buffer-size"); if (buffer_size) { map.set_buffer_size(*buffer_size); } optional maximum_extent = map_node.get_opt_attr("maximum-extent"); if (maximum_extent) { box2d box; if (box.from_string(*maximum_extent)) { map.set_maximum_extent(box); } else { std::ostringstream s_err; s_err << "failed to parse 'maximum-extent'"; if (strict_) throw config_error(s_err.str()); else std::clog << "### WARNING: " << s_err.str() << std::endl; } } optional font_directory = map_node.get_opt_attr("font-directory"); if (font_directory) { extra_attr["font-directory"] = *font_directory; freetype_engine::register_fonts(ensure_relative_to_xml(font_directory), false); } optional min_version_string = map_node.get_opt_attr("minimum-version"); if (min_version_string) { extra_attr["minimum-version"] = *min_version_string; boost::char_separator sep("."); boost::tokenizer > tokens(*min_version_string, sep); unsigned i = 0; bool success = false; int n[3]; for (boost::tokenizer >::iterator beg = tokens.begin(); beg != tokens.end(); ++beg) { try { n[i] = boost::lexical_cast(boost::trim_copy(*beg)); } catch (boost::bad_lexical_cast & ex) { std::clog << *beg << " : " << ex.what() << "\n"; break; } if (i==2) { success = true; break; } ++i; } if (success) { int min_version = (n[0] * 100000) + (n[1] * 100) + (n[2]); if (min_version > MAPNIK_VERSION) { throw config_error(std::string("This map uses features only present in Mapnik version ") + *min_version_string + " and newer"); } } } map.set_extra_attributes(extra_attr); } catch (const config_error & ex) { ex.append_context("", &map_node, filename_); throw; } parse_map_include(map, map_node); } catch (node_not_found const&) { throw config_error("Not a map file. Node 'Map' not found."); } } void map_parser::parse_map_include(Map & map, xml_node const& include) { try { xml_node::const_iterator itr = include.begin(); xml_node::const_iterator end = include.end(); for (; itr != end; ++itr) { if (itr->is_text()) continue; if (itr->name() == "Include") { parse_map_include(map, *itr); } else if (itr->name() == "Style") { parse_style(map, *itr); } else if (itr->name() == "Layer") { parse_layer(map, *itr); } else if (itr->name() == "FontSet") { parse_fontset(map, *itr); } else if (itr->name() == "MetaWriter") { parse_metawriter(map, *itr); } else if (itr->name() == "FileSource") { std::string name = itr->get_attr("name"); std::string value = itr->get_text(); file_sources_[name] = value; } else if (itr->name() == "Datasource") { std::string name = itr->get_attr("name", std::string("Unnamed")); parameters params; xml_node::const_iterator paramIter = itr->begin(); xml_node::const_iterator endParam = itr->end(); for (; paramIter != endParam; ++paramIter) { if (paramIter->name() == "Parameter") { std::string name = paramIter->get_attr("name"); std::string value = paramIter->get_text(); params[name] = value; } } datasource_templates_[name] = params; } else if (itr->name() == "Parameters") { std::string name = itr->get_attr("name", std::string("Unnamed")); parameters & params = map.get_extra_parameters(); xml_node::const_iterator paramIter = itr->begin(); xml_node::const_iterator endParam = itr->end(); for (; paramIter != endParam; ++paramIter) { if (paramIter->name() == "Parameter") { std::string name = paramIter->get_attr("name"); bool is_string = true; boost::optional type = paramIter->get_opt_attr("type"); if (type) { if (*type == "int") { is_string = false; int value = paramIter->get_value(); params[name] = value; } else if (*type == "float") { is_string = false; double value = paramIter->get_value(); params[name] = value; } } if (is_string) { std::string value = paramIter->get_text(); params[name] = value; } } } } } } catch (const config_error & ex) { ex.append_context("", &include, filename_); throw; } map.init_metawriters(); } void map_parser::parse_style(Map & map, xml_node const& sty) { std::string name(""); try { name = sty.get_attr("name"); feature_type_style style; filter_mode_e filter_mode = sty.get_attr("filter-mode", FILTER_ALL); style.set_filter_mode(filter_mode); xml_node::const_iterator ruleIter = sty.begin(); xml_node::const_iterator endRule = sty.end(); for (; ruleIter!=endRule; ++ruleIter) { if (ruleIter->is("Rule")) { parse_rule(style, *ruleIter); } } map.insert_style(name, style); } catch (const config_error & ex) { ex.append_context(std::string("in style '") + name + "'", &sty, filename_); throw; } } void map_parser::parse_metawriter(Map & map, xml_node const& pt) { std::string name(""); metawriter_ptr writer; try { name = pt.get_attr("name"); writer = metawriter_create(pt); map.insert_metawriter(name, writer); } catch (const config_error & ex) { ex.append_context(std::string("in meta writer '") + name + "'", &pt, filename_); } } void map_parser::parse_fontset(Map & map, xml_node const& fset) { std::string name(""); try { name = fset.get_attr("name"); font_set fontset(name); xml_node::const_iterator itr = fset.begin(); xml_node::const_iterator end = fset.end(); for (; itr != end; ++itr) { if (itr->is("Font")) { parse_font(fontset, *itr); } } map.insert_fontset(name, fontset); // XXX Hack because map object isn't accessible by text_symbolizer // when it's parsed fontsets_.insert(pair(name, fontset)); } catch (const config_error & ex) { ex.append_context(std::string("in FontSet '") + name + "'", &fset, filename_); throw; } } void map_parser::parse_font(font_set &fset, xml_node const& f) { optional face_name = f.get_opt_attr("face-name"); if (face_name) { if ( strict_ ) { ensure_font_face(*face_name); } fset.add_face_name(*face_name); } else { throw config_error("Must have 'face-name' set", &f, filename_); } } void map_parser::parse_layer(Map & map, xml_node const& lay) { std::string name; try { name = lay.get_attr("name", std::string("Unnamed")); // XXX if no projection is given inherit from map? [DS] std::string srs = lay.get_attr("srs", map.srs()); layer lyr(name, srs); optional status = lay.get_opt_attr("status"); if (status) { lyr.setActive(*status); } optional minZoom = lay.get_opt_attr("minzoom"); if (minZoom) { lyr.setMinZoom( * minZoom ); } optional maxZoom = lay.get_opt_attr("maxzoom"); if (maxZoom) { lyr.setMaxZoom( * maxZoom ); } optional queryable = lay.get_opt_attr("queryable"); if (queryable) { lyr.setQueryable( * queryable ); } optional clear_cache = lay.get_opt_attr("clear-label-cache"); if (clear_cache) { lyr.set_clear_label_cache( * clear_cache ); } optional cache_features = lay.get_opt_attr("cache-features"); if (cache_features) { lyr.set_cache_features( * cache_features ); } optional group_by = lay.get_opt_attr("group-by"); if (group_by) { lyr.set_group_by( * group_by ); } xml_node::const_iterator child = lay.begin(); xml_node::const_iterator end = lay.end(); for(; child != end; ++child) { if (child->is("StyleName")) { std::string style_name = child->get_text(); if (style_name.empty()) { std::ostringstream ss; ss << "StyleName is empty in Layer: '" << lyr.name() << "'"; if (strict_) throw config_error(ss.str()); else std::clog << "### WARNING: " << ss.str() << std::endl; } else { lyr.add_style(style_name); } } else if (child->is("Datasource")) { parameters params; optional base = child->get_opt_attr("base"); if( base ) { std::map::const_iterator base_itr = datasource_templates_.find(*base); if (base_itr!=datasource_templates_.end()) params = base_itr->second; } xml_node::const_iterator paramIter = child->begin(); xml_node::const_iterator endParam = child->end(); for (; paramIter != endParam; ++paramIter) { if (paramIter->is("Parameter")) { std::string name = paramIter->get_attr("name"); std::string value = paramIter->get_text(); params[name] = value; } } boost::optional base_param = params.get("base"); boost::optional file_param = params.get("file"); if (base_param){ params["base"] = ensure_relative_to_xml(base_param); } else if (file_param){ params["file"] = ensure_relative_to_xml(file_param); } //now we are ready to create datasource try { boost::shared_ptr ds = datasource_cache::instance()->create(params); lyr.set_datasource(ds); } catch (const std::exception & ex ) { throw config_error( ex.what() ); } catch (...) { throw config_error("Unknown exception occured attempting to create datasoure for layer '" + lyr.name() + "'"); } } } map.addLayer(lyr); } catch (const config_error & ex) { if (!name.empty()) { ex.append_context(std::string(" encountered during parsing of layer '") + name + "'"); } throw; } } void map_parser::parse_rule(feature_type_style & style, xml_node const& r) { std::string name; try { name = r.get_attr("name", std::string()); rule rule(name); xml_node const* child = r.get_opt_child("Filter"); if (child) { rule.set_filter(child->get_value()); } if (r.has_child("ElseFilter")) { rule.set_else(true); } if (r.has_child("AlsoFilter")) { rule.set_also(true); } child = r.get_opt_child("MinScaleDenominator"); if (child) { rule.set_min_scale(child->get_value()); } child = r.get_opt_child("MaxScaleDenominator"); if (child) { rule.set_max_scale(child->get_value()); } xml_node::const_iterator symIter = r.begin(); xml_node::const_iterator endSym = r.end(); for( ;symIter != endSym; ++symIter) { if ( symIter->is("PointSymbolizer")) { parse_point_symbolizer(rule, *symIter); } else if (symIter->is("LinePatternSymbolizer")) { parse_line_pattern_symbolizer(rule, *symIter); } else if (symIter->is("PolygonPatternSymbolizer")) { parse_polygon_pattern_symbolizer(rule, *symIter); } else if (symIter->is("TextSymbolizer")) { parse_text_symbolizer(rule, *symIter); } else if (symIter->is("ShieldSymbolizer")) { parse_shield_symbolizer(rule, *symIter); } else if (symIter->is("LineSymbolizer")) { parse_line_symbolizer(rule, *symIter); } else if (symIter->is("PolygonSymbolizer")) { parse_polygon_symbolizer(rule, *symIter); } else if (symIter->is("BuildingSymbolizer")) { parse_building_symbolizer(rule, *symIter); } else if (symIter->is("RasterSymbolizer")) { parse_raster_symbolizer( rule, *symIter); } else if (symIter->is("MarkersSymbolizer")) { parse_markers_symbolizer(rule, *symIter); } } style.add_rule(rule); } catch (const config_error & ex) { if (!name.empty() ) { ex.append_context(std::string("in rule '") + name + "'"); } throw; } } void map_parser::parse_metawriter_in_symbolizer(symbolizer_base &sym, xml_node const &pt) { optional writer = pt.get_opt_attr("meta-writer"); if (!writer) return; optional output = pt.get_opt_attr("meta-output"); sym.add_metawriter(*writer, output); } void map_parser::parse_point_symbolizer(rule & rule, xml_node const & sym) { try { optional file = sym.get_opt_attr("file"); optional base = sym.get_opt_attr("base"); optional allow_overlap = sym.get_opt_attr("allow-overlap"); optional ignore_placement = sym.get_opt_attr("ignore-placement"); optional opacity = sym.get_opt_attr("opacity"); optional transform_wkt = sym.get_opt_attr("transform"); point_symbolizer symbol; if (allow_overlap) { symbol.set_allow_overlap( * allow_overlap ); } if (opacity) { symbol.set_opacity( * opacity ); } if (ignore_placement) { symbol.set_ignore_placement( * ignore_placement ); } point_placement_e placement = sym.get_attr("placement", CENTROID_POINT_PLACEMENT); symbol.set_point_placement( placement ); if (file) { try { if( base ) { std::map::const_iterator itr = file_sources_.find(*base); if (itr!=file_sources_.end()) { *file = itr->second + "/" + *file; } } *file = ensure_relative_to_xml(file); symbol.set_filename(parse_path(*file)); if (transform_wkt) { agg::trans_affine tr; if (!mapnik::svg::parse_transform((*transform_wkt).c_str(),tr)) { std::stringstream ss; ss << "Could not parse transform from '" << transform_wkt << "', expected string like: 'matrix(1, 0, 0, 1, 0, 0)'"; if (strict_) throw config_error(ss.str()); // value_error here? else std::clog << "### WARNING: " << ss << endl; } boost::array matrix; tr.store_to(&matrix[0]); symbol.set_transform(matrix); } } catch (image_reader_exception const & ex ) { std::string msg("Failed to load image file '" + * file + "': " + ex.what()); if (strict_) { throw config_error(msg); } else { std::clog << "### WARNING: " << msg << endl; } } } parse_metawriter_in_symbolizer(symbol, sym); rule.append(symbol); } catch (const config_error & ex) { ex.append_context("in PointSymbolizer"); throw; } } void map_parser::parse_markers_symbolizer(rule & rule, xml_node const& sym) { try { std::string filename(""); optional file = sym.get_opt_attr("file"); optional base = sym.get_opt_attr("base"); optional transform_wkt = sym.get_opt_attr("transform"); if (file) { try { if (base) { std::map::const_iterator itr = file_sources_.find(*base); if (itr!=file_sources_.end()) { *file = itr->second + "/" + *file; } } filename = ensure_relative_to_xml(file); } catch (...) { std::string msg("Failed to load marker file '" + *file + "'!"); if (strict_) { throw config_error(msg); } else { std::clog << "### WARNING: " << msg << endl; } } } markers_symbolizer symbol(parse_path(filename)); optional opacity = sym.get_opt_attr("opacity"); if (opacity) symbol.set_opacity( *opacity ); if (transform_wkt) { agg::trans_affine tr; if (!mapnik::svg::parse_transform((*transform_wkt).c_str(),tr)) { std::stringstream ss; ss << "Could not parse transform from '" << transform_wkt << "', expected string like: 'matrix(1, 0, 0, 1, 0, 0)'"; if (strict_) throw config_error(ss.str()); // value_error here? else std::clog << "### WARNING: " << ss << endl; } boost::array matrix; tr.store_to(&matrix[0]); symbol.set_transform(matrix); } optional c = sym.get_opt_attr("fill"); if (c) symbol.set_fill(*c); optional spacing = sym.get_opt_attr("spacing"); if (spacing) symbol.set_spacing(*spacing); optional max_error = sym.get_opt_attr("max-error"); if (max_error) symbol.set_max_error(*max_error); optional allow_overlap = sym.get_opt_attr("allow-overlap"); if (allow_overlap) symbol.set_allow_overlap(*allow_overlap); optional w = sym.get_opt_attr("width"); optional h = sym.get_opt_attr("height"); if (w && h) { symbol.set_width(*w); symbol.set_height(*h); } else if (w) { symbol.set_width(*w); symbol.set_height(*w); } else if (h) { symbol.set_width(*h); symbol.set_height(*h); } stroke strk; parse_stroke(strk,sym); symbol.set_stroke(strk); marker_placement_e placement = sym.get_attr("placement", MARKER_LINE_PLACEMENT); symbol.set_marker_placement( placement ); marker_type_e dfl_marker_type = ARROW; if (placement == MARKER_POINT_PLACEMENT) dfl_marker_type = ELLIPSE; marker_type_e marker_type = sym.get_attr("marker-type", dfl_marker_type); symbol.set_marker_type( marker_type ); parse_metawriter_in_symbolizer(symbol, sym); rule.append(symbol); } catch (const config_error & ex) { ex.append_context("in MarkersSymbolizer"); throw; } } void map_parser::parse_line_pattern_symbolizer( rule & rule, xml_node const & sym ) { try { std::string file = sym.get_attr("file"); optional base = sym.get_opt_attr("base"); try { if( base ) { std::map::const_iterator itr = file_sources_.find(*base); if (itr!=file_sources_.end()) { file = itr->second + "/" + file; } } file = ensure_relative_to_xml(file); line_pattern_symbolizer symbol(parse_path(file)); parse_metawriter_in_symbolizer(symbol, sym); rule.append(symbol); } catch (image_reader_exception const & ex ) { std::string msg("Failed to load image file '" + file + "': " + ex.what()); if (strict_) { throw config_error(msg); } else { std::clog << "### WARNING: " << msg << endl; } } } catch (const config_error & ex) { ex.append_context("in LinePatternSymbolizer"); throw; } } void map_parser::parse_polygon_pattern_symbolizer( rule & rule, xml_node const & sym ) { try { std::string file = sym.get_attr("file"); optional base = sym.get_opt_attr("base"); try { if( base ) { std::map::iterator itr = file_sources_.find(*base); if (itr!=file_sources_.end()) { file = itr->second + "/" + file; } } file = ensure_relative_to_xml(file); polygon_pattern_symbolizer symbol(parse_path(file)); // pattern alignment pattern_alignment_e p_alignment = sym.get_attr("alignment",LOCAL_ALIGNMENT); symbol.set_alignment(p_alignment); // gamma optional gamma = sym.get_opt_attr("gamma"); if (gamma) symbol.set_gamma(*gamma); // gamma method optional gamma_method = sym.get_opt_attr("gamma-method"); if (gamma_method) symbol.set_gamma_method(*gamma_method); parse_metawriter_in_symbolizer(symbol, sym); rule.append(symbol); } catch (image_reader_exception const & ex ) { std::string msg("Failed to load image file '" + file + "': " + ex.what()); if (strict_) { throw config_error(msg); } else { std::clog << "### WARNING: " << msg << endl; } } } catch (const config_error & ex) { ex.append_context("in PolygonPatternSymbolizer"); throw; } } void map_parser::parse_text_symbolizer( rule & rule, xml_node const& sym ) { try { text_placements_ptr placement_finder; optional placement_type = sym.get_opt_attr("placement-type"); if (placement_type) { placement_finder = placements::registry::instance()->from_xml(*placement_type, sym, fontsets_); } else { placement_finder = text_placements_ptr(boost::make_shared()); placement_finder->defaults.from_xml(sym, fontsets_); } if (strict_ && !placement_finder->defaults.format.fontset.size()) ensure_font_face(placement_finder->defaults.format.face_name); text_symbolizer text_symbol = text_symbolizer(placement_finder); parse_metawriter_in_symbolizer(text_symbol, sym); rule.append(text_symbol); } catch (const config_error & ex) { ex.append_context("in TextSymbolizer"); throw; } } void map_parser::parse_shield_symbolizer(rule & rule, xml_node const& sym ) { try { text_placements_ptr placement_finder; optional placement_type = sym.get_opt_attr("placement-type"); if (placement_type) { placement_finder = placements::registry::instance()->from_xml(*placement_type, sym, fontsets_); } else { placement_finder = text_placements_ptr(boost::make_shared()); } placement_finder->defaults.from_xml(sym, fontsets_); if (strict_ && !placement_finder->defaults.format.fontset.size()) ensure_font_face(placement_finder->defaults.format.face_name); shield_symbolizer shield_symbol = shield_symbolizer(placement_finder); /* Symbolizer specific attributes. */ optional transform_wkt = sym.get_opt_attr("transform"); if (transform_wkt) { agg::trans_affine tr; if (!mapnik::svg::parse_transform((*transform_wkt).c_str(),tr)) { std::stringstream ss; ss << "Could not parse transform from '" << transform_wkt << "', expected string like: 'matrix(1, 0, 0, 1, 0, 0)'"; if (strict_) throw config_error(ss.str()); // value_error here? else std::clog << "### WARNING: " << ss << endl; } boost::array matrix; tr.store_to(&matrix[0]); shield_symbol.set_transform(matrix); } // shield displacement double shield_dx = sym.get_attr("shield-dx", 0.0); double shield_dy = sym.get_attr("shield-dy", 0.0); shield_symbol.set_shield_displacement(shield_dx,shield_dy); // opacity optional opacity = sym.get_opt_attr("opacity"); if (opacity) { shield_symbol.set_opacity(*opacity); } // text-opacity // TODO: Could be problematic because it is named opacity in TextSymbolizer but opacity has a diffrent meaning here. optional text_opacity = sym.get_opt_attr("text-opacity"); if (text_opacity) { shield_symbol.set_text_opacity( * text_opacity ); } // unlock_image optional unlock_image = sym.get_opt_attr("unlock-image"); if (unlock_image) { shield_symbol.set_unlock_image( * unlock_image ); } parse_metawriter_in_symbolizer(shield_symbol, sym); std::string image_file = sym.get_attr("file"); optional base = sym.get_opt_attr("base"); try { if( base ) { std::map::const_iterator itr = file_sources_.find(*base); if (itr!=file_sources_.end()) { image_file = itr->second + "/" + image_file; } } image_file = ensure_relative_to_xml(image_file); shield_symbol.set_filename(parse_path(image_file)); } catch (image_reader_exception const & ex ) { std::string msg("Failed to load image file '" + image_file + "': " + ex.what()); if (strict_) { throw config_error(msg); } else { std::clog << "### WARNING: " << msg << endl; } } rule.append(shield_symbol); } catch (const config_error & ex) { ex.append_context("in ShieldSymbolizer"); throw; } } void map_parser::parse_stroke(stroke & strk, xml_node const & sym) { // stroke color optional c = sym.get_opt_attr("stroke"); if (c) strk.set_color(*c); // stroke-width optional width = sym.get_opt_attr("stroke-width"); if (width) strk.set_width(*width); // stroke-opacity optional opacity = sym.get_opt_attr("stroke-opacity"); if (opacity) strk.set_opacity(*opacity); // stroke-linejoin optional line_join = sym.get_opt_attr("stroke-linejoin"); if (line_join) strk.set_line_join(*line_join); // stroke-linecap optional line_cap = sym.get_opt_attr("stroke-linecap"); if (line_cap) strk.set_line_cap(*line_cap); // stroke-gamma optional gamma = sym.get_opt_attr("stroke-gamma"); if (gamma) strk.set_gamma(*gamma); // stroke-gamma-method optional gamma_method = sym.get_opt_attr("stroke-gamma-method"); if (gamma_method) strk.set_gamma_method(*gamma_method); // stroke-dashoffset optional dash_offset = sym.get_opt_attr("stroke-dashoffset"); if (dash_offset) strk.set_dash_offset(*dash_offset); // stroke-dasharray optional str = sym.get_opt_attr("stroke-dasharray"); if (str) { tokenizer<> tok (*str); std::vector dash_array; tokenizer<>::iterator itr = tok.begin(); for (; itr != tok.end(); ++itr) { try { double f = boost::lexical_cast(*itr); dash_array.push_back(f); } catch ( boost::bad_lexical_cast &) { throw config_error(std::string("Failed to parse dasharray ") + "'. Expected a " + "list of floats but got '" + (*str) + "'"); } } if (dash_array.size()) { size_t size = dash_array.size(); if ( size % 2) { for (size_t i=0; i < size ;++i) { dash_array.push_back(dash_array[i]); } } std::vector::const_iterator pos = dash_array.begin(); while (pos != dash_array.end()) { strk.add_dash(*pos,*(pos + 1)); pos +=2; } } } } void map_parser::parse_line_symbolizer( rule & rule, xml_node const & sym ) { try { stroke strk; parse_stroke(strk,sym); line_symbolizer symbol = line_symbolizer(strk); // rasterizer method line_rasterizer_e rasterizer = sym.get_attr("rasterizer", RASTERIZER_FULL); //optional rasterizer_method = sym.get_opt_attr("full"); symbol.set_rasterizer(rasterizer); parse_metawriter_in_symbolizer(symbol, sym); rule.append(symbol); } catch (const config_error & ex) { ex.append_context("in LineSymbolizer"); throw; } } void map_parser::parse_polygon_symbolizer( rule & rule, xml_node const & sym ) { try { polygon_symbolizer poly_sym; // fill optional fill = sym.get_opt_attr("fill"); if (fill) poly_sym.set_fill(*fill); // fill-opacity optional opacity = sym.get_opt_attr("fill-opacity"); if (opacity) poly_sym.set_opacity(*opacity); // gamma optional gamma = sym.get_opt_attr("gamma"); if (gamma) poly_sym.set_gamma(*gamma); // gamma method optional gamma_method = sym.get_opt_attr("gamma-method"); if (gamma_method) poly_sym.set_gamma_method(*gamma_method); parse_metawriter_in_symbolizer(poly_sym, sym); rule.append(poly_sym); } catch (const config_error & ex) { ex.append_context("in PolygonSymbolizer"); throw; } } void map_parser::parse_building_symbolizer( rule & rule, xml_node const & sym ) { try { building_symbolizer building_sym; // fill optional fill = sym.get_opt_attr("fill"); if (fill) building_sym.set_fill(*fill); // fill-opacity optional opacity = sym.get_opt_attr("fill-opacity"); if (opacity) building_sym.set_opacity(*opacity); // height optional height = sym.get_opt_attr("height"); if (height) building_sym.set_height(parse_expr(*height)); parse_metawriter_in_symbolizer(building_sym, sym); rule.append(building_sym); } catch (const config_error & ex) { ex.append_context("in BuildingSymbolizer"); throw; } } void map_parser::parse_raster_symbolizer( rule & rule, xml_node const & sym ) { try { raster_symbolizer raster_sym; // mode optional mode = sym.get_opt_attr("mode"); if (mode) raster_sym.set_mode(*mode); // scaling optional scaling = sym.get_opt_attr("scaling"); if (scaling) raster_sym.set_scaling(*scaling); // opacity optional opacity = sym.get_opt_attr("opacity"); if (opacity) raster_sym.set_opacity(*opacity); // filter factor optional filter_factor = sym.get_opt_attr("filter-factor"); if (filter_factor) raster_sym.set_filter_factor(*filter_factor); // mesh-size optional mesh_size = sym.get_opt_attr("mesh-size"); if (mesh_size) raster_sym.set_mesh_size(*mesh_size); xml_node::const_iterator cssIter = sym.begin(); xml_node::const_iterator endCss = sym.end(); for(; cssIter != endCss; ++cssIter) { if (cssIter->is("RasterColorizer")) { raster_colorizer_ptr colorizer(new raster_colorizer()); raster_sym.set_colorizer(colorizer); parse_raster_colorizer(colorizer, *cssIter); } } //Note: raster_symbolizer doesn't support metawriters rule.append(raster_sym); } catch (const config_error & ex) { ex.append_context("in RasterSymbolizer"); throw; } } void map_parser::parse_raster_colorizer(raster_colorizer_ptr const& rc, xml_node const& node ) { try { // mode colorizer_mode default_mode = node.get_attr("default-mode", COLORIZER_LINEAR); if(default_mode == COLORIZER_INHERIT) { throw config_error("RasterColorizer mode must not be INHERIT. "); } rc->set_default_mode( default_mode ); // default colour optional default_color = node.get_opt_attr("default-color"); if (default_color) { rc->set_default_color( *default_color ); } // epsilon optional eps = node.get_opt_attr("epsilon"); if (eps) { if(*eps < 0) { throw config_error("RasterColorizer epsilon must be > 0. "); } rc->set_epsilon( *eps ); } xml_node::const_iterator stopIter = node.begin(); xml_node::const_iterator endStop = node.end(); float maximumValue = -std::numeric_limits::max(); for(; stopIter != endStop; ++stopIter) { if (stopIter->is("stop")) { // colour is optional. optional stopcolor = stopIter->get_opt_attr("color"); if (!stopcolor) { *stopcolor = *default_color; } // mode default to INHERIT colorizer_mode mode = stopIter->get_attr("mode", COLORIZER_INHERIT); // value is required, and it must be bigger than the previous optional value = stopIter->get_opt_attr("value"); if(!value) { throw config_error("stop tag missing value"); } if(value < maximumValue) { throw config_error("stop tag values must be in ascending order"); } maximumValue = *value; optional label = stopIter->get_opt_attr("label"); //append the stop colorizer_stop tmpStop; tmpStop.set_color(*stopcolor); tmpStop.set_mode(mode); tmpStop.set_value(*value); if (label) tmpStop.set_label(*label); rc->add_stop(tmpStop); } } } catch (const config_error & ex) { ex.append_context("in RasterColorizer"); throw; } } void map_parser::ensure_font_face( const std::string & face_name ) { if ( ! font_manager_.get_face( face_name ) ) { throw config_error("Failed to find font face '" + face_name + "'"); } } std::string map_parser::ensure_relative_to_xml( boost::optional opt_path ) { if (relative_to_xml_) { boost::filesystem::path xml_path = filename_; boost::filesystem::path rel_path = *opt_path; if ( !rel_path.has_root_path() ) { #if (BOOST_FILESYSTEM_VERSION == 3) // TODO - normalize is now deprecated, use make_preferred? boost::filesystem::path full = boost::filesystem::absolute(xml_path.parent_path()/rel_path); #else // v2 boost::filesystem::path full = boost::filesystem::complete(xml_path.branch_path()/rel_path).normalize(); #endif #ifdef MAPNIK_DEBUG std::clog << "\nModifying relative paths to be relative to xml...\n"; std::clog << "original base path: " << *opt_path << "\n"; std::clog << "relative base path: " << full.string() << "\n"; #endif return full.string(); } } return *opt_path; } } // end of namespace mapnik