Skip to content

Commit cae90c3

Browse files
feat: Increase tile index range to int32_t, use uint32_t for row count, and fix logging format specifiers.
- Changed tile index variables to int32_t to support high zoom levels. - Changed numberOfRows to uint32_t to better reflect its non-negative nature. - Corrected logging format specifiers to match variable types.
1 parent 2439443 commit cae90c3

File tree

3 files changed

+41
-40
lines changed

3 files changed

+41
-40
lines changed

src/CachedTile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
struct CachedTile
77
{
88
uint16_t *buffer;
9-
int z, x, y;
9+
uint32_t x, y;
10+
uint8_t z;
1011
bool valid;
1112

1213
CachedTile() : buffer(nullptr), valid(false) {}

src/OpenStreetMap-esp32.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ void OpenStreetMap::setResolution(uint16_t w, uint16_t h)
1111
mapHeight = h;
1212
}
1313

14-
double OpenStreetMap::lon2tile(double lon, int zoom)
14+
double OpenStreetMap::lon2tile(double lon, uint8_t zoom)
1515
{
1616
return (lon + 180.0) / 360.0 * (1 << zoom);
1717
}
1818

19-
double OpenStreetMap::lat2tile(double lat, int zoom)
19+
double OpenStreetMap::lat2tile(double lat, uint8_t zoom)
2020
{
2121
double latRad = lat * M_PI / 180.0;
2222
return (1.0 - log(tan(latRad) + 1.0 / cos(latRad)) / M_PI) / 2.0 * (1 << zoom);
@@ -36,23 +36,23 @@ void OpenStreetMap::PNGDraw(PNGDRAW *pDraw)
3636
currentInstance->png.getLineAsRGB565(pDraw, destRow, PNG_RGB565_BIG_ENDIAN, 0x0000);
3737
}
3838

