1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "../../expat/escape_xml.h" 20#include "../frontend/basic_formats.h" 21#include "output_custom.h" 27bool Output_Custom::write_http_headers() 33void Output_Custom::write_payload_header 34 (const std::string& db_dir_, const std::string& timestamp_, const std::string& area_timestamp_) 3711 q timestamp = timestamp_; 3811 q area_timestamp = area_timestamp_; 42std::string process_template(const std::string& raw_template, unsigned int count) 440 std::ostringstream result; 450 std::string::size_type old_pos = 0; 460 std::string::size_type new_pos = 0; 480 new_pos = raw_template.find("{{{", old_pos); 490 while (new_pos != std::string::npos) 510 result<<raw_template.substr(old_pos, new_pos - old_pos); 530 if (raw_template.substr(new_pos + 3, 8) == "count}}}") 560 old_pos = new_pos + 11; 610 old_pos = new_pos + 3; 630 new_pos = raw_template.find("{{{", old_pos); 650 result<<raw_template.substr(old_pos); 71void Output_Custom::display_remark(const std::string& text) 77void Output_Custom::display_error(const std::string& text) 83std::string::size_type find_block_end(std::string data, std::string::size_type pos) 850 if (pos == std::string::npos || data.substr(pos, 2) != "{{") 880 std::string::size_type curly_brace_count = 2; 890 if (data.substr(pos, 3) == "{{{") 900 curly_brace_count = 3; 910 pos += curly_brace_count; 930 while (pos < data.size()) 950 if (data[pos] == '{' && data.substr(pos, 2) == "{{") 960 pos = find_block_end(data, pos); 970 else if ((data[pos] == '}') && curly_brace_count == 2 && data.substr(pos, 2) == "}}") 990 else if ((data[pos] == '}') && curly_brace_count == 3 && data.substr(pos, 3) == "}}}") 1050 return std::string::npos; 109void Output_Custom::set_output_templates() 1130 std::ifstream in((db_dir + "/templates/" + template_name).c_str()); 1170 std::getline(in, buf); 1210 if (data.find("<includeonly>") != std::string::npos) 1230 std::string::size_type start = data.find("<includeonly>") + 13; 1240 std::string::size_type end = data.find("</includeonly>"); 1250 data = data.substr(start, end - start); 1280 template_contains_js = (data.find("<script") != std::string::npos); 1310 data = "\n<p>Template not found.</p>\n"; 1330 node_template = "\n<p>No {{node:..}} found in template.</p>\n"; 1340 way_template = "\n<p>No {{way:..}} found in template.</p>\n"; 1350 relation_template = "\n<p>No {{relation:..}} found in template.</p>\n"; 1370 bool header_written = false; 1380 std::string::size_type pos = 0; 1390 while (pos < data.size()) 1410 if (data[pos] == '{') 1430 if (data.substr(pos, 7) == "{{node:") 1450 std::string::size_type end_pos = find_block_end(data, pos); 1470 if (!header_written) 1490 header = data.substr(0, pos); 1500 header_written = true; 1530 if (end_pos != std::string::npos) 1550 node_template = data.substr(pos + 7, end_pos - pos - 9); 1610 else if (data.substr(pos, 6) == "{{way:") 1630 std::string::size_type end_pos = find_block_end(data, pos); 1650 if (!header_written) 1670 header = data.substr(0, pos); 1680 header_written = true; 1710 if (end_pos != std::string::npos) 1730 way_template = data.substr(pos + 6, end_pos - pos - 8); 1790 else if (data.substr(pos, 11) == "{{relation:") 1810 std::string::size_type end_pos = find_block_end(data, pos); 1830 if (!header_written) 1850 header = data.substr(0, pos); 1860 header_written = true; 1890 if (end_pos != std::string::npos) 1910 relation_template = data.substr(pos + 11, end_pos - pos - 13); 2040 if (!header_written) 2050 header = data.substr(0, pos); 209std::string process_members(const std::string& raw_template, Node::Id_Type ref) 2110 std::ostringstream result; 2120 std::string::size_type old_pos = 0; 2130 std::string::size_type new_pos = 0; 2150 new_pos = raw_template.find("{{", old_pos); 2160 while (new_pos != std::string::npos) 2180 result<<raw_template.substr(old_pos, new_pos - old_pos); 2190 old_pos = find_block_end(raw_template, new_pos); 2200 if (old_pos == std::string::npos) 2220 result<<raw_template.substr(new_pos); 2230 return result.str(); 2250 else if (raw_template.substr(new_pos, 9) == "{{{ref}}}") 2270 else if (raw_template.substr(new_pos, 8) == "{{first:") 2300 result<<raw_template.substr(new_pos, old_pos - new_pos); 2310 new_pos = raw_template.find("{{", old_pos); 2330 result<<raw_template.substr(old_pos, new_pos - old_pos); 2350 return result.str(); 239std::string process_members(const std::string& raw_template, uint32 id, const Relation_Entry& entry, 240 const std::map< uint32, std::string >& roles) 2420 std::ostringstream result; 2430 std::string::size_type old_pos = 0; 2440 std::string::size_type new_pos = 0; 2460 new_pos = raw_template.find("{{", old_pos); 2470 while (new_pos != std::string::npos) 2490 result<<raw_template.substr(old_pos, new_pos - old_pos); 2500 old_pos = find_block_end(raw_template, new_pos); 2510 if (old_pos == std::string::npos) 2530 result<<raw_template.substr(new_pos); 2540 return result.str(); 2560 else if (raw_template.substr(new_pos, 8) == "{{{id}}}") 2580 else if (raw_template.substr(new_pos, 9) == "{{{ref}}}") 2590 result<<entry.ref.val(); 2600 else if (raw_template.substr(new_pos, 10) == "{{{type}}}") 2610 result<<member_type_name(entry.type); 2620 else if (raw_template.substr(new_pos, 10) == "{{{role}}}") 2640 std::map< uint32, std::string >::const_iterator rit = roles.find(entry.role); 2650 result<<escape_xml(rit != roles.end() ? rit->second : "???"); 2670 else if (raw_template.substr(new_pos, 8) == "{{first:") 2700 result<<raw_template.substr(new_pos, old_pos - new_pos); 2710 new_pos = raw_template.find("{{", old_pos); 2730 result<<raw_template.substr(old_pos, new_pos - old_pos); 2750 return result.str(); 279std::string process_tags(const std::string& raw_template, uint32 id, const std::string& key, const std::string& value) 2810 std::ostringstream result; 2820 std::string::size_type old_pos = 0; 2830 std::string::size_type new_pos = 0; 2850 new_pos = raw_template.find("{{", old_pos); 2860 while (new_pos != std::string::npos) 2880 result<<raw_template.substr(old_pos, new_pos - old_pos); 2890 old_pos = find_block_end(raw_template, new_pos); 2900 if (old_pos == std::string::npos) 2920 result<<raw_template.substr(new_pos); 2930 return result.str(); 2950 else if (raw_template.substr(new_pos, 8) == "{{{id}}}") 2970 else if (raw_template.substr(new_pos, 9) == "{{{key}}}") 2990 else if (raw_template.substr(new_pos, 11) == "{{{value}}}") 3010 else if (raw_template.substr(new_pos, 8) == "{{first:") 3040 result<<raw_template.substr(new_pos, old_pos - new_pos); 3050 new_pos = raw_template.find("{{", old_pos); 3070 result<<raw_template.substr(old_pos, new_pos - old_pos); 3090 return result.str(); 313std::string process_coords(const std::string& raw_template, uint32 id, double lat, double lon) 3150 std::ostringstream result; 3160 std::string::size_type old_pos = 0; 3170 std::string::size_type new_pos = 0; 3190 new_pos = raw_template.find("{{", old_pos); 3200 while (new_pos != std::string::npos) 3220 result<<raw_template.substr(old_pos, new_pos - old_pos); 3230 old_pos = find_block_end(raw_template, new_pos); 3240 if (old_pos == std::string::npos) 3260 result<<raw_template.substr(new_pos); 3270 return result.str(); 3290 else if (raw_template.substr(new_pos, 8) == "{{{id}}}") 3310 else if (raw_template.substr(new_pos, 9) == "{{{lat}}}") 3320 result<<std::fixed<<std::setprecision(7)<<lat; 3330 else if (raw_template.substr(new_pos, 9) == "{{{lon}}}") 3340 result<<std::fixed<<std::setprecision(7)<<lon; 3360 result<<raw_template.substr(new_pos, old_pos - new_pos); 3370 new_pos = raw_template.find("{{", old_pos); 3390 result<<raw_template.substr(old_pos, new_pos - old_pos); 3410 return result.str(); 345std::string process_coords(const std::string& raw_template, uint32 id, 346 double south, double west, double north, double east, uint zoom) 3480 std::ostringstream result; 3490 std::string::size_type old_pos = 0; 3500 std::string::size_type new_pos = 0; 3520 new_pos = raw_template.find("{{", old_pos); 3530 while (new_pos != std::string::npos) 3550 result<<raw_template.substr(old_pos, new_pos - old_pos); 3560 old_pos = find_block_end(raw_template, new_pos); 3570 if (old_pos == std::string::npos) 3590 result<<raw_template.substr(new_pos); 3600 return result.str(); 3620 else if (raw_template.substr(new_pos, 8) == "{{{id}}}") 3640 else if (raw_template.substr(new_pos, 11) == "{{{south}}}") 3650 result<<std::fixed<<std::setprecision(7)<<south; 3660 else if (raw_template.substr(new_pos, 10) == "{{{west}}}") 3670 result<<std::fixed<<std::setprecision(7)<<west; 3680 else if (raw_template.substr(new_pos, 11) == "{{{north}}}") 3690 result<<std::fixed<<std::setprecision(7)<<north; 3700 else if (raw_template.substr(new_pos, 10) == "{{{east}}}") 3710 result<<std::fixed<<std::setprecision(7)<<east; 3720 else if (raw_template.substr(new_pos, 9) == "{{{lat}}}") 3730 result<<std::fixed<<std::setprecision(7)<<(south + north)/2.0; 3740 else if (raw_template.substr(new_pos, 9) == "{{{lon}}}") 3750 result<<std::fixed<<std::setprecision(7)<<(east + west)/2.0; 3760 else if (raw_template.substr(new_pos, 10) == "{{{zoom}}}") 3780 else if (raw_template.substr(new_pos, 7) == "{{none:") 3810 result<<raw_template.substr(new_pos, old_pos - new_pos); 3820 new_pos = raw_template.find("{{", old_pos); 3840 result<<raw_template.substr(old_pos, new_pos - old_pos); 3860 return result.str(); 390std::string extract_first(const std::string& raw_template) 3920 std::string::size_type old_pos = 0; 3930 std::string::size_type new_pos = 0; 3950 new_pos = raw_template.find("{{", old_pos); 3960 while (new_pos != std::string::npos) 3980 old_pos = find_block_end(raw_template, new_pos); 3990 if (old_pos == std::string::npos) 4010 else if (raw_template.substr(new_pos, 8) == "{{first:") 4020 return raw_template.substr(new_pos + 8, old_pos - new_pos - 10); 4030 new_pos = raw_template.find("{{", old_pos); 410std::string antiprocess_coords(const std::string& raw_template) 4120 std::ostringstream result; 4130 std::string::size_type old_pos = 0; 4140 std::string::size_type new_pos = 0; 4160 new_pos = raw_template.find("{{", old_pos); 4170 while (new_pos != std::string::npos) 4190 result<<raw_template.substr(old_pos, new_pos - old_pos); 4200 old_pos = find_block_end(raw_template, new_pos); 4210 if (old_pos == std::string::npos) 4230 result<<raw_template.substr(new_pos); 4240 return result.str(); 4260 else if (raw_template.substr(new_pos, 7) == "{{none:") 4270 return raw_template.substr(new_pos + 7, old_pos - new_pos - 9); 4290 result<<raw_template.substr(new_pos, old_pos - new_pos); 4300 new_pos = raw_template.find("{{", old_pos); 4320 result<<raw_template.substr(old_pos, new_pos - old_pos); 4340 return result.str(); 438std::string process_template(const std::string& raw_template, unsigned long long id, std::string type, 439 double south, double west, double north, double east, uint zoom, 440 const std::vector< std::pair< std::string, std::string > >* tags, 441 const std::vector< Node::Id_Type >* nds, 442 const std::vector< Relation_Entry >* members, 443 const std::map< uint32, std::string >* roles) 4450 std::ostringstream result; 4460 std::string::size_type old_pos = 0; 4470 std::string::size_type new_pos = 0; 4490 new_pos = raw_template.find("{{", old_pos); 4500 while (new_pos != std::string::npos) 4520 result<<raw_template.substr(old_pos, new_pos - old_pos); 4530 old_pos = find_block_end(raw_template, new_pos); 4540 if (old_pos == std::string::npos) 4560 result<<raw_template.substr(new_pos); 4570 return result.str(); 4590 else if (raw_template.substr(new_pos, 8) == "{{{id}}}") 4610 else if (raw_template.substr(new_pos, 10) == "{{{type}}}") 4630 else if (raw_template.substr(new_pos, 9) == "{{coords:") 4660 result<<process_coords(raw_template.substr(new_pos + 9, old_pos - new_pos - 11), id, 4690 else if (raw_template.substr(new_pos, 7) == "{{bbox:") 4710 if (south < 100.0 && north < 100.0) 4720 result<<process_coords(raw_template.substr(new_pos + 7, old_pos - new_pos - 9), id, 4730 south, west, north, east, zoom); 4740 else if (south == 200.0) 4750 result<<antiprocess_coords(raw_template.substr(new_pos + 7, old_pos - new_pos - 9)); 4770 else if (raw_template.substr(new_pos, 7) == "{{tags:") 4790 if (tags != 0 && !tags->empty()) 4810 std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin(); 4830 std::string first = extract_first(raw_template.substr(new_pos + 7, old_pos - new_pos - 9)); 4840 if (first != "" && it != tags->end()) 4860 result<<process_tags(first, id, escape_xml(it->first), escape_xml(it->second)); 4900 for (; it != tags->end(); ++it) 4910 result<<process_tags(raw_template.substr(new_pos + 7, old_pos - new_pos - 9), id, 4920 escape_xml(it->first), escape_xml(it->second)); 4950 else if (raw_template.substr(new_pos, 10) == "{{members:") 4970 if (nds != 0 && !nds->empty()) 4990 std::vector< Node::Id_Type >::const_iterator it = nds->begin(); 5010 std::string first = extract_first(raw_template.substr(new_pos + 10, old_pos - new_pos - 12)); 5020 if (first != "" && it != nds->end()) 5040 result<<process_members(first, *it); 5080 for (; it != nds->end(); ++it) 5090 result<<process_members(raw_template.substr(new_pos + 10, old_pos - new_pos - 12), *it); 5110 else if (members != 0 && !members->empty()) 5130 std::vector< Relation_Entry >::const_iterator it = members->begin(); 5150 std::string first = extract_first(raw_template.substr(new_pos + 10, old_pos - new_pos - 12)); 5160 if (first != "" && it != members->end()) 5180 result<<process_members(first, id, *it, *roles); 5220 for (; it != members->end(); ++it) 5230 result<<process_members(raw_template.substr(new_pos + 10, old_pos - new_pos - 12), id, 5280 result<<raw_template.substr(new_pos, old_pos - new_pos); 5290 new_pos = raw_template.find("{{", old_pos); 5310 result<<raw_template.substr(old_pos); 5330 return result.str(); 537void Output_Custom::write_footer() 53911 q if (count == 0 && redirect) 54111 q ::write_html_header(timestamp, area_timestamp, 200, false, true); 54211 q std::cout<<"<p>No results found.</p>\n"; 54311 q std::cout<<"\n</body>\n</html>\n"; 5450 else if (count == 1 && redirect) 5470 std::cout<<"Status: 302 Moved\n"; 5480 std::cout<<"Location: " 5490 <<process_template(url, first_id, first_type, 100.0, 200.0, 0, 17, 0, 0, 0, 0, 0)<<"\n\n"; 5530 ::write_html_header(timestamp, area_timestamp, 200, template_contains_js, true); 5540 std::cout<<process_template(header, count); 5570 std::cout<<"\n</body>\n</html>\n"; 562void Output_Custom::print_item(const Node_Skeleton& skel, 563 const Opaque_Geometry& geometry, 564 const std::vector< std::pair< std::string, std::string > >* tags, 565 const OSM_Element_Metadata_Skeleton< Node::Id_Type >* meta, 566 const std::map< uint32, std::string >* users, 568 const Feature_Action& action, 569 const Node_Skeleton* new_skel, 570 const Opaque_Geometry* new_geometry, 571 const std::vector< std::pair< std::string, std::string > >* new_tags, 572 const OSM_Element_Metadata_Skeleton< Node::Id_Type >* new_meta) 5760 set_output_templates(); 5780 first_type = "node"; 5790 first_id = skel.id.val(); 5850 if (mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER)) 5870 lat = geometry.center_lat(); 5880 lon = geometry.center_lon(); 5900 output += process_template(node_template, skel.id.val(), "node", lat, lon, 100.0, 0, 17, tags, 0, 0, 0); 596// Box_Coords() : south(100.0), west(200.0), north(0), east(0) {} 598// Box_Coords(Uint31_Index ll_upper) 600// pair< Uint32_Index, Uint32_Index > bbox_bounds = calc_bbox_bounds(ll_upper); 601// if (bbox_bounds.second.val() == 1) 610// south = ::lat(bbox_bounds.first.val(), 0); 611// west = ::lon(bbox_bounds.first.val(), 0); 612// north = ::lat(bbox_bounds.second.val() - 1, 0xffffffffu); 613// east = ::lon(bbox_bounds.second.val() - 1, 0xffffffffu); 617// double south, west, north, east; 621unsigned int detect_zoom(const Opaque_Geometry& geometry) 6230 if (!geometry.has_bbox()) 6260 double delta_lat = geometry.north() - geometry.south(); 6270 double delta_lon = fabs(geometry.west() - geometry.east()); 6290 double threshold = 0.012; 6300 unsigned int result = 15; 6310 while (delta_lat > threshold || delta_lon > threshold) 640void Output_Custom::print_item(const Way_Skeleton& skel, 641 const Opaque_Geometry& geometry, 642 const std::vector< std::pair< std::string, std::string > >* tags, 643 const OSM_Element_Metadata_Skeleton< Way::Id_Type >* meta, 644 const std::map< uint32, std::string >* users, 646 const Feature_Action& action, 647 const Way_Skeleton* new_skel, 648 const Opaque_Geometry* new_geometry, 649 const std::vector< std::pair< std::string, std::string > >* new_tags, 650 const OSM_Element_Metadata_Skeleton< Way::Id_Type >* new_meta) 6540 set_output_templates(); 6570 first_id = skel.id.val(); 6610 if (geometry.has_bbox()) 6630 output += process_template(way_template, skel.id.val(), "way", 6640 geometry.south(), geometry.west(), geometry.north(), geometry.east(), 6650 detect_zoom(geometry), 6660 tags, mode.mode & Output_Mode::NDS ? &skel.nds : 0, 0, 0); 6700 output += process_template(way_template, skel.id.val(), "way", 6710 200.0, 200.0, 200.0, 200.0, 6720 detect_zoom(geometry), 6730 tags, mode.mode & Output_Mode::NDS ? &skel.nds : 0, 0, 0); 678void Output_Custom::print_item(const Relation_Skeleton& skel, 679 const Opaque_Geometry& geometry, 680 const std::vector< std::pair< std::string, std::string > >* tags, 681 const OSM_Element_Metadata_Skeleton< Relation::Id_Type >* meta, 682 const std::map< uint32, std::string >* roles, 683 const std::map< uint32, std::string >* users, 685 const Feature_Action& action, 686 const Relation_Skeleton* new_skel, 687 const Opaque_Geometry* new_geometry, 688 const std::vector< std::pair< std::string, std::string > >* new_tags, 689 const OSM_Element_Metadata_Skeleton< Relation::Id_Type >* new_meta) 6930 set_output_templates(); 6950 first_type = "relation"; 6960 first_id = skel.id.val(); 7000 if (geometry.has_bbox()) 7020 output += process_template(relation_template, skel.id.val(), "relation", 7030 geometry.south(), geometry.west(), geometry.north(), geometry.east(), 7040 detect_zoom(geometry), 7050 tags, 0, mode.mode & Output_Mode::MEMBERS ? &skel.members : 0, roles); 7090 output += process_template(relation_template, skel.id.val(), "relation", 7100 200.0, 200.0, 200.0, 200.0, 7110 detect_zoom(geometry), 7120 tags, 0, mode.mode & Output_Mode::MEMBERS ? &skel.members : 0, roles); 717void Output_Custom::print_item(const Derived_Skeleton& skel, 718 const Opaque_Geometry& geometry, 719 const std::vector< std::pair< std::string, std::string > >* tags, 721 const Feature_Action& action) 723 // Intentionally empty