Changeset 574

Show
Ignore:
Timestamp:
12/17/07 09:21:04 (3 years ago)
Author:
artem
Message:

Applied patches from Martijn van Oosterhout:

1. The first allows the user to add a <FileSource?
name="foo">/home/bar/baz/</FileSource> to the beginning of the file
and then in any of the symbolisers you can say:

<FooSymboliser? base="foo" name="bridge">
It it will refer to the file /home/bar/baz/bridge.

2. The second allows you to create Datasource templates at the top
level, which can be used later in the actual layers like so:
<Map>

<Datasource name="db">

<Paramaeter name="host">/tmp</Parameter>

</Datasource>
<Layer name="lay">

<Datasource base="db">

<Parameter name="table">points</Parameter>

</Datasource>

</Layer>

</Map>

And the host parameter will be used in the layer.

3. The third adds the "base" parameter to the raster and shape input
plugins. All it does is specify a path to prefix to the filename prior
to using it. Together with the above feature it allows things like:
<Map>

<Datasource name="shapes">

<Paramaeter name="base">/home/foo/shapes</Parameter>

</Datasource>
<Layer name="lay">

<Datasource base="shapes">

<Parameter name="file">places</Parameter>

</Datasource>

</Layer>

</Map>

And it will use the shapefile /home/foo/shapes/places

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/plugins/input/raster/raster_datasource.cpp

    r485 r574  
    5252  
    5353   boost::optional<std::string> file=params.get<std::string>("file"); 
     54   boost::optional<std::string> base=params.get<std::string>("base"); 
    5455   if (!file) throw datasource_exception("missing <file> parameter "); 
    55    filename_ = *file; 
     56   if (base) 
     57      filename_ = *base + "/" + *file; 
     58   else 
     59      filename_ = *file; 
    5660   format_=*params.get<std::string>("format","tiff"); 
    5761   boost::optional<double> lox = params.get<double>("lox"); 
  • trunk/plugins/input/shape/shape.cpp

    r525 r574  
    4242   : datasource (params), 
    4343     type_(datasource::Vector), 
    44      shape_name_(*params_.get<std::string>("file","")), 
    4544     file_length_(0), 
    4645     indexed_(false), 
    4746     desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8")) 
    4847{ 
     48   boost::optional<std::string> base = params.get<std::string>("base"); 
     49   if (base) 
     50      shape_name_ = *base + "/" + *params_.get<std::string>("file",""); 
     51   else 
     52      shape_name_ = *params_.get<std::string>("file",""); 
    4953   try 
    5054   {   
  • trunk/src/load_map.cpp

    r564 r574  
    8383          
    8484         bool strict_; 
     85         std::map<std::string,parameters> datasource_templates_; 
    8586         freetype_engine font_engine_; 
    8687         face_manager<freetype_engine> font_manager_; 
     88         std::map<std::string,std::string> file_sources_; 
    8789    }; 
    8890 
     
    144146                    parse_layer(map, v.second ); 
    145147                } 
    146                 else if (v.first != "<xmlcomment>" && 
    147                         v.first != "<xmlattr>") 
    148                 { 
    149                     throw config_error(std::string("Unknown child node in 'Map'. ") + 
    150                             "Expected 'Style' or 'Layer' but got '" + v.first + "'"); 
    151                 } 
    152             } 
    153         } 
    154         catch (const boost::property_tree::ptree_bad_path &) 
    155         { 
    156             throw config_error("Not a map file. Node 'Map' not found."); 
    157         } 
    158     } 
    159  
    160     void map_parser::parse_style( Map & map, ptree const & sty ) 
    161     { 
    162         string name("<missing name>"); 
    163         try 
    164         { 
    165             name = get_attr<string>(sty, "name"); 
    166             feature_type_style style; 
    167  
    168             ptree::const_iterator ruleIter = sty.begin(); 
    169             ptree::const_iterator endRule = sty.end(); 
    170  
    171             for (; ruleIter!=endRule; ++ruleIter)     
    172             { 
    173                 ptree::value_type const& rule_tag = *ruleIter; 
    174                 if (rule_tag.first == "Rule") 
    175                 { 
    176                     parse_rule( style, rule_tag.second ); 
    177                 } 
    178                 else if (rule_tag.first != "<xmlcomment>" && 
    179                         rule_tag.first != "<xmlattr>" ) 
    180                 { 
    181                     throw config_error(std::string("Unknown child node in 'Style'.") + 
    182                             "Expected 'Rule' but got '" + rule_tag.first + "'"); 
    183                 } 
    184             } 
    185  
    186             map.insert_style(name, style); 
    187  
    188         } catch (const config_error & ex) { 
    189             if ( ! name.empty() ) { 
    190                 ex.append_context(string("in style '") + name + "'"); 
    191             } 
    192             throw; 
    193         } 
    194     } 
    195  
    196     void map_parser::parse_layer( Map & map, ptree const & lay ) 
    197     { 
    198         std::string name; 
    199         try 
    200         { 
    201             name = get_attr(lay, "name", string("Unnamed")); 
    202             // XXX if no projection is given inherit from map? [DS] 
    203             std::string srs = get_attr(lay, "srs", map.srs()); 
    204  
    205             Layer lyr(name, srs); 
    206  
    207             optional<boolean> status = get_opt_attr<boolean>(lay, "status"); 
    208             if (status) 
    209             { 
    210                 lyr.setActive( * status ); 
    211             } 
    212  
    213             optional<boolean> clear_cache = 
    214                 get_opt_attr<boolean>(lay, "clear_label_cache"); 
    215             if (clear_cache) 
    216             { 
    217                 lyr.set_clear_label_cache( * clear_cache ); 
    218             } 
    219  
    220  
    221             ptree::const_iterator itr2 = lay.begin(); 
    222             ptree::const_iterator end2 = lay.end(); 
    223  
    224             for(; itr2 != end2; ++itr2) 
    225             { 
    226                 ptree::value_type const& child = *itr2; 
    227  
    228                 if (child.first == "StyleName") 
    229                 { 
    230                     // TODO check references [DS] 
    231                     lyr.add_style(child.second.data()); 
    232                 } 
    233                 else if (child.first == "Datasource") 
    234                 { 
     148                else if (v.first == "FileSource") 
     149                { 
     150                  std::string name = get_attr<string>( v.second, "name"); 
     151                  std::string value = get_own<string>( v.second, ""); 
     152                  file_sources_[name] = value; 
     153                } 
     154                else if (v.first == "Datasource") 
     155                { 
     156                    std::string name = get_attr(v.second, "name", string("Unnamed")); 
    235157                    parameters params; 
    236                     ptree::const_iterator paramIter = child.second.begin(); 
    237                     ptree::const_iterator endParam = child.second.end(); 
     158                    ptree::const_iterator paramIter = v.second.begin(); 
     159                    ptree::const_iterator endParam = v.second.end(); 
    238160                    for (; paramIter != endParam; ++paramIter) 
    239161                    { 
     
    247169                            params[name] = value;  
    248170                        } 
    249                         else 
     171                        else if( paramIter->first != "<xmlattr>" ) 
     172                        { 
     173                            throw config_error(std::string("Unknown child node in ") + 
     174                                    "'Datasource'. Expected 'Parameter' but got '" + 
     175                                    paramIter->first + "'"); 
     176                        } 
     177                    } 
     178                    datasource_templates_[name] = params; 
     179                } 
     180                else if (v.first != "<xmlcomment>" && 
     181                        v.first != "<xmlattr>") 
     182                { 
     183                    throw config_error(std::string("Unknown child node in 'Map'. ") + 
     184                            "Expected 'Style' or 'Layer' but got '" + v.first + "'"); 
     185                } 
     186            } 
     187        } 
     188        catch (const boost::property_tree::ptree_bad_path &) 
     189        { 
     190            throw config_error("Not a map file. Node 'Map' not found."); 
     191        } 
     192    } 
     193 
     194    void map_parser::parse_style( Map & map, ptree const & sty ) 
     195    { 
     196        string name("<missing name>"); 
     197        try 
     198        { 
     199            name = get_attr<string>(sty, "name"); 
     200            feature_type_style style; 
     201 
     202            ptree::const_iterator ruleIter = sty.begin(); 
     203            ptree::const_iterator endRule = sty.end(); 
     204 
     205            for (; ruleIter!=endRule; ++ruleIter)     
     206            { 
     207                ptree::value_type const& rule_tag = *ruleIter; 
     208                if (rule_tag.first == "Rule") 
     209                { 
     210                    parse_rule( style, rule_tag.second ); 
     211                } 
     212                else if (rule_tag.first != "<xmlcomment>" && 
     213                        rule_tag.first != "<xmlattr>" ) 
     214                { 
     215                    throw config_error(std::string("Unknown child node in 'Style'.") + 
     216                            "Expected 'Rule' but got '" + rule_tag.first + "'"); 
     217                } 
     218            } 
     219 
     220            map.insert_style(name, style); 
     221 
     222        } catch (const config_error & ex) { 
     223            if ( ! name.empty() ) { 
     224                ex.append_context(string("in style '") + name + "'"); 
     225            } 
     226            throw; 
     227        } 
     228    } 
     229 
     230    void map_parser::parse_layer( Map & map, ptree const & lay ) 
     231    { 
     232        std::string name; 
     233        try 
     234        { 
     235            name = get_attr(lay, "name", string("Unnamed")); 
     236            // XXX if no projection is given inherit from map? [DS] 
     237            std::string srs = get_attr(lay, "srs", map.srs()); 
     238 
     239            Layer lyr(name, srs); 
     240 
     241            optional<boolean> status = get_opt_attr<boolean>(lay, "status"); 
     242            if (status) 
     243            { 
     244                lyr.setActive( * status ); 
     245            } 
     246 
     247            optional<boolean> clear_cache = 
     248                get_opt_attr<boolean>(lay, "clear_label_cache"); 
     249            if (clear_cache) 
     250            { 
     251                lyr.set_clear_label_cache( * clear_cache ); 
     252            } 
     253 
     254 
     255            ptree::const_iterator itr2 = lay.begin(); 
     256            ptree::const_iterator end2 = lay.end(); 
     257 
     258            for(; itr2 != end2; ++itr2) 
     259            { 
     260                ptree::value_type const& child = *itr2; 
     261 
     262                if (child.first == "StyleName") 
     263                { 
     264                    // TODO check references [DS] 
     265                    lyr.add_style(child.second.data()); 
     266                } 
     267                else if (child.first == "Datasource") 
     268                { 
     269                    parameters params; 
     270                    optional<std::string> base = get_opt_attr<std::string>( child.second, "base" ); 
     271                    if( base ) 
     272                    { 
     273                       std::map<std::string,parameters>::const_iterator base_itr = datasource_templates_.find(*base); 
     274                       if (base_itr!=datasource_templates_.end()) 
     275                          params = base_itr->second; 
     276                    }    
     277                     
     278                    ptree::const_iterator paramIter = child.second.begin(); 
     279                    ptree::const_iterator endParam = child.second.end(); 
     280                    for (; paramIter != endParam; ++paramIter) 
     281                    { 
     282                        ptree const& param = paramIter->second; 
     283 
     284                        if (paramIter->first == "Parameter") 
     285                        { 
     286                            std::string name = get_attr<string>(param, "name"); 
     287                            std::string value = get_own<string>( param, 
     288                                    "datasource parameter"); 
     289                            params[name] = value;  
     290                        } 
     291                        else if( paramIter->first != "<xmlattr>" ) 
    250292                        { 
    251293                            throw config_error(std::string("Unknown child node in ") + 
     
    401443        { 
    402444            optional<std::string> file =  get_opt_attr<string>(sym, "file"); 
     445            optional<std::string> base =  get_opt_attr<string>(sym, "base"); 
    403446            optional<std::string> type =  get_opt_attr<string>(sym, "type");  
    404447            optional<boolean> allow_overlap =  
     
    412455                try 
    413456                { 
    414                     point_symbolizer symbol(*file,*type,*width,*height); 
    415                     if (allow_overlap) 
    416                     { 
    417                         symbol.set_allow_overlap( * allow_overlap ); 
    418                     } 
    419                     rule.append(symbol); 
     457                   if( base ) 
     458                   { 
     459                      std::map<std::string,std::string>::const_iterator itr = file_sources_.find(*base); 
     460                      if (itr!=file_sources_.end()) 
     461                      { 
     462                         *file = itr->second + "/" + *file; 
     463                      } 
     464                   } 
     465                   point_symbolizer symbol(*file,*type,*width,*height); 
     466                   if (allow_overlap) 
     467                   { 
     468                      symbol.set_allow_overlap( * allow_overlap ); 
     469                   } 
     470                   rule.append(symbol); 
    420471                } 
    421472                catch (ImageReaderException const & ex ) 
     
    461512        { 
    462513            std::string file = get_attr<string>(sym, "file"); 
     514            optional<std::string> base = get_opt_attr<string>(sym, "base"); 
    463515            std::string type = get_attr<string>(sym, "type");  
    464516            unsigned width = get_attr<unsigned>(sym, "width"); 
     
    467519            try 
    468520            { 
    469                 rule.append(line_pattern_symbolizer(file,type,width,height)); 
     521               if( base ) 
     522               { 
     523                   std::map<std::string,std::string>::const_iterator itr = file_sources_.find(*base); 
     524                   if (itr!=file_sources_.end()) 
     525                   { 
     526                      file = itr->second + "/" + file; 
     527                   } 
     528               } 
     529               rule.append(line_pattern_symbolizer(file,type,width,height)); 
    470530            } 
    471531            catch (ImageReaderException const & ex ) 
     
    496556        { 
    497557            std::string file = get_attr<string>(sym, "file"); 
     558            optional<std::string> base = get_opt_attr<string>(sym, "base"); 
    498559            std::string type = get_attr<string>(sym, "type");  
    499560            unsigned width = get_attr<unsigned>(sym, "width"); 
     
    502563            try 
    503564            { 
     565                if( base ) 
     566                { 
     567                   std::map<std::string,std::string>::iterator itr = file_sources_.find(*base); 
     568                   if (itr!=file_sources_.end()) 
     569                   { 
     570                      file = itr->second + "/" + file; 
     571                   } 
     572                } 
    504573                rule.append(polygon_pattern_symbolizer(file,type,width,height));  
    505574            } 
     
    632701 
    633702            std::string image_file = get_attr<string>(sym, "file"); 
     703            optional<std::string> base = get_opt_attr<string>(sym, "base"); 
    634704            std::string type = get_attr<string>(sym, "type"); 
    635705            unsigned width =  get_attr<unsigned>(sym, "width"); 
     
    638708            try 
    639709            { 
     710                if( base ) 
     711                { 
     712                    std::map<std::string,std::string>::const_iterator itr = file_sources_.find(*base); 
     713                    if (itr!=file_sources_.end()) 
     714                    { 
     715                       image_file = itr->second + "/" + image_file; 
     716                    } 
     717                } 
    640718                shield_symbolizer shield_symbol(name,face_name,size,fill, 
    641                         image_file,type,width,height); 
    642  
     719                                                image_file,type,width,height); 
     720                 
    643721                // minimum distance between labels 
    644722                optional<unsigned> min_distance =  
     
    652730            catch (ImageReaderException const & ex ) 
    653731            { 
    654                     string msg("Failed to load image file '" + image_file + 
     732               string msg("Failed to load image file '" + image_file + 
    655733                            "': " + ex.what()); 
    656                 if (strict_) 
    657                 { 
    658                     throw config_error(msg); 
    659                 } 
    660                 else 
    661                 { 
    662                     clog << "### WARNING: " << msg << endl; 
    663                 } 
    664             } 
    665  
     734               if (strict_) 
     735               { 
     736                  throw config_error(msg); 
     737               } 
     738               else 
     739               { 
     740                  clog << "### WARNING: " << msg << endl; 
     741               } 
     742            } 
     743             
    666744        } 
    667745        catch (const config_error & ex)