39-
void OpenStreetMap::computeRequiredTiles(double longitude, double latitude, int zoom, std::vector<std::pair<int, int>> &requiredTiles)
39+
void OpenStreetMap::computeRequiredTiles(double longitude, double latitude, uint8_t zoom, std::vector<std::pair<int32_t, int32_t>> &requiredTiles)
4040
{
4141
// Compute exact tile coordinates
4242
const double exactTileX = lon2tile(longitude, zoom);
4343
const double exactTileY = lat2tile(latitude, zoom);
4444

4545
// Determine the integer tile indices
46-
const int targetTileX = static_cast<int>(exactTileX);
47-
const int targetTileY = static_cast<int>(exactTileY);
46+
const int32_t targetTileX = static_cast<int32_t>(exactTileX);
47+
const int32_t targetTileY = static_cast<int32_t>(exactTileY);
4848

4949
// Compute the offset inside the tile for the given coordinates
50-
const int targetOffsetX = (exactTileX - targetTileX) * OSM_TILESIZE;
51-
const int targetOffsetY = (exactTileY - targetTileY) * OSM_TILESIZE;
50+
const int16_t targetOffsetX = (exactTileX - targetTileX) * OSM_TILESIZE;
51+
const int16_t targetOffsetY = (exactTileY - targetTileY) * OSM_TILESIZE;
5252

5353
// Compute the offset for tiles covering the map area to keep the location centered
54-
const int tilesOffsetX = mapWidth / 2 - targetOffsetX;
55-
const int tilesOffsetY = mapHeight / 2 - targetOffsetY;
54+
const int16_t tilesOffsetX = mapWidth / 2 - targetOffsetX;
55+
const int16_t tilesOffsetY = mapHeight / 2 - targetOffsetY;
5656

5757
// Compute number of colums required
5858
const float colsLeft = 1.0 * tilesOffsetX / OSM_TILESIZE;
@@ -64,28 +64,28 @@ void OpenStreetMap::computeRequiredTiles(double longitude, double latitude, int
6464
// Compute number of rows required
6565
const float rowsTop = 1.0 * tilesOffsetY / OSM_TILESIZE;
6666
const float rowsBottom = float(mapHeight - (tilesOffsetY + OSM_TILESIZE)) / OSM_TILESIZE;
67-
const int numberOfRows = ceil(rowsTop) + 1 + ceil(rowsBottom);
67+
const uint32_t numberOfRows = ceil(rowsTop) + 1 + ceil(rowsBottom);
6868

6969
startOffsetY = tilesOffsetY - (ceil(rowsTop) * OSM_TILESIZE);
7070

71-
log_v(" Need %i * %i tiles. First tile offset is %i,%i",
71+
log_v(" Need %i * %i tiles. First tile offset is %d,%d",
7272
numberOfColums, numberOfRows, startOffsetX, startOffsetY);
7373

7474
startTileIndexX = targetTileX - ceil(colsLeft);
7575
startTileIndexY = targetTileY - ceil(rowsTop);
7676

77-
log_v("top left tile indices: %i, %i", startTileIndexX, startTileIndexY);
77+
log_v("top left tile indices: %d, %d", startTileIndexX, startTileIndexY);
7878

7979
requiredTiles.clear();
8080

81-
const int worldTileWidth = 1 << zoom;
81+
const int32_t worldTileWidth = 1 << zoom;
8282

83-
for (int y = 0; y < numberOfRows; ++y)
83+
for (int32_t y = 0; y < numberOfRows; ++y)
8484
{
85-
for (int x = 0; x < numberOfColums; ++x)
85+
for (int32_t x = 0; x < numberOfColums; ++x)
8686
{
87-
int tileX = startTileIndexX + x;
88-
int tileY = startTileIndexY + y;
87+
int32_t tileX = startTileIndexX + x;
88+
int32_t tileY = startTileIndexY + y;
8989

9090
// Apply modulo wrapping for tileX
9191
tileX = tileX % worldTileWidth;
@@ -97,7 +97,7 @@ void OpenStreetMap::computeRequiredTiles(double longitude, double latitude, int
9797
}
9898
}
9999

100-
bool OpenStreetMap::isTileCached(int x, int y, int z)
100+
bool OpenStreetMap::isTileCached(uint32_t x, uint32_t y, uint8_t z)
101101
{
102102
for (const auto &tile : tilesCache)
103103
if (tile.valid && tile.x == x && tile.y == y && tile.z == z)
@@ -106,7 +106,7 @@ bool OpenStreetMap::isTileCached(int x, int y, int z)
106106
return false;
107107
}
108108

109-
CachedTile *OpenStreetMap::findUnusedTile(const std::vector<std::pair<int, int>> &requiredTiles, int zoom)
109+
CachedTile *OpenStreetMap::findUnusedTile(const std::vector<std::pair<int32_t, int32_t>> &requiredTiles, uint8_t zoom)
110110
{
111111
for (auto &tile : tilesCache)
112112
{
@@ -161,9 +161,9 @@ bool OpenStreetMap::resizeTilesCache(uint8_t cacheSize)
161161
return true;
162162
}
163163

164-
bool OpenStreetMap::fetchMap(LGFX_Sprite &mapSprite, double longitude, double latitude, int zoom)
164+
bool OpenStreetMap::fetchMap(LGFX_Sprite &mapSprite, double longitude, double latitude, uint8_t zoom)
165165
{
166-
if (zoom < 0 || zoom > 20)
166+
if (!zoom || zoom > 18)
167167
{
168168
log_e("Invalid zoom level: %d", zoom);
169169
return false;
@@ -181,11 +181,11 @@ bool OpenStreetMap::fetchMap(LGFX_Sprite &mapSprite, double longitude, double la
181181
resizeTilesCache(10);
182182
}
183183

184-
// normalize the input
184+
// normalize the coordinates
185185
longitude = fmod(longitude + 180.0, 360.0) - 180.0;
186186
latitude = std::clamp(latitude, -90.0, 90.0);
187187

188-
std::vector<std::pair<int, int>> requiredTiles;
188+
std::vector<std::pair<int32_t, int32_t>> requiredTiles;
189189
computeRequiredTiles(longitude, latitude, zoom, requiredTiles);
190190

191191
if (tilesCache.capacity() < requiredTiles.size())
@@ -250,9 +250,9 @@ bool OpenStreetMap::fetchMap(LGFX_Sprite &mapSprite, double longitude, double la
250250
return true;
251251
}
252252

253-
bool OpenStreetMap::downloadAndDecodeTile(CachedTile &tile, int x, int y, int zoom, String &result)
253+
bool OpenStreetMap::downloadAndDecodeTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result)
254254
{
255-
String url = "https://tile.openstreetmap.org/" + String(zoom) + "/" + String(x) + "/" + String(y) + ".png";
255+
const String url = "https://tile.openstreetmap.org/" + String(zoom) + "/" + String(x) + "/" + String(y) + ".png";
256256

257257
HTTPClient http;
258258
http.setUserAgent("OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)");
@@ -262,7 +262,7 @@ bool OpenStreetMap::downloadAndDecodeTile(CachedTile &tile, int x, int y, int zo
262262
return false;
263263
}
264264

265-
int httpCode = http.GET();
265+
const int httpCode = http.GET();
266266
if (httpCode != HTTP_CODE_OK)
267267
{
268268
http.end();

src/OpenStreetMap-esp32.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "CachedTile.h"
1313
#include "MemoryBuffer.h"
1414

15-
constexpr int OSM_TILESIZE = 256;
15+
constexpr int16_t OSM_TILESIZE = 256;
1616

1717
class OpenStreetMap
1818
{
@@ -23,13 +23,13 @@ class OpenStreetMap
2323
uint16_t *currentTileBuffer = nullptr;
2424
PNG png;
2525

26-
int startOffsetX = 0;
27-
int startOffsetY = 0;
26+
int16_t startOffsetX = 0;
27+
int16_t startOffsetY = 0;
2828

29-
int startTileIndexX = 0;
30-
int startTileIndexY = 0;
29+
int32_t startTileIndexX = 0;
30+
int32_t startTileIndexY = 0;
3131

32-
int numberOfColums = 0;
32+
uint16_t numberOfColums = 0;
3333

3434
public:
3535
OpenStreetMap() = default;
@@ -43,18 +43,18 @@ class OpenStreetMap
4343
void setResolution(uint16_t w, uint16_t h);
4444
bool resizeTilesCache(uint8_t cacheSize);
4545
void freeTilesCache();
46-
bool fetchMap(LGFX_Sprite &sprite, double longitude, double latitude, int zoom);
46+
bool fetchMap(LGFX_Sprite &sprite, double longitude, double latitude, uint8_t zoom);
4747
bool saveMap(const char *filename, LGFX_Sprite &display, String &result, uint8_t sdPin = SS);
4848

4949
private:
5050
static OpenStreetMap *currentInstance;
5151
static void PNGDraw(PNGDRAW *pDraw);
52-
double lon2tile(double lon, int zoom);
53-
double lat2tile(double lat, int zoom);
54-
void computeRequiredTiles(double longitude, double latitude, int zoom, std::vector<std::pair<int, int>> &requiredTiles);
55-
CachedTile *findUnusedTile(const std::vector<std::pair<int, int>> &requiredTiles, int zoom);
56-
bool isTileCached(int x, int y, int z);
57-
bool downloadAndDecodeTile(CachedTile &tile, int x, int y, int zoom, String &result);
52+
double lon2tile(double lon, uint8_t zoom);
53+
double lat2tile(double lat, uint8_t zoom);
54+
void computeRequiredTiles(double longitude, double latitude, uint8_t zoom, std::vector<std::pair<int32_t, int32_t>> &requiredTiles);
55+
CachedTile *findUnusedTile(const std::vector<std::pair<int32_t, int32_t>> &requiredTiles, uint8_t zoom);
56+
bool isTileCached(uint32_t x, uint32_t y, uint8_t z);
57+
bool downloadAndDecodeTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result);
5858
};
5959

6060
#endif

0 commit comments

Comments
 (0)