libosmscout 1.1.1
Loading...
Searching...
No Matches
WaterIndexProcessor.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_IMPORT_WATERINDEXPROCESSOR_H
2#define OSMSCOUT_IMPORT_WATERINDEXPROCESSOR_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2017 Tim Teulings
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 <fstream>
24#include <list>
25#include <map>
26#include <set>
27#include <memory>
28#include <vector>
29
30#include <osmscout/GeoCoord.h>
31#include <osmscout/Pixel.h>
32
33#include <osmscout/GroundTile.h>
34
36
40
42
44
46
47namespace osmscout {
48
57 template<typename InputIterator>
58 void WriteGpx(InputIterator begin, InputIterator end, const std::string &name)
59 {
60 std::ofstream gpxFile;
61
62 gpxFile.open(name.c_str());
63 gpxFile.imbue(std::locale("C"));
64 gpxFile.precision(100);
65
66 gpxFile << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
67 gpxFile << "<gpx creator=\"osmscout\" version=\"1.1\" xmlns=\"http://www.topografix.com/GPX/1/1\"";
68 gpxFile << " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
69 gpxFile << " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n";
70
71 gpxFile << " <trk><trkseg>\n";
72 for (InputIterator it=begin; it!=end;it++) {
73 gpxFile << "<trkpt lat=\"" << it->GetLat() << "\" lon=\"" << it->GetLon() << "\"></trkpt>\n";
74 }
75 gpxFile << " </trkseg></trk>\n</gpx>";
76
77 gpxFile.close();
78 }
79
80 extern void WriteGpx(const std::vector<Point> &path, const std::string& name);
81
111 class OSMSCOUT_IMPORT_API WaterIndexProcessor CLASS_FINAL
112 {
113 public:
118 enum class CoastState : uint8_t
119 {
120 undefined = 0,
121 land = 1,
122 water = 2,
124 };
125
144
145 using CoastRef = std::shared_ptr<Coast>;
146
150 enum State : uint8_t
151 {
152 unknown = 0,
153 land = 1,
154 water = 2,
155 coast = 3
156 };
157
162 {
163 private:
164 double cellWidth;
165 double cellHeight;
166 uint32_t cellXStart;
167 uint32_t cellXEnd;
168 uint32_t cellYStart;
169 uint32_t cellYEnd;
170 uint32_t cellXCount;
171 uint32_t cellYCount;
172 std::vector<uint8_t> area;
173
174 public:
175 void SetBox(const GeoBox& boundingBox,
176 double cellWidth,
177 double cellHeight);
178
179 inline double GetCellWidth() const
180 {
181 return cellWidth;
182 }
183
184 inline double GetCellHeight() const
185 {
186 return cellHeight;
187 }
188
189 inline uint32_t GetXStart() const
190 {
191 return cellXStart;
192 }
193
194 inline uint32_t GetYStart() const
195 {
196 return cellYStart;
197 }
198
199 inline uint32_t GetXEnd() const
200 {
201 return cellXEnd;
202 }
203
204 inline uint32_t GetYEnd() const
205 {
206 return cellYEnd;
207 }
208
209 inline uint32_t GetXCount() const
210 {
211 return cellXCount;
212 }
213
214 inline uint32_t GetYCount() const
215 {
216 return cellYCount;
217 }
218
219 inline bool IsInAbsolute(uint32_t x, uint32_t y) const
220 {
221 return x>=cellXStart &&
222 x<=cellXEnd &&
223 y>=cellYStart &&
224 y<=cellYEnd;
225 }
226
227 State GetState(uint32_t x, uint32_t y) const;
228 inline State GetStateAbsolute(uint32_t x, uint32_t y) const
229 {
230 return GetState(x-cellXStart,
231 y-cellYStart);
232 }
233
234 void SetState(uint32_t x, uint32_t y, State state);
235 inline void SetStateAbsolute(uint32_t x, uint32_t y, State state)
236 {
237 SetState(x-cellXStart,
238 y-cellYStart,
239 state);
240 }
241 };
242
247 {
248 // Transient
249 uint32_t level;
251
252 // Persistent
257
259
260 void SetBox(const GeoBox& boundingBox,
261 double cellWidth,
262 double cellHeight);
263 };
264
268 enum Direction : int8_t
269 {
270 out = -1,
271 touch = 0,
272 in = 1
273 };
274
288
289 using IntersectionRef = std::shared_ptr<Intersection>;
290
296 {
298 bool isArea;
302 std::vector<GeoCoord> points;
303 std::map<Pixel,std::list<IntersectionRef>> cellIntersections;
306 };
307
308 using CoastlineDataRef = std::shared_ptr<CoastlineData>;
309
314 {
315 std::vector<CoastlineDataRef> coastlines;
316 std::map<Pixel,std::list<size_t>> cellCoastlines;
317 std::map<Pixel,std::list<size_t>> cellCoveredCoastlines;
318 };
319
320 private:
321
325 struct IntersectionByPathComparator
326 {
327 inline bool operator()(const IntersectionRef& a, const IntersectionRef& b) const
328 {
329 if (a->prevWayPointIndex==b->prevWayPointIndex) {
330 return a->distanceSquare<b->distanceSquare;
331 }
332
333 return a->prevWayPointIndex<b->prevWayPointIndex;
334 }
335 };
336
340 struct IntersectionCWComparator
341 {
342 inline bool operator()(const IntersectionRef& a, const IntersectionRef& b) const
343 {
344 if (a->borderIndex==b->borderIndex) {
345 switch (a->borderIndex) {
346 case 0:
347 if (a->point.GetLon()==b->point.GetLon()){
348 return a->prevPoint.GetLon()<b->prevPoint.GetLon();
349 }
350 return a->point.GetLon()<b->point.GetLon();
351
352 case 1:
353 if (a->point.GetLat()==b->point.GetLat()){
354 return a->prevPoint.GetLat()>b->prevPoint.GetLat();
355 }
356 return a->point.GetLat()>b->point.GetLat();
357
358 case 2:
359 if (a->point.GetLon()==b->point.GetLon()){
360 return a->prevPoint.GetLon()>b->prevPoint.GetLon();
361 }
362 return a->point.GetLon()>b->point.GetLon();
363
364 default: /* 3 */
365 if (a->point.GetLat()==b->point.GetLat()){
366 return a->prevPoint.GetLat()<b->prevPoint.GetLat();
367 }
368 return a->point.GetLat()<b->point.GetLat();
369 }
370 }
371 else {
372 return a->borderIndex<b->borderIndex;
373 }
374 }
375 };
376
380 struct CellBoundaries
381 {
382 double lonMin;
383 double lonMax;
384 double latMin;
385 double latMax;
386
387 using BorderCoords = std::array<GroundTile::Coord, 4>;
388 using BorderPoints = std::array<GeoCoord, 4>;
389 BorderCoords borderCoords;
390 BorderPoints borderPoints;
391
392 inline CellBoundaries(const StateMap &stateMap, const Pixel &cell)
393 {
394 lonMin=(stateMap.GetXStart()+cell.x)*stateMap.GetCellWidth()-180.0;
395 lonMax=(stateMap.GetXStart()+cell.x+1)*stateMap.GetCellWidth()-180.0;
396 latMin=(stateMap.GetYStart()+cell.y)*stateMap.GetCellHeight()-90.0;
397 latMax=(stateMap.GetYStart()+cell.y+1)*stateMap.GetCellHeight()-90.0;
398
399 borderPoints[0]=GeoCoord(latMax,lonMin); // top left
400 borderPoints[1]=GeoCoord(latMax,lonMax); // top right
401 borderPoints[2]=GeoCoord(latMin,lonMax); // bottom right
402 borderPoints[3]=GeoCoord(latMin,lonMin); // bottom left
403
404 borderCoords[0].Set(0,GroundTile::Coord::CELL_MAX,false); // top left
405 borderCoords[1].Set(GroundTile::Coord::CELL_MAX,GroundTile::Coord::CELL_MAX,false); // top right
406 borderCoords[2].Set(GroundTile::Coord::CELL_MAX,0,false); // bottom right
407 borderCoords[3].Set(0,0,false); // bottom left
408 }
409 };
410
411 private:
412 std::string StateToString(State state) const;
413 std::string TypeToString(GroundTile::Type type) const;
414
430 GroundTile::Coord Transform(const GeoCoord& point,
431 const StateMap& stateMap,
432 double cellMinLat,
433 double cellMinLon,
434 bool coast);
435
449 void GetCells(const StateMap& stateMap,
450 const GeoCoord& a,
451 const GeoCoord& b,
452 std::set<Pixel>& cellIntersections) const;
453
463 void GetCellIntersections(const StateMap& stateMap,
464 const std::vector<GeoCoord>& points,
465 size_t coastline,
466 std::map<Pixel,std::list<IntersectionRef>>& cellIntersections);
467
471 bool IsCellInBoundingPolygon(const CellBoundaries& cellBoundary,
472 const std::list<CoastRef>& boundingPolygons);
473
477 bool ContainsCoord(const std::list<GroundTile> &tiles,
478 const GroundTile::Coord &coord,
479 GroundTile::Type type);
480
484 bool ContainsCoord(const std::list<GroundTile> &tiles,
485 const GroundTile::Coord &coord);
486
490 bool ContainsWater(const Pixel &coord,
491 const StateMap &stateMap,
492 const std::map<Pixel,std::list<GroundTile>>& cellGroundTileMap,
493 const GroundTile::Coord &testCoord1,
494 const GroundTile::Coord &testCoord2);
495
501 void WalkBorderCW(GroundTile& groundTile,
502 const StateMap& stateMap,
503 double cellMinLat,
504 double cellMinLon,
505 const IntersectionRef& incoming,
506 const IntersectionRef& outgoing,
507 const CellBoundaries::BorderCoords &borderCoords);
508
512 IntersectionRef GetNextCW(const std::list<IntersectionRef>& intersectionsCW,
513 const IntersectionRef& current) const;
514
519 void WalkPathBack(GroundTile& groundTile,
520 const StateMap& stateMap,
521 double cellMinLat,
522 double cellMinLon,
523 const IntersectionRef& pathStart,
524 const IntersectionRef& pathEnd,
525 const std::vector<GeoCoord>& points,
526 bool isArea);
527
532 void WalkPathForward(GroundTile& groundTile,
533 const StateMap& stateMap,
534 double cellMinLat,
535 double cellMinLon,
536 const IntersectionRef& pathStart,
537 const IntersectionRef& pathEnd,
538 const std::vector<GeoCoord>& points,
539 bool isArea);
540
547 IntersectionRef FindSiblingIntersection(const IntersectionRef &intersection,
548 const std::list<IntersectionRef> &intersectionsCW,
549 bool isArea);
550
561 bool WalkFromTripoint(GroundTile &groundTile,
562 const StateMap& stateMap,
563 const CellBoundaries &cellBoundaries,
564 IntersectionRef &pathStart,
565 IntersectionRef &pathEnd,
566 Data &data,
567 const std::list<IntersectionRef> &intersectionsCW,
568 const std::vector<size_t> &containingPaths);
569
574 void WalkPath(GroundTile &groundTile,
575 const StateMap& stateMap,
576 const CellBoundaries &cellBoundaries,
577 const IntersectionRef pathStart,
578 const IntersectionRef pathEnd,
579 CoastlineDataRef coastline);
580
586 bool WalkBoundaryCW(GroundTile &groundTile,
587 const StateMap &stateMap,
588 const IntersectionRef outIntersection,
589 const std::list<IntersectionRef> &intersectionsCW,
590 std::set<IntersectionRef> &visitedIntersections,
591 const CellBoundaries &cellBoundaries,
592 Data& data,
593 const std::vector<size_t> &containingPaths);
594
606 void SynthesizeCoastlines2(Progress& progress,
607 const std::list<CoastRef>& boundingPolygons,
608 const std::list<CoastRef>& coastlines,
609 std::list<CoastRef>& synthesized);
610
614 void HandleCoastlineCell(Progress& progress,
615 const Pixel &cell,
616 const std::list<size_t>& intersectCoastlines,
617 const StateMap& stateMap,
618 std::map<Pixel,std::list<GroundTile> >& cellGroundTileMap,
619 Data& data);
620
621 void TransformCoastlines(Progress& progress,
622 TransPolygon::OptimizeMethod optimizationMethod,
623 double tolerance,
624 double minObjectDimension,
625 const Projection& projection,
626 const std::list<CoastRef>& coastlines,
627 std::vector<CoastlineDataRef> &transformedCoastlines);
628
629 void FilterIntersectCoastlines(Progress& progress,
630 std::vector<CoastlineDataRef> &transformedCoastlines);
631
632 void FilterEncapsulatedCoastlines(Progress& progress,
633 std::vector<CoastlineDataRef> &transformedCoastlines);
634
635 void ComputeCoveredTiles(Progress& progress,
636 const StateMap& stateMap,
637 Data& data,
638 std::vector<CoastlineDataRef> &transformedCoastlines);
639
643 static bool CoastlineGeoSizeSorter(const CoastlineDataRef &a, const CoastlineDataRef &b);
644
645public:
649 void MergeCoastlines(Progress& progress,
650 std::list<WaterIndexProcessor::CoastRef>& coastlines);
651
662 void GetCells(const StateMap& stateMap,
663 const std::vector<GeoCoord>& points,
664 std::set<Pixel>& cellIntersections) const;
665
676 void GetCells(const StateMap& stateMap,
677 const std::vector<Point>& points,
678 std::set<Pixel>& cellIntersections) const;
679
689 std::list<CoastRef>& coastlines,
690 const std::list<CoastRef>& boundingPolygons);
691
696 TransPolygon::OptimizeMethod optimizationMethod,
697 double tolerance,
698 double minObjectDimension,
699 const Projection& projection,
700 const StateMap& stateMap,
701 const std::list<CoastRef>& coastlines,
702 Data& data);
703
708 StateMap& stateMap,
709 const Data& data);
710
720 const StateMap& stateMap,
721 Data& data,
722 std::map<Pixel,std::list<GroundTile> >& cellGroundTileMap);
723
728 const StateMap& stateMap,
729 std::map<Pixel,std::list<GroundTile> >& cellGroundTileMap,
730 Data& data);
731
740 StateMap& stateMap,
741 const std::map<Pixel,std::list<GroundTile> >& cellGroundTileMap);
742
747 StateMap& stateMap,
748 std::map<Pixel,std::list<GroundTile> >& cellGroundTileMap,
749 const std::list<CoastRef>& boundingPolygons);
750
757 void FillWater(Progress& progress,
758 Level& level,
759 size_t tileCount,
760 const std::list<CoastRef>& boundingPolygons);
761
768 void FillLand(Progress& progress,
769 StateMap& stateMap);
770
775 const std::map<Pixel,std::list<GroundTile> >& cellGroundTileMap) const;
776
781 std::vector<Level>& levels);
782
786 void WriteTiles(Progress& progress,
787 const std::map<Pixel,std::list<GroundTile>>& cellGroundTileMap,
788 Level& level,
789 FileWriter& writer);
790 };
791}
792
793#endif
#define OSMSCOUT_IMPORT_API
Definition ImportImportExport.h:45
Definition WaterIndexProcessor.h:162
uint32_t GetYStart() const
Definition WaterIndexProcessor.h:194
uint32_t GetXCount() const
Definition WaterIndexProcessor.h:209
uint32_t GetXStart() const
Definition WaterIndexProcessor.h:189
bool IsInAbsolute(uint32_t x, uint32_t y) const
Definition WaterIndexProcessor.h:219
uint32_t GetYCount() const
Definition WaterIndexProcessor.h:214
uint32_t GetYEnd() const
Definition WaterIndexProcessor.h:204
double GetCellHeight() const
Definition WaterIndexProcessor.h:184
double GetCellWidth() const
Definition WaterIndexProcessor.h:179
State GetStateAbsolute(uint32_t x, uint32_t y) const
Definition WaterIndexProcessor.h:228
uint32_t GetXEnd() const
Definition WaterIndexProcessor.h:199
void SetBox(const GeoBox &boundingBox, double cellWidth, double cellHeight)
State GetState(uint32_t x, uint32_t y) const
void SetStateAbsolute(uint32_t x, uint32_t y, State state)
Definition WaterIndexProcessor.h:235
void SetState(uint32_t x, uint32_t y, State state)
Definition Area.h:88
void WriteTiles(Progress &progress, const std::map< Pixel, std::list< GroundTile > > &cellGroundTileMap, Level &level, FileWriter &writer)
void CalculateCoastEnvironment(Progress &progress, StateMap &stateMap, const std::map< Pixel, std::list< GroundTile > > &cellGroundTileMap)
void CalculateHasCellData(Level &level, const std::map< Pixel, std::list< GroundTile > > &cellGroundTileMap) const
void FillWaterAroundIsland(Progress &progress, StateMap &stateMap, std::map< Pixel, std::list< GroundTile > > &cellGroundTileMap, const std::list< CoastRef > &boundingPolygons)
State
Definition WaterIndexProcessor.h:151
@ coast
right side of the coast => a water tile
Definition GroundTile.h:52
@ water
left side of the coast => a land tile
Definition GroundTile.h:51
@ unknown
Definition GroundTile.h:49
@ land
We do not know yet.
Definition GroundTile.h:50
void DumpIndexHeader(FileWriter &writer, std::vector< Level > &levels)
void MarkCoastlineCells(Progress &progress, StateMap &stateMap, const Data &data)
void FillWater(Progress &progress, Level &level, size_t tileCount, const std::list< CoastRef > &boundingPolygons)
CoastState
Definition WaterIndexProcessor.h:119
void GetCells(const StateMap &stateMap, const std::vector< GeoCoord > &points, std::set< Pixel > &cellIntersections) const
void FillLand(Progress &progress, StateMap &stateMap)
void HandleAreaCoastlinesCompletelyInACell(Progress &progress, const StateMap &stateMap, Data &data, std::map< Pixel, std::list< GroundTile > > &cellGroundTileMap)
void CalculateCoastlineData(Progress &progress, TransPolygon::OptimizeMethod optimizationMethod, double tolerance, double minObjectDimension, const Projection &projection, const StateMap &stateMap, const std::list< CoastRef > &coastlines, Data &data)
uint32_t x
Definition Pixel.h:48
uint32_t y
Definition Pixel.h:49
void SynthesizeCoastlines(Progress &progress, std::list< CoastRef > &coastlines, const std::list< CoastRef > &boundingPolygons)
Direction
Definition WaterIndexProcessor.h:269
@ touch
Definition WaterIndexProcessor.h:271
@ out
Definition WaterIndexProcessor.h:270
@ in
Definition WaterIndexProcessor.h:272
double cellWidth
Width of cell.
Definition GroundTile.h:97
std::shared_ptr< Intersection > IntersectionRef
Definition WaterIndexProcessor.h:289
bool operator()(const ObjectFileRef &a, const ObjectFileRef &b) const
Definition ObjectRef.h:249
double cellHeight
Height of cell.
Definition GroundTile.h:98
std::shared_ptr< Coast > CoastRef
Definition WaterIndexProcessor.h:145
void MergeCoastlines(Progress &progress, std::list< WaterIndexProcessor::CoastRef > &coastlines)
std::shared_ptr< CoastlineData > CoastlineDataRef
Definition WaterIndexProcessor.h:308
void HandleCoastlinesPartiallyInACell(Progress &progress, const StateMap &stateMap, std::map< Pixel, std::list< GroundTile > > &cellGroundTileMap, Data &data)
RoutableObjectsRef data
Definition DataAgent.h:63
void GetCells(const StateMap &stateMap, const std::vector< Point > &points, std::set< Pixel > &cellIntersections) const
Definition Projection.h:46
uint64_t Id
Definition OSMScoutTypes.h:40
int64_t OSMId
Definition OSMScoutTypes.h:33
uint64_t FileOffset
Definition OSMScoutTypes.h:46
Definition Area.h:39
void WriteGpx(InputIterator begin, InputIterator end, const std::string &name)
Definition WaterIndexProcessor.h:58
codepoint(*)(const character *, int context) Transform
functor implements desired transformation of the character It has 2 arguments:
Definition utf8helper.h:49
Definition WaterIndexProcessor.h:135
Id frontNodeId
Id of the first coast node (coords.front().GetId()).
Definition WaterIndexProcessor.h:138
Id backNodeId
Id of the last coast node (coords.back().GetId()).
Definition WaterIndexProcessor.h:139
OSMId id
Definition WaterIndexProcessor.h:136
bool isArea
Definition WaterIndexProcessor.h:137
std::vector< Point > coast
Definition WaterIndexProcessor.h:140
CoastState right
Definition WaterIndexProcessor.h:142
CoastState left
Definition WaterIndexProcessor.h:141
Definition WaterIndexProcessor.h:296
Id id
Definition WaterIndexProcessor.h:297
std::vector< GeoCoord > points
The cell that completely contains the coastline.
Definition WaterIndexProcessor.h:302
std::map< Pixel, std::list< IntersectionRef > > cellIntersections
The points of the coastline.
Definition WaterIndexProcessor.h:303
CoastState right
Definition WaterIndexProcessor.h:305
Pixel cell
GeoBox from points (already transformed).
Definition WaterIndexProcessor.h:301
bool isCompletelyInCell
true,if the boundary forms an area
Definition WaterIndexProcessor.h:299
GeoBox boundingBox
true, if the complete coastline is within one cell
Definition WaterIndexProcessor.h:300
CoastState left
All intersections for each cell.
Definition WaterIndexProcessor.h:304
bool isArea
The id of the coastline.
Definition WaterIndexProcessor.h:298
Definition WaterIndexProcessor.h:314
std::map< Pixel, std::list< size_t > > cellCoastlines
data for each coastline
Definition WaterIndexProcessor.h:316
std::vector< CoastlineDataRef > coastlines
Definition WaterIndexProcessor.h:315
std::map< Pixel, std::list< size_t > > cellCoveredCoastlines
Contains for each cell the list of intersecting coastlines.
Definition WaterIndexProcessor.h:317
Definition WaterIndexProcessor.h:279
double distanceSquare
The intersection point.
Definition WaterIndexProcessor.h:284
GeoCoord point
just helper for sorting
Definition WaterIndexProcessor.h:283
GeoCoord prevPoint
The index of the path point before the intersection.
Definition WaterIndexProcessor.h:282
uint8_t borderIndex
1 in, 0 touch, -1 out
Definition WaterIndexProcessor.h:286
size_t prevWayPointIndex
Running number of the intersecting coastline.
Definition WaterIndexProcessor.h:281
size_t coastline
Definition WaterIndexProcessor.h:280
Direction direction
The distance^2 between the path point and the intersectionPoint.
Definition WaterIndexProcessor.h:285
Definition WaterIndexProcessor.h:247
bool hasCellData
If true, we have cell data.
Definition WaterIndexProcessor.h:253
uint8_t dataOffsetBytes
Number of bytes per entry in bitmap.
Definition WaterIndexProcessor.h:254
FileOffset indexEntryOffset
File offset of this entry on disk.
Definition WaterIndexProcessor.h:250
void SetBox(const GeoBox &boundingBox, double cellWidth, double cellHeight)
State defaultCellData
If hasCellData is false, this is the value to be returned for all cells.
Definition WaterIndexProcessor.h:255
FileOffset indexDataOffset
File offset of start cell state data on disk.
Definition WaterIndexProcessor.h:256
StateMap stateMap
Index to handle state of cells.
Definition WaterIndexProcessor.h:258
uint32_t level
The actual zoom level.
Definition WaterIndexProcessor.h:249