Overpass API regression suite coverage (Nova Scotia) / 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)
356060 q if (padding != "")
360 std::cout<<padding<<"(";
386060 q std::cout<<"{\n"
396060 q " \"version\": 0.6,\n"
406060 q " \"generator\": \"Overpass API "<<basic_settings().version<<" "
416060 q <<basic_settings().source_hash.substr(0, 8)<<"\",\n"
426060 q " \"osm3s\": {\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"
486060 q " },\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)
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)
739257960 q{
749257960 q if (!first_elem)
759251953 q std::cout<<",\n";
769257960 q first_elem = false;
779257960 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)
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)
959257960 q{
969257960 q if (tags != 0 && !tags->empty())
973234660 q {
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)<<"\"";
1033234660 q std::cout<<"\n }";
1043234660 q }
1059257960 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)
1193650730 q{
1203650730 q handle_first_elem(first_elem);
1213650730 q std::cout<<"{\n"
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();
1293650730 q if (meta)
130472 q print_meta_json(*meta, *users);
1323650730 q print_tags(tags);
1333650730 q std::cout<<"\n}";
1343650730 q}
137void print_bounds(const Opaque_Geometry& geometry, Output_Mode mode)
1383672329 q{
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"
145112114 q " }";
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"
15043063 q " }";
1513672329 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)
1653346925 q{
1663346925 q handle_first_elem(first_elem);
1673346925 q std::cout<<"{\n"
1683346925 q " \"type\": \"way\"";
1693346925 q if (mode.mode & Output_Mode::ID)
1703332524 q std::cout<<",\n \"id\": "<<skel.id.val();
1723346925 q if (meta)
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())
178271227 q {
179271227 q std::vector< Node::Id_Type >::const_iterator it = skel.nds.begin();
180271227 q std::cout<<",\n \"nodes\": ["
181271227 q "\n "<<it->val();
1828074207 q for (++it; it != skel.nds.end(); ++it)
1837802987 q std::cout<<",\n "<<it->val();
184271227 q std::cout<<"\n ]";
185271227 q }
1873346925 q if ((mode.mode & Output_Mode::GEOMETRY) != 0 && geometry.has_faithful_way_geometry())
18883312 q {
18983312 q std::cout<<",\n \"geometry\": [";
1903578152 q for (uint i = 0; i < geometry.way_size(); ++i)
1913494842 q {
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)<<" }";
1970 else
1980 std::cout<<"\n null";
2003494842 q if (i < geometry.way_size() - 1)
2013411532 q std::cout << ",";
2023494842 q }
20383312 q std::cout<<"\n ]";
20483312 q }
2063346925 q print_tags(tags);
2073346925 q std::cout<<"\n}";
2083346925 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)
223325414 q{
224325414 q handle_first_elem(first_elem);
225325414 q std::cout<<"{\n"
226325414 q " \"type\": \"relation\"";
227325414 q if (mode.mode & Output_Mode::ID)
228324413 q std::cout<<",\n \"id\": "<<skel.id.val();
230325414 q if (meta)
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())
23629068 q {
23729068 q std::cout<<",\n \"members\": [";
238350168 q for (uint i = 0; i < skel.members.size(); i++)
239321108 q {
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())
253265302 q {
254265302 q std::cout<<",\n \"geometry\": [";
2558642512 q for (uint j = 0; j < geometry.relation_way_size(i); ++j)
2568377212 q {
2578377212 q if (geometry.relation_pos_is_valid(i, j))
2588377212 q {
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)<<" }";
2638377212 q }
2640 else
2650 std::cout<<"\n null";
2668377212 q if (j < geometry.relation_way_size(i) - 1)
2678143142 q std::cout << ",";
2688377212 q }
269265302 q std::cout<<"\n ]";
270265302 q }
272321108 q std::cout<<"\n }";
273321108 q }
27429068 q std::cout<<"\n ]";
27529068 q }
277325414 q print_tags(tags);
278325414 q std::cout<<"\n}";
279325414 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)
3531934957 q{
3541934957 q if ((mode.mode & Output_Mode::GEOMETRY) && (geometry.has_center()))
3550 {
3560 std::cout<<",\n \"geometry\": ";
3570 print_geometry(geometry, " ");
3580 }
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\": ["
3632881 q "\n ["
3642881 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
3652881 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
3662881 q ",\n ["
3672881 q <<std::fixed<<std::setprecision(7)<<geometry.east()<<", "
3682881 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
3692881 q ",\n ["
3702881 q <<std::fixed<<std::setprecision(7)<<geometry.east()<<", "
3712881 q <<std::fixed<<std::setprecision(7)<<geometry.north()<<"]"
3722881 q ",\n ["
3732881 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
3742881 q <<std::fixed<<std::setprecision(7)<<geometry.north()<<"]"
3752881 q ",\n ["
3762881 q <<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
3772881 q <<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
3782881 q "\n ]\n }";
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()<<" ]"
3850 "\n }";
3861934957 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)
3941934957 q{
3951934957 q handle_first_elem(first_elem);
3961934957 q std::cout<<"{\n"
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);
4021934957 q print_tags(tags);
4031934957 q std::cout<<"\n}";
4041934957 q}