forked from mapsme/omim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedits_migration.cpp
122 lines (103 loc) · 3.63 KB
/
edits_migration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "editor/edits_migration.hpp"
#include "editor/feature_matcher.hpp"
#include "indexer/feature.hpp"
#include "indexer/feature_source.hpp"
#include "geometry/algorithm.hpp"
#include "geometry/mercator.hpp"
#include "base/logging.hpp"
#include <boost/optional.hpp>
namespace editor
{
FeatureID MigrateNodeFeatureIndex(osm::Editor::ForEachFeaturesNearByFn & forEach,
XMLFeature const & xml,
FeatureStatus const featureStatus,
GenerateIDFn const & generateID)
{
if (featureStatus == FeatureStatus::Created)
return generateID();
FeatureID fid;
auto count = 0;
forEach(
[&fid, &count](FeatureType const & ft) {
if (ft.GetGeomType() != feature::GeomType::Point)
return;
// TODO(mgsergio): Check that ft and xml correspond to the same feature.
fid = ft.GetID();
++count;
},
MercatorBounds::FromLatLon(xml.GetCenter()));
if (count == 0)
MYTHROW(MigrationError, ("No pointed features returned."));
if (count > 1)
{
LOG(LWARNING,
(count, "features returned for point", MercatorBounds::FromLatLon(xml.GetCenter())));
}
return fid;
}
FeatureID MigrateWayOrRelatonFeatureIndex(
osm::Editor::ForEachFeaturesNearByFn & forEach, XMLFeature const & xml,
FeatureStatus const /* Unused for now (we don't create/delete area features)*/,
GenerateIDFn const & /*Unused for the same reason*/)
{
boost::optional<FeatureID> fid;
auto bestScore = 0.6; // initial score is used as a threshold.
auto geometry = xml.GetGeometry();
auto count = 0;
if (geometry.empty())
MYTHROW(MigrationError, ("Feature has invalid geometry", xml));
// This can be any point on a feature.
auto const someFeaturePoint = geometry[0];
forEach(
[&fid, &geometry, &count, &bestScore](FeatureType & ft) {
if (ft.GetGeomType() != feature::GeomType::Area)
return;
++count;
auto ftGeometry = ft.GetTrianglesAsPoints(FeatureType::BEST_GEOMETRY);
double score = 0.0;
try
{
score = matcher::ScoreTriangulatedGeometries(geometry, ftGeometry);
}
catch (matcher::NotAPolygonException & ex)
{
// Support migration for old application versions.
// TODO(a): To remove it when version 8.0.x will no longer be supported.
base::SortUnique(geometry);
base::SortUnique(ftGeometry);
score = matcher::ScoreTriangulatedGeometriesByPoints(geometry, ftGeometry);
}
if (score > bestScore)
{
bestScore = score;
fid = ft.GetID();
}
},
someFeaturePoint);
if (count == 0)
MYTHROW(MigrationError, ("No ways returned for point", someFeaturePoint));
if (!fid)
{
MYTHROW(MigrationError,
("None of returned ways suffice. Possibly, the feature has been deleted."));
}
return fid.get();
}
FeatureID MigrateFeatureIndex(osm::Editor::ForEachFeaturesNearByFn & forEach,
XMLFeature const & xml,
FeatureStatus const featureStatus,
GenerateIDFn const & generateID)
{
switch (xml.GetType())
{
case XMLFeature::Type::Unknown:
MYTHROW(MigrationError, ("Migration for XMLFeature::Type::Unknown is not possible"));
case XMLFeature::Type::Node:
return MigrateNodeFeatureIndex(forEach, xml, featureStatus, generateID);
case XMLFeature::Type::Way:
case XMLFeature::Type::Relation:
return MigrateWayOrRelatonFeatureIndex(forEach, xml, featureStatus, generateID);
}
UNREACHABLE();
}
} // namespace editor