Welcome to mirror list, hosted at ThFree Co, Russian Federation.

srtm_parser.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b71cbf3b2bcfa029029454f8769a130396399b8 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "generator/srtm_parser.hpp"

#include "coding/endianness.hpp"
#include "coding/zip_reader.hpp"

#include "base/file_name_utils.hpp"
#include "base/logging.hpp"

#include <iomanip>
#include <sstream>

namespace generator
{
namespace
{
size_t constexpr kArcSecondsInDegree = 60 * 60;
size_t constexpr kSrtmTileSize = (kArcSecondsInDegree + 1) * (kArcSecondsInDegree + 1) * 2;

struct UnzipMemDelegate : public ZipFileReader::Delegate
{
  explicit UnzipMemDelegate(std::string & buffer) : m_buffer(buffer), m_completed(false) {}

  // ZipFileReader::Delegate overrides:
  void OnBlockUnzipped(size_t size, char const * data) override { m_buffer.append(data, size); }

  void OnStarted() override
  {
    m_buffer.clear();
    m_completed = false;
  }

  void OnCompleted() override { m_completed = true; }

  std::string & m_buffer;
  bool m_completed;
};

std::string GetSrtmContFileName(std::string const & dir, std::string const & base)
{
  return base::JoinPath(dir, base + ".SRTMGL1.hgt.zip");
}
}  // namespace

// SrtmTile ----------------------------------------------------------------------------------------
SrtmTile::SrtmTile()
{
  Invalidate();
}

SrtmTile::SrtmTile(SrtmTile && rhs) : m_data(move(rhs.m_data)), m_valid(rhs.m_valid)
{
  rhs.Invalidate();
}

void SrtmTile::Init(std::string const & dir, ms::LatLon const & coord)
{
  Invalidate();

  std::string const base = GetBase(coord);
  std::string const cont = GetSrtmContFileName(dir, base);
  std::string file = base + ".hgt";

  UnzipMemDelegate delegate(m_data);
  try
  {
    ZipFileReader::UnzipFile(cont, file, delegate);
  }
  catch (ZipFileReader::LocateZipException const & e)
  {
    // Sometimes packed file has different name. See N39E051 measure.
    file = base + ".SRTMGL1.hgt";

    ZipFileReader::UnzipFile(cont, file, delegate);
  }

  if (!delegate.m_completed)
  {
    LOG(LWARNING, ("Can't decompress SRTM file:", cont));
    Invalidate();
    return;
  }

  if (m_data.size() != kSrtmTileSize)
  {
    LOG(LWARNING, ("Bad decompressed SRTM file size:", cont, m_data.size()));
    Invalidate();
    return;
  }

  m_valid = true;
}

geometry::Altitude SrtmTile::GetHeight(ms::LatLon const & coord) const
{
  if (!IsValid())
    return geometry::kInvalidAltitude;

  double ln = coord.m_lon - static_cast<int>(coord.m_lon);
  if (ln < 0)
    ln += 1;
  double lt = coord.m_lat - static_cast<int>(coord.m_lat);
  if (lt < 0)
    lt += 1;
  lt = 1 - lt;  // from North to South

  auto const row = static_cast<size_t>(std::round(kArcSecondsInDegree * lt));
  auto const col = static_cast<size_t>(std::round(kArcSecondsInDegree * ln));

  size_t const ix = row * (kArcSecondsInDegree + 1) + col;

  CHECK_LESS(ix, Size(), (coord));
  return ReverseByteOrder(Data()[ix]);
}

// static
std::string SrtmTile::GetPath(std::string const & dir, std::string const & base)
{
  return GetSrtmContFileName(dir, base);
}

// static
ms::LatLon SrtmTile::GetCenter(ms::LatLon const & coord)
{
  return {floor(coord.m_lat) + 0.5, floor(coord.m_lon) + 0.5};
}

// static
std::string SrtmTile::GetBase(ms::LatLon const & coord)
{
  auto center = GetCenter(coord);
  std::ostringstream ss;
  if (center.m_lat < 0)
  {
    ss << "S";
    center.m_lat *= -1;
    center.m_lat += 1;
  }
  else
  {
    ss << "N";
  }
  ss << std::setw(2) << std::setfill('0') << static_cast<int>(center.m_lat);

  if (center.m_lon < 0)
  {
    ss << "W";
    center.m_lon *= -1;
    center.m_lon += 1;
  }
  else
  {
    ss << "E";
  }
  ss << std::setw(3) << static_cast<int>(center.m_lon);
  return ss.str();
}

void SrtmTile::Invalidate()
{
  m_data.clear();
  m_data.shrink_to_fit();
  m_valid = false;
}

// SrtmTileManager ---------------------------------------------------------------------------------
SrtmTileManager::SrtmTileManager(std::string const & dir) : m_dir(dir) {}
geometry::Altitude SrtmTileManager::GetHeight(ms::LatLon const & coord)
{
  auto const key = GetKey(coord);

  auto it = m_tiles.find(key);
  if (it == m_tiles.end())
  {
    SrtmTile tile;
    try
    {
      tile.Init(m_dir, coord);
    }
    catch (RootException const & e)
    {
      std::string const base = SrtmTile::GetBase(coord);
      LOG(LINFO, ("Can't init SRTM tile:", base, "reason:", e.Msg()));
    }

    // It's OK to store even invalid tiles and return invalid height
    // for them later.
    it = m_tiles.emplace(key, std::move(tile)).first;
  }

  return it->second.GetHeight(coord);
}

// static
SrtmTileManager::LatLonKey SrtmTileManager::GetKey(ms::LatLon const & coord)
{
  auto const tileCenter = SrtmTile::GetCenter(coord);
  return {static_cast<int32_t>(tileCenter.m_lat), static_cast<int32_t>(tileCenter.m_lon)};
}

SrtmTile const & SrtmTileManager::GetTile(ms::LatLon const & coord)
{
  // Touch the tile to force its loading.
  GetHeight(coord);
  auto const key = GetKey(coord);
  auto const it = m_tiles.find(key);
  CHECK(it != m_tiles.end(), (coord));
  return it->second;
}
}  // namespace generator