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_json.h" 20#include "../core/settings.h" 21#include "../frontend/basic_formats.h" 22#include "output_json.h" 25bool Output_JSON::write_http_headers() 270 std::cout<<"Content-type: application/json\n"; 32void Output_JSON::write_payload_header 33 (const std::string& db_dir, const std::string& timestamp, const std::string& area_timestamp) 360 std::cout<<padding<<"("; 396060 q " \"version\": 0.6,\n" 406060 q " \"generator\": \"Overpass API "<<basic_settings().version<<" " 416060 q <<basic_settings().source_hash.substr(0, 8)<<"\",\n" 436060 q " \"timestamp_osm_base\": \""<<timestamp<<"\",\n"; 446060 q if (area_timestamp != "") 450 std::cout<<" \"timestamp_areas_base\": \""<<area_timestamp<<"\",\n"; 466060 q std::cout<<" \"copyright\": \"The data included in this document is from www.openstreetmap.org." 476060 q " The data is made available under ODbL.\"\n" 496060 q std::cout<< " \"elements\": [\n\n"; 53void Output_JSON::write_footer() 556060 q std::cout<<"\n\n ]"<<(messages != "" ? ",\n\"remark\": \"" + escape_cstr(messages) + "\"" : "") 566060 q <<"\n}"<<(padding != "" ? ");\n" : "\n"); 60void Output_JSON::display_remark(const std::string& text) 66void Output_JSON::display_error(const std::string& text) 72void handle_first_elem(bool& first_elem) 769257960 q first_elem = false; 80template< typename Id_Type > 81void print_meta_json(const OSM_Element_Metadata_Skeleton< Id_Type >& meta, 82 const std::map< uint32, std::string >& users) 84742 q std::cout<<",\n \"timestamp\": \""<<iso_string(meta.timestamp)<<"\"" 85742 q ",\n \"version\": "<<meta.version<< 86742 q ",\n \"changeset\": "<<meta.changeset; 87742 q std::map< uint32, std::string >::const_iterator it = users.find(meta.user_id); 88742 q if (it != users.end()) 89742 q std::cout<<",\n \"user\": \""<<escape_cstr(it->second)<<"\""; 90742 q std::cout<<",\n \"uid\": "<<meta.user_id; 94void print_tags(const std::vector< std::pair< std::string, std::string > >* tags) 969257960 q if (tags != 0 && !tags->empty()) 983234660 q std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin(); 993234660 q std::cout<<",\n \"tags\": {" 1003234660 q "\n \""<<escape_cstr(it->first)<<"\": \""<<escape_cstr(it->second)<<"\""; 10114446460 q for (++it; it != tags->end(); ++it) 10211211860 q std::cout<<",\n \""<<escape_cstr(it->first)<<"\": \""<<escape_cstr(it->second)<<"\""; 108void Output_JSON::print_item(const Node_Skeleton& skel, 109 const Opaque_Geometry& geometry, 110 const std::vector< std::pair< std::string, std::string > >* tags, 111 const OSM_Element_Metadata_Skeleton< Node::Id_Type >* meta, 112 const std::map< uint32, std::string >* users, 114 const Feature_Action& action, 115 const Node_Skeleton* new_skel, 116 const Opaque_Geometry* new_geometry, 117 const std::vector< std::pair< std::string, std::string > >* new_tags, 118 const OSM_Element_Metadata_Skeleton< Node::Id_Type >* new_meta) 1203650730 q handle_first_elem(first_elem); 1223650730 q " \"type\": \"node\""; 1233650730 q if (mode.mode & Output_Mode::ID) 1243438430 q std::cout<<",\n \"id\": "<<skel.id.val(); 1263650730 q if (mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER)) 127129517 q std::cout<<",\n \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lat() 128129517 q <<",\n \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lon(); 130472 q print_meta_json(*meta, *users); 137void print_bounds(const Opaque_Geometry& geometry, Output_Mode mode) 1393672329 q if ((mode.mode & Output_Mode::BOUNDS) && geometry.has_bbox()) 140112114 q std::cout<<",\n \"bounds\": {\n" 141112114 q " \"minlat\": "<<std::fixed<<std::setprecision(7)<<geometry.south()<<",\n" 142112114 q " \"minlon\": "<<std::fixed<<std::setprecision(7)<<geometry.west()<<",\n" 143112114 q " \"maxlat\": "<<std::fixed<<std::setprecision(7)<<geometry.north()<<",\n" 144112114 q " \"maxlon\": "<<std::fixed<<std::setprecision(7)<<geometry.east()<<"\n" 1462551229 q else if ((mode.mode & Output_Mode::CENTER) && geometry.has_center()) 14743063 q std::cout<<",\n \"center\": {\n" 14843063 q " \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lat()<<",\n" 14943063 q " \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lon()<<"\n" 154void Output_JSON::print_item(const Way_Skeleton& skel, 155 const Opaque_Geometry& geometry, 156 const std::vector< std::pair< std::string, std::string > >* tags, 157 const OSM_Element_Metadata_Skeleton< Way::Id_Type >* meta, 158 const std::map< uint32, std::string >* users, 160 const Feature_Action& action, 161 const Way_Skeleton* new_skel, 162 const Opaque_Geometry* new_geometry, 163 const std::vector< std::pair< std::string, std::string > >* new_tags, 164 const OSM_Element_Metadata_Skeleton< Way::Id_Type >* new_meta) 1663346925 q handle_first_elem(first_elem); 1693346925 q if (mode.mode & Output_Mode::ID) 1703332524 q std::cout<<",\n \"id\": "<<skel.id.val(); 173241 q print_meta_json(*meta, *users); 1753346925 q print_bounds(geometry, mode); 1773346925 q if ((mode.mode & Output_Mode::NDS) != 0 && !skel.nds.empty()) 179271227 q std::vector< Node::Id_Type >::const_iterator it = skel.nds.begin(); 180271227 q std::cout<<",\n \"nodes\": [" 1828074207 q for (++it; it != skel.nds.end(); ++it) 1837802987 q std::cout<<",\n "<<it->val(); 184271227 q std::cout<<"\n ]"; 1873346925 q if ((mode.mode & Output_Mode::GEOMETRY) != 0 && geometry.has_faithful_way_geometry()) 18983312 q std::cout<<",\n \"geometry\": ["; 1903578152 q for (uint i = 0; i < geometry.way_size(); ++i) 1923494842 q if (geometry.way_pos_is_valid(i)) 1933494842 q std::cout<<"\n { \"lat\": "<<std::fixed<<std::setprecision(7) 1943494842 q <<geometry.way_pos_lat(i) 1953494842 q <<", \"lon\": "<<std::fixed<<std::setprecision(7) 1963494842 q <<geometry.way_pos_lon(i)<<" }"; 1980 std::cout<<"\n null"; 2003494842 q if (i < geometry.way_size() - 1) 2013411532 q std::cout << ","; 211void Output_JSON::print_item(const Relation_Skeleton& skel, 212 const Opaque_Geometry& geometry, 213 const std::vector< std::pair< std::string, std::string > >* tags, 214 const OSM_Element_Metadata_Skeleton< Relation::Id_Type >* meta, 215 const std::map< uint32, std::string >* roles, 216 const std::map< uint32, std::string >* users, 218 const Feature_Action& action, 219 const Relation_Skeleton* new_skel, 220 const Opaque_Geometry* new_geometry, 221 const std::vector< std::pair< std::string, std::string > >* new_tags, 222 const OSM_Element_Metadata_Skeleton< Relation::Id_Type >* new_meta) 224325414 q handle_first_elem(first_elem); 226325414 q " \"type\": \"relation\""; 227325414 q if (mode.mode & Output_Mode::ID) 228324413 q std::cout<<",\n \"id\": "<<skel.id.val(); 23131 q print_meta_json(*meta, *users); 233325414 q print_bounds(geometry, mode); 235325414 q if (roles && (mode.mode & Output_Mode::MEMBERS) != 0 && !skel.members.empty()) 23729068 q std::cout<<",\n \"members\": ["; 238350168 q for (uint i = 0; i < skel.members.size(); i++) 240321108 q std::map< uint32, std::string >::const_iterator rit = roles->find(skel.members[i].role); 241321108 q std::cout<< (i == 0 ? "" : ","); 242321108 q std::cout <<"\n {" 243321108 q "\n \"type\": \""<<member_type_name(skel.members[i].type)<< 244321108 q "\",\n \"ref\": "<<skel.members[i].ref.val()<< 245321108 q ",\n \"role\": \""<<escape_cstr(rit != roles->end() ? rit->second : "???") << "\""; 247321108 q if (skel.members[i].type == Relation_Entry::NODE && 248321108 q geometry.has_faithful_relation_geometry() && geometry.relation_pos_is_valid(i)) 24924122 q std::cout<<",\n \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.relation_pos_lat(i) 25024122 q <<",\n \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.relation_pos_lon(i); 252321108 q if (skel.members[i].type == Relation_Entry::WAY && geometry.has_faithful_relation_geometry()) 254265302 q std::cout<<",\n \"geometry\": ["; 2558642512 q for (uint j = 0; j < geometry.relation_way_size(i); ++j) 2578377212 q if (geometry.relation_pos_is_valid(i, j)) 2598377212 q std::cout<<"\n { \"lat\": "<<std::fixed<<std::setprecision(7) 2608377212 q <<geometry.relation_pos_lat(i, j) 2618377212 q <<", \"lon\": "<<std::fixed<<std::setprecision(7) 2628377212 q <<geometry.relation_pos_lon(i, j)<<" }"; 2650 std::cout<<"\n null"; 2668377212 q if (j < geometry.relation_way_size(i) - 1) 2678143142 q std::cout << ","; 269265302 q std::cout<<"\n ]"; 272321108 q std::cout<<"\n }"; 282void print_geometry(const Opaque_Geometry& geometry, const std::string& indent) 2840 if (geometry.has_components()) 2870 "\n"<<indent<<" \"type\": \"GeometryCollection\"," 2880 "\n"<<indent<<" \"geometries\": ["; 2900 bool first_printed = true; 2910 const std::vector< Opaque_Geometry* >* components = geometry.get_components(); 2920 for (std::vector< Opaque_Geometry* >::const_iterator it = components->begin(); it != components->end(); ++it) 2940 if (*it && (*it)->has_center()) 2970 first_printed = false; 3010 std::cout<<"\n"<<indent<<" "; 3020 print_geometry(**it, indent + " "); 3060 std::cout<<"\n"<<indent<<" ]\n"<<indent<<"}"; 3080 else if (geometry.has_line_geometry()) 3110 "\n"<<indent<<" \"type\": \"LineString\"," 3120 "\n"<<indent<<" \"coordinates\": ["; 3140 const std::vector< Point_Double >* line = geometry.get_line_geometry(); 3150 for (std::vector< Point_Double >::const_iterator it = line->begin(); it != line->end(); ++it) 3160 std::cout<<(it == line->begin() ? "" : ",")<<"\n"<<indent<<" [" 3170 <<std::fixed<<std::setprecision(7)<<it->lon<<", " 3180 <<std::fixed<<std::setprecision(7)<<it->lat<<"]"; 3200 std::cout<<"\n"<<indent<<" ]\n"<<indent<<"}"; 3220 else if (geometry.has_multiline_geometry()) 3250 "\n"<<indent<<" \"type\": \"Polygon\"," 3260 "\n"<<indent<<" \"coordinates\": ["; 3280 const std::vector< std::vector< Point_Double > >* linestrings = geometry.get_multiline_geometry(); 3290 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings->begin(); 3300 iti != linestrings->end(); ++iti) 3320 std::cout<<(iti == linestrings->begin() ? "" : ",")<<"\n"<<indent<<" ["; 3330 for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it) 3340 std::cout<<(it == iti->begin() ? "" : ",")<<"\n"<<indent<<" [" 3350 <<std::fixed<<std::setprecision(7)<<it->lon<<", " 3360 <<std::fixed<<std::setprecision(7)<<it->lat<<"]"; 3370 std::cout<<"\n"<<indent<<" ]"; 3400 std::cout<<"\n"<<indent<<" ]\n"<<indent<<"}"; 3420 else if (geometry.has_center()) 3440 "\n"<<indent<<" \"type\": \"Point\"," 3450 "\n"<<indent<<" \"coordinates\": [ " 3460 <<std::fixed<<std::setprecision(7)<<geometry.center_lon()<<", " 3470 <<std::fixed<<std::setprecision(7)<<geometry.center_lat()<<" ]" 352void print_geometry(const Opaque_Geometry& geometry, Output_Mode mode) 3541934957 q if ((mode.mode & Output_Mode::GEOMETRY) && (geometry.has_center())) 3560 std::cout<<",\n \"geometry\": "; 3570 print_geometry(geometry, " "); 3591934957 q else if ((mode.mode & Output_Mode::BOUNDS) && geometry.has_bbox()) 3602881 q std::cout<<",\n \"geometry\": {" 3612881 q "\n \"type\": \"Polygon\"," 3622881 q "\n \"coordinates\": [" 3642881 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", " 3652881 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]" 3672881 q <<std::fixed<<std::setprecision(7)<<geometry.east()<<", " 3682881 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]" 3702881 q <<std::fixed<<std::setprecision(7)<<geometry.east()<<", " 3712881 q <<std::fixed<<std::setprecision(7)<<geometry.north()<<"]" 3732881 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", " 3742881 q <<std::fixed<<std::setprecision(7)<<geometry.north()<<"]" 3762881 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", " 3772881 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]" 3791906157 q else if ((mode.mode & Output_Mode::CENTER) && geometry.has_center()) 3800 std::cout<<",\n \"geometry\": {" 3810 "\n \"type\": \"Point\"," 3820 "\n \"coordinates\": [ " 3830 <<std::fixed<<std::setprecision(7)<<geometry.center_lon()<<", " 3840 <<std::fixed<<std::setprecision(7)<<geometry.center_lat()<<" ]" 389void Output_JSON::print_item(const Derived_Skeleton& skel, 390 const Opaque_Geometry& geometry, 391 const std::vector< std::pair< std::string, std::string > >* tags, 393 const Feature_Action& action) 3951934957 q handle_first_elem(first_elem); 3971934957 q " \"type\": \""<<skel.type_name<<"\""; 3981934957 q if (mode.mode & Output_Mode::ID) 3991934857 q std::cout<<",\n \"id\": "<<skel.id.val(); 4011934957 q print_geometry(geometry, mode);