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

overpass_api/output_formats/output_csv.cc

153 of 167 instrumented lines run by the suite (91.6%) 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 "output_csv.h"
23bool Output_CSV::write_http_headers()
240{
250 std::cout<<"Content-type: text/csv\n";
260 return true;
270}
30// Escape CSV output according to RFC 4180
31std::string escape_csv(const std::string& input)
3266128 q{
3366128 q std::string result;
3466128 q bool quotes_needed = false;
361155428 q for (int i = input.size() - 1; i >= 0; --i)
371089328 q {
381089328 q if (input[i] == '\n' || input[i] == ',')
3972 q quotes_needed = true;
401088628 q else if (input[i] == '\"')
4221 q quotes_needed = true;
4321 q if (result.empty())
4411 q result = input.substr(0, i) + "\"" + input.substr(i);
4511 q else
4611 q result = result.substr(0, i) + "\"" + result.substr(i);
481089328 q }
5066128 q if (!quotes_needed)
5165328 q return input;
5282 q else if (result.empty())
5372 q return std::string("\"") + input + "\"";
5411 q else
5511 q return std::string("\"") + result + "\"";
5666128 q}
59void Output_CSV::write_payload_header
60 (const std::string& db_dir, const std::string& timestamp, const std::string& area_timestamp)
622929 q if (csv_settings.with_headerline)
632828 q {
642828 q for (std::vector< std::pair< std::string, bool > >::const_iterator it = csv_settings.keyfields.begin();
658828 q it != csv_settings.keyfields.end(); ++it)
666028 q {
676028 q std::cout<<(it->second ? "@" : "")<<escape_csv(it->first);
686028 q if (it + 1 != csv_settings.keyfields.end())
693218 q std::cout<<csv_settings.separator;
706028 q }
712828 q std::cout<<'\n';
722828 q }
76void Output_CSV::write_footer()
78 // Intentionally empty
82void Output_CSV::display_remark(const std::string& text)
830{
84 // Intentionally empty
850}
88void Output_CSV::display_error(const std::string& text)
890{
90 // Intentionally empty
910}
94std::string Output_CSV::dump_config() const
9517429 q{
9617429 q std::string result = "(";
9817429 q for (std::vector< std::pair< std::string, bool > >::const_iterator it = csv_settings.keyfields.begin();
9954629 q it != csv_settings.keyfields.end(); ++it)
10037229 q {
10137229 q if (it != csv_settings.keyfields.begin())
10219819 q result += ",";
10437229 q if (it->second)
10528827 q result += "::" + it->first;
1068411 q else
1078411 q result += "\"" + escape_cstr(it->first) + "\"";
10837229 q }
11017429 q if (csv_settings.separator != "\t")
111183 q result += std::string(";") + (csv_settings.with_headerline ? "true" : "false") + ";\""
112183 q + escape_cstr(csv_settings.separator) + "\"";
11315626 q else if (!csv_settings.with_headerline)
1140 result += ";false";
11617429 q return result + ")";
120template< typename OSM_Element_Metadata_Skeleton >
121void print_meta(const std::string& keyfield,
122 const OSM_Element_Metadata_Skeleton& meta, const std::map< uint32, std::string >* users)
1231171619 q{
1241171619 q if (keyfield == "version")
12582 q std::cout<<meta.version;
1261171538 q else if (keyfield == "timestamp")
1270 std::cout<<Timestamp(meta.timestamp).str();
1281171538 q else if (keyfield == "changeset")
12959872 q std::cout<<meta.changeset;
1301111667 q else if (keyfield == "uid")
1311111312 q std::cout<<meta.user_id;
132355 q else if (users && keyfield == "user")
13482 q std::map< uint32, std::string >::const_iterator uit = users->find(meta.user_id);
13582 q if (uit != users->end())
13682 q std::cout<<uit->second;
1381171619 q}
141template< >
142void print_meta< int >(const std::string& keyfield,
1430 const int& meta, const std::map< uint32, std::string >* users) {}
145std::string get_count_tag(const std::vector< std::pair< std::string, std::string> >* tags, std::string tag)
14783 q if (tags)
14883 q for (std::vector< std::pair< std::string, std::string> >::const_iterator it_tags = tags->begin();
149363 q it_tags != tags->end(); ++it_tags)
150323 q if (it_tags->first == tag)
15142 q return it_tags->second;
15241 q return "0";
156template< typename Id_Type, typename OSM_Element_Metadata_Skeleton >
157void process_csv_line(int otype, const std::string& type, Id_Type id, const Opaque_Geometry& geometry,
158 const OSM_Element_Metadata_Skeleton* meta,
159 const std::vector< std::pair< std::string, std::string> >* tags,
160 const std::map< uint32, std::string >* users,
161 const Csv_Settings& csv_settings,
162 Output_Mode mode)
16314214129 q{
16414214129 q std::vector< std::pair< std::string, bool > >::const_iterator it = csv_settings.keyfields.begin();
16522868529 q while (true)
16622868529 q {
16722868529 q if (!it->second)
1683086811 q {
1693086811 q if (tags)
1701656011 q {
1711656011 q for (std::vector< std::pair< std::string, std::string> >::const_iterator it_tags = tags->begin();
1726993811 q it_tags != tags->end(); ++it_tags)
1735397911 q {
1745397911 q if (it_tags->first == it->first)
1756015 q {
1766015 q std::cout<<escape_csv(it_tags->second);
1776015 q break;
1786015 q }
1795397911 q }
1801656011 q }
1813086811 q }
18219781727 q else
18319781727 q {
18419781727 q if (meta)
1851171619 q print_meta(it->first, *meta, users);
18719781727 q if (it->first == "id")
188138378 q {
189138378 q if (mode.mode & Output_Mode::ID)
190138218 q std::cout<<id.val();
191138378 q }
19218398025 q else if (it->first == "otype")
193403 q std::cout<<otype;
19418394024 q else if (it->first == "type")
195138084 q std::cout<<type;
19617013224 q else if (it->first == "lat")
197231269 q {
198231269 q if ((mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER))
199231269 q && geometry.has_center())
200112987 q std::cout<<std::fixed<<std::setprecision(7)<<geometry.center_lat();
201231269 q }
20214700622 q else if (it->first == "lon")
203218328 q {
204218328 q if ((mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER))
205218328 q && geometry.has_center())
206112186 q std::cout<<std::fixed<<std::setprecision(7)<<geometry.center_lon();
207218328 q }
20819781727 q if (type == "count")
209235327 q {
210235327 q if (it->first == "count")
21142 q std::cout << get_count_tag(tags, "total");
212234925 q else if (it->first == "count:nodes")
2130 std::cout << get_count_tag(tags, "nodes");
214234925 q else if (it->first == "count:ways")
2150 std::cout << get_count_tag(tags, "ways");
216234925 q else if (it->first == "count:relations")
2170 std::cout << get_count_tag(tags, "relations");
218234925 q else if (it->first == "count:areas")
21941 q std::cout << get_count_tag(tags, "areas");
220235327 q }
22119781727 q }
22322868529 q if (++it == csv_settings.keyfields.end())
22414214129 q break;
2258654419 q std::cout<<csv_settings.separator;
2268654419 q }
22714214129 q std::cout<<"\n";
22814214129 q}
231void Output_CSV::print_item(const Node_Skeleton& skel,
232 const Opaque_Geometry& geometry,
233 const std::vector< std::pair< std::string, std::string > >* tags,
234 const OSM_Element_Metadata_Skeleton< Node::Id_Type >* meta,
235 const std::map< uint32, std::string >* users,
236 Output_Mode mode,
237 const Feature_Action& action,
238 const Node_Skeleton* new_skel,
239 const Opaque_Geometry* new_geometry,
240 const std::vector< std::pair< std::string, std::string > >* new_tags,
241 const OSM_Element_Metadata_Skeleton< Node::Id_Type >* new_meta)
24211478312 q{
24311478312 q process_csv_line(1, "node", skel.id, geometry, meta, tags, users, csv_settings, mode);
24411478312 q}
247void Output_CSV::print_item(const Way_Skeleton& skel,
248 const Opaque_Geometry& geometry,
249 const std::vector< std::pair< std::string, std::string > >* tags,
250 const OSM_Element_Metadata_Skeleton< Way::Id_Type >* meta,
251 const std::map< uint32, std::string >* users,
252 Output_Mode mode,
253 const Feature_Action& action,
254 const Way_Skeleton* new_skel,
255 const Opaque_Geometry* new_geometry,
256 const std::vector< std::pair< std::string, std::string > >* new_tags,
257 const OSM_Element_Metadata_Skeleton< Way::Id_Type >* new_meta)
2581244615 q{
2591244615 q process_csv_line(2, "way", skel.id, geometry, meta, tags, users, csv_settings, mode);
2601244615 q}
263void Output_CSV::print_item(const Relation_Skeleton& skel,
264 const Opaque_Geometry& geometry,
265 const std::vector< std::pair< std::string, std::string > >* tags,
266 const OSM_Element_Metadata_Skeleton< Relation::Id_Type >* meta,
267 const std::map< uint32, std::string >* roles,
268 const std::map< uint32, std::string >* users,
269 Output_Mode mode,
270 const Feature_Action& action,
271 const Relation_Skeleton* new_skel,
272 const Opaque_Geometry* new_geometry,
273 const std::vector< std::pair< std::string, std::string > >* new_tags,
274 const OSM_Element_Metadata_Skeleton< Relation::Id_Type >* new_meta)
27538646 q{
27638646 q process_csv_line(3, "relation", skel.id, geometry, meta, tags, users, csv_settings, mode);
27738646 q}
280void Output_CSV::print_item(const Derived_Skeleton& skel,
281 const Opaque_Geometry& geometry,
282 const std::vector< std::pair< std::string, std::string > >* tags,
283 Output_Mode mode,
284 const Feature_Action& action)
2851104829 q{
2861104829 q process_csv_line< Derived_Skeleton::Id_Type, int >(
2871104829 q 4, skel.type_name, skel.id, geometry, 0, tags, 0, csv_settings, mode);
2881104829 q}