Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ nobase_dist_HEADERS = \
overpass_api/osm-backend/way_updater.h\
overpass_api/output_formats/output_csv.h\
overpass_api/output_formats/output_custom.h\
overpass_api/output_formats/fixed7.h\
overpass_api/output_formats/output_json.h\
overpass_api/output_formats/output_popup.h\
overpass_api/output_formats/output_xml.h\
Expand Down
88 changes: 88 additions & 0 deletions src/overpass_api/output_formats/fixed7.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/** Copyright 2026 the Overpass API contributors
*
* This file is part of Overpass_API.
*
* Overpass_API is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Overpass_API is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef DE__OSM3S___OVERPASS_API__OUTPUT_FORMATS__FIXED7_H
#define DE__OSM3S___OVERPASS_API__OUTPUT_FORMATS__FIXED7_H

#include <cmath>
#include <cstdio>
#include <cstring>
#include <ostream>


/* Writes a coordinate with exactly the same characters as
out<<std::fixed<<std::setprecision(7)<<value, but without the general-purpose
floating-point formatting, which dominates the cost of large geometry outputs.

Fast path: scale by 1e7 and round. For |value| <= 1e9 the scaling error is far
below 1e-6, so unless the scaled value is within 1e-6 of a rounding tie, rounding
it gives the same digits as printf's correctly rounded "%.7f". Near ties, and for
large, infinite or NaN values, it falls back to snprintf("%.7f").

The stream is left in fixed notation with precision 7, as the original
manipulators did, so later output that relies on that state is unaffected. */
struct Fixed7
{
explicit Fixed7(double value_) : value(value_) {}
double value;
};


inline int format_fixed7(char* buf, double value)
{
if (!(std::abs(value) <= 1e9))
return std::snprintf(buf, 64, "%.7f", value);

double scaled = std::abs(value) * 1e7;
double rounded = std::floor(scaled + 0.5);
double frac = scaled - std::floor(scaled);
if (std::abs(frac - 0.5) < 1e-6)
return std::snprintf(buf, 64, "%.7f", value);

unsigned long long n = (unsigned long long)rounded;
char digits[32];
int len = 0;
while (n > 0 || len < 8)
{
digits[len++] = '0' + (n % 10);
n /= 10;
}
int pos = 0;
if (std::signbit(value))
buf[pos++] = '-';
for (int i = len - 1; i >= 7; --i)
buf[pos++] = digits[i];
buf[pos++] = '.';
for (int i = 6; i >= 0; --i)
buf[pos++] = digits[i];
buf[pos] = 0;
return pos;
}


inline std::ostream& operator<<(std::ostream& out, const Fixed7& f)
{
out.setf(std::ios::fixed, std::ios::floatfield);
out.precision(7);
char buf[64];
out.write(buf, format_fixed7(buf, f.value));
return out;
}


#endif
5 changes: 3 additions & 2 deletions src/overpass_api/output_formats/output_csv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "../../expat/escape_json.h"
#include "output_csv.h"
#include "fixed7.h"


bool Output_CSV::write_http_headers()
Expand Down Expand Up @@ -197,13 +198,13 @@ void process_csv_line(int otype, const std::string& type, Id_Type id, const Opaq
{
if ((mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER))
&& geometry.has_center())
std::cout<<std::fixed<<std::setprecision(7)<<geometry.center_lat();
std::cout<<Fixed7(geometry.center_lat());
}
else if (it->first == "lon")
{
if ((mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER))
&& geometry.has_center())
std::cout<<std::fixed<<std::setprecision(7)<<geometry.center_lon();
std::cout<<Fixed7(geometry.center_lon());
}
if (type == "count")
{
Expand Down
17 changes: 9 additions & 8 deletions src/overpass_api/output_formats/output_custom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "../../expat/escape_xml.h"
#include "../frontend/basic_formats.h"
#include "output_custom.h"
#include "fixed7.h"

#include <cmath>
#include <fstream>
Expand Down Expand Up @@ -329,9 +330,9 @@ std::string process_coords(const std::string& raw_template, uint32 id, double la
else if (raw_template.substr(new_pos, 8) == "{{{id}}}")
result<<id;
else if (raw_template.substr(new_pos, 9) == "{{{lat}}}")
result<<std::fixed<<std::setprecision(7)<<lat;
result<<Fixed7(lat);
else if (raw_template.substr(new_pos, 9) == "{{{lon}}}")
result<<std::fixed<<std::setprecision(7)<<lon;
result<<Fixed7(lon);
else
result<<raw_template.substr(new_pos, old_pos - new_pos);
new_pos = raw_template.find("{{", old_pos);
Expand Down Expand Up @@ -362,17 +363,17 @@ std::string process_coords(const std::string& raw_template, uint32 id,
else if (raw_template.substr(new_pos, 8) == "{{{id}}}")
result<<id;
else if (raw_template.substr(new_pos, 11) == "{{{south}}}")
result<<std::fixed<<std::setprecision(7)<<south;
result<<Fixed7(south);
else if (raw_template.substr(new_pos, 10) == "{{{west}}}")
result<<std::fixed<<std::setprecision(7)<<west;
result<<Fixed7(west);
else if (raw_template.substr(new_pos, 11) == "{{{north}}}")
result<<std::fixed<<std::setprecision(7)<<north;
result<<Fixed7(north);
else if (raw_template.substr(new_pos, 10) == "{{{east}}}")
result<<std::fixed<<std::setprecision(7)<<east;
result<<Fixed7(east);
else if (raw_template.substr(new_pos, 9) == "{{{lat}}}")
result<<std::fixed<<std::setprecision(7)<<(south + north)/2.0;
result<<Fixed7((south + north)/2.0);
else if (raw_template.substr(new_pos, 9) == "{{{lon}}}")
result<<std::fixed<<std::setprecision(7)<<(east + west)/2.0;
result<<Fixed7((east + west)/2.0);
else if (raw_template.substr(new_pos, 10) == "{{{zoom}}}")
result<<zoom;
else if (raw_template.substr(new_pos, 7) == "{{none:")
Expand Down
69 changes: 33 additions & 36 deletions src/overpass_api/output_formats/output_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "../core/settings.h"
#include "../frontend/basic_formats.h"
#include "output_json.h"
#include "fixed7.h"


bool Output_JSON::write_http_headers()
Expand Down Expand Up @@ -124,8 +125,8 @@ void Output_JSON::print_item(const Node_Skeleton& skel,
std::cout<<",\n \"id\": "<<skel.id.val();

if (mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER))
std::cout<<",\n \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lat()
<<",\n \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lon();
std::cout<<",\n \"lat\": "<<Fixed7(geometry.center_lat())
<<",\n \"lon\": "<<Fixed7(geometry.center_lon());
if (meta)
print_meta_json(*meta, *users);

Expand All @@ -138,15 +139,15 @@ void print_bounds(const Opaque_Geometry& geometry, Output_Mode mode)
{
if ((mode.mode & Output_Mode::BOUNDS) && geometry.has_bbox())
std::cout<<",\n \"bounds\": {\n"
" \"minlat\": "<<std::fixed<<std::setprecision(7)<<geometry.south()<<",\n"
" \"minlon\": "<<std::fixed<<std::setprecision(7)<<geometry.west()<<",\n"
" \"maxlat\": "<<std::fixed<<std::setprecision(7)<<geometry.north()<<",\n"
" \"maxlon\": "<<std::fixed<<std::setprecision(7)<<geometry.east()<<"\n"
" \"minlat\": "<<Fixed7(geometry.south())<<",\n"
" \"minlon\": "<<Fixed7(geometry.west())<<",\n"
" \"maxlat\": "<<Fixed7(geometry.north())<<",\n"
" \"maxlon\": "<<Fixed7(geometry.east())<<"\n"
" }";
else if ((mode.mode & Output_Mode::CENTER) && geometry.has_center())
std::cout<<",\n \"center\": {\n"
" \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lat()<<",\n"
" \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.center_lon()<<"\n"
" \"lat\": "<<Fixed7(geometry.center_lat())<<",\n"
" \"lon\": "<<Fixed7(geometry.center_lon())<<"\n"
" }";
}

Expand Down Expand Up @@ -190,10 +191,8 @@ void Output_JSON::print_item(const Way_Skeleton& skel,
for (uint i = 0; i < geometry.way_size(); ++i)
{
if (geometry.way_pos_is_valid(i))
std::cout<<"\n { \"lat\": "<<std::fixed<<std::setprecision(7)
<<geometry.way_pos_lat(i)
<<", \"lon\": "<<std::fixed<<std::setprecision(7)
<<geometry.way_pos_lon(i)<<" }";
std::cout<<"\n { \"lat\": "<<Fixed7(geometry.way_pos_lat(i))
<<", \"lon\": "<<Fixed7(geometry.way_pos_lon(i))<<" }";
else
std::cout<<"\n null";

Expand Down Expand Up @@ -246,8 +245,8 @@ void Output_JSON::print_item(const Relation_Skeleton& skel,

if (skel.members[i].type == Relation_Entry::NODE &&
geometry.has_faithful_relation_geometry() && geometry.relation_pos_is_valid(i))
std::cout<<",\n \"lat\": "<<std::fixed<<std::setprecision(7)<<geometry.relation_pos_lat(i)
<<",\n \"lon\": "<<std::fixed<<std::setprecision(7)<<geometry.relation_pos_lon(i);
std::cout<<",\n \"lat\": "<<Fixed7(geometry.relation_pos_lat(i))
<<",\n \"lon\": "<<Fixed7(geometry.relation_pos_lon(i));

if (skel.members[i].type == Relation_Entry::WAY && geometry.has_faithful_relation_geometry())
{
Expand All @@ -256,10 +255,8 @@ void Output_JSON::print_item(const Relation_Skeleton& skel,
{
if (geometry.relation_pos_is_valid(i, j))
{
std::cout<<"\n { \"lat\": "<<std::fixed<<std::setprecision(7)
<<geometry.relation_pos_lat(i, j)
<<", \"lon\": "<<std::fixed<<std::setprecision(7)
<<geometry.relation_pos_lon(i, j)<<" }";
std::cout<<"\n { \"lat\": "<<Fixed7(geometry.relation_pos_lat(i, j))
<<", \"lon\": "<<Fixed7(geometry.relation_pos_lon(i, j))<<" }";
}
else
std::cout<<"\n null";
Expand Down Expand Up @@ -314,8 +311,8 @@ void print_geometry(const Opaque_Geometry& geometry, const std::string& indent)
const std::vector< Point_Double >* line = geometry.get_line_geometry();
for (std::vector< Point_Double >::const_iterator it = line->begin(); it != line->end(); ++it)
std::cout<<(it == line->begin() ? "" : ",")<<"\n"<<indent<<" ["
<<std::fixed<<std::setprecision(7)<<it->lon<<", "
<<std::fixed<<std::setprecision(7)<<it->lat<<"]";
<<Fixed7(it->lon)<<", "
<<Fixed7(it->lat)<<"]";

std::cout<<"\n"<<indent<<" ]\n"<<indent<<"}";
}
Expand All @@ -332,8 +329,8 @@ void print_geometry(const Opaque_Geometry& geometry, const std::string& indent)
std::cout<<(iti == linestrings->begin() ? "" : ",")<<"\n"<<indent<<" [";
for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it)
std::cout<<(it == iti->begin() ? "" : ",")<<"\n"<<indent<<" ["
<<std::fixed<<std::setprecision(7)<<it->lon<<", "
<<std::fixed<<std::setprecision(7)<<it->lat<<"]";
<<Fixed7(it->lon)<<", "
<<Fixed7(it->lat)<<"]";
std::cout<<"\n"<<indent<<" ]";
}

Expand All @@ -343,8 +340,8 @@ void print_geometry(const Opaque_Geometry& geometry, const std::string& indent)
std::cout<<"{"
"\n"<<indent<<" \"type\": \"Point\","
"\n"<<indent<<" \"coordinates\": [ "
<<std::fixed<<std::setprecision(7)<<geometry.center_lon()<<", "
<<std::fixed<<std::setprecision(7)<<geometry.center_lat()<<" ]"
<<Fixed7(geometry.center_lon())<<", "
<<Fixed7(geometry.center_lat())<<" ]"
"\n"<<indent<<"}";
}

Expand All @@ -361,27 +358,27 @@ void print_geometry(const Opaque_Geometry& geometry, Output_Mode mode)
"\n \"type\": \"Polygon\","
"\n \"coordinates\": ["
"\n ["
<<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
<<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
<<Fixed7(geometry.west())<<", "
<<Fixed7(geometry.south())<<"]"
",\n ["
<<std::fixed<<std::setprecision(7)<<geometry.east()<<", "
<<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
<<Fixed7(geometry.east())<<", "
<<Fixed7(geometry.south())<<"]"
",\n ["
<<std::fixed<<std::setprecision(7)<<geometry.east()<<", "
<<std::fixed<<std::setprecision(7)<<geometry.north()<<"]"
<<Fixed7(geometry.east())<<", "
<<Fixed7(geometry.north())<<"]"
",\n ["
<<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
<<std::fixed<<std::setprecision(7)<<geometry.north()<<"]"
<<Fixed7(geometry.west())<<", "
<<Fixed7(geometry.north())<<"]"
",\n ["
<<std::fixed<<std::setprecision(7)<<geometry.west()<<", "
<<std::fixed<<std::setprecision(7)<<geometry.south()<<"]"
<<Fixed7(geometry.west())<<", "
<<Fixed7(geometry.south())<<"]"
"\n ]\n }";
else if ((mode.mode & Output_Mode::CENTER) && geometry.has_center())
std::cout<<",\n \"geometry\": {"
"\n \"type\": \"Point\","
"\n \"coordinates\": [ "
<<std::fixed<<std::setprecision(7)<<geometry.center_lon()<<", "
<<std::fixed<<std::setprecision(7)<<geometry.center_lat()<<" ]"
<<Fixed7(geometry.center_lon())<<", "
<<Fixed7(geometry.center_lat())<<" ]"
"\n }";
}

Expand Down
Loading