Overpass API regression suite coverage (Saxony) / overpass_api/output_formats/output_json.cc

overpass_api/output_formats/output_json.cc

189 of 275 instrumented lines run by the suite (68.7%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
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.
9 *
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.
14 *
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/>.
17 */
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()
260{
270 std::cout<<"Content-type: application/json\n";
280 return true;
290}
32void Output_JSON::write_payload_header
33 (const std::string& db_dir, const std::string& timestamp, const std::string& area_timestamp)
356464 q if (padding != "")
360 std::cout<<padding<<"(";
386464 q std::cout<<"{\n"
396464 q " \"version\": 0.6,\n"
406464 q " \"generator\": \"Overpass API "<<basic_settings().version<<" "
416464 q <<basic_settings().source_hash.substr(0, 8)<<"\",\n"
426464 q " \"osm3s\": {\n"
436464 q " \"timestamp_osm_base\": \""<<timestamp<<"\",\n";
446464 q if (area_timestamp != "")
450 std::cout<<" \"timestamp_areas_base\": \""<<area_timestamp<<"\",\n";
466464 q std::cout<<" \"copyright\": \"The data included in this document is from www.openstreetmap.org."
476464 q " The data is made available under ODbL.\"\n"
486464 q " },\n";
496464 q std::cout<< " \"elements\": [\n\n";
53void Output_JSON::write_footer()
556464 q std::cout<<"\n\n ]"<<(messages != "" ? ",\n\"remark\": \"" + escape_cstr(messages) + "\"" : "")
566464 q <<"\n}"<<(padding != "" ? ");\n" : "\n");
60void Output_JSON::display_remark(const std::string& text)
610{
620 messages += text;
630}
66void Output_JSON::display_error(const std::string& text)
670{
680 messages += text;
690}
72void handle_first_elem(bool& first_elem)
7320885363 q{
7420885363 q if (!first_elem)
7520879053 q std::cout<<",\n";
7620885363 q first_elem = false;
7720885363 q}
80template< typename Id_Type >
81void print_meta_json(const OSM_Element_Metadata_Skeleton< Id_Type >& meta,
82 const std::map< uint32, std::string >& users)
84565 q std::cout<<",\n \"timestamp\": \""<<iso_string(meta.timestamp)<<"\""
85565 q ",\n \"version\": "<<meta.version<<
86565 q ",\n \"changeset\": "<<meta.changeset;
87565 q std::map< uint32, std::string >::const_iterator it = users.find(meta.user_id);
88565 q if (it != users.end())
89565 q std::cout<<",\n \"user\": \""<<escape_cstr(it->second)<<"\"";
90565 q std::cout<<",\n \"uid\": "<<meta.user_id;
94void print_tags(const std::vector< std::pair< std::string, std::string > >* tags)
9520885363 q{
9620885363 q if (tags != 0 && !tags->empty())
978428963 q {
988428963 q std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin();
998428963 q std::cout<<",\n \"tags\": {"
1008428963 q "\n \""<<escape_cstr(it->first)<<"\": \""<<escape_cstr(it->second)<<"\"";
10125158963 q for (++it; it != tags->end(); ++it)
10216730063 q std::cout<<",\n \""<<escape_cstr(it->first)<<"\": \""<<escape_cstr(it->second)<<"\"";
1038428963 q std::cout<<"\n }";
1048428963 q }
10520885363 q}
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,
113 Output_Mode mode,
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)
11913602521 q{
12013602521 q handle_first_elem(first_elem);
12113602521 q std::cout<<"{\n"
12213602521 q " \"type\": \"node\"";
12313602521 q if (mode.mode & Output_Mode::ID)
1249468221 q std::cout<<",\n \"id\": "<<skel.id.val();
12613602521 q if (mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER))
127416667 q std::cout<<",\n \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lat()
128416667 q <<",\n \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lon();
12913602521 q if (meta)
130122 q print_meta_json(*meta, *users);
13213602521 q print_tags(tags);
13313602521 q std::cout<<"\n}";
13413602521 q}
137void print_bounds(const Opaque_Geometry& geometry, Output_Mode mode)
1386218130 q{
1396218130 q if ((mode.mode & Output_Mode::BOUNDS) && geometry.has_bbox())
14047758 q std::cout<<",\n \"bounds\": {\n"
14147758 q " \"minlat\": "<<std::fixed<<std::setprecision(7)<<geometry.south()<<",\n"
14247758 q " \"minlon\": "<<std::fixed<<std::setprecision(7)<<geometry.west()<<",\n"
14347758 q " \"maxlat\": "<<std::fixed<<std::setprecision(7)<<geometry.north()<<",\n"
14447758 q " \"maxlon\": "<<std::fixed<<std::setprecision(7)<<geometry.east()<<"\n"
14547758 q " }";
1465740629 q else if ((mode.mode & Output_Mode::CENTER) && geometry.has_center())
147231 q std::cout<<",\n \"center\": {\n"
148231 q " \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lat()<<",\n"
149231 q " \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lon()<<"\n"
150231 q " }";
1516218130 q}
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,
159 Output_Mode mode,
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)
1656060524 q{
1666060524 q handle_first_elem(first_elem);
1676060524 q std::cout<<"{\n"
1686060524 q " \"type\": \"way\"";
1696060524 q if (mode.mode & Output_Mode::ID)
1703341023 q std::cout<<",\n \"id\": "<<skel.id.val();
1726060524 q if (meta)
173144 q print_meta_json(*meta, *users);
1756060524 q print_bounds(geometry, mode);
1776060524 q if ((mode.mode & Output_Mode::NDS) != 0 && !skel.nds.empty())
1783525413 q {
1793525413 q std::vector< Node::Id_Type >::const_iterator it = skel.nds.begin();
1803525413 q std::cout<<",\n \"nodes\": ["
1813525413 q "\n "<<it->val();
18226164713 q for (++it; it != skel.nds.end(); ++it)
18322639313 q std::cout<<",\n "<<it->val();
1843525413 q std::cout<<"\n ]";
1853525413 q }
1876060524 q if ((mode.mode & Output_Mode::GEOMETRY) != 0 && geometry.has_faithful_way_geometry())
18836502 q {
18936502 q std::cout<<",\n \"geometry\": [";
190270332 q for (uint i = 0; i < geometry.way_size(); ++i)
191233832 q {
192233832 q if (geometry.way_pos_is_valid(i))
193233832 q std::cout<<"\n { \"lat\": "<<std::fixed<<std::setprecision(7)
194233832 q <<geometry.way_pos_lat(i)
195233832 q <<", \"lon\": "<<std::fixed<<std::setprecision(7)
196233832 q <<geometry.way_pos_lon(i)<<" }";
1970 else
1980 std::cout<<"\n null";
200233832 q if (i < geometry.way_size() - 1)
201197332 q std::cout << ",";
202233832 q }
20336502 q std::cout<<"\n ]";
20436502 q }
2066060524 q print_tags(tags);
2076060524 q std::cout<<"\n}";
2086060524 q}
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,
217 Output_Mode mode,
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)
223157610 q{
224157610 q handle_first_elem(first_elem);
225157610 q std::cout<<"{\n"
226157610 q " \"type\": \"relation\"";
227157610 q if (mode.mode & Output_Mode::ID)
228154810 q std::cout<<",\n \"id\": "<<skel.id.val();
230157610 q if (meta)
231302 q print_meta_json(*meta, *users);
233157610 q print_bounds(geometry, mode);
235157610 q if (roles && (mode.mode & Output_Mode::MEMBERS) != 0 && !skel.members.empty())
23612498 q {
23712498 q std::cout<<",\n \"members\": [";
238809558 q for (uint i = 0; i < skel.members.size(); i++)
239797068 q {
240797068 q std::map< uint32, std::string >::const_iterator rit = roles->find(skel.members[i].role);
241797068 q std::cout<< (i == 0 ? "" : ",");
242797068 q std::cout <<"\n {"
243797068 q "\n \"type\": \""<<member_type_name(skel.members[i].type)<<
244797068 q "\",\n \"ref\": "<<skel.members[i].ref.val()<<
245797068 q ",\n \"role\": \""<<escape_cstr(rit != roles->end() ? rit->second : "???") << "\"";
247797068 q if (skel.members[i].type == Relation_Entry::NODE &&
248797068 q geometry.has_faithful_relation_geometry() && geometry.relation_pos_is_valid(i))
24917052 q std::cout<<",\n \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.relation_pos_lat(i)
25017052 q <<",\n \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.relation_pos_lon(i);
252797068 q if (skel.members[i].type == Relation_Entry::WAY && geometry.has_faithful_relation_geometry())
253150544 q {
254150544 q std::cout<<",\n \"geometry\": [";
2551997164 q for (uint j = 0; j < geometry.relation_way_size(i); ++j)
2561846624 q {
2571846624 q if (geometry.relation_pos_is_valid(i, j))
2581846624 q {
2591846624 q std::cout<<"\n { \"lat\": "<<std::fixed<<std::setprecision(7)
2601846624 q <<geometry.relation_pos_lat(i, j)
2611846624 q <<", \"lon\": "<<std::fixed<<std::setprecision(7)
2621846624 q <<geometry.relation_pos_lon(i, j)<<" }";
2631846624 q }
2640 else
2650 std::cout<<"\n null";
2661846624 q if (j < geometry.relation_way_size(i) - 1)
2671721874 q std::cout << ",";
2681846624 q }
269150544 q std::cout<<"\n ]";
270150544 q }
272797068 q std::cout<<"\n }";
273797068 q }
27412498 q std::cout<<"\n ]";
27512498 q }
277157610 q print_tags(tags);
278157610 q std::cout<<"\n}";
279157610 q}
282void print_geometry(const Opaque_Geometry& geometry, const std::string& indent)
2830{
2840 if (geometry.has_components())
2850 {
2860 std::cout<<"{"
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)
2930 {
2940 if (*it && (*it)->has_center())
2950 {
2960 if (first_printed)
2970 first_printed = false;
2980 else
2990 std::cout<<",";
3010 std::cout<<"\n"<<indent<<" ";
3020 print_geometry(**it, indent + " ");
3030 }
3040 }
3060 std::cout<<"\n"<<indent<<" ]\n"<<indent<<"}";
3070 }
3080 else if (geometry.has_line_geometry())
3090 {
3100 std::cout<<"{"
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<<"}";
3210 }
3220 else if (geometry.has_multiline_geometry())
3230 {
3240 std::cout<<"{"
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)
3310 {
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<<" ]";
3380 }
3400 std::cout<<"\n"<<indent<<" ]\n"<<indent<<"}";
3410 }
3420 else if (geometry.has_center())
3430 std::cout<<"{"
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()<<" ]"
3480 "\n"<<indent<<"}";
3490}
352void print_geometry(const Opaque_Geometry& geometry, Output_Mode mode)
3531064760 q{
3541064760 q if ((mode.mode & Output_Mode::GEOMETRY) && (geometry.has_center()))
3550 {
3560 std::cout<<",\n \"geometry\": ";
3570 print_geometry(geometry, " ");
3580 }
3591064760 q else if ((mode.mode & Output_Mode::BOUNDS) && geometry.has_bbox())
3605461 q std::cout<<",\n \"geometry\": {"
3615461 q "\n \"type\": \"Polygon\","
3625461 q "\n \"coordinates\": ["
3635461 q "\n ["
3645461 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
3655461 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
3665461 q ",\n ["
3675461 q <<std::fixed<<std::setprecision(7)<<geometry.east()<<", "
3685461 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
3695461 q ",\n ["
3705461 q <<std::fixed<<std::setprecision(7)<<geometry.east()<<", "
3715461 q <<std::fixed<<std::setprecision(7)<<geometry.north()<<"]"
3725461 q ",\n ["
3735461 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
3745461 q <<std::fixed<<std::setprecision(7)<<geometry.north()<<"]"
3755461 q ",\n ["
3765461 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
3775461 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
3785461 q "\n ]\n }";
3791010160 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()<<" ]"
3850 "\n }";
3861064760 q}
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,
392 Output_Mode mode,
393 const Feature_Action& action)
3941064760 q{
3951064760 q handle_first_elem(first_elem);
3961064760 q std::cout<<"{\n"
3971064760 q " \"type\": \""<<skel.type_name<<"\"";
3981064760 q if (mode.mode & Output_Mode::ID)
3991064660 q std::cout<<",\n \"id\": "<<skel.id.val();
4011064760 q print_geometry(geometry, mode);
4021064760 q print_tags(tags);
4031064760 q std::cout<<"\n}";
4041064760 q}