libosmscout 1.1.1
Loading...
Searching...
No Matches
ElevationService.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_ELEVATION_SERVICE_H
2#define OSMSCOUT_ELEVATION_SERVICE_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2019 Lukas Karas
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <osmscout/Way.h>
24#include <osmscout/TypeConfig.h>
28#include <osmscout/log/Logger.h>
30
31#include <vector>
32
33namespace osmscout {
34
36{
38 std::vector<WayRef> contours;
39};
40
42{
43 Distance distance;
44 Distance elevation;
45 GeoCoord coord;
47};
48
49template <typename DataLoader>
50class ElevationService CLASS_FINAL
51{
52private:
53 DataLoader &dataLoader;
54 MagnificationLevel loadTileMag;
55
56public:
57 explicit ElevationService(DataLoader &dataLoader,
58 MagnificationLevel loadTileMag = Magnification::magSuburb):
59 dataLoader(dataLoader), loadTileMag(loadTileMag)
60 {}
61
62 std::vector<ElevationPoint> ElevationProfile(const std::vector<GeoCoord> &way)
63 {
64 std::vector<ElevationPoint> result;
65 ElevationProfile(way, [&result](const Distance &, const std::vector<ElevationPoint> &points){
66 result.insert(result.end(), points.begin(), points.end());
67 });
68 return result;
69 }
70
71 size_t ElevationProfile(const std::vector<GeoCoord> &way,
72 const std::function<void(const Distance &distance, const std::vector<ElevationPoint> &points)>& callback,
73 const BreakerRef& breaker=nullptr)
74 {
75 if (way.empty()){
76 return 0;
77 }
78
79 Distance distance;
80 size_t pointCnt = 0;
81 GeoCoord intersection;
82
83 std::vector<ContoursData> contours;
84 GeoBox loadBox;
85
86 for (size_t i=0; i < way.size()-1; i+=1){
87 std::vector<ElevationPoint> result;
88 GeoCoord a1=way[i];
89 GeoCoord a2=way[i+1];
90 GeoBox lineBox(a1,a2);
91
92 if (i%100==0 && breaker && breaker->IsAborted()){
93 break;
94 }
95
96 if (!loadBox.Includes(a1) || !loadBox.Includes(a2)) {
97 TileId tile1=TileId::GetTile(loadTileMag, lineBox.GetMinCoord());
98 TileId tile2=TileId::GetTile(loadTileMag, lineBox.GetMaxCoord());
99 TileIdBox tileBox(tile1, tile2);
100 loadBox=tileBox.GetBoundingBox(Magnification(loadTileMag));
101 if (tileBox.GetCount() > 100){
102 osmscout::log.Warn() << "Too huge area for loading " << loadBox.GetDisplayText();
103 continue;
104 }
105 //osmscout::log.Debug() << "box " << lineBox.GetDisplayText() << " -> " << loadBox.GetDisplayText() << " size: " << tileBox.GetCount();
106 assert(loadBox.Includes(a1));
107 assert(loadBox.Includes(a2));
108 contours = dataLoader.LoadContours(loadBox);
109 }
110
111 for (const ContoursData &contoursData:contours) {
112
113 for (const auto &contour:contoursData.contours) {
114 if (!lineBox.Intersects(contour->GetBoundingBox())){
115 continue;
116 }
117
118 EleFeatureValue *eleValue=contoursData.reader.GetValue(contour->GetFeatureValueBuffer());
119 if (eleValue==nullptr) {
120 continue;
121 }
122
123 for (size_t bi=0; bi < contour->nodes.size()-1; bi+=1){
124 GeoCoord b1=contour->nodes[bi].GetCoord();
125 GeoCoord b2=contour->nodes[bi+1].GetCoord();
126 if (GetLineIntersection(a1,a2,
127 b1,b2,
128 intersection)) {
129
130 result.push_back(ElevationPoint{
131 distance + GetEllipsoidalDistance(a1, intersection),
132 Meters(eleValue->GetEle()),
133 intersection,
134 contour
135 });
136
137 //log.Debug() << " " << eleValue->GetEle() << " m, distance " << distance << " + " << GetEllipsoidalDistance(a1, intersection) << " : " << intersection.GetDisplayText();
138 }
139 }
140
141 }
142 }
143
144 //log.Debug() << "At distance " << distance << " adding " << GetEllipsoidalDistance(a1,a2) << " (" << a1.GetDisplayText() << " -> " << a2.GetDisplayText() << ")";
145 distance+=GetEllipsoidalDistance(a1,a2);
146 if (!result.empty()) {
147 std::sort(result.begin(), result.end(), [](const ElevationPoint &a, const ElevationPoint &b) {
148 if (a.distance != b.distance) {
149 return a.distance < b.distance;
150 }
151 if (a.elevation != b.elevation) {
152 return a.elevation < b.elevation;
153 }
154 return a.coord < b.coord;
155 });
156 pointCnt += result.size();
157 callback(distance, result);
158 }
159 }
160 return pointCnt;
161 }
162};
163
164}
165
166#endif //OSMSCOUT_ELEVATION_SERVICE_H
Definition Area.h:88
size_t ElevationProfile(const std::vector< GeoCoord > &way, const std::function< void(const Distance &distance, const std::vector< ElevationPoint > &points)> &callback, const BreakerRef &breaker=nullptr)
Definition ElevationService.h:71
TileIdBox(const TileId &a, const TileId &b)
std::vector< ElevationPoint > ElevationProfile(const std::vector< GeoCoord > &way)
Definition ElevationService.h:62
ElevationService(DataLoader &dataLoader, MagnificationLevel loadTileMag=Magnification::magSuburb)
Definition ElevationService.h:57
MagnificationLevel()=default
Definition EleFeature.h:31
int16_t GetEle() const
Definition EleFeature.h:49
Definition TileId.h:46
static TileId GetTile(const Magnification &magnification, const GeoCoord &coord)
OSMSCOUT_API Distance GetEllipsoidalDistance(double aLon, double aLat, double bLon, double bLat)
bool GetLineIntersection(const N &a1, const N &a2, const N &b1, const N &b2, I &intersection)
Definition Geometry.h:274
OSMSCOUT_API Log log
Definition LoggerImpl.h:95
Definition Area.h:39
Distance Meters(double m)
Definition Distance.h:358
std::shared_ptr< Way > WayRef
Definition Way.h:213
std::shared_ptr< Breaker > BreakerRef
Definition Breaker.h:64
FeatureValueReader< EleFeature, EleFeatureValue > EleFeatureValueReader
Definition EleFeature.h:108
Definition ElevationService.h:36
EleFeatureValueReader reader
Definition ElevationService.h:37
std::vector< WayRef > contours
Definition ElevationService.h:38
Definition ElevationService.h:42
WayRef contour
Definition ElevationService.h:46
GeoCoord coord
Definition ElevationService.h:45
Distance elevation
Definition ElevationService.h:44
Distance distance
Definition ElevationService.h:43