allow exists() to check either for an exact tile or a parent tile

This commit is contained in:
Jindřich Makovička 2012-12-03 23:17:03 +01:00
parent 4fcd45f413
commit ef5807ba3c
4 changed files with 36 additions and 39 deletions

View file

@ -222,14 +222,14 @@ public class ResourceManager {
}
public synchronized boolean tileExistOnFileSystem(String file, ITileSource map, int x, int y, int zoom){
public synchronized boolean tileExistOnFileSystem(String file, ITileSource map, int x, int y, int zoom, boolean exact){
if(!imagesOnFS.containsKey(file)){
boolean ex = false;
if(map instanceof SQLiteTileSource){
if(((SQLiteTileSource) map).isLocked()){
return false;
}
ex = ((SQLiteTileSource) map).exists(x, y, zoom);
ex = ((SQLiteTileSource) map).exists(x, y, zoom, exact);
} else {
if(file == null){
file = calculateTileId(map, x, y, zoom);
@ -306,7 +306,7 @@ public class ResourceManager {
if (loadFromFs && cacheOfImages.get(tileId) == null && map != null) {
boolean locked = map instanceof SQLiteTileSource && ((SQLiteTileSource) map).isLocked();
if(!loadFromInternetIfNeeded && !locked && !tileExistOnFileSystem(tileId, map, x, y, zoom)){
if(!loadFromInternetIfNeeded && !locked && !tileExistOnFileSystem(tileId, map, x, y, zoom, false)){
return null;
}
String url = loadFromInternetIfNeeded ? map.getUrlToLoad(x, y, zoom) : null;

View file

@ -158,29 +158,25 @@ public class SQLiteTileSource implements ITileSource {
return db;
}
public boolean exists(int zoom) {
public boolean exists(int x, int y, int zoom, boolean exact) {
SQLiteDatabase db = getDatabase();
if(db == null){
return false;
}
return true; // Always true in resampling mode
// Cursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE z = ? LIMIT 1", new String[] {(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
// try {
// boolean e = cursor.moveToFirst();
// cursor.close();
// return e;
// } catch (SQLiteDiskIOException e) {
// return false;
// }
}
public boolean exists(int x, int y, int zoom) {
SQLiteDatabase db = getDatabase();
if(db == null){
return false;
}
//return true; // Cheat to test resampling /o modifying ressourceManager
long time = System.currentTimeMillis();
if (exact || zoom <= baseZoom) {
Cursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {x+"", y+"",(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
try {
boolean e = cursor.moveToFirst();
cursor.close();
if (log.isDebugEnabled()) {
log.debug("Checking tile existance x = " + x + " y = " + y + " z = " + zoom + " for " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
return e;
} catch (SQLiteDiskIOException e) {
return false;
}
} else {
int n = zoom - baseZoom;
int base_xtile = x >> n;
int base_ytile = y >> n;
@ -196,6 +192,7 @@ public class SQLiteTileSource implements ITileSource {
return false;
}
}
}
public boolean isLocked() {
SQLiteDatabase db = getDatabase();
@ -340,7 +337,7 @@ public class SQLiteTileSource implements ITileSource {
if (db == null || db.isReadOnly()) {
return;
}
if (exists(x, y, zoom)) {
if (exists(x, y, zoom, true)) {
return;
}
ByteBuffer buf = ByteBuffer.allocate((int) fileToSave.length());

View file

@ -167,7 +167,7 @@ public class DownloadTilesDialog {
for (int x = x1; x <= x2 && !cancel; x++) {
for (int y = y1; y <= y2 && !cancel; y++) {
String tileId = rm.calculateTileId(map, x, y, z);
if (rm.tileExistOnFileSystem(tileId, map, x, y, z)) {
if (rm.tileExistOnFileSystem(tileId, map, x, y, z, true)) {
progressDlg.setProgress(progressDlg.getProgress() + 1);
} else {
rm.getTileImageForMapSync(tileId, map, x, y, z, true);

View file

@ -158,7 +158,7 @@ public class MapTileLayer extends BaseMapLayer {
float y1 = (top + j - tileY) * ftileSize + h;
String ordImgTile = mgr.calculateTileId(map, leftPlusI, topPlusJ, nzoom);
// asking tile image async
boolean imgExist = mgr.tileExistOnFileSystem(ordImgTile, map, leftPlusI, topPlusJ, nzoom);
boolean imgExist = mgr.tileExistOnFileSystem(ordImgTile, map, leftPlusI, topPlusJ, nzoom, false);
Bitmap bmp = null;
boolean originalBeLoaded = useInternet && nzoom <= maxLevel;
if (imgExist || originalBeLoaded) {
@ -178,11 +178,11 @@ public class MapTileLayer extends BaseMapLayer {
}
}
if (!originalBeLoaded && !imgExist) {
if (mgr.tileExistOnFileSystem(imgTile2, map, leftPlusI / 2, topPlusJ / 2, nzoom - 1)
if (mgr.tileExistOnFileSystem(imgTile2, map, leftPlusI / 2, topPlusJ / 2, nzoom - 1, false)
|| (useInternet && nzoom - 1 <= maxLevel)) {
bmp = mgr.getTileImageForMapAsync(imgTile2, map, leftPlusI / 2, topPlusJ / 2, nzoom - 1, useInternet);
div = 2;
} else if (mgr.tileExistOnFileSystem(imgTile4, map, leftPlusI / 4, topPlusJ / 4, nzoom - 2)
} else if (mgr.tileExistOnFileSystem(imgTile4, map, leftPlusI / 4, topPlusJ / 4, nzoom - 2, false)
|| (useInternet && nzoom - 2 <= maxLevel)) {
bmp = mgr.getTileImageForMapAsync(imgTile4, map, leftPlusI / 4, topPlusJ / 4, nzoom - 2, useInternet);
div = 4;