@@ -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 ();
0 commit comments