[generator] real numbers precision

This commit is contained in:
LaGrunge 2019-06-14 12:37:22 +03:00 committed by mpimenov
parent 7889a2a982
commit 3ce5c0a6fa
6 changed files with 38 additions and 7 deletions

View file

@ -1,4 +1,5 @@
#include "generator/feature_builder.hpp"
#include "generator/regions/to_string_policy.hpp"
#include "indexer/classificator.hpp"
#include "indexer/classificator_loader.hpp"
@ -79,7 +80,8 @@ void PrintFeature(FeatureBuilder const & fb, uint64_t)
ToJSONObject(*feature, "geometry", geometry);
ToJSONObject(*feature, "properties", properties);
std::cout << json_dumps(feature.release(), JSON_COMPACT) << std::endl;
std::cout << json_dumps(feature.release(),
JSON_REAL_PRECISION(generator::regions::JsonPolicy::kDefaultPrecision) | JSON_COMPACT) << std::endl;
}
int main(int argc, char * argv[])

View file

@ -8,6 +8,7 @@
#include "generator/locality_sorter.hpp"
#include "generator/regions/region_base.hpp"
#include "generator/regions/region_info_getter.hpp"
#include "generator/regions/to_string_policy.hpp"
#include "indexer/classificator.hpp"
#include "indexer/ftypes_matcher.hpp"
@ -93,7 +94,8 @@ std::unique_ptr<char, JSONFreeDeleter>
MakeGeoObjectValueWithAddress(FeatureBuilder const & fb, KeyValue const & keyValue)
{
auto const jsonWithAddress = AddAddress(fb, keyValue);
auto const cstr = json_dumps(jsonWithAddress.get(), JSON_COMPACT);
auto const cstr = json_dumps(jsonWithAddress.get(),
JSON_REAL_PRECISION(regions::JsonPolicy::kDefaultPrecision) | JSON_COMPACT);
return std::unique_ptr<char, JSONFreeDeleter>(cstr);
}
@ -110,7 +112,8 @@ MakeGeoObjectValueWithoutAddress(FeatureBuilder const & fb, JsonValue const & js
auto properties = json_object_get(jsonWithAddress.get(), "properties");
ToJSONObject(*properties, "name", fb.GetName());
UpdateCoordinates(fb.GetKeyPoint(), jsonWithAddress);
auto const cstr = json_dumps(jsonWithAddress.get(), JSON_COMPACT);
auto const cstr = json_dumps(jsonWithAddress.get(),
JSON_REAL_PRECISION(generator::regions::JsonPolicy::kDefaultPrecision) | JSON_COMPACT);
return std::unique_ptr<char, JSONFreeDeleter>(cstr);
}

View file

@ -93,6 +93,8 @@ private:
size_t countryObjectCount = 0;
auto jsonPolicy = std::make_unique<JsonPolicy>(m_verbose);
jsonPolicy->SetRealPrecision(JsonPolicy::kDefaultPrecision);
for (auto const & tree : outers)
{
if (m_verbose)

View file

@ -2,6 +2,8 @@
#include "geometry/mercator.hpp"
#include "base/assert.hpp"
#include <cstdint>
#include <memory>
@ -13,6 +15,14 @@ namespace generator
{
namespace regions
{
JsonPolicy & JsonPolicy::SetRealPrecision(int32_t precision)
{
CHECK_LESS(precision, 32, ());
m_precision = precision;
return *this;
}
std::string JsonPolicy::ToString(NodePath const & path) const
{
auto const & main = path.back()->GetData();
@ -69,7 +79,11 @@ std::string JsonPolicy::ToString(NodePath const & path) const
ToJSONObject(*feature, "geometry", geometry);
ToJSONObject(*feature, "properties", properties);
auto const cstr = json_dumps(feature.get(), JSON_COMPACT);
uint32_t jsonFlags = JSON_COMPACT;
if (m_precision >= 0)
jsonFlags |= JSON_REAL_PRECISION(m_precision);
auto const cstr = json_dumps(feature.get(), jsonFlags);
std::unique_ptr<char, JSONFreeDeleter> buffer(cstr);
return buffer.get();
}

View file

@ -20,11 +20,20 @@ class JsonPolicy : public ToStringPolicyInterface
{
public:
JsonPolicy(bool extendedOutput = false) : m_extendedOutput(extendedOutput) {}
JsonPolicy & SetRealPrecision(int32_t precision);
std::string ToString(NodePath const & path) const override;
public:
// Longitude and latitude has maximum 3 digits before comma. So we have minimum 6 digits after comma.
// Nautical mile is good approximation for one angle minute, so we can rely, that
// final precision is 60 (minutes in degree) * 1852 (meters in one mile) / 1000000 = 0.111 = 111 millimeters.
// Also, if you are quizzed by nautical mile, just forget, precision was defined in https://jira.mail.ru/browse/MAPSB2B-41
static uint32_t constexpr kDefaultPrecision = 9;
private:
bool m_extendedOutput;
int32_t m_precision = kDefaultPrecision;
};
} // namespace regions
} // namespace generator

View file

@ -1,5 +1,5 @@
#include "generator/regions/to_string_policy.hpp"
#include "generator/streets/streets_builder.hpp"
#include "generator/streets/street_regions_tracing.hpp"
#include "indexer/classificator.hpp"
@ -188,7 +188,8 @@ std::unique_ptr<char, JSONFreeDeleter> StreetsBuilder::MakeStreetValue(
auto const & pinArray = std::vector<double>{pinLatLon.m_lon, pinLatLon.m_lat};
ToJSONObject(*streetObject, "pin", std::move(pinArray));
auto const value = json_dumps(streetObject.get(), JSON_COMPACT);
auto const value = json_dumps(streetObject.get(),
JSON_REAL_PRECISION(regions::JsonPolicy::kDefaultPrecision) | JSON_COMPACT);
return std::unique_ptr<char, JSONFreeDeleter>{value};
